forked from 7oSkaaa/LeetCode_DailyChallenge_2023
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9eb68f4
commit 83c0a3b
Showing
1 changed file
with
53 additions
and
0 deletions.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
05- May/27- Stone Game III/27- Stone Game III (Ahmed Gamal).cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// Author: Ahmed Gamal | ||
|
||
// for this problem, we can use dynamic programming to solve it | ||
// the approach is similar to the approach of the problem 26- Stone Game II | ||
// the difference is that we will not use the m value, and we will check if that maximum score of the first player is larger than the sum of the rest of the elements or not | ||
|
||
class Solution { | ||
static const int N = 5e4 + 5; | ||
int memo[N][2]; | ||
|
||
int dp(int idx, bool turn, vector<int>& v) { | ||
if(idx >= v.size()) { | ||
return 0; | ||
} | ||
|
||
int& ret = memo[idx][turn]; | ||
if(~ret) { | ||
return ret; | ||
} | ||
|
||
// if it's the first player's turn, we will try to maximize the score | ||
// notice that we are initializing the ret with -2e9 instead of 0 because the maximum score of the first player can be negative | ||
ret = turn ? -2e9 : 2e9; | ||
int sum = 0; | ||
|
||
// we will try to choose from 1 to 3 elements from the piles vector | ||
for(int i = 0; i < 3; i++) { | ||
if(idx + i >= v.size()) { | ||
break; | ||
} | ||
sum += v[idx + i]; | ||
if(turn) { | ||
ret = max(ret, sum + dp(idx + i + 1, false, v)); | ||
} else { | ||
ret = min(ret, dp(idx + i + 1, true, v)); | ||
} | ||
} | ||
|
||
return ret; | ||
} | ||
public: | ||
string stoneGameIII(vector<int>& stoneValue) { | ||
memset(memo, -1, sizeof memo); | ||
|
||
// the maximum score of the first player | ||
int ans = dp(0, true, stoneValue); | ||
|
||
// the sum of the elements in the piles vector | ||
int sum = accumulate(stoneValue.begin(), stoneValue.end(), 0); | ||
|
||
return ans > sum - ans ? "Alice" : ans < sum - ans ? "Bob" : "Tie"; | ||
} | ||
}; |