LeetCode Best Time to Buy and Sell Stock II
来源:互联网 发布:雅士尼处理器软件 编辑:程序博客网 时间:2024/06/05 16:27
屡明白就行了,就是找小的,然后找大的,没找到这样一个组合就进行一次交易,把利润加到结果里,继续找。假设小数是5大数是9,对于之间出现的按序的数比如6,7,8不影响结果,因此直接略过,但对于不安续出现的比如5,8,7,9这样的话因为可以进行两次交易,这样利润会更大。
int maxProfit(vector<int> &prices) {if(prices.size()==0||prices.size()==1)return 0;int sumprofit = 0;int buypoint=0,sellpoint=0;while(buypoint<prices.size()){while(buypoint<prices.size()-1&&prices[buypoint]>=prices[buypoint+1])buypoint++;sellpoint = buypoint + 1;if(sellpoint==prices.size())break;while(sellpoint<prices.size()-1&&prices[sellpoint]<=prices[sellpoint+1])sellpoint++;if(prices[sellpoint]>prices[buypoint])sumprofit += (prices[sellpoint]-prices[buypoint]);buypoint = sellpoint + 1;}return sumprofit;}
0 0
- [LeetCode]Best Time to Buy and Sell Stock II
- LeetCode Best Time to Buy and Sell Stock II
- Leetcode: Best Time to Buy and Sell Stock II
- [Leetcode] Best Time to Buy and Sell Stock II
- LeetCode : Best Time to Buy and Sell Stock II
- LeetCode Best Time to Buy and Sell Stock II
- [LeetCode] Best Time to Buy and Sell Stock II
- leetcode 61: Best Time to Buy and Sell Stock II
- [LeetCode] Best time to buy and sell a stock II
- 【leetcode】Best Time to Buy and Sell Stock II
- LeetCode - Best Time to Buy and Sell Stock II
- LeetCode之Best Time to Buy and Sell Stock II
- [LeetCode]Best Time to Buy and Sell Stock II
- [Leetcode]Best Time to Buy and Sell Stock II
- [leetcode]Best Time to Buy and Sell Stock II
- [LeetCode] Best Time to Buy and Sell Stock II
- LeetCode-Best Time to Buy and Sell Stock II
- [leetcode] Best Time to Buy and Sell Stock II
- 人际交流
- python数据结构与算法 6栈的应用之符号平衡(通用)
- 10400 - Game Show Math
- Sicily10359(优先队列)
- POJ 1195 Mobile phones
- LeetCode Best Time to Buy and Sell Stock II
- zoj 2975 Kinds of Fuwas(数学题)
- 12款很棒的浏览器兼容性测试工具推荐
- Handler的使用
- (libgdx学习)compass(指南针)
- 苹果帮助文档
- OllyDBG 入门系列(五)-消息断点及 RUN 跟踪
- 支持向量机的发展
- 图的邻接表表示