首页 > 编程语言 >代码随想录算法训练营第三十二天 | 122.买卖股票的最佳时机II,55. 跳跃游戏,45.跳跃游戏II

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

时间:2023-02-17 02:11:52浏览次数:55  
标签:下标 游戏 nextDistance nums int II prices 跳跃 return

一、参考资料

买卖股票的最佳时机II

https://programmercarl.com/0122.%E4%B9%B0%E5%8D%96%E8%82%A1%E7%A5%A8%E7%9A%84%E6%9C%80%E4%BD%B3%E6%97%B6%E6%9C%BAII.html

跳跃游戏

https://programmercarl.com/0055.%E8%B7%B3%E8%B7%83%E6%B8%B8%E6%88%8F.html

跳跃游戏 II

https://programmercarl.com/0045.%E8%B7%B3%E8%B7%83%E6%B8%B8%E6%88%8FII.html

二、LeetCode122.买卖股票的最佳时机II

https://leetcode.cn/problems/best-time-to-buy-and-sell-stock-ii/description/

给你一个整数数组 prices ,其中 prices[i] 表示某支股票第 i 天的价格。
在每一天,你可以决定是否购买和/或出售股票。你在任何时候 最多 只能持有 一股 股票。你也可以先购买,然后在 同一天 出售。
返回 你能获得的 最大 利润 。

示例 1:
输入:prices = [7,1,5,3,6,4] 输出:7 解释:在第 2 天(股票价格 = 1)的时候买入,在第 3 天(股票价格 = 5)的时候卖出, 这笔交易所能获得利润 = 5 - 1 = 4 。 随后,在第 4 天(股票价格 = 3)的时候买入,在第 5 天(股票价格 = 6)的时候卖出, 这笔交易所能获得利润 = 6 - 3 = 3 。 总利润为 4 + 3 = 7 。
示例 2:
输入:prices = [1,2,3,4,5] 输出:4 解释:在第 1 天(股票价格 = 1)的时候买入,在第 5 天 (股票价格 = 5)的时候卖出, 这笔交易所能获得利润 = 5 - 1 = 4 。 总利润为 4 。
示例 3:
输入:prices = [7,6,4,3,1] 输出:0 解释:在这种情况下, 交易无法获得正利润,所以不参与交易可以获得最大利润,最大利润为 0 。

把利润分解为每天为单位的维度,而不是从0天到第3天整体去考虑!

那么根据prices可以得到每天的利润序列:(prices[i] - prices[i - 1]).....(prices[1] - prices[0])

  1. class Solution:
  2. def maxProfit(self, prices: List[int]) -> int:
  3. res = 0
  4. for i in range(1, len(prices)):
  5. res += max(prices[i] - prices[i - 1], 0)
  6. return res

动态规划:

  1. class Solution:
  2. def maxProfit(self, prices: List[int]) -> int:
  3. length = len(prices)
  4. dp = [[0] * 2 for _ in range(length)]
  5. dp[0][0] = -prices[0]
  6. dp[0][1] = 0
  7. for i in range(1, length):
  8. dp[i][0] = max(dp[i-1][0], dp[i-1][1] - prices[i]) #注意这里是和121. 买卖股票的最佳时机唯一不同的地方
  9. dp[i][1] = max(dp[i-1][1], dp[i-1][0] + prices[i])
  10. return dp[-1][1]

三、LeetCode55. 跳跃游戏

https://leetcode.cn/problems/jump-game/description/

给定一个非负整数数组 nums ,你最初位于数组的 第一个下标 。
数组中的每个元素代表你在该位置可以跳跃的最大长度。
判断你是否能够到达最后一个下标。

示例 1:
输入:nums = [2,3,1,1,4] 输出:true 解释:可以先跳 1 步,从下标 0 到达下标 1, 然后再从下标 1 跳 3 步到达最后一个下标。
示例 2:
输入:nums = [3,2,1,0,4] 输出:false 解释:无论怎样,总会到达下标为 3 的位置。但该下标的最大跳跃长度是 0 , 所以永远不可能到达最后一个下标。
提示:
1 <= nums.length <= 3 * 104
0 <= nums[i] <= 105

思路:

这个问题就转化为跳跃覆盖范围究竟可不可以覆盖到终点!

每次移动取最大跳跃步数(得到最大的覆盖范围),每移动一个单位,就更新最大覆盖范围。

贪心算法局部最优解:每次取最大跳跃步数(取最大覆盖范围),整体最优解:最后得到整体最大覆盖范围,看是否能到终点。

  1. class Solution:
  2. def canJump(self, nums: List[int]) -> bool:
  3. cover = 0
  4. if (len(nums) == 1): return True
  5. for i in range(len(nums)):
  6. # 每次只能在cover范围内移动
  7. if i <= cover:
  8. cover = max(i + nums[i], cover)
  9. if cover >= len(nums) - 1:
  10. return True
  11. return False

四、LeetCode45.跳跃游戏II

https://leetcode.cn/problems/jump-game-ii/

给定一个长度为 n 的 0 索引整数数组 nums。初始位置为 nums[0]。
每个元素 nums[i] 表示从索引 i 向前跳转的最大长度。换句话说,如果你在 nums[i] 处,你可以跳转到任意 nums[i + j] 处:
0 <= j <= nums[i]
i + j < n
返回到达 nums[n - 1] 的最小跳跃次数。生成的测试用例可以到达 nums[n - 1]。

示例 1:

示例 2:
输入: nums = [2,3,0,1,4] 输出: 2
提示:
1 <= nums.length <= 104
0 <= nums[i] <= 1000
题目保证可以到达 nums[n-1]

版本一:

  1. class Solution {
  2. public:
  3. int jump(vector<int>& nums) {
  4. if (nums.size() == 1) return 0;
  5. int curDistance = 0; // 当前覆盖最远距离下标
  6. int ans = 0; // 记录走的最大步数
  7. int nextDistance = 0; // 下一步覆盖最远距离下标
  8. for (int i = 0; i < nums.size(); i++) {
  9. nextDistance = max(nums[i] + i, nextDistance); // 更新下一步覆盖最远距离下标
  10. if (i == curDistance) { // 遇到当前覆盖最远距离下标
  11. if (curDistance < nums.size() - 1) { // 如果当前覆盖最远距离下标不是终点
  12. ans++; // 需要走下一步
  13. curDistance = nextDistance; // 更新当前覆盖最远距离下标(相当于加油了)
  14. if (nextDistance >= nums.size() - 1) break; // 下一步的覆盖范围已经可以达到终点,结束循环
  15. } else break; // 当前覆盖最远距到达集合终点,不用做ans++操作了,直接结束
  16. }
  17. }
  18. return ans;
  19. }
  20. };

Python版本:

  1. class Solution:
  2. def jump(self, nums: List[int]) -> int:
  3. if len(nums) == 1: return 0
  4. ans = 0
  5. curDistance = 0
  6. nextDistance = 0
  7. for i in range(len(nums)):
  8. nextDistance = max(i + nums[i], nextDistance)
  9. if i == curDistance:
  10. if curDistance != len(nums) - 1:
  11. ans += 1
  12. curDistance = nextDistance
  13. if nextDistance >= len(nums) - 1: break
  14. return ans
  15. # 贪心版本二
  16. class Solution:
  17. def jump(self, nums: List[int]) -> int:
  18. if len(nums) == 1:
  19. return 0
  20. curDistance, nextDistance = 0, 0
  21. step = 0
  22. for i in range(len(nums)-1):
  23. nextDistance = max(nextDistance, nums[i]+i)
  24. if i == curDistance:
  25. curDistance = nextDistance
  26. step += 1
  27. return step

今日总结:

不焦虑也不自卑,不过是有一腔热血和勇敢的心

继续加油哈小赵~

标签:下标,游戏,nextDistance,nums,int,II,prices,跳跃,return
From: https://www.cnblogs.com/ucaszym/p/17128794.html

相关文章