Datawhale LeetCode腾讯精选50——Task10

LeetCode 121 买卖股票的最佳时机

You are given an array prices where prices[i] is the price of a given stock on the ith day.

You want to maximize your profit by choosing a single day to buy one stock 
and choosing a different day in the future to sell that stock.

Return the maximum profit you can achieve from this transaction. 
If you cannot achieve any profit, return 0.

在这里插入图片描述
三种思路的详细解释和代码参见LeetCode:121. 买卖股票的最佳时机(python)
思路一:从后往前遍历,通过已找到的最高股票价格更新最大利润值,随后查看是否需要更新最高股票价格,若是,则更新。
思路二:动态规划

dp[i][0] = max(dp[i-1][0], dp[i-1][1] + prices[i])

dp[i][1] = max(dp[i-1][1], -prices[i])

思路三:优化后的动态规划,当天状态只与前一天状态有关,因此可采用单变量代替 dp 数组。

LeetCode 121 买卖股票的最佳时机官方解决方案

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

Say you have an array prices for which the ith element is the price of a given stock on day i.

Design an algorithm to find the maximum profit. 
You may complete as many transactions as you like 
(i.e., buy one and sell one share of the stock multiple times).

Note: You may not engage in multiple transactions at the same time 
(i.e., you must sell the stock before you buy again).

在这里插入图片描述
在这里插入图片描述
思路一:动态规划

dp[i][0] = max(dp[i-1][0], dp[i-1][1] + prices[i])

dp[i][1] = max(dp[i-1][1], dp[i-1][0] - prices[i])

详细解释和代码参见LeetCode:122. 买卖股票的最佳时机 II(python)
思路二:只要相邻的两天有收益,那就是我的,是一种将收益分散到相邻两天进行累加的方式。
思路三:记录高点低点,当高点后一天的价格小于高点,那么将之前的抛出,重新定位高点和低点,记得最后一次记录操作要变现。
思路二和三的详细解释和代码参见leetcode 122 python 买卖股票的最佳时机II

这里放思路二的代码,因为比较简单直接,哈哈~(代码出自leetcode 122 python 买卖股票的最佳时机II

class Solution:
    def maxProfit(self, prices):
        """
        :type prices: List[int]
        :rtype: int
        """
        if not prices:
            return 0
        profit = 0
        for i in range(1, len(prices)):
            p = prices[i] - prices[i-1]
            if p > 0:
                profit += p
        return profit

LeetCode 122 买卖股票的最佳时机 II官方解决方案

LeetCode 124 二叉树中的最大路径和

A path in a binary tree is a sequence of nodes 
where each pair of adjacent nodes in the sequence has an edge connecting them. 
A node can only appear in the sequence at most once. 
Note that the path does not need to pass through the root.

The path sum of a path is the sum of the node's values in the path.

Given the root of a binary tree, return the maximum path sum of any path.

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
思路:递归,详细解释参见Leetcode 124:二叉树中的最大路径和(超详细的解法!!!)

是路径的root,此时的最大值来自于node.left+node.right
不是路径的root,此时的最大值来自于node.left或node.right
class Solution:
    def maxPathSum(self, root):
        """
        :type root: TreeNode
        :rtype: int
        """
        res = float('-inf')
        def maxPath(node):
            nonlocal res 
            if not node:
                return 0
            
            left = max(0, maxPath(node.left))
            right = max(0, maxPath(node.right))
            res = max(res, left + right + node.val)
            return max(left, right) + node.val
        
        maxPath(root)
        return res

LeetCode 124 二叉树中的最大路径和官方解决方案

任务链接

team-learning-program/LeetCodeTencent/121 买卖股票的最佳时机.md
team-learning-program/LeetCodeTencent/122 买卖股票的最佳时机 II.md
team-learning-program/LeetCodeTencent/124 二叉树中的最大路径和.md

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值