leetcode解题报告:121 Best Time to Buy and Sell Stock

问题:

给定一个列表,第i个元素代表股票第i天的价值,只允许买入卖出一次,求最大收益

思路:动态规划

开始没想清楚,觉着遍历一次找出最大与最小股价求差就是,后来发现脑子绣住了,买入要在买出之前。


输入为列表p1p2...pm

m[i]表示前i天的最小股价,m[i] = min(p[i], m[i-1]) i >= 1 <-- O(n)时间开销

第i天卖出的最大收益是pi - m[i-1] <-- 遍历一次O(n)求出最大值

代码:Python

class Solution:
    # @param prices, a list of integer
    # @return an integer
    def maxProfit(self, prices):
        n = len(prices)
        if n < 2:
            return 0
        else:
            # m[i] means minimal value of stock before day i
            m = [0 for i in range(n)]
            m[0] = prices[0]
            for i in range(1,n):
                m[i] = min(prices[i], m[i-1])
            ret = 0
            for i in range(1, n):
                ret = max(ret, prices[i] - m[i-1])
            return ret

时间: 2024-10-10 17:52:48

leetcode解题报告:121 Best Time to Buy and Sell Stock的相关文章

121. Best Time to Buy and Sell Stock (一) leetcode解题笔记

121. Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algor

leetCode 121. Best Time to Buy and Sell Stock 数组

121. Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algor

【leetcode】121. Best Time to Buy and Sell Stock

@requires_authorization @author johnsondu @create_time 2015.7.19 21:01 @url [best time to buy and sell stock](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/) /************************ * @description: dynamic programming. * 相邻元素做差,然后转化

LeetCode之“动态规划”:Best Time to Buy and Sell Stock I &amp;&amp; II &amp;&amp; III &amp;&amp; IV

1. Best Time to Buy and Sell Stock I 题目链接 题目要求: Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), desi

LeetCode练题——122. Best Time to Buy and Sell Stock II

1.题目 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. 设计一个算法来计算你所能获取的最大利润.你可以尽可能地完成更多的交易(多次买卖一支股票). 注意:你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票). 示例 1: 输入: [7,1,5,3,6,4]输出: 7解释: 在第 2 天(股票价格 = 1)的时候买入,在第 3 天(股票价格 = 5)的时候卖出, 这笔交易所能获得利润 = 5-1 = 4 .  随后,在第 4 天(股票价格 = 3)的时候买入,在第 5

[email&#160;protected] [121]Best Time to Buy and Sell Stock

Best Time to Buy and Sell Stock Total Accepted: 69292 Total Submissions: 206193 Difficulty: Medium Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitted to complete at most one transaction

LeetCode 122, 123, 188. Best Time to Buy and Sell Stock II+III+IV

Best Time to Buy and Sell Stock II class Solution { public: int maxProfit(vector<int>& prices) { int res=0; for (int i=1;i<prices.size();++i){ if (prices[i]>prices[i-1]){ res += prices[i]-prices[i-1]; } } return res; } }; Best Time to Buy

[leetcode] 121. Best Time to Buy and Sell Stock 解题报告

动态规划,注意处理当前最大值小于0的情况 public int maxProfit(int[] prices) { if (prices == null || prices.length <= 1) return 0; int curMax = 0,result = 0; for (int i = 1; i < prices.length; i++) { curMax = Math.max(0, curMax + prices[i] - prices[i-1]); result = Math.

【LeetCode】121. Best Time to Buy and Sell Stock 解题小结

题目: Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.