leetcode Best Time to Buy and Sell Stock
来源:互联网 发布:范志毅数据 编辑:程序博客网 时间:2024/05/18 01:00
题目链接
思路:
贪心算法
找出当前的最小值。如果现在碰到的值是当前最大的,那么减去最小值,得到当前的最优解。
如果发现比当前最小值还小的值, 那么重新开始找解的过程,也就是把当前最大值设为0。
public class Solution { public int maxProfit(int[] prices) { if(prices.length<=1) { return 0; } int max=prices[1]; int min=prices[0]; int maxSell=0; for(int i=1;i<prices.length;i++) { if(max<=prices[i]) { max=prices[i]; maxSell=Math.max(maxSell,max-min); } if(min>prices[i]) { min=prices[i]; max=0; } } return maxSell; }}
0 0
- 【LeetCode】Best Time to Buy and Sell Stock & Best Time to Buy and Sell Stock
- [LeetCode] [LeetCode]Best Time to Buy and Sell Stock
- 【LeetCode with Python】 Best Time to Buy and Sell Stock
- [LeetCode]Best Time to Buy and Sell Stock
- [LeetCode]Best Time to Buy and Sell Stock II
- LeetCode Best Time to Buy and Sell Stock
- LeetCode Best Time to Buy and Sell Stock II
- LeetCode Best Time to Buy and Sell Stock III
- Leetcode: Best time to buy and sell a stock I
- Leetcode: Best Time to Buy and Sell Stock II
- LeetCode Best Time to Buy and Sell Stock III
- [Leetcode] Best Time to Buy and Sell Stock
- [Leetcode] Best Time to Buy and Sell Stock II
- [Leetcode] Best Time to Buy and Sell Stock III
- LeetCode : Best Time to Buy and Sell Stock II
- [leetcode] Best Time to Buy and Sell Stock
- leetcode 16: Best Time to Buy and Sell Stock III
- LeetCode Best Time to Buy and Sell Stock
- Swift之" ?与! "区别
- PAC脚本语法(代理自动配置)
- make clean rm执行时忽略错误
- Mac常用快捷键备忘
- Android本地服务写法 及 生成脚本
- leetcode Best Time to Buy and Sell Stock
- jsp页面第一行报错问题
- 何时该用DOM脚本设置样式
- Java与持久相关的锁总结
- C++标准库set类型
- i++是否原子操作?并解释为什么?
- 支持向量机之序列最小最优化算法
- zabbix安装
- HDU 1201 18岁生日