Skip to content

Commit 43e30bb

Browse files
refactor 188
1 parent b314490 commit 43e30bb

File tree

1 file changed

+40
-28
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+40
-28
lines changed
Lines changed: 40 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,58 @@
11
package com.fishercoder.solutions;
22

33
/**
4-
* 188. Best Time to Buy and Sell Stock IV
5-
*
6-
* Say you have an array for which the ith element is the price of a given stock on day i.
4+
5+
188. Best Time to Buy and Sell Stock IV
6+
7+
Say you have an array for which the ith element is the price of a given stock on day i.
78
89
Design an algorithm to find the maximum profit. You may complete at most k transactions.
910
1011
Note:
1112
You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
13+
14+
Example 1:
15+
Input: [2,4,1], k = 2
16+
Output: 2
17+
Explanation: Buy on day 1 (price = 2) and sell on day 2 (price = 4), profit = 4-2 = 2.
18+
19+
Example 2:
20+
Input: [3,2,6,5,0,3], k = 2
21+
Output: 7
22+
Explanation: Buy on day 2 (price = 2) and sell on day 3 (price = 6), profit = 6-2 = 4.
23+
Then buy on day 5 (price = 0) and sell on day 6 (price = 3), profit = 3-0 = 3.
24+
1225
*/
1326
public class _188 {
14-
15-
/**credit: https://discuss.leetcode.com/topic/8984/a-concise-dp-solution-in-java*/
27+
public static class Solution1 {
28+
/** credit: https://discuss.leetcode.com/topic/8984/a-concise-dp-solution-in-java */
1629
public int maxProfit(int k, int[] prices) {
17-
int len = prices.length;
18-
if (k >= len / 2) {
19-
return quickSolve(prices);
20-
}
21-
22-
int[][] t = new int[k + 1][len];
23-
for (int i = 1; i <= k; i++) {
24-
int tmpMax = -prices[0];
25-
for (int j = 1; j < len; j++) {
26-
t[i][j] = Math.max(t[i][j - 1], prices[j] + tmpMax);
27-
tmpMax = Math.max(tmpMax, t[i - 1][j - 1] - prices[j]);
28-
}
30+
int len = prices.length;
31+
if (k >= len / 2) {
32+
return quickSolve(prices);
33+
}
34+
35+
int[][] t = new int[k + 1][len];
36+
for (int i = 1; i <= k; i++) {
37+
int tmpMax = -prices[0];
38+
for (int j = 1; j < len; j++) {
39+
t[i][j] = Math.max(t[i][j - 1], prices[j] + tmpMax);
40+
tmpMax = Math.max(tmpMax, t[i - 1][j - 1] - prices[j]);
2941
}
30-
return t[k][len - 1];
42+
}
43+
return t[k][len - 1];
3144
}
3245

33-
3446
private int quickSolve(int[] prices) {
35-
int len = prices.length;
36-
int profit = 0;
37-
for (int i = 1; i < len; i++) {
38-
// as long as there is a price gap, we gain a profit.
39-
if (prices[i] > prices[i - 1]) {
40-
profit += prices[i] - prices[i - 1];
41-
}
47+
int len = prices.length;
48+
int profit = 0;
49+
for (int i = 1; i < len; i++) {
50+
// as long as there is a price gap, we gain a profit.
51+
if (prices[i] > prices[i - 1]) {
52+
profit += prices[i] - prices[i - 1];
4253
}
43-
return profit;
54+
}
55+
return profit;
4456
}
45-
57+
}
4658
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy