//122. 买卖股票的最佳时机 II
func maxProfit(prices []int) int {
result := 0 //利润总和
for i := 1; i < len(prices); i++ {
if prices[i]-prices[i-1] > 0 {
result = result + (prices[i] - prices[i-1])
}
}
return result
}
//55. 跳跃游戏
func canJump(nums []int) bool {
step := 0 //步数
for i := 0; i <= step; i++ {
if i+nums[i] > step {
step = i + nums[i]
}
if step >= len(nums)-1 {
return true
}
}
return false
}
//45. 跳跃游戏 II
func jump(nums []int) int {
cur := 0 //当前指针
next := 0 //下一个指针
result := 0 //步骤统计
for i := 0; i < len(nums); i++ {
if i+nums[i] > next {
next = i + nums[i]
}
if i == cur && cur != len(nums)-1 {
result++
cur = next
if cur >= len(nums)-1 {
return result
}
} else if i == cur {
return result
}
}
return result
}