leetcode-Best Time to Buy and Sell Stock II
来源:互联网 发布:ad软件视频教程 编辑:程序博客网 时间:2024/05/16 04:00
Say you have an array for which the ith element is the price of a given stock on day i.
Design an algorithm to find the maximum profit. 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).
思路:找出连续递增的子序列
代码:
int maxProfit(vector<int> &prices) {
int len=prices.size();
if(len<=1)
{
return 0;
}
int p=0;
int q=0;
int maxPro=0;
while(q<len-1)
{
if(prices[q]<prices[q+1])
{
++q;
}
else
{
maxPro+=prices[q]-prices[p];
p=++q;
}
}
if(prices[q]>prices[q-1])
{
maxPro+=prices[q]-prices[p];
}
return maxPro;
}
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
- java编程怎么避免内存溢出
- Java的JDK安装以及其环境配置
- sed 多行替换,多行模式处理字符串;一次替换
- [转载]Groovy 和 JAVA 比较
- Stanford coreNLP 出现 in thread "main" java.lang.OutOfMemoryError: Java heap space
- leetcode-Best Time to Buy and Sell Stock II
- xen 网络设置
- Zookeeper集群环境安装过程详解
- 我们梦想新
- 第二章 D - Number Sequence(1.5.10)
- android Configuration系统设置
- oracle RAC--归档日志的开启方法
- 断言使用,调试宏定义
- 经纬财富:西安7月16日金银交易须知