110_leetcode_Best Time to Buy and sell Stock II
来源:互联网 发布:知乎 韩信 刘邦男宠 编辑:程序博客网 时间:2024/06/16 19:44
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).
1:注意特殊情况;2:找到数组相邻的凹点和凸点;3:两者的差值是当前的最大值;4:在查找凸凹值的时候注意边界
int maxProfit(vector<int> &prices) { if(prices.size() <= 1) { return 0; } int maxValue = 0; int start = 0; int end = 0; int size = (int)prices.size(); while(start < size) { while(start < size - 1 && prices[start] >= prices[start + 1]) { start++; } end = start + 1; while(end < size - 1 && prices[end] <= prices[end + 1]) { end++; } if(end == size) { break; } else { maxValue += prices[end] - prices[start]; } start = end + 1; } return maxValue; }
0 0
- 110_leetcode_Best Time to Buy and sell Stock II
- 99_leetcode_Best Time to Buy and sell Stock
- 111_leetcode_Best Time to Buy and Sell Stock III
- [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
- 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
- Best Time to Buy and Sell Stock II
- 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
- Best Time to Buy and Sell Stock II
- IOS下载程序自动安装
- 设计模式(2)——组合模式(Composite Pattern)
- win7中用 httplistener 出现 503 错误的问题
- 针对PHPCMS绑定二级域名地址不正确问题分析
- clone报错:afmkinit.sh失败error while loading shared libraries:librt.so.1
- 110_leetcode_Best Time to Buy and sell Stock II
- easyDialog v2.0
- 简要分析如何对汽车的起动机进行检修
- centos 安装vm74 + jeffy插件
- 深入理解JavaScript系列 ----(7):SOLID五大原则之开闭原则OCP
- 最长上升子序列(模板)
- 导入导出封装工具类(二) jXLS Excel报表生成工具类
- VC2010中"Include Directories" 和 "Additional Include Directories"的区别
- C++ primer (5th) 随想与学习笔记 8