Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 22c2ce5

Browse files
Merge pull request youngyangyang04#2129 from Lozakaka/patch-21
新增java 2*2 array solution
2 parents f975588 + 652fa2b commit 22c2ce5

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

‎problems/0714.买卖股票的最佳时机含手续费(动态规划).md‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,25 @@ class Solution {
154154
return dp[1];
155155
}
156156
}
157+
```Java
158+
//使用 2*2 array
159+
class Solution {
160+
public int maxProfit(int[] prices, int fee) {
161+
int dp[][] = new int[2][2];
162+
int len = prices.length;
163+
//[i][0] = holding the stock
164+
//[i][1] = not holding the stock
165+
dp[0][0] = -prices[0];
166+
167+
for(int i = 1; i < len; i++){
168+
dp[i % 2][0] = Math.max(dp[(i - 1) % 2][0], dp[(i - 1) % 2][1] - prices[i]);
169+
dp[i % 2][1] = Math.max(dp[(i - 1) % 2][1], dp[(i - 1) % 2][0] + prices[i] - fee);
170+
}
171+
172+
return dp[(len - 1) % 2][1];
173+
}
174+
}
175+
```
157176
```
158177
159178
Python:

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /