Skip to content

Commit 09ec18b

Browse files
author
applewjg
committed
BestTimetoBuyandSellStock*
Change-Id: I37a3b8ddeecc253528579180fadfaa8c996c6944
1 parent a23170f commit 09ec18b

File tree

3 files changed

+90
-0
lines changed

3 files changed

+90
-0
lines changed

BestTimetoBuyandSellStock.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
Author: King, [email protected]
3+
Date: Oct 07, 2014
4+
Problem: Best Time to Buy and Sell Stock
5+
Difficulty: Easy
6+
Source: http://leetcode.com/onlinejudge#question_121
7+
Notes:
8+
Say you have an array for which the ith element is the price of a given stock on day i.
9+
If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock),
10+
design an algorithm to find the maximum profit.
11+
12+
Solution: For each element, calculate the max difference with the former elements.
13+
*/
14+
15+
public class Solution {
16+
public int maxProfit(int[] prices) {
17+
int n = prices.length;
18+
if (n <= 1) return 0;
19+
int res = 0, minVal = prices[0];
20+
for (int i = 1; i < n; ++i) {
21+
res = Math.max(res, prices[i] - minVal);
22+
minVal = Math.min(minVal, prices[i]);
23+
}
24+
return res;
25+
}
26+
}

BestTimetoBuyandSellStockII.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
Author: King, [email protected]
3+
Date: Jan 02, 2015
4+
Problem: Best Time to Buy and Sell Stock II
5+
Difficulty: Medium
6+
Source: https://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/
7+
Notes:
8+
Say you have an array for which the ith element is the price of a given stock on day i.
9+
Design an algorithm to find the maximum profit. You may complete as many transactions as you like
10+
(ie, buy one and sell one share of the stock multiple times).
11+
However, you may not engage in multiple transactions at the same time
12+
(ie, you must sell the stock before you buy again).
13+
14+
Solution: 1. At the beginning of the ascending order: buy.
15+
At the ending of the ascending order: sell.
16+
*/
17+
public class Solution {
18+
public int maxProfit(int[] prices) {
19+
int res = 0;
20+
for (int i = 1; i < prices.length; ++i) {
21+
res += Math.max(0, prices[i] - prices[i-1]);
22+
}
23+
return res;
24+
}
25+
}

BestTimetoBuyandSellStockIII.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
Author: King, [email protected]
3+
Date: Jan 02, 2015
4+
Problem: Best Time to Buy and Sell Stock III
5+
Difficulty: Medium
6+
Source: https://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/
7+
Notes:
8+
Say you have an array for which the ith element is the price of a given stock on day i.
9+
Design an algorithm to find the maximum profit. You may complete at most two transactions.
10+
Note:
11+
You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
12+
13+
Solution: dp. max profit = max { l2r[0...i] + r2l[i+1...N-1] }.
14+
0 <= i <= N-1
15+
*/
16+
17+
public class Solution {
18+
public int maxProfit(int[] prices) {
19+
int n = prices.length;
20+
if (n <= 1) return 0;
21+
int[] l2r = new int[n];
22+
int[] r2l = new int[n];
23+
l2r[0] = 0; r2l[n-1] = 0;
24+
int minVal = prices[0], maxVal = prices[n-1];
25+
for (int i = 1; i < n; ++i) {
26+
l2r[i] = Math.max(l2r[i-1], prices[i] - minVal);
27+
minVal = Math.min(minVal, prices[i]);
28+
}
29+
for (int i = n - 2; i >= 0; --i) {
30+
r2l[i] = Math.max(r2l[i+1], maxVal - prices[i]);
31+
maxVal = Math.max(maxVal, prices[i]);
32+
}
33+
int res = l2r[n-1];
34+
for (int i = 0; i < n - 1; ++i) {
35+
res = Math.max(res, l2r[i] + r2l[i+1]);
36+
}
37+
return res;
38+
}
39+
}

0 commit comments

Comments
 (0)