122. Best Time to Buy and Sell Stock II
来源:互联网 发布:员工管理系统数组java 编辑:程序博客网 时间:2024/06/05 04:24
题目
122. Best Time to Buy and Sell Stock II
解题思路
这道题tag是greedy,但我还没仔细学过贪心算法,就先讲下解题思路。
这道题是要保证买入和卖出的差价最大,设买入的下标是buyprice_index,累计最多能赚取max,都初始化为0。
根据数组长度分情况讨论,
当长度≤1,返回0;
当长度=2,则看price[2] - price[1]是不是大于0,大于0返回price[2] - price[1],否则返回0;
当长度>2,则从第2个元素开始遍历到倒数第2个元素,
price[i] - price[buyprice_index] < 0,说明可以当前价格比之前低,比之前更适合买入,因此:
buyprice_index = i 设为当前price的下标
price[i] - price[buyprice_index] > 0,这个时候价格比之前高,适合考虑卖出而不是买入,并且要考虑是当前就可以卖出,还是看price[i+1]会不会比price[i]还高价更适合卖出,这时候要对price[i] - price[buyprice_index]和price[i+1] - price[buyprice_index]做比较:
price[i] - price[buyprice_index] > price[i+1] - price[buyprice_index]:
说明当前的i比i+1价格高,更适合卖出,i+1价格较低,i卖出后,可以考虑买入:
max += prices[i] - prices[buyprice_index];buyprice_index = i+1;
price[i] - price[buyprice_index] < price[i+1] - price[buyprice_index]:
i+1的价格比i的高,i+1可能比i更适合卖出,但由于可能i+2,i+3有更高价格,所以这里先不做任何交易,继续遍历下去直到找到最高价
遍历到倒数第2个元素后遍历结束,查看由于最后一个元素未被遍历,很可能在前面的buyprice_index买入后,适合在最后一个元素被卖出,检测一下最后一个元素是否符合卖出的条件:
是否符合prices[prices.size() - 1] - prices[buyprice_index] > 0
具体代码
class Solution {public: int maxProfit(vector<int>& prices) { if (prices.empty() || prices.size() == 1) return 0; if (prices.size() == 2) { return prices[1] - prices[0] > 0 ? prices[1] - prices[0] : 0; } int max = 0; int buyprice_index = 0; for (int i = 1; i < prices.size() - 1; i++) { if (prices[i] - prices[buyprice_index] <= 0) buyprice_index = i; else { if (prices[i] - prices[buyprice_index] >= prices[i+1] - prices[buyprice_index]) { max += prices[i] - prices[buyprice_index]; buyprice_index = i+1; i++; } } } if (buyprice_index < prices.size() - 1 && prices[prices.size() - 1] - prices[buyprice_index] > 0) { max += prices[prices.size() - 1] - prices[buyprice_index]; } return max; }};
ps: 中间卡过一个bug才知道当vector为空时,size()-1不是等于-1而是发送一出, vector::size是size_type,可表示很大的容器长度且是unsigned。
P S !!!!
大佬的答案
以下:
public class Solution {public int maxProfit(int[] prices) { int total = 0; for (int i=0; i< prices.length-1; i++) { if (prices[i+1]>prices[i]) total += prices[i+1]-prices[i]; } return total;}
我承认我长了个假脑子qaq
- 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] 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】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
- 122. Best Time to Buy and Sell Stock II
- 122. Best Time to Buy and Sell Stock II
- 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 122. Best Time to Buy and Sell Stock II
- leetcode 122. Best Time to Buy and Sell Stock II
- 系统间通信——RPC实例Apache Thrift
- 解决SQLSERVER查询连接数据库表问题:OLE DB 访问接口 "MSDASQL" 为列提供的元数据不一致
- 转载 基于bayer型阵列的空间域插值
- GKStateMaching
- qt5.8+vs2015使用Qt5WebEngine搭建环境
- 122. Best Time to Buy and Sell Stock II
- Improving Deep Neural Networks Optimization Methods Homework
- TableView顶部空出20像素
- mongoDB也许你不懂得缩写
- C#Dictionary 字典的用法
- 优雅降级和渐进增强的区别
- ORACLE中的rowid用法
- No mapping found for HTTP request with URI [XXX]
- iOS