Member-only story

Can I Win Problem Explained

Saloni Kaur
4 min readJul 3, 2019

--

Two players take turns drawing from a common pool of numbers of 1..15 without replacement until they reach a total ≥ 100.

Given an integer maxChoosableInt and another integer desiredTotal, determine if the first player to move can force a win, assuming both players play optimally.

You can always assume that maxChoosableInt will not be larger than 20 and desiredTotal will not be larger than 300. Below is an example explanation.

Input:
maxChoosableInteger = 10
desiredTotal = 11

Output:
false

Explanation:
No matter which integer the first player choose, the first player will lose.
The first player can choose an integer from 1 up to 10.
If the first player choose 1, the second player can only choose integers from 2 up to 10.
The second player will win by choosing 10 and get a total = 11, which is >= desiredTotal.
Same with other integers chosen by the first player, the second player will always win.

Please try this question on your own first before reading the explanation. I had spent over a week on this question before even coming close to the solution.

When players play optimally means, both players will try to win as best as possible. This means if there is a chance for player 2 to win in the series of number choosing, player one will not win.

--

--

Saloni Kaur
Saloni Kaur

Responses (1)