代码随想录算法训练营第三十二天|122. 买卖股票的最佳时机 II、55. 跳跃游戏

代码随想录算法训练营第三十二天|122. 买卖股票的最佳时机 II、55. 跳跃游戏

    • 122. 买卖股票的最佳时机 II
      • 思路
      • 解题代码
    • 55. 跳跃游戏
      • 思路
      • 解题代码

难死我了

122. 买卖股票的最佳时机 II

题目链接:122. 买卖股票的最佳时机 II

思路

最终盈利等于所有正利润相加

解题代码

func maxProfit(prices []int) int {
    ans := 0
    for i:=1;i<len(prices);i++{
        todayM := prices[i]-prices[i-1]
        if todayM > 0 {
            ans += todayM
        }
    }
    return ans
}

55. 跳跃游戏

题目链接:55. 跳跃游戏

思路

重点:不拘泥于每次跳几步,看覆盖范围,覆盖范围内一定是可以跳过来的,不用管是怎么跳的。
很糊。。。。

解题代码

func canJump(nums []int) bool {
    index := 0
    for i:=0;i<=index;i++{
        index = max(i+nums[i], index) //记录当前最远距离
        if index>=len(nums)-1{
            return true
        }
    }
    return false
}

func max(a, b int ) int {
    if a > b {
        return a
    }
    return b
}

你可能感兴趣的:(代码随想录训练营,算法,游戏,leetcode)