【一天一道LeetCode】#122. Best Time to Buy and Sell Stock II
一天一道LeetCode
本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github
欢迎大家关注我的新浪微博,我的新浪微博
欢迎转载,转载请注明出处
(一)题目
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).
(二)解题
题目大意:相比于上题【一天一道LeetCode】#121. Best Time to Buy and Sell Stock来说,本题有多次买卖的机会。不过,买完之后卖了才能继续买。
解题思路:本题可以采用贪心算法,每次买卖都获取当前的最优值。
思路很简单,每次买卖取得利益最大化,即递增区间,一旦破坏了递增就卖,然后买下一个。遍历完了之后就获得了整体的利益最大化。
具体解释见代码:
class Solution {
public:
int maxProfit(vector<int>& prices) {
int size = prices.size();
int max = 0;
for(int i = 0 ; i < size ; )
{
int j = i;
while(j+1<size&&prices[j+1]>prices[j]) j++;//一旦破坏了递增就停下来
if(j==i) i++;//一开始就不递增,就直接考虑下一个
else {
max+=prices[j]-prices[i];//计算当前最大利润,叠加到全部利润上
i = j+1;//买下一个
}
}
return max;
}
};
【一天一道LeetCode】#122. Best Time to Buy and Sell Stock II的更多相关文章
- 31. leetcode 122. Best Time to Buy and Sell Stock II
122. Best Time to Buy and Sell Stock II Say you have an array for which the ith element is the price ...
- [LeetCode] 122. Best Time to Buy and Sell Stock II 买卖股票的最佳时间 II
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- LeetCode 122. Best Time to Buy and Sell Stock II (买卖股票的最好时机之二)
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- leetcode 122. Best Time to Buy and Sell Stock II ----- java
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- Java [Leetcode 122]Best Time to Buy and Sell Stock II
题目描述: Say you have an array for which the ith element is the price of a given stock on day i. Design ...
- LeetCode 122 Best Time to Buy and Sell Stock II 解题报告
题目要求 Say you have an array for which the ith element is the price of a given stock on day i. Design ...
- [leetcode]122. Best Time to Buy and Sell Stock II 最佳炒股时机之二
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- Java for LeetCode 122 Best Time to Buy and Sell Stock II
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- LeetCode 122. Best Time to Buy and Sell Stock II (stock problem)
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- LeetCode 122 Best Time to Buy and Sell Stock II(股票买入卖出的最佳时间 II)
翻译 话说你有一个数组,当中第i个元素表示第i天的股票价格. 设计一个算法以找到最大利润. 你能够尽可能多的进行交易(比如.多次买入卖出股票). 然而,你不能在同一时间来多次交易. (比如.你必须在下 ...
随机推荐
- Struts2 之 modelDriven & prepare 拦截器详解
struts2 ModelDriven & Prepareable 拦截器 前面对于 Struts2 的开发环境的搭建.配置文件以及 Struts2 的值栈都已经进行过叙述了!这次博文我们讲解 ...
- 天梯赛-L1-018. 大笨钟
L1-018. 大笨钟 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 微博上有个自称"大笨钟V"的家伙,每 ...
- vue项目开发中遇到的问题总结--内部分享
1.路由变化页面数据不刷新问题 这种情况一般出现在vue-router的history模式下,初次进入会执行钩子函数,再次进入时则不会. 解决方案: 监听路由变化 watch : { "$ ...
- Oracle中的行转列例子详解
--场景1: A B a a a b b 希望实现如下效果: a ,, b , create table tmp as B from dual union all B from dual union ...
- 剑指架构师系列-持续集成之Maven实现项目的编译、发布和部署
Maven组织项目进行编译.部署 Maven项目基本的结构说明如下: mazhi // 控制所有荐的编译.部署.发布 mazhi-app-parent // 项目的父项目,有一些公共的设置可以被子 ...
- ACM Ignatius and the Princess II
Problem Description Now our hero finds the door to the BEelzebub feng5166. He opens the door and fin ...
- Docker常见仓库CentOS
CentOS 基本信息 CentOS 是流行的 Linux 发行版,其软件包大多跟 RedHat 系列保持一致. 该仓库提供了 CentOS 从 5 ~ 7 各个版本的镜像. 使用方法 默认会启动一个 ...
- 记一次MySQL删库的数据恢复
昨天因为不可描述的原因,数据库直接被 drop database删除.在第一时间停止数据库服务和Web服务,备份MySQL数据目录下的所有文件之后,开始走上数据恢复之路. 第一次干这种事,各种不得法. ...
- Bootstrap3 排版-列表
无序列表 排列顺序无关紧要的一列元素. <ul> <li>...</li> </ul> 有序列表 顺序至关重要的一组元素. <ol> < ...
- [boost] Windows下编译
编译命令 32位 编译 bjam variant=release link=static threading=multi runtime-link=static -a -q bjam variant= ...