mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-10 04:06:51 +08:00
0122买卖股票最佳时机2添加Java动态规划实现
This commit is contained in:
@ -133,7 +133,34 @@ public:
|
|||||||
|
|
||||||
|
|
||||||
Java:
|
Java:
|
||||||
|
```java
|
||||||
|
// 动态规划
|
||||||
|
class Solution
|
||||||
|
public int maxProfit(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];
|
||||||
|
}
|
||||||
|
|
||||||
|
public int maxProfit(int[] prices) {
|
||||||
|
int n = prices.length;
|
||||||
|
int dp0 = 0, dp1 = -prices[0];
|
||||||
|
for (int i = 1; i < n; ++i) {
|
||||||
|
int newDp0 = Math.max(dp0, dp1 + prices[i]);
|
||||||
|
int newDp1 = Math.max(dp1, dp0 - prices[i]);
|
||||||
|
dp0 = newDp0;
|
||||||
|
dp1 = newDp1;
|
||||||
|
}
|
||||||
|
return dp0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user