【一天一道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天的股票价格. 设计一个算法以找到最大利润. 你能够尽可能多的进行交易(比如.多次买入卖出股票). 然而,你不能在同一时间来多次交易. (比如.你必须在下 ...
随机推荐
- TeamForge使用指南
1.什么是TeamForge 可以把TeamForge简单的理解为另外一种github 2.TeamForge的地址 与Project有关,一般会有明确的Link 3.TeamForge登录 用户名和 ...
- OpenCV环境搭建(一)
此环境搭建是OpenCV的python(一下简称py)开发环境搭建,建立在py3的环境和语法上实现的. windows系统搭建 系统环境:windows 10 + python 3.6 + OpenC ...
- java 获得当前时间前指定几个小时的时间?
//@2016-12-13 获取当前时间,指定前面多少小时的时间 //返回格式YYYYMMDDHHMMSS public static String getBeforeHourTime(int iho ...
- 初识Redis系列之一:简单介绍
一:Redis是什么? Redis全称:REmote DIctionary Server(Redis) .Redis是一个由Salvatore Sanfilippo写的key-value存储系统,AN ...
- 2. struct A 和 typedef struct A
2. struct A 和 typedef struct A 2.1 struct A struct A{}定义一个名为struct A的结构体. 下例定义了struct A同时,声明了两个变量(注意 ...
- 【机器学习】从SVM到SVR
注:最近在工作中,高频率的接触到了SVM模型,而且还有使用SVM模型做回归的情况,即SVR.另外考虑到自己从第一次知道这个模型到现在也差不多两年时间了,从最开始的腾云驾雾到现在有了一点直观的认识,花费 ...
- [CSDN_Markdown]Markdown基本语法2
简介 前文 Markdown基本语法 中介绍了Markdown的基本语法,知道那些基本的语法,实际上已经可以写纯文本的博客了.对我们这群写代码的人或者将要写代码的人来说,貌似这些还不够,我们还希望能插 ...
- Hadoop 伪分布式安装、运行测试例子
1. 配置linux系统环境 centos 6.4 下载地址:http://pan.baidu.com/s/1geoSWuv[VMWare专用CentOS.rar](安装打包好的VM压缩包) 并配置虚 ...
- 高性能的Redis代理TwemProxy
TwemProxy是一个Redis的中间件代理,具有很多有用的功能,可以暂时替代一部分Redis Cluster的功能: ² 支持和6479.之后相应地,配置好两个Redis实例并启动.现在就可以启 ...
- YYModel V1.0.4源码解析
YYKit出现了很长时间了,一直想要详细解析一下它的源码,都是各种缘由推迟了. 最近稍微闲了一点,决定先从最简单的YYModel开始吧. 首先,我也先去搜索了一下YYModel相关的文章,解析主要AP ...