LeetCode 122. Best Time to Buy and Sell Stock II
来源:互联网 发布:java开源架构 编辑:程序博客网 时间:2024/05/29 16:50
描述
给出一支股票在每天的价格,求在允许多次交易的情况下可以获得的最大收益
解决
记录当前天的最小值,遇到比当前天价格高的就卖出,然后重新买入,买入之前先卖出。
class Solution {public: int maxProfit(vector<int>& prices) { int lenth = prices.size(); int tmp = 0x3f3f3f, res = 0; for (int i = 0; i < lenth; ++i){ if (prices[i] <= tmp){ tmp = prices[i]; } else if (prices[i] > tmp){ res += prices[i] - tmp; tmp = prices[i]; } } return res; }};
0 0
- LeetCode 122. Best Time to Buy and Sell Stock II
- [Leetcode] 122. Best Time to Buy and Sell Stock II
- [leetcode] 122.Best Time to Buy and Sell Stock II
- 【leetcode】122.Best Time to Buy and Sell Stock II
- 【LeetCode】122.Best Time to Buy and Sell Stock II
- [leetcode] 122. Best Time to Buy and Sell Stock II
- LeetCode #122. Best Time to Buy and Sell Stock II
- 122. Best Time to Buy and Sell Stock II LeetCode
- [LeetCode]122. Best Time to Buy and Sell Stock II
- Leetcode 122. Best Time to Buy and Sell Stock II
- leetcode 122. Best Time to Buy and Sell Stock II
- LeetCode 122. Best Time to Buy and Sell Stock II
- LeetCode *** 122. Best Time to Buy and Sell Stock II
- leetcode-122. Best Time to Buy and Sell Stock II
- leetcode 122. Best Time to Buy and Sell Stock II
- leetcode 122. Best Time to Buy and Sell Stock II
- 【LeetCode】122. Best Time to Buy and Sell Stock II
- LeetCode - 122. Best Time to Buy and Sell Stock II
- java实现顺序栈
- RelativeLayout 属性含义
- android开发 重启app
- ServletContext与网站计数器
- 讓iframe自適應高度
- LeetCode 122. Best Time to Buy and Sell Stock II
- Android源码解析系列
- PHP按比例生成縮略圖片
- 详读List,Map源码心得笔记
- HDU 1213 How Many Tables
- JVM实用参数-新生代垃圾回收算法
- Android初级教程反射+AIDL+内容观察者监控黑名单号码代码模板
- jQuery 對table的基本操作
- Android 工作记录一