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 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.
Example 3:
Input: [7,6,4,3,1]
Output: 0
Explanation: In this case, no transaction is done, i.e. max profit = 0.
package leetcode.easy; public class BestTimeToBuyAndSellStockII {
@org.junit.Test
public void test() {
int[] prices1 = { 7, 1, 5, 3, 6, 4 };
int[] prices2 = { 1, 2, 3, 4, 5 };
int[] prices3 = { 7, 6, 4, 3, 1 };
System.out.println(maxProfit1(prices1));
System.out.println(maxProfit1(prices2));
System.out.println(maxProfit1(prices3));
System.out.println(maxProfit2(prices1));
System.out.println(maxProfit2(prices2));
System.out.println(maxProfit2(prices3));
System.out.println(maxProfit3(prices1));
System.out.println(maxProfit3(prices2));
System.out.println(maxProfit3(prices3));
} public int maxProfit1(int[] prices) {
return calculate(prices, 0);
} public int calculate(int prices[], int s) {
if (s >= prices.length) {
return 0;
}
int max = 0;
for (int start = s; start < prices.length; start++) {
int maxprofit = 0;
for (int i = start + 1; i < prices.length; i++) {
if (prices[start] < prices[i]) {
int profit = calculate(prices, i + 1) + prices[i] - prices[start];
if (profit > maxprofit) {
maxprofit = profit;
}
}
}
if (maxprofit > max) {
max = maxprofit;
}
}
return max;
} public int maxProfit2(int[] prices) {
if (null == prices || 0 == prices.length) {
return 0;
}
int i = 0;
int valley = prices[0];
int peak = prices[0];
int maxprofit = 0;
while (i < prices.length - 1) {
while (i < prices.length - 1 && prices[i] >= 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;
} public int maxProfit3(int[] prices) {
int maxprofit = 0;
for (int i = 1; i < prices.length; i++) {
if (prices[i] > prices[i - 1]) {
maxprofit += prices[i] - prices[i - 1];
}
}
return maxprofit;
}
}
LeetCode_122. Best Time to Buy and Sell Stock II的更多相关文章
- [LintCode] 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 —— 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 ...
- 27. Best Time to Buy and Sell Stock && Best Time to Buy and Sell Stock II && Best Time to Buy and Sell Stock III
Best Time to Buy and Sell Stock (onlineJudge: https://oj.leetcode.com/problems/best-time-to-buy-and- ...
- 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 pric ...
- 【leetcode】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 ...
- 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: Best Time to Buy and Sell Stock II 解题报告
Best Time to Buy and Sell Stock IIQuestion SolutionSay you have an array for which the ith element i ...
- Algorithm - 贪心算法使用场景 ( LEETCODE —— Best Time to Buy and Sell Stock II)
先看一道leetcode题: Best Time to Buy and Sell Stock II Say you have an array for which the ith element is ...
- 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 题目的要求是只买卖一次,买的价格越低,卖的价格越高,肯定收益就越大 遍历整个数组,维护一个当前位置之前最低的买入价格,然后每次 ...
随机推荐
- P1338 末日的传说[水题]
题目描述 只要是参加jsoi活动的同学一定都听说过Hanoi塔的传说:三根柱子上的金片每天被移动一次,当所有的金片都被移完之后,世界末日也就随之降临了. 在古老东方的幻想乡,人们都采用一种奇特的方式记 ...
- 操作socket笔记
网络编程 1.tcp协议 #tcpserver #单纯一对一发 tcp = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # 创建tcp套接字 参 ...
- 前端学习笔记--CSS3
本本记录了css3的样式:浏览器支持度.圆角边框.阴影.文字与文本.过渡.动画.2d旋转.3d旋转 浏览器支持度: 1.圆角边框 例:只要确定了x.y值,就能知道弧度 画一个圆形:长=宽,border ...
- python_反射:应用
class User(object): def denglu(self): print('欢迎来到登录页面!') def zhuce(self): print('欢迎来到注册页面!') def you ...
- 【轉】mantis安裝
一.mantis简介 可以看出,mantis是一个基于php技术的,个人觉得这个系统还是很完善的. 安装mantis,需要安装一下软件: phpMyAdmin 下载地址https://w ...
- TDOA 之 基站逻辑代码实现
在前一篇博文里描述了基站的逻辑部分,这里贴出来具体代码实现.https://www.cnblogs.com/tuzhuke/p/11689881.html 1 Sync 信息部分 case 'S': ...
- HR#7 题解
T1 签到题 #include<bits/stdc++.h> #define R register int using namespace std; inline int g() { R ...
- E:first-of-type
E:first-of-type 语法: E:first-of-type { sRules } 说明: 匹配同类型中的第一个同级兄弟元素E.大理石平台[1200mm*1000mm*150mm] 要使该属 ...
- MongoDB 分片键分类与数据分发
In sharded clusters, if you do not use the _id field as the shard key, then your application must en ...
- java实现大视频上传
javaweb上传文件 上传文件的jsp中的部分 上传文件同样可以使用form表单向后端发请求,也可以使用 ajax向后端发请求 1.通过form表单向后端发送请求 <form id=" ...