Skip to content

Commit 81cc4c4

Browse files
committed
feat: add 375. Guess Number Higher or Lower II
1 parent f51d662 commit 81cc4c4

File tree

3 files changed

+88
-0
lines changed

3 files changed

+88
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ Medium: https://medium.com/@nphausg
5454
| [0242. Valid Anagram](https://leetcode.com/problems/valid-anagram) | [Solution](src/com/nphausg/leetcode/easy/ValidAnagram.java) | [Solution](src/com/nphausg/leetcode/easy/ValidAnagramKt.kt) | - |
5555
| [0268. Missing Number](https://leetcode.com/problems/missing-number) | [Solution](src/com/nphausg/leetcode/easy/MissingNumber.java) | [Solution](src/com/nphausg/leetcode/easy/MissingNumberKt.kt) | [Medium](https://nphausg.medium.com/leetcode-0268-missing-number-a-deep-dive-into-efficient-solutions-with-java-91d3f983defc) |
5656
| [0374. Guess Number Higher or Lower](https://leetcode.com/problems/guess-number-higher-or-lower) | [Solution](src/com/nphausg/leetcode/easy/GuessNumberHigherOrLower.java) | [Solution](src/com/nphausg/leetcode/easy/GuessNumberHigherOrLowerKt.kt) | - |
57+
| [0375. Guess Number Higher or Lower II](https://leetcode.com/problems/guess-number-higher-or-lower-ii) | [Solution](src/com/nphausg/leetcode/medium/GuessNumberHigherOrLowerII.java) | [Solution](src/com/nphausg/leetcode/medium/GuessNumberHigherOrLowerIIKt.kt) | - |
5758
| [0387. First Unique Character in a String](https://leetcode.com/problems/first-unique-character-in-a-string) | [Solution](src/com/nphausg/leetcode/easy/FirstUniqueCharacterInAString.java) | [Solution](src/com/nphausg/leetcode/easy/FirstUniqueCharacterInAStringKt.kt) | [Medium](https://nphausg.medium.com/leetcode-387-first-unique-character-22bf7752c35e) |
5859
| [0389. Find the Difference](https://leetcode.com/problems/find-the-difference) | [Solution](src/com/nphausg/leetcode/easy/FindTheDifference.java) | - | [Medium](https://levelup.gitconnected.com/leetcode-389-find-the-difference-exploring-all-solutions-can-be-with-java-a2be916767a0) |
5960
| [0409. Longest Palindrome](https://leetcode.com/problems/longest-palindrome) | [Solution](src/com/nphausg/leetcode/easy/LongestPalindrome.java) | - | - |
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.nphausg.leetcode.medium;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* <a href="https://leetcode.com/problems/guess-number-higher-or-lower-ii">375. Guess Number Higher or Lower II/a>
7+
*/
8+
public class GuessNumberHigherOrLowerII {
9+
10+
public int topDownGetMoneyAmount(int n) {
11+
int[][] mem = new int[n + 1][n + 1];
12+
Arrays.stream(mem).forEach(A -> Arrays.fill(A, Integer.MAX_VALUE));
13+
return getMoneyAmount(1, n, mem);
14+
}
15+
16+
private int getMoneyAmount(int i, int j, int[][] mem) {
17+
if (i >= j)
18+
return 0;
19+
if (mem[i][j] != Integer.MAX_VALUE)
20+
return mem[i][j];
21+
22+
for (int k = i; k <= j; ++k)
23+
mem[i][j] = Math.min(
24+
mem[i][j],
25+
Math.max(
26+
getMoneyAmount(i, k - 1, mem),
27+
getMoneyAmount(k + 1, j, mem)
28+
) + k
29+
);
30+
31+
return mem[i][j];
32+
}
33+
34+
public int bottomUpGetMoneyAmount(int n) {
35+
int[][] dp = new int[n + 1][n + 1];
36+
for (int len = 2; len <= n; len++) {
37+
for (int start = 1; start <= n - len + 1; start++) {
38+
int end = start + len - 1;
39+
dp[start][end] = Integer.MAX_VALUE;
40+
for (int k = start; k < end; k++) {
41+
int cost = k + Math.max(dp[start][k - 1], dp[k + 1][end]);
42+
dp[start][end] = Math.min(dp[start][end], cost);
43+
}
44+
}
45+
}
46+
return dp[1][n];
47+
}
48+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.nphausg.leetcode.medium
2+
3+
/**
4+
* @see <a href="https://leetcode.com/problems/guess-number-higher-or-lower-ii">375. Guess Number Higher or Lower II/a>
5+
*/
6+
class GuessNumberHigherOrLowerIIKt {
7+
8+
fun topDownGetMoneyAmount(n: Int): Int {
9+
val memo = Array(n + 1) { IntArray(n + 1) }
10+
return calculateCost(memo, 1, n)
11+
}
12+
13+
private fun calculateCost(memo: Array<IntArray>, start: Int, end: Int): Int {
14+
if (start >= end) return 0
15+
if (memo[start][end] != 0) return memo[start][end]
16+
var minCost = Int.MAX_VALUE
17+
for (k in start..end) {
18+
val cost = k + maxOf(calculateCost(memo, start, k - 1), calculateCost(memo, k + 1, end))
19+
minCost = minOf(minCost, cost)
20+
}
21+
memo[start][end] = minCost
22+
return minCost
23+
}
24+
25+
fun bottomUpGetMoneyAmount(n: Int): Int {
26+
val dp = Array(n + 1) { IntArray(n + 1) }
27+
for (len in 2..n) {
28+
for (start in 1..n - len + 1) {
29+
val end = start + len - 1
30+
dp[start][end] = Int.MAX_VALUE
31+
for (k in start until end) {
32+
val cost = k + maxOf(dp[start][k - 1], dp[k + 1][end])
33+
dp[start][end] = minOf(dp[start][end], cost)
34+
}
35+
}
36+
}
37+
return dp[1][n]
38+
}
39+
}

0 commit comments

Comments
 (0)