LeetCode题解-Best Time to Buy and Sell Stock II

来源:互联网 发布:淘宝上新抢拍怎么快 编辑:程序博客网 时间:2024/06/06 03:06

在Best Time to Buy and Sell Stock 基础上添加了新的条件:You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times). However, you may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).即:可以多次交易,但是手中只能有一只股票。

结题思路:只要有差价,即买入卖出(低买高卖)
代码如下:
<span style="font-family:KaiTi_GB2312;font-size:18px;">int maxProfit(vector<int>& prices) {       int size=prices.size();if(size<=1)return 0;int sum=0;for (int i=1;i<size;i++)if(prices[i-1]<prices[i])sum+=prices[i]-prices[i-1];return sum;     }</span>



0 0
原创粉丝点击