leetcode-java-121. Best Time to Buy and Sell Stock
来源:互联网 发布:淘宝团队架构 编辑:程序博客网 时间:2024/06/05 04:05
思路:
要获得最大利益,其实就是找到相差最大的两个数,也就是求最优解的问题。
所以可以用动态规划。
动态规划:
1.找出最优解的性质,刻画其结构特征–特征就是找到当前数组最小值和当前最大利益
2.递归的定义最优值
3.自底向上方式计算最优值
4.根据计算最优值时得到的信息,构造一个最优解
public class Solution { public int maxProfit(int[] prices) { int maxProfit = 0, len = prices.length, i = 1; if(len < 1) { return 0; } int curMin = prices[0]; for(;i < len;i++) { curMin = Math.min(curMin,prices[i]); maxProfit = Math.max(maxProfit,prices[i] - curMin); } return maxProfit; }}
0 0
- leetcode-java-121. Best Time to Buy and Sell Stock
- 【leetcode】121. Best Time to Buy and Sell Stock【java】
- LeetCode-121. Best Time to Buy and Sell Stock (Java)
- [leetcode]121. Best Time to Buy and Sell Stock(Java)
- [Leetcode] Best Time to Buy and Sell Stock (Java)
- [Leetcode] Best Time to Buy and Sell Stock II (Java)
- [Leetcode] Best Time to Buy and Sell Stock III (Java)
- Leetcode-Best Time to Buy and Sell Stock -java
- leetcode java Best Time to Buy and Sell Stock
- LeetCode(121) Best Time to Buy and Sell Stock (Java)
- Best Time to Buy and Sell Stock IV Leetcode Java
- [LeetCode][Java] Best Time to Buy and Sell Stock
- [LeetCode][Java] Best Time to Buy and Sell Stock IV
- [LeetCOde][Java] Best Time to Buy and Sell Stock III
- leetcode:Best Time to Buy and Sell Stock 【Java】
- leetcode:Best Time to Buy and Sell Stock II 【Java】
- LeetCode 121 -Best Time to Buy and Sell Stock ( JAVA )
- LeetCode Best Time to Buy and Sell Stock JAVA
- Spring中的循环依赖问题介绍及解决方法
- xml笔记
- Android SwipeRefreshLayout 谷歌自己下拉刷新组件
- 第十五周程序-阅读程序-2
- 腾讯实习mini项目总结
- leetcode-java-121. Best Time to Buy and Sell Stock
- Menu菜单方法
- Eclipse快捷键
- 博客搬家~
- 智能电视、数字家庭、智慧城市的三位一体化<中层设计>
- 《App后台开发运维和架构实践》推荐序
- Docker Swarm搭建生产环境的集群
- 深度学习之CNN一 卷积与池化
- 亿级Web系统搭建:单机到分布式集群