LeetCode--Best Time to Buy and Sell Stock(最大利润)Python
2017-11-23 15:39
507 查看
题目:
给定一个数组,第i个位置表示第i天的价格,要求只能完成一组交易(买入一次+卖出一次),求出最大利润。
解题思路:
对数组进行遍历,保存当前的最小买入价格和当前的最大利润,遍历结束后,返回最终的最大利润即可。
代码(python):
给定一个数组,第i个位置表示第i天的价格,要求只能完成一组交易(买入一次+卖出一次),求出最大利润。
解题思路:
对数组进行遍历,保存当前的最小买入价格和当前的最大利润,遍历结束后,返回最终的最大利润即可。
代码(python):
class Solution(object): def maxProfit(self, prices): """ :type prices: List[int] :rtype: int """ n = len(prices) if n==0 or n==1: return 0 min0 = prices[0] max0 = 0 for i in range(n): if prices[i]<min0: min0 = prices[i] continue if prices[i]>=min0: if prices[i]-min0>max0: max0 = prices[i]-min0 continue else: continue return max0
相关文章推荐
- LeetCode-122:Best Time to Buy and Sell Stock II (多次股票交易最大利润)
- LeetCode-714:Best Time to Buy and Sell Stock with Transaction Fee (带有抛售费用的股票最大利润) -- medium
- (LeetCode)Best Time to Buy and Sell Stock -- 求股票买卖的最大利润
- leetcode——Best Time to Buy and Sell Stock III 买卖股票最大收益(AC)
- [LeetCode]题解(python):121-Best Time to Buy and Sell Stock
- leetcode 【 Best Time to Buy and Sell Stock 】python 实现
- Leetcode_best-time-to-buy-and-sell-stock(c++ and python version)
- Python [Leetcode 121]Best Time to Buy and Sell Stock
- [leetcode]Best Time to Buy and Sell Stock III @ Python
- 【LeetCode with Python】 Best Time to Buy and Sell Stockd
- [Leetcode][python]Best Time to Buy and Sell Stock I/II/III
- [LeetCode]题解(python):123-Best Time to Buy and Sell Stock III
- Best Time to Buy and Sell Stock III Leetcode Python
- leetcode【121+122+123 best time to buy and sell stock】【python】
- leetcode 123. Best Time to Buy and Sell Stock III 最大k次字段和 + DP
- [LeetCode] [Python] [DP] Best Time to Buy and Sell Stock III
- 【LeetCode】【Python题解】Best Time to Buy and Sell Stock II
- leetcode 188. Best Time to Buy and Sell Stock IV 最大子段和
- 188. Best Time to Buy and Sell Stock IV Leetcode Python
- Leetcode 188 Best Time to Buy and Sell Stock IV 至多k次买卖股票最大收益