Skip to content

Commit 6162e3f

Browse files
committed
added Predict the Winner (medium)
1 parent cda6823 commit 6162e3f

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

Medium/PredictWinner/README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
# Predict the Winner
3+
[Leetcode Link](https://leetcode.com/problems/predict-the-winner/)
4+
5+
## Problem:
6+
7+
Given an array of scores that are non-negative integers. Player 1 picks one of the numbers from either end of the array followed by the player 2 and then player 1 and so on. Each time a player picks a number, that number will not be available for the next player. This continues until all the scores have been chosen. The player with the maximum score wins.
8+
9+
Given an array of scores, predict whether player 1 is the winner. You can assume each player plays to maximize his score.
10+
11+
## Example:
12+
13+
```
14+
Input: [1, 5, 2]
15+
Output: False
16+
Explanation: Initially, player 1 can choose between 1 and 2.
17+
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).
18+
So, final score of player 1 is 1 + 2 = 3, and player 2 is 5.
19+
Hence, player 1 will never be the winner and you need to return False.
20+
```
21+
```
22+
Input: [1, 5, 233, 7]
23+
Output: True
24+
Explanation: Player 1 first chooses 1. Then player 2 have to choose between 5 and 7. No matter which number player 2 choose, player 1 can choose 233.
25+
Finally, player 1 has more score (234) than player 2 (12), so you need to return True representing player1 can win.
26+
```
27+
28+
## Note:
29+
30+
- `1 <= length of the array <= 20`.
31+
- Any scores in the given array are non-negative integers and will not exceed `10,000,000`.
32+
- If the scores of both players are equal, then player 1 is still the winner.

Medium/PredictWinner/solution.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from typing import List
2+
3+
class Solution:
4+
def PredictTheWinner(self, nums: List[int]) -> bool:
5+
score = self.minMax(nums, 0, len(nums)-1, 1)
6+
return score >= 0
7+
8+
9+
def minMax(self, nums: List[int], start, end, turn = 1) -> int:
10+
# print(nums[start:end+1])
11+
if start == end:
12+
# print("base case:", turn*nums[start])
13+
return turn * nums[start]
14+
pickLeft = turn * nums[start] + self.minMax(nums, start+1, end, -turn)
15+
# print("left:", pickLeft)
16+
pickRight = turn * nums[end] + self.minMax(nums, start, end-1, -turn)
17+
# print("right:", pickRight)
18+
return turn * max(turn * pickLeft, turn * pickRight)
19+
20+
21+
22+
23+
# test driver
24+
sol = Solution()
25+
nums = [1, 5, 233, 7]
26+
print("Output:", sol.PredictTheWinner(nums))

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ Languages used: Java and Python
6464
- [Reverse Substrings Between Each Pair of Parentheses](Medium/ReverseSubstringsBetweenParentheses)
6565
- [Asteroid Collision](Medium/AsteroidCollision)
6666
- [Surrounded Regions](Medium/SurroundedRegions)
67+
- [Predict the Winner](Medium/PredictWinner)
6768
- Hard
6869
- [Maximum Score Words Formed by Letters](Hard/MaximumScoreWords)
6970
- [Reducing Dishes](Hard/ReducingDishes)

0 commit comments

Comments
 (0)