Movatterモバイル変換


[0]ホーム

URL:


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

Commit71cf48e

Browse files
Merge pull request#338 from shehanck/main
leetcode-309 solution in Java
2 parents264dd81 +4b22cd9 commit71cf48e

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
classSolution {
2+
publicintmaxProfit(int[]prices) {
3+
Map<String,Integer>cache =newHashMap<>();
4+
returndfs(prices,cache,0,true);
5+
}
6+
7+
publicintdfs(int[]prices,Map<String,Integer>cache,intindex,booleanbuying) {
8+
if (index >=prices.length) {
9+
return0;
10+
}
11+
Stringkey =index+"-"+buying;
12+
13+
if (cache.containsKey(key)) {
14+
returncache.get(key);
15+
}
16+
17+
intcooldown =dfs(prices,cache,index +1,buying);
18+
intbuyOsell =Integer.MIN_VALUE;
19+
20+
if (buying) {
21+
buyOsell =dfs(prices,cache,index +1, !buying) -prices[index];
22+
}else {
23+
buyOsell =dfs(prices,cache,index +2, !buying) +prices[index];
24+
}
25+
26+
cache.put(key,Math.max(buyOsell,cooldown));
27+
returncache.get(key);
28+
}
29+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp