122. Best Time to Buy and Sell Stock II (Array)
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 (i.e., buy one and sell one share of the stock multiple times).
Note: You may not engage in multiple transactions at the same time (i.e., you must sell the stock before you buy again).
Example 1:
Input: [7,1,5,3,6,4]
Output: 7
Explanation: Buy on day 2 (price = 1) and sell on day 3 (price = 5), profit = 5-1 = 4.
Then buy on day 4 (price = 3) and sell on day 5 (price = 6), profit = 6-3 = 3.
Example 2:
Input: [1,2,3,4,5]
Output: 4
Explanation: Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 5-1 = 4.
Note that you cannot buy on day 1, buy on day 2 and sell them later, as you are
engaging multiple transactions at the same time. You must sell before buying again.
##### 分析:
和另外一道类似,但是不同的在于可以多次买卖,但是手中同时进行的交易不能大于1个。
###### 方法一:
用max保存最大值,min保存最小值,res保存临时收益,profit保存最终收益,当当前价格比max小时应该结束当前交易,从当前价格买入,然后更新min,max都是为当前价格,temp加入profit,temp归零;如果当前价格比max大时,继续当前交易,更新max。时间复杂度O(n),空间复杂度O(1)
public int maxProfit(int[] prices) {
if(prices==null ||prices.length==0)
return 0;
int min=prices[0], max=prices[0];
int profit=0;
int temp = max-min;
for(int i=1;i<prices.length;i++){
if(prices[i]<max){
temp = max-min;
profit += temp;
temp = 0;
min = prices[i];
max = prices[i];
}
else{
max = prices[i];
temp = max-min;
max = prices[i];
}
}
profit += temp;
return profit;
}
###### 方法二:Peak Valley Approach
TotalProfit=∑i(height(peaki)−height(valleyi))
和方法一差不多,在一个while循环中首先找valley,遇到升高就开始找peak.再次降低结束这次循环。继续开始搜索下一个上坡。
Time complexity : O(n)O(n). Single pass.
Space complexity : O(1)O(1). Constant space required.
public int maxProfit(int[] prices) {
int i = 0;
int valley www.dasheng178.com= prices[0];
int peak = prices[0];
int maxprofit = 0;
while (i < prices.length - 1) {
while (i < prices.length - 1 && prices[www.xiaomiyulezc.com ] >= prices[i + 1])
i++;
valley = prices[i];
while (i < prices.length - 1 && prices[i] <= prices[i + 1])
i++;
peak = prices[i];
maxprofit += peak - valley;
}
return maxprofit;
}
方法三:
只关注上坡,然后将每一段的profit相加
Time complexity : O(n)O(n). Single pass.
Space complexity : O(1)O(1). Constant space required.
public int maxProfit(int[] prices) {
int maxprofit www.michenggw.com= 0;
for (int i = 1; i <www.mhylpt.com/ prices.length; i++) {
if (prices[i] > prices[i - 1])
maxprofit += prices[i] - prices[i - 1];
}
return maxprofit;
122. Best Time to Buy and Sell Stock II (Array)的更多相关文章
- 122. Best Time to Buy and Sell Stock II (Array;Greedy)
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- 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 121. Best Time to Buy and Sell Stock 、122.Best Time to Buy and Sell Stock II 、309. Best Time to Buy and Sell Stock with Cooldown
121. Best Time to Buy and Sell Stock 题目的要求是只买卖一次,买的价格越低,卖的价格越高,肯定收益就越大 遍历整个数组,维护一个当前位置之前最低的买入价格,然后每次 ...
- 122. Best Time to Buy and Sell Stock II@python
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)解答
转载请注明出处:z_zhaojun的博客 原文地址 题目地址 Best Time to Buy and Sell Stock II Say you have an array for which th ...
- 【刷题-LeetCode】122 Best Time to Buy and Sell Stock II
Best Time to Buy and Sell Stock II Say you have an array for which the ith element is the price of a ...
- 【一天一道LeetCode】#122. Best Time to Buy and Sell Stock II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Say you ...
- [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 ...
- !!!!!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 ...
随机推荐
- swoole 相关
安装虚拟机 VMware Workstation Pro 安装CentOS CentOS-7-x86_64-Minimal-1708.iso 安装FinalShell 教程地址 安装lnmp 教程地址 ...
- ASP.NET数据库连接
启动Visual Studio,新建一个web工程 点开工程目录下web.config文件, 找到节点,新增数据库配置 aspx界面新建一个button和一个文本框用于测试数据库连接, 其中butto ...
- 「日常温习」Hungary算法解决二分图相关问题
前言 二分图的重点在于建模.以下的题目大家可以清晰的看出来这一点.代码相似度很高,但是思路基本上是各不相同. 题目 HDU 1179 Ollivanders: Makers of Fine Wands ...
- Python汉诺塔问题递归算法与程序
汉诺塔问题: 问题来源:汉诺塔来源于印度传说的一个故事,上帝创造世界时作了三根金刚石柱子,在一根柱子上从上往下从小到大顺序摞着64片黄金圆盘.上帝命令婆罗门把圆盘从下面开始按大小顺序重新摆放在另一根柱 ...
- Python元组与列表的区别和联系?
1. 元组和列表比较相似,不过它们之间也有着不同: (1)列表:一个大仓库,你可以随时往里边添加和删除任何东西. (2)元组:封闭的列表,一旦定义,就不可改变(不能添加.删除或修改). 2. 什么情 ...
- 油田 (Oil Deposits UVA - 572)
题目描述: 原题:https://vjudge.net/problem/UVA-572 题目思路: 1.图的DFS遍历 2.二重循环找到相邻的八个格子 AC代码: #include <iostr ...
- python3 SQLAlchemy模块使用
更详细的操作介绍:https://www.imooc.com/article/22343 定义: SQLAlchemy是Python编程语言下的一款ORM框架,该框架建立在数据库API之上,使用关系对 ...
- 关于excle导数据的一些代码笔记
package com.bonc.util; import java.io.File; import java.io.FileInputStream; import java.io.FileOutpu ...
- 词嵌入向量WordEmbedding
词嵌入向量WordEmbedding的原理和生成方法 WordEmbedding 词嵌入向量(WordEmbedding)是NLP里面一个重要的概念,我们可以利用WordEmbedding将一个单 ...
- Agri-Net(最小生成树)
Description Farmer John has been elected mayor of his town! One of his campaign promises was to brin ...