hot100-贪心

121. 买卖股票的最佳时机

121. 买卖股票的最佳时机 - 力扣(LeetCode)

python 复制代码
class Solution(object):
    def maxProfit(self, prices):
        """
        :type prices: List[int]
        :rtype: int
        """
        # 贪心思路:因为仅仅买卖一次,所以找到最低点和最高点即可【最低点要在最高点左侧】
        minf = float('inf')
        maxf = 0
        for p in prices:
            maxf = max(p-minf,maxf)
            minf = min(p,minf)
        return maxf

时间复杂度:O(n)

空间复杂度:O(1)

动态规划是解决股票问题的最佳思路【本题股票:仅允许买卖一次,无手续费】

python 复制代码
class Solution(object):
    def maxProfit(self, prices):
        """
        :type prices: List[int]
        :rtype: int
        """
        # 股票买卖,其实dp思路非常好理解
        # dp[i][0]  表示持有股票的最大收益
        # dp[i][1]  表示卖出股票的最大收益
        dp = [[0,0] for _ in prices]
        dp[0][0] = -prices[0]
        dp[0][1] = 0
        for i in range(1,len(prices)):
            dp[i][0] = max(dp[i-1][0],-prices[i])  # 要么继续前一天的持有,要么直接从今天开始买入
            dp[i][1] = max(dp[i-1][1],dp[i-1][0]+prices[i])  # 要么继续前一天的卖出,要么卖出前一天的持有
        return max(dp[-1][0],dp[-1][1])

时间复杂度:O(n)

空间复杂度:O(n)

55. 跳跃游戏

55. 跳跃游戏 - 力扣(LeetCode)

python 复制代码
class Solution(object):
    def canJump(self, nums):
        """
        :type nums: List[int]
        :rtype: bool
        """
        # 在每一个节点,跳跃的最大步数是元素值,元素值以内的步数都可以跳跃!
        # 所以我们只要保证所有点的最大值范围可以到达终点[并且中间不可以间断!]
        maxl = 0
        for i in range(len(nums)):
            if i>maxl:
                # 这意味着i位置在之前根本不可达!
                return False
            maxl = max(maxl,i+nums[i])
        return True

时间复杂度:O(n)

空间复杂度:O(1)

45. 跳跃游戏II

45. 跳跃游戏 II - 力扣(LeetCode)

python 复制代码
class Solution(object):
    def jump(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        # 当我们已经达到当前的最大覆盖度,那么接下来我们就需要真正的跳一次
        # 这样得到的就是最小跳跃次数
        n = len(nums)
        if n<=1:
            return 0  # 就一个点,不需要跳跃
        # 当前节点位置如果已经达到了过去的最大覆盖位置,那么就需要跳出去
        # 如果跳出去以后发现最大覆盖位置达到了终点,那么就结束
        curl = 0
        nextl = 0
        count = 0
        for i in range(n):
            nextl = max(nextl,i+nums[i])
            if i == curl:
                count += 1
                curl = nextl
                if nextl >= n-1:
                    break
        return count

时间复杂度:O(n)

空间复杂度:O(1)

763. 划分字母区间

763. 划分字母区间 - 力扣(LeetCode)

python 复制代码
class Solution(object):
    def partitionLabels(self, s):
        """
        :type s: str
        :rtype: List[int]
        """
        # 我们需要先存储每个元素的最远位置
        # 然后不断的更新最大所需要的右边界,当前位置等于右边界的时候,就可以做一次切断
        # 注意存储的是切断的这一段的长度
        # 然后更新左边界为当前位置+1
        d = {}
        for i in range(len(s)):
            d[s[i]] = i
        left = right = 0
        res = []
        for i in range(len(s)):
            right = max(right, d[s[i]])
            if i == right:
                res.append(right-left+1)
                left = i+1
        return res

时间复杂度:O(n)

空间复杂度:O(∣Σ∣) # 其实这里的∣Σ∣最大是26

相关推荐
To_OC1 天前
LC 994 腐烂的橘子:人人都说是 BFS 入门题,我却写了三遍才过
javascript·算法·leetcode
金銀銅鐵1 天前
[Python] 扩展欧几里得算法
python·数学·算法
Duckdblab1 天前
DuckDB 性能调优终极指南:打造闪电般的分析体验
python
带派擂总1 天前
Python全栈开发精华版最全合集(包含各种面试题) Day24_异常和错误
python
To_OC1 天前
LC 200 岛屿数量:经典 DFS 入门题,我第一次写居然连方向都搞错了
javascript·算法·leetcode
金銀銅鐵1 天前
n^5 和 n 的个位数是否总相等?
python·数学
aqi001 天前
15天学会AI应用开发(九)利用Chroma持久化向量数据
人工智能·python·大模型·ai编程·ai应用
金銀銅鐵1 天前
借助 Pygame 探索最大公约数的规律
python·数学·游戏
To_OC2 天前
LC 128 最长连续序列:别上来就排序,O (n) 解法才是这题的灵魂
javascript·算法·leetcode