【Day24】LeetCode:122. 买卖股票的最佳时机 II,55. 跳跃游戏,45. 跳跃游戏II,1005. K次取反后最大化的数组和

文章目录

  • [LeetCode:122. 买卖股票的最佳时机 II](#LeetCode:122. 买卖股票的最佳时机 II)
  • [LeetCode:55. 跳跃游戏](#LeetCode:55. 跳跃游戏)
  • [LeetCode:45. 跳跃游戏II](#LeetCode:45. 跳跃游戏II)
  • [LeetCode:1005. K次取反后最大化的数组和](#LeetCode:1005. K次取反后最大化的数组和)

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

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

思路

找出数组的单调递增区间,在每个单调递增区间持有就行。

解答

python 复制代码
class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        results = 0

        n = len(prices)

        for i in range(1, n):
            diff = prices[i] - prices[i-1]
            if diff > 0:
                results += diff

        return results

LeetCode:55. 跳跃游戏

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

思路

从头开始,每次跳之前都规划跳到哪里能使得下一跳更远。

解答

python 复制代码
class Solution:
    def canJump(self, nums: List[int]) -> bool:
        n = len(nums)

        if n == 1: # 长度为1的必然能跳到
            return True

        i = 0
        while i < n:
            steps = nums[i]
            next_max = 0 # 记录跳到哪里能使得下一跳走得最远
            next_max_idx = i

            if steps == 0 and i < n - 1: # 特殊处理,防止死循环
                return False

            for step in range(1, steps + 1): # 尝试每一跳的可能性
                if i + step >= n - 1: # 可以跳到数组末尾
                    return True
                next_step = step + nums[i + step]
                if next_step > next_max: # 找出跳到哪里能使得下一跳走得最远,同时标记此时需要跳到的位置
                    next_max = next_step
                    next_max_idx = i + step
            i = next_max_idx

        return False

扩展

https://leetcode.cn/problems/jump-game/solutions/24322/55-by-ikaruga/

k 表示当前能够到达的最远位置。遍历数组,如果 i 的位置 k 能到达( i 不在 k 的右边),则用 max(k, i + nums[i]) 更新 k;如果 i 的位置 k 不能到达,则立即返回 False。

python 复制代码
class Solution {
public:
    bool canJump(vector<int>& nums) {
        int k = 0;
        for (int i = 0; i < nums.size(); i++) {
            if (i > k) return false;
            k = max(k, i + nums[i]);
        }
        return true;
    }
};

作者:Ikaruga
链接:https://leetcode.cn/problems/jump-game/solutions/24322/55-by-ikaruga/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

LeetCode:45. 跳跃游戏II

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

思路

思路和上题一致,从头开始,每次跳之前都规划跳到哪里能使得下一跳更远(使得跳的次数更少)。

解答

python 复制代码
class Solution:
    def jump(self, nums: List[int]) -> int:
        n = len(nums)
        results = 0

        if n == 1: # 长度为1的不用跳
            return results

        i = 0
        while i < n:
            cur_steps = nums[i]
            next_max = 0 # 记录跳到哪里能使得下一跳走得最远
            next_max_idx = i

            for cur_step in range(1, cur_steps + 1): # 尝试每一跳的可能性
                if i + cur_step >= n - 1:
                    return results + 1
                next_step = cur_step + nums[i + cur_step]
                if next_step > next_max: # 找出跳到哪里能使得下一跳走得最远,同时标记此时需要跳到的位置
                    next_max = next_step
                    next_max_idx = i + cur_step

            i = next_max_idx
            results += 1

        return results

LeetCode:1005. K次取反后最大化的数组和

https://leetcode.cn/problems/maximize-sum-of-array-after-k-negations/description/

思路

首先对数组排序,优先使得绝对值最大的负数变成正数。

  1. 如果取反次数没有剩余,则直接计算总和。
  2. 如果取反次数还有剩余,则此时数组里都是非负数。
    (1)如果剩余次数是偶数,则可以通过对同一个数不断取反的方式来保持结果不变;
    (2)如果剩余次数是奇数,则选择一个绝对值最小的正数来变成负数。

解答

python 复制代码
class Solution:
    def largestSumAfterKNegations(self, nums: List[int], k: int) -> int:
        n = len(nums)
        nums.sort()

        i = 0
        while i < n and k > 0:
            if nums[i] < 0: # 负数就取反
                nums[i] = -nums[i]
                k -= 1
            i += 1
        
        if k > 0 and k % 2 == 1: # 如果次数还有剩余且为奇数,此时数组中全都是非负数,则找一个最小的转为负数
            nums.sort()
            nums[0] = -nums[0]

        results = 0
        for num in nums:
            results += num
        return results
相关推荐
Li emily1 天前
解决了加密货币api多币种订阅时的数据乱序问题
人工智能·python·api·fastapi
csdn_aspnet1 天前
C语言 Lomuto分区算法(Lomuto Partition Algorithm)
c语言·开发语言·算法
2301_781571421 天前
Golang格式化输出占位符都有什么_Golang fmt占位符教程【通俗】
jvm·数据库·python
asdzx671 天前
使用 Python 为 PDF 添加页码 (详细教程)
python·pdf·页码
谙弆悕博士1 天前
【附C源码】从零实现C语言堆数据结构:原理、实现与应用
c语言·数据结构·算法··数据结构与算法
AI技术控1 天前
《Transformers are Inherently Succinct》论文解读:从“能表达什么”到“多紧凑地表达”
人工智能·python·深度学习·机器学习·自然语言处理
金融大 k1 天前
Python 全球指数监控面板:TickDB + REST + WebSocket 完整方案
python·websocket
啊哈哈121381 天前
系统设计复盘:为什么 Agent 的 ReAct 循环必须内嵌确定性保护层——以 FitMind 健康助手的路由与步骤控制为例
人工智能·python·react
gaosushexiangji1 天前
DIC系统推荐:基于千眼狼三维数字图像相关的无人机旋翼疲劳试验全场应变与位移测量
人工智能·算法
一颗牙牙1 天前
安装mmcv
开发语言·python·深度学习