LeetCode--Best Time to Buy and Sell Stock(最大利润)Python
来源:互联网 发布:詹姆斯场均数据 编辑:程序博客网 时间:2024/06/07 04:48
题目:
给定一个数组,第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
阅读全文
0 0
- LeetCode--Best Time to Buy and Sell Stock(最大利润)Python
- Best Time to Buy and Sell Stock【找出最大利润】
- (LeetCode)Best Time to Buy and Sell Stock -- 求股票买卖的最大利润
- LeetCode-121:Best Time to Buy and Sell Stock (一次股票交易最大利润)
- LeetCode-122:Best Time to Buy and Sell Stock II (多次股票交易最大利润)
- 【LeetCode with Python】 Best Time to Buy and Sell Stock
- [LeetCode] [Python] [DP] Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock III Leetcode Python
- [leetcode: Python] 121. Best Time to Buy and Sell Stock
- Leetcode-Best Time to Buy and Sell Stock-Python
- [LeetCode]121. Best Time to Buy and Sell Stock(求近期股票能获得的最大利润)
- LeetCode-714:Best Time to Buy and Sell Stock with Transaction Fee (带有抛售费用的股票最大利润) -- medium
- LeetCode(121) Best Time to Buy and Sell Stock
- LeetCode(121) Best Time to Buy and Sell Stock
- 【LeetCode】Best Time to Buy and Sell Stock & Best Time to Buy and Sell Stock
- [LeetCode] [LeetCode]Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock 股票交易最大收益
- leetcode 121. Best Time to Buy and Sell Stock | 最大差值和最大子序列关系
- 《大象 Thinking in UML》学习笔记(五)——UML核心元素之关系、组件和节点
- Android开发中dip,dpi,density,px等详解
- 笔记本电脑如何保养电脑电池?
- 用sum case when做分类统计sql优化
- MYSQL日常维护
- LeetCode--Best Time to Buy and Sell Stock(最大利润)Python
- 关于mysql_upgrade工具的使用的注意事项及限制
- Netty源码分析之零【分析环境搭建】
- 大数据正式12
- android商品属性选择标签控件,可实现自动换行
- 网易云音乐API获取分析
- QT设置文件对话框
- 循环 迭代 遍历 递归
- JQuery中的杂项方法---grep()