【一天一道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天的股票价格. 设计一个算法以找到最大利润. 你能够尽可能多的进行交易(比如.多次买入卖出股票). 然而,你不能在同一时间来多次交易. (比如.你必须在下 ...
随机推荐
- JVM指令集介绍
转载自:http://glutinit.iteye.com/blog/1263446 延伸参考 JVM接收参数和方法调用 void spin() { int i; for (i = 0 ...
- ignorable tips
枚举 索引从0开始 sort 默认升序排列 Array.Sort(intSort); //复制数组 Array.Copy(intSort,intNew,3); intsort 源数组 intnew ...
- 虚拟机搭建Zookeeper服务器集群完整笔记
虚拟机搭建Zookeeper服务器集群完整笔记 本笔记主要记录自己搭建Zookeeper服务器的全过程,默认已经安装部署好Centos7. 一.虚拟机下Centos无法联网解决方案 1.首先调整虚拟机 ...
- Linux下安装 mysql 5.7
安装环境:系统是 centos6.5 1.下载 下载地址:https://dev.mysql.com/downloads/file/?id=467556 下载版本:我这里选择的57.17,通用版,li ...
- Ubuntu14.04和Windows双系统时无法挂载磁盘解决方法
基本状况:我电脑Ubuntu14.04 和 Windows10 双系统,一个固态磁盘,一个机械磁盘.Ubuntu14.04装固态里面了,固态里没有Windows内容. 问题:Ubuntu14.04系统 ...
- 重写轮子之 GaussionNB
我仿照sk-learn 中 GaussionNB 的结构, 重写了该算法的轮子,命名为 MyGaussionNB, 如下: # !/usr/bin/python # -*- coding:utf-8 ...
- rhel7 启动网络
我装的是rhel7 服务器版本(在virtualbox虚拟机里),安装后默认不启动网络,另外还有很多命令也不能用,比如ifconfig, yum-config-manager等. 先解决网络问题: 切 ...
- Oracle中打印99乘法表的13种方法
--实现1: select r1 || '*' || r1 || '=' || r1 * r1 A, decode(r2, '', '', r2 || '*' || r1 || '=' || r2 * ...
- Oracle中case用法总结
--case语句的种类: .简单case语句 语法: case exp when comexp then returnvalue ... when comexp then returnvalue el ...
- iOS不能交互的几种情况
alpha <=0.01 hidden = YES userInteraction = NO 父试图不允许交互,子试图也不允许交互: 在父试图可见范围内,可以交互,超出部分失效,不能交互