【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
相关推荐
To_OC6 小时前
LC 131 分割回文串:刚学回溯时,我连怎么切字符串都想不明白
javascript·算法·leetcode
旖-旎7 小时前
LeetCode 518:零钱兑换||(完全背包)—— 题解
c++·算法·leetcode·动态规划·背包问题
To_OC7 小时前
LC 42 接雨水:暴力超时卡半天?前后缀数组一用就通了
javascript·算法·leetcode
delishcomcn8 小时前
AI视觉识别+分切算法:电化铝缺陷检测与裁切一体化解锁
人工智能·算法
触底反弹8 小时前
深入理解大模型采样:Temperature、Top-K、Top-P 的原理与实战
人工智能·算法·面试
大模型码小白8 小时前
【Python零基础教程】继承、多态与魔法函数:面向对象编程三大核心特性详解
java·大数据·开发语言·人工智能·python·ai编程
雪碧聊技术9 小时前
力扣 LCR 091. 粉刷房子 —— 动态规划入门详解
算法·动态规划
麻雀飞吧9 小时前
最新量化学习路径,交易认知和技术实现要并行
人工智能·python
CV-Climber11 小时前
检索技术的实际应用
人工智能·算法