forked from SHY-Corp/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1510. StoneGameIV.cpp
More file actions
55 lines (38 loc) · 1.61 KB
/
1510. StoneGameIV.cpp
File metadata and controls
55 lines (38 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*
Title: 1510. Stone Game IV
Problem:
Alice and Bob take turns playing a game, with Alice starting first.
Initially, there are n stones in a pile. On each player's turn, that player makes a move consisting of removing any non-zero square number of stones in the pile.
Also, if a player cannot make a move, he/she loses the game.
Given a positive integer n. Return True if and only if Alice wins the game otherwise return False, assuming both players play optimally.
Approach: As alice is starting the game and Bob is playing optimally, alice has to make a move such that in the next move Bob looses.
As both play optimally, if Alice looses in any move that should be the move of Bob. So we should give the move to Bob wre Alice looses.
We can use DP and store the values were alice wins and looses. At any particular point we have to choose a number such that of remaining numbers Bob looses as it is his turn.
*/
class Solution {
public:
bool winnerSquareGame(int n) {
vector<int> v(n+1,0);
int done,r,s;
for(int i = 1; i <= n; i++){
s = sqrt(i);
if((s*s) == i){
v[i] = 1;
}else{
done = 0;
for(int j = 1; j*j < i && !done; j++){
r = i - (j*j);
if(v[r] == 0){
done = 1;
}
}
if(done == 1){
v[i] = 1;
}else{
v[i] = 0;
}
}
}
return v[n] == 1;
}
};