【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
相关推荐
jufeng130721 分钟前
【系列:手搓自主 AI Agent:Hermes 架构原理剖析 · 第 8 篇】
python·ai agent·配置系统
circuitsosk33 分钟前
跨境电商智能化实战:AI如何赋能客服自动回复、广告智能投放与供应链预测
大数据·人工智能·python·langchain·智能客服
2601_956319881 小时前
2026年零基础学量化:从看懂示例到写清条件和动作
人工智能·python
过期的秋刀鱼!2 小时前
LangChain-D1-模型的工作流程
人工智能·python·langchain
半夏微凉半夏殇2 小时前
x11与weston对比,优先选哪个
leetcode·均值算法·eclipse
2501_906565122 小时前
数学之美探究
算法
萤火工厂目视化设计2 小时前
智能制造与企业文化目视化浪潮下:中小工厂的机遇与挑战
python·制造
oier_Asad.Chen2 小时前
【洛谷题解/AcWing题解/OI学习笔记】洛谷P2868【USACO07DEC】Sightseeing Cows G(01分数规划求最大比率)
算法·图论·spfa·二分·负环
DeepVisionary3 小时前
从GPT-5.6 Sol的Ultrafast模式看推理速度竞赛:750 token/秒背后,Cerebras的晶圆级生意
python·自动化
leisoo80974 小时前
财报数据怎么排雷本地化Python构建财务异常预警系统
人工智能·python·算法