Skip to content

Commit 9d953bd

Browse files
Add: Solution.java
1 parent 00588d6 commit 9d953bd

File tree

1 file changed

+35
-0
lines changed
  • solution/1900-1999/1900.The Earliest and Latest Rounds Where Players Compete

1 file changed

+35
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import java.util.*;
2+
3+
public class Solution {
4+
private Map<List<Integer>, int[]> memo = new HashMap<>();
5+
6+
public int[] earliestAndLatest(int n, int firstPlayer, int secondPlayer) {
7+
return dp(firstPlayer, n - secondPlayer + 1, n);
8+
}
9+
10+
private int[] dp(int l, int r, int k) {
11+
if (l == r) return new int[]{1, 1};
12+
if (l > r) return dp(r, l, k);
13+
14+
List<Integer> key = Arrays.asList(l, r, k);
15+
if (memo.containsKey(key)) return memo.get(key);
16+
17+
int earliest = Integer.MAX_VALUE;
18+
int latest = Integer.MIN_VALUE;
19+
20+
// Enumerate all possible positions
21+
for (int i = 1; i <= l; ++i) {
22+
for (int j = l - i + 1; j <= r - i + 1; ++j) {
23+
if (!(l + r - k / 2 <= i + j && i + j <= (k + 1) / 2)) continue;
24+
25+
int[] result = dp(i, j, (k + 1) / 2);
26+
earliest = Math.min(earliest, result[0] + 1);
27+
latest = Math.max(latest, result[1] + 1);
28+
}
29+
}
30+
31+
int[] result = new int[]{earliest, latest};
32+
memo.put(key, result);
33+
return result;
34+
}
35+
}

0 commit comments

Comments
 (0)