Skip to content

Commit 0630ece

Browse files
committed
leetcode
1 parent ae8dea0 commit 0630ece

File tree

3 files changed

+404
-0
lines changed

3 files changed

+404
-0
lines changed
+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
3+
4+
486. Predict the Winner
5+
6+
You are given an integer array nums. Two players are playing a game with this array: player 1 and player 2.
7+
8+
Player 1 and player 2 take turns, with player 1 starting first. Both players start the game with a score of 0. At each turn, the player takes one of the numbers from either end of the array (i.e., nums[0] or nums[nums.length - 1]) which reduces the size of the array by 1. The player adds the chosen number to their score. The game ends when there are no more elements in the array.
9+
10+
Return true if Player 1 can win the game. If the scores of both players are equal, then player 1 is still the winner, and you should also return true. You may assume that both players are playing optimally.
11+
12+
13+
14+
Example 1:
15+
16+
Input: nums = [1,5,2]
17+
Output: false
18+
Explanation: Initially, player 1 can choose between 1 and 2.
19+
If he chooses 2 (or 1), then player 2 can choose from 1 (or 2) and 5. If player 2 chooses 5, then player 1 will be left with 1 (or 2).
20+
So, final score of player 1 is 1 + 2 = 3, and player 2 is 5.
21+
Hence, player 1 will never be the winner and you need to return false.
22+
Example 2:
23+
24+
Input: nums = [1,5,233,7]
25+
Output: true
26+
Explanation: Player 1 first chooses 1. Then player 2 has to choose between 5 and 7. No matter which number player 2 choose, player 1 can choose 233.
27+
Finally, player 1 has more score (234) than player 2 (12), so you need to return True representing player1 can win.
28+
29+
30+
Constraints:
31+
32+
1 <= nums.length <= 20
33+
0 <= nums[i] <= 107
34+
35+
*/
36+
37+
38+
// BitMask
39+
class A {
40+
bool PredictTheWinner(List<int> arr) {
41+
if (arr.length <= 1) return true;
42+
final List<int> dp = List.filled(1 << arr.length, 0);
43+
int tar = 0;
44+
for (int i = 0; i < arr.length; i++) tar += 1 << i;
45+
final int val = solve(arr, 0, dp, tar);
46+
return val >= 0;
47+
}
48+
49+
int solve(List<int> arr, int current, List<int> dp, int tar) {
50+
if (current == tar) return 0; // all integers have been picked
51+
if (dp[current] != 0) return dp[current]; // cache
52+
int max = -2147483648; // equivalent to Integer.MIN_VALUE
53+
54+
for (int i = 0; i < arr.length; i++) {
55+
// finding first unpicked integer
56+
if ((current & (1 << i)) == 0) {
57+
max = _max(max, arr[i] - solve(arr, (current | (1 << i)), dp, tar));
58+
break;
59+
}
60+
}
61+
62+
for (int i = arr.length - 1; i >= 0; i--) {
63+
// finding last unpicked integer
64+
if ((current & (1 << i)) == 0) {
65+
max = _max(max, arr[i] - solve(arr, (current | (1 << i)), dp, tar));
66+
break;
67+
}
68+
}
69+
return dp[current] = max;
70+
}
71+
72+
int _max(int a, int b) => a > b ? a : b;
73+
}
+144
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
package main
2+
3+
4+
/*
5+
Slow BitMask
6+
func PredictTheWinner(arr []int) bool {
7+
if len(arr) <= 1 {
8+
return true
9+
}
10+
dp := make([]int, 1<<len(arr))
11+
tar := 0
12+
for i := 0; i < len(arr); i++ {
13+
tar += 1 << i
14+
}
15+
val := solve(arr, 0, dp, tar)
16+
return val >= 0
17+
}
18+
19+
func solve(arr []int, current int, dp []int, tar int) int {
20+
if current == tar {
21+
return 0 // all integers have been picked
22+
}
23+
if dp[current] != 0 {
24+
return dp[current] // cache
25+
}
26+
// max := math.MinInt32
27+
max := -2147483648
28+
29+
for i := 0; i < len(arr); i++ {
30+
// finding first unpicked integer
31+
if current&(1<<i) == 0 {
32+
max = _max(max, arr[i]-solve(arr, (current|(1<<i)), dp, tar))
33+
break
34+
}
35+
}
36+
37+
for i := len(arr) - 1; i >= 0; i-- {
38+
// finding last unpicked integer
39+
if current&(1<<i) == 0 {
40+
max = _max(max, arr[i]-solve(arr, (current|(1<<i)), dp, tar))
41+
break
42+
}
43+
}
44+
dp[current] = max
45+
return max
46+
}
47+
48+
func _max(a, b int) int {
49+
if a > b {
50+
return a
51+
}
52+
return b
53+
}
54+
55+
56+
57+
58+
// Dynamic Programming
59+
60+
func max(a, b int) int {
61+
if a > b {
62+
return a
63+
}
64+
return b
65+
}
66+
67+
func PredictTheWinner(nums []int) bool {
68+
n := len(nums)
69+
dp := make([][]int, n)
70+
for i := 0; i < n; i++ {
71+
dp[i] = make([]int, n)
72+
}
73+
74+
for i := n - 1; i >= 0; i-- {
75+
for j := i; j < n; j++ {
76+
if i == j {
77+
dp[i][j] = nums[i]
78+
} else {
79+
dp[i][j] = max(nums[i]-dp[i+1][j], nums[j]-dp[i][j-1])
80+
}
81+
}
82+
}
83+
84+
return dp[0][n-1] >= 0
85+
}
86+
87+
88+
*/
89+
90+
91+
92+
93+
94+
func max(a, b int) int {
95+
if a > b {
96+
return a
97+
}
98+
return b
99+
}
100+
101+
func PredictTheWinner(nums []int) bool {
102+
n := len(nums)
103+
t := make([][]int, n+1)
104+
for i := 0; i <= n; i++ {
105+
t[i] = make([]int, n+1)
106+
for j := 0; j <= n; j++ {
107+
t[i][j] = -1
108+
}
109+
}
110+
rangeSum := 0
111+
for _, n := range nums {
112+
rangeSum += n
113+
}
114+
115+
max1 := maxPosAmount(nums, 0, n-1, t)
116+
max2 := rangeSum - max1
117+
return max1 >= max2
118+
}
119+
120+
func maxPosAmount(nums []int, i, j int, t [][]int) int {
121+
if i > j || i < 0 || j >= len(nums) || i >= len(nums) || j < 0 {
122+
return 0
123+
}
124+
if t[i][j] != -1 {
125+
return t[i][j]
126+
}
127+
f1 := nums[i] + min(maxPosAmount(nums, i+1, j-1, t), maxPosAmount(nums, i+2, j, t))
128+
f2 := nums[j] + min(maxPosAmount(nums, i, j-2, t), maxPosAmount(nums, i+1, j-1, t))
129+
t[i][j] = max(f1, f2)
130+
return t[i][j]
131+
}
132+
133+
func min(a, b int) int {
134+
if a < b {
135+
return a
136+
}
137+
return b
138+
}
139+
140+
141+
142+
143+
144+

0 commit comments

Comments
 (0)