解题思路:best time to buy and sell stock i && ii && iii
这三道题都是同一个背景下的变形:给定一个数组,数组里的值表示当日的股票价格,问你如何通过爱情买卖来发家致富?
best time to buy and sell stock i:
最多允许买卖一次
best time to buy and sell stock ii:
不限制买卖次数
best time to buy and sell stock iii:
最多允许买卖两次
对于i:
思路就是在第i个位置抛出时,希望在0~i-1个位置上的最低价买入,才能使得第i个位置的收益最大。然后比较所有可以抛出的位置i,返回最大的收益。
int maxP = 0, minV = prices[0];
for(int i = 1;i<prices.size();i++){
maxP = max(maxP,prices[i]-minV);
minV = min(minV,prices[i]);
}
return maxP;
对于ii:
由于可以无限买,所以思路就是只要赚钱我就卖掉。你可能会问,如果我在i天卖掉,发现i+1天能赚更多,怎么破?血亏!在线等!当然就是在第i天再买入,第i+1天再卖出,虽然规则上不允许同一天有多次买卖,但是这样其实等效于i-1天买入,i天划水,i+1天卖出。
int maxP = 0;
for(int i = 1;i<prices.size();i++){
if(prices[i]-prices[i-1] > 0) maxP+=(prices[i] - prices[i-1]);
}
return maxP;
对于iii:
同样不需要额外的空间,而且只需要O(n)的空间复杂度。思路就是我一开始没有钱,buy1是我第一次买股票之后剩的钱(肯定是负的,我白手起家,借钱买股票啊),sell1是我卖完第一次买的股票之后剩的钱(>=0),buy2是我用上回挣得钱(有可能需要找别人再借一点)买第二次股票,sell2是我卖完第二次股票之后剩下的钱。
int buy1 = INT_MIN, sell1 = 0, buy2 = INT_MIN, sell2 = 0;
for(auto p:prices){
buy1 = max(buy1,-p);
sell1 = max(sell1,buy1+p);
buy2 = max(buy2,sell1-p);
sell2 = max(sell2, buy2+p);
}
return sell2;
解题思路:best time to buy and sell stock i && ii && iii的更多相关文章
- leetcode day6 -- String to Integer (atoi) && Best Time to Buy and Sell Stock I II III
1. String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully con ...
- [Leetcode][JAVA] Best Time to Buy and Sell Stock I, II, III
Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price of a gi ...
- [LeetCode] 递推思想的美妙 Best Time to Buy and Sell Stock I, II, III O(n) 解法
题记:在求最大最小值的类似题目中,递推思想的奇妙之处,在于递推过程也就是比较求值的过程,从而做到一次遍历得到结果. LeetCode 上面的这三道题最能展现递推思想的美丽之处了. 题1 Best Ti ...
- LeetCode:Best Time to Buy and Sell Stock I II III
LeetCode:Best Time to Buy and Sell Stock Say you have an array for which the ith element is the pric ...
- Best Time to Buy and Sell Stock I II III
Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price of a gi ...
- Best Time to Buy and Sell Stock I,II,III [leetcode]
Best Time to Buy and Sell Stock I 你只能一个操作:维修preMin拍摄前最少发生值 代码例如以下: int maxProfit(vector<int> & ...
- LeetCode之“动态规划”:Best Time to Buy and Sell Stock I && II && III && IV
Best Time to Buy and Sell Stock I 题目链接 题目要求: Say you have an array for which the ith element is the ...
- Best Time to Buy and Sell Stock I II III IV
一.Best Time to Buy and Sell Stock I Say you have an array for which the ith element is the price of ...
- LeetCode解题报告—— Best Time to Buy and Sell Stock
Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price of a gi ...
随机推荐
- [最短路][部分转] P1073 最优贸易
题目描述 C 国有 n 个大城市和 m 条道路,每条道路连接这 n 个城市中的某两个城市.任意两个 城市之间最多只有一条道路直接相连.这 m 条道路中有一部分为单向通行的道路,一部分 为双向通行的道路 ...
- C#学习笔记随笔(1)----C#中static关键字的作用
静态分配的,有两种情况: 1. 用在类里的属性.方法前面,这样的静态属性与方法不需要创建实例就能访问, 通过类名或对象名都能访问它,静态属性.方法只有“一份”:即如果一个类新建有N个 对象,这N 个对 ...
- 杭电ACM题单
杭电acm题目分类版本1 1002 简单的大数 1003 DP经典问题,最大连续子段和 1004 简单题 1005 找规律(循环点) 1006 感觉有点BT的题,我到现在还没过 1007 经典问题,最 ...
- C语言之循环次数
#include<stdio.h>int main(){int num,count=0,i=0,ret=0;scanf("%d",&num);while(num ...
- Dom模型
1.dom之对象分类 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...
- dnsmasq服务的安装与配置
在ubuntu16.04上安装dnsmasq服务,在本地做泛域名解析 安装 $ apt-get install dnsmasq -y $ /etc/init.d/dnsmasq start 配置 Dn ...
- python解释执行原理(转载)
Python解释执行原理 转自:http://l62s.iteye.com/blog/1481421 这里的解释执行是相对于编译执行而言的.我们都知道,使用C/C++之类的编译性语言编写的程序,是需要 ...
- Java集合(5)一 HashMap与HashSet
目录 Java集合(1)一 集合框架 Java集合(2)一 ArrayList 与 LinkList Java集合(3)一 红黑树.TreeMap与TreeSet(上) Java集合(4)一 红黑树. ...
- UWP Listview 多选
最近在做一个项目的时候,用到了Listview,需要选择一个item,来进行删除. 但是当开启了 PullToRefreshListViewControl.IsMultiSelectCheckBoxE ...
- c语言的发展历程
C语言的发展颇为有趣.它的原型ALGOL 60语言. 1963年,剑桥大学将ALGOL 60语言发展成为CPL(Combined Programming Language)语言. 1967年,剑桥大学 ...