-
Notifications
You must be signed in to change notification settings - Fork 212
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
nibnait
committed
Oct 27, 2020
1 parent
2e3e310
commit 3551f23
Showing
9 changed files
with
640 additions
and
5 deletions.
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
src/main/java/algorithm_practice/LeetCode/code100/E121_买卖股票的最佳时机.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package algorithm_practice.LeetCode.code100; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
/* | ||
给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格。 | ||
如果你最多只允许完成一笔交易(即买入和卖出一支股票一次),设计一个算法来计算你所能获取的最大利润。 | ||
注意:你不能在买入股票前卖出股票。 | ||
示例 1: | ||
输入: [7,1,5,3,6,4] | ||
输出: 5 | ||
解释: 在第 2 天(股票价格 = 1)的时候买入,在第 5 天(股票价格 = 6)的时候卖出,最大利润 = 6-1 = 5 。 | ||
注意利润不能是 7-1 = 6, 因为卖出价格需要大于买入价格;同时,你不能在买入前卖出股票。 | ||
示例 2: | ||
输入: [7,6,4,3,1] | ||
输出: 0 | ||
解释: 在这种情况下, 没有交易完成, 所以最大利润为 0。 | ||
来源:力扣(LeetCode) | ||
链接:https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock | ||
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 | ||
*/ | ||
public class E121_买卖股票的最佳时机 { | ||
|
||
@Test | ||
public void testCase() { | ||
int[] prices = new int[]{7,1,5,3,6,4}; | ||
int maxProfit = 5; | ||
Assert.assertEquals(maxProfit, maxProfit(prices)); | ||
|
||
prices = new int[]{7,6,4,3,1}; | ||
maxProfit = 0; | ||
Assert.assertEquals(maxProfit, maxProfit(prices)); | ||
|
||
} | ||
|
||
// 只允许完成一笔交易(即买入和卖出一支股票一次) | ||
public int maxProfit(int[] prices) { | ||
int n = prices.length; | ||
if (n <= 1) { | ||
return 0; | ||
} | ||
|
||
int[][] dp = new int[n][2]; | ||
dp[0][0] = 0; | ||
dp[0][1] = -prices[0]; | ||
|
||
for (int i = 1; i < n; i++) { | ||
dp[i][0] = Math.max(dp[i-1][0], dp[i-1][1] + prices[i]); | ||
dp[i][1] = Math.max(dp[i-1][1], -prices[i]); | ||
} | ||
|
||
return Math.max(dp[n - 1][0], 0); | ||
} | ||
|
||
} |
75 changes: 75 additions & 0 deletions
75
src/main/java/algorithm_practice/LeetCode/code100/E122_买卖股票的最佳时机2.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package algorithm_practice.LeetCode.code100; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
/* | ||
给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格。 | ||
设计一个算法来计算你所能获取的最大利润。你可以尽可能地完成更多的交易(多次买卖一支股票)。 | ||
注意:你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。 | ||
示例 1: | ||
输入: [7,1,5,3,6,4] | ||
输出: 7 | ||
解释: 在第 2 天(股票价格 = 1)的时候买入,在第 3 天(股票价格 = 5)的时候卖出, 这笔交易所能获得利润 = 5-1 = 4 。 | ||
随后,在第 4 天(股票价格 = 3)的时候买入,在第 5 天(股票价格 = 6)的时候卖出, 这笔交易所能获得利润 = 6-3 = 3 。 | ||
示例 2: | ||
输入: [1,2,3,4,5] | ||
输出: 4 | ||
解释: 在第 1 天(股票价格 = 1)的时候买入,在第 5 天 (股票价格 = 5)的时候卖出, 这笔交易所能获得利润 = 5-1 = 4 。 | ||
注意你不能在第 1 天和第 2 天接连购买股票,之后再将它们卖出。 | ||
因为这样属于同时参与了多笔交易,你必须在再次购买前出售掉之前的股票。 | ||
示例 3: | ||
输入: [7,6,4,3,1] | ||
输出: 0 | ||
解释: 在这种情况下, 没有交易完成, 所以最大利润为 0。 | ||
来源:力扣(LeetCode) | ||
链接:https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-ii | ||
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 | ||
*/ | ||
public class E122_买卖股票的最佳时机2 { | ||
|
||
@Test | ||
public void testCase() { | ||
int[] prices = new int[]{7,1,5,3,6,4}; | ||
int maxProfit = 7; | ||
Assert.assertEquals(maxProfit, maxProfit(prices)); | ||
|
||
prices = new int[]{1,2,3,4,5}; | ||
maxProfit = 4; | ||
Assert.assertEquals(maxProfit, maxProfit(prices)); | ||
|
||
prices = new int[]{7,6,4,3,1}; | ||
maxProfit = 0; | ||
Assert.assertEquals(maxProfit, maxProfit(prices)); | ||
|
||
} | ||
|
||
public int maxProfit(int[] prices) { | ||
int n = prices.length; | ||
if (n <= 1) { | ||
return 0; | ||
} | ||
|
||
int[][] dp = new int[n][2]; | ||
dp[0][0] = 0; | ||
dp[0][1] = -prices[0]; | ||
|
||
for (int i = 1; i < n; i++) { | ||
dp[i][0] = Math.max(dp[i-1][0], dp[i-1][1] + prices[i]); | ||
dp[i][1] = Math.max(dp[i-1][1], dp[i-1][0] - prices[i]); | ||
} | ||
|
||
return dp[n-1][0]; | ||
} | ||
|
||
} |
78 changes: 78 additions & 0 deletions
78
src/main/java/algorithm_practice/LeetCode/code100/H123_买卖股票的最佳时机3.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package algorithm_practice.LeetCode.code100; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
/* | ||
给定一个数组,它的第 i 个元素是一支给定的股票在第 i 天的价格。 | ||
设计一个算法来计算你所能获取的最大利润。你最多可以完成 两笔 交易。 | ||
注意: 你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。 | ||
示例 1: | ||
输入: [3,3,5,0,0,3,1,4] | ||
输出: 6 | ||
解释: 在第 4 天(股票价格 = 0)的时候买入,在第 6 天(股票价格 = 3)的时候卖出,这笔交易所能获得利润 = 3-0 = 3 。 | ||
随后,在第 7 天(股票价格 = 1)的时候买入,在第 8 天 (股票价格 = 4)的时候卖出,这笔交易所能获得利润 = 4-1 = 3 。 | ||
示例 2: | ||
输入: [1,2,3,4,5] | ||
输出: 4 | ||
解释: 在第 1 天(股票价格 = 1)的时候买入,在第 5 天 (股票价格 = 5)的时候卖出, 这笔交易所能获得利润 = 5-1 = 4 。 | ||
注意你不能在第 1 天和第 2 天接连购买股票,之后再将它们卖出。 | ||
因为这样属于同时参与了多笔交易,你必须在再次购买前出售掉之前的股票。 | ||
示例 3: | ||
输入: [7,6,4,3,1] | ||
输出: 0 | ||
解释: 在这个情况下, 没有交易完成, 所以最大利润为 0。 | ||
来源:力扣(LeetCode) | ||
链接:https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-iii | ||
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 | ||
*/ | ||
public class H123_买卖股票的最佳时机3 { | ||
|
||
@Test | ||
public void testCase() { | ||
int[] prices = new int[]{3,3,5,0,0,3,1,4}; | ||
int maxProfit = 6; | ||
Assert.assertEquals(maxProfit, maxProfit(prices)); | ||
|
||
prices = new int[]{1,2,3,4,5}; | ||
maxProfit = 4; | ||
Assert.assertEquals(maxProfit, maxProfit(prices)); | ||
|
||
prices = new int[]{7,6,4,3,1}; | ||
maxProfit = 0; | ||
Assert.assertEquals(maxProfit, maxProfit(prices)); | ||
|
||
} | ||
|
||
public int maxProfit(int[] prices) { | ||
int n = prices.length; | ||
if (n <= 1) { | ||
return 0; | ||
} | ||
|
||
int maxK = 2; | ||
int[][][] dp = new int[n][maxK+1][2]; | ||
dp[0][maxK][1] = -prices[0]; | ||
dp[0][1][1] = -prices[0]; | ||
dp[0][maxK][0] = 0; | ||
dp[0][1][0] = 0; | ||
|
||
for (int i = 1; i < n; i++) { | ||
for (int k = maxK; k >= 1; k--) { | ||
dp[i][k][0] = Math.max(dp[i - 1][k][0], dp[i - 1][k][1] + prices[i]); | ||
dp[i][k][1] = Math.max(dp[i - 1][k][1], dp[i - 1][k - 1][0] - prices[i]); | ||
} | ||
} | ||
|
||
return dp[n-1][maxK][0]; | ||
} | ||
|
||
} |
133 changes: 133 additions & 0 deletions
133
src/main/java/algorithm_practice/LeetCode/code100/H188_买卖股票的最佳时机4.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
package algorithm_practice.LeetCode.code100; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
/* | ||
给定一个数组,它的第 i 个元素是一支给定的股票在第 i 天的价格。 | ||
设计一个算法来计算你所能获取的最大利润。你最多可以完成 k 笔交易。 | ||
注意: 你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。 | ||
示例 1: | ||
输入: [2,4,1], k = 2 | ||
输出: 2 | ||
解释: 在第 1 天 (股票价格 = 2) 的时候买入,在第 2 天 (股票价格 = 4) 的时候卖出,这笔交易所能获得利润 = 4-2 = 2 。 | ||
示例 2: | ||
输入: [3,2,6,5,0,3], k = 2 | ||
输出: 7 | ||
解释: 在第 2 天 (股票价格 = 2) 的时候买入,在第 3 天 (股票价格 = 6) 的时候卖出, 这笔交易所能获得利润 = 6-2 = 4 。 | ||
随后,在第 5 天 (股票价格 = 0) 的时候买入,在第 6 天 (股票价格 = 3) 的时候卖出, 这笔交易所能获得利润 = 3-0 = 3 。 | ||
来源:力扣(LeetCode) | ||
链接:https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-iv | ||
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 | ||
*/ | ||
public class H188_买卖股票的最佳时机4 { | ||
|
||
@Test | ||
public void testCase() { | ||
int[] prices = new int[]{2,4,1}; | ||
int k = 2; | ||
int maxProfit = 2; | ||
Assert.assertEquals(maxProfit, maxProfit(k, prices)); | ||
|
||
prices = new int[]{3,2,6,5,0,3}; | ||
k = 2; | ||
maxProfit = 7; | ||
Assert.assertEquals(maxProfit, maxProfit(k, prices)); | ||
|
||
prices = new int[]{1,2,4,2,5,7,2,4,9,0}; | ||
k = 4; | ||
maxProfit = 15; | ||
Assert.assertEquals(maxProfit, maxProfit(k, prices)); | ||
|
||
prices = new int[]{2,1,4}; | ||
k = 2; | ||
maxProfit = 3; | ||
Assert.assertEquals(maxProfit, maxProfit(k, prices)); | ||
|
||
} | ||
|
||
/** | ||
* 将三维数组 去掉天数维度,优化为 二维数组 | ||
*/ | ||
public int maxProfit(int maxK, int[] prices) { | ||
int n = prices.length; | ||
if (n <= 1) { | ||
return 0; | ||
} | ||
|
||
if (maxK > n / 2) { | ||
return maxProfixWithKInfinity(prices); | ||
} | ||
|
||
int[][] dp = new int[maxK+1][2]; | ||
|
||
for (int k = maxK; k >= 0 ; k--) { | ||
dp[k][0] = 0; | ||
dp[k][1] = -prices[0]; | ||
} | ||
|
||
for (int i = 0; i < n; i++) { | ||
for (int k = maxK; k >= 1; k--) { | ||
// 第 i 天,第 k 次 买入 | ||
dp[k-1][1] = Math.max(dp[k-1][1], dp[k-1][0] - prices[i]); | ||
|
||
// 第 i 天,第 k 次 卖出 | ||
dp[k][0] = Math.max(dp[k][0], dp[k-1][1] + prices[i]); | ||
} | ||
} | ||
|
||
return dp[maxK][0]; | ||
} | ||
|
||
/** | ||
* 最原始的想法 dp数组 | ||
*/ | ||
public int maxProfit_base(int maxK, int[] prices) { | ||
int n = prices.length; | ||
if (n <= 1) { | ||
return 0; | ||
} | ||
|
||
if (maxK > n / 2) { | ||
return maxProfixWithKInfinity(prices); | ||
} | ||
|
||
int[][][] dp = new int[n][maxK+1][2]; | ||
for (int k = maxK; k > 0; k--) { | ||
dp[0][k][0] = 0; | ||
dp[0][k][1] = -prices[0]; | ||
} | ||
|
||
for (int i = 1; i < n; i++) { | ||
for (int k = maxK; k >= 1; k--) { | ||
dp[i][k][0] = Math.max(dp[i-1][k][0], dp[i-1][k][1] + prices[i]); | ||
dp[i][k][1] = Math.max(dp[i-1][k][1], dp[i-1][k-1][0] - prices[i]); | ||
} | ||
} | ||
|
||
return dp[n-1][maxK][0]; | ||
} | ||
|
||
private int maxProfixWithKInfinity(int[] prices) { | ||
int n = prices.length; | ||
|
||
int[][] dp = new int[n][2]; | ||
dp[0][0] = 0; | ||
dp[0][1] = -prices[0]; | ||
|
||
for (int i = 1; i < n; i++) { | ||
dp[i][0] = Math.max(dp[i-1][0], dp[i-1][1] + prices[i]); | ||
dp[i][1] = Math.max(dp[i-1][1], dp[i-1][0] - prices[i]); | ||
} | ||
|
||
return dp[n-1][0]; | ||
} | ||
|
||
} |
Oops, something went wrong.