leetcode 【 Best Time to Buy and Sell Stock 】python 实现

思路

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.

代码:oj测试代码 Runtime: 111 ms

 1 class Solution:
 2     # @param prices, a list of integer
 3     # @return an integer
 4     def maxProfit(self, prices):
 5         # none case or one element case
 6         if prices is None or len(prices)<2 :
 7             return 0
 8         # dp
 9         min_buy = 0
10         max_profit = 0
11         for i in range(1,len(prices)):
12             if prices[i]<prices[min_buy]:
13                 min_buy = i
14             max_profit = max(prices[i]-prices[min_buy],max_profit)
15         return max_profit

思路

典型的动态规划题目。

跟求最小子集的和这道题(http://www.cnblogs.com/xbf9xbf/p/4240510.html)类似。

个人认为这类题目的精髓在于“在每一步,我们维护两个变量,一个是全局最优,就是到当前元素为止最优的解是,一个是局部最优,就是必须包含当前元素的最优的解”。

对于这道题来说,有一个小梗需要想明白:如果prices[i]就是到i为止最小的值,那么prices[i]-prices[min_buy]不是等于0了么?

请注意,回顾动态规划的规则:一定要包含当前元素的最优解。既然当前元素已经是历史最小值了,那么还非要包含当前元素的最优解,不正是他自身减去自身么?

维护一个max_profit(最大利润),一个min_buy(历史最低点)。

1. 如果当前的prices[i]小于prices[min_buy],则更新min_buy。

2. 取prices[i]-prices[min_buy]和max_profit中较大的一个,作为max_profit的值。

最后返回max_profit即可。

时间: 2024-08-01 22:30:34

leetcode 【 Best Time to Buy and Sell Stock 】python 实现的相关文章

leetcode——Best Time to Buy and Sell Stock III 买卖股票最大收益(AC)

Say you have an array 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 at most two transactions. Note: You may not engage in multiple transactions at the same time (ie,

LeetCode: Best Time to Buy and Sell Stock II [122]

[题目] Say you have an array 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 (ie, buy one and sell one share of the stock multiple times

LeetCode: Best Time to Buy and Sell Stock III [123]

[题目] Say you have an array 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 at most two transactions. Note: You may not engage in multiple transactions at the same time

[leetcode]_Best Time to Buy and Sell Stock I &amp;&amp; II

一个系列三道题,我都不会做,google之答案.过了两道,第三道看不懂,放置,稍后继续. 一.Best Time to Buy and Sell Stock I 题目:一个数组表示一支股票的价格变换.要求只买卖一次,获得最大收益. 思路:一开始我认为是寻找最大.最小值,但由于最大值不一定总是出现在最小值的后面,因此WA. 参考思路:DP.对第i个价格,减去前i-1个价格中的最小值(保证该收益是在第i个价格卖出的最大收益),其收益与之前获得的最大收益相比. 代码: 1 public int max

LeetCode: Best Time to Buy and Sell Stock [121]

[题目] 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.

LeetCode——Best Time to Buy and Sell Stock III

Say you have an array 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 at most two transactions. Note: You may not engage in multiple transactions at the same time (ie,

LeetCode Best Time to Buy and Sell Stock with Cooldown

原题链接在这里:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/ 题目: Say you have an array 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

LeetCode Best Time to Buy and Sell Stock with Transaction Fee

原题链接在这里:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/description/ 题目: Your are given an array of integers prices, for which the i-th element is the price of a given stock on day i; and a non-negative integer fee 

LeetCode Best Time to Buy and Sell Stock II

Best Time to Buy and Sell Stock II Total Accepted: 41127 Total Submissions: 108434 My Submissions Question Solution Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit

[LeetCode]Best Time to Buy and Sell Stock III 动态规划

本题是Best Time to Buy and Sell Stock/的改进版. 本题中,可以买最多买进卖出两次股票. 买两次股票可以看成是第0~i天买进卖出以及第i+1~n-1天买进卖出两部分.这要枚举i并求出0th~ith的最大利益与(i+1)th~(n-1)th的最大利益之和的最大值就是买进卖出两次可以得到的最大利益.即状态转移方程: dp[0,n-1]=max{dp[0,k]+dp[k+1,n-1]},k=1,...,n-2 而只买进卖出一次的最大利润通过0th~ith可以求得. 这里求