Skip to content

Commit b9d4549

Browse files
add 188
1 parent d3aaf46 commit b9d4549

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,7 @@ Your ideas/fixes/algorithms are more than welcome!
341341
|191|[Number of 1 Bits](https://leetcode.com/problems/number-of-1-bits/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_191.java)| O(n)|O(1)| Easy | Bit Manipulation
342342
|190|[Reverse Bits](https://leetcode.com/problems/reverse-bits/)|[Solution](../master/src/main/java/com/fishercoder/solutions/ReverseBits.java)| O(n)|O(1)| Easy | Bit Manipulation
343343
|189|[Rotate Array](https://leetcode.com/problems/rotate-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_189.java)| O(n)|O(n), could be optimized to O(1) | Easy
344+
|188|[Best Time to Buy and Sell Stock IV](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_188.java)| O(n*k)|O(n*k) | Hard | DP
344345
|187|[Repeated DNA Sequences](https://leetcode.com/problems/repeated-dna-sequences/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_187.java)| O(n)|O(n) | Medium
345346
|186|[Reverse Words in a String II](https://leetcode.com/problems/reverse-words-in-a-string-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_186.java)| O(n)|O(1)| Medium
346347
|179|[Largest Number](https://leetcode.com/problems/largest-number/)|[Queue](../master/src/main/java/com/fishercoder/solutions/BSTIterator_using_q.java) [Stack](../../blmaster/MEDIUM/src/medium/LargestNumber.java)| O(?) |O(?) | Medium|
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.fishercoder.solutions;
2+
3+
/**
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.
7+
8+
Design an algorithm to find the maximum profit. You may complete at most k transactions.
9+
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+
public class _188 {
14+
15+
/**credit: https://discuss.leetcode.com/topic/8984/a-concise-dp-solution-in-java*/
16+
public int maxProfit(int k, int[] prices) {
17+
int len = prices.length;
18+
if (k >= len / 2) return quickSolve(prices);
19+
20+
int[][] t = new int[k + 1][len];
21+
for (int i = 1; i <= k; i++) {
22+
int tmpMax = -prices[0];
23+
for (int j = 1; j < len; j++) {
24+
t[i][j] = Math.max(t[i][j - 1], prices[j] + tmpMax);
25+
tmpMax = Math.max(tmpMax, t[i - 1][j - 1] - prices[j]);
26+
}
27+
}
28+
return t[k][len - 1];
29+
}
30+
31+
32+
private int quickSolve(int[] prices) {
33+
int len = prices.length, profit = 0;
34+
for (int i = 1; i < len; i++) {
35+
// as long as there is a price gap, we gain a profit.
36+
if (prices[i] > prices[i - 1]) profit += prices[i] - prices[i - 1];
37+
}
38+
return profit;
39+
}
40+
41+
}

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