forked from saadfareed/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Find the Winner of the Circular Game Problem No.1823 (saadfareed#62)
- Loading branch information
1 parent
ce7d25d
commit 65a37d9
Showing
1 changed file
with
30 additions
and
0 deletions.
There are no files selected for viewing
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,30 @@ | ||
//Name: Hussain Iftikhar | ||
// userName: hussainiftikhar5242 | ||
//approach: First store the n values in vector then appy outer loop which will check the first condition and then appply inner for loop to k value which will firstly store the front value in variable and then remove the first value then again push back the value in vector after for loop remove the front value. again for removing the player | ||
class Solution { | ||
public: | ||
int findTheWinner(int n, int k) { | ||
vector<int> player; | ||
for(int i=1;i<=n;i++) | ||
{ | ||
player.push_back(i); | ||
} | ||
int value; | ||
while(true) | ||
{ | ||
if(player.size() == 1) | ||
{ | ||
break; | ||
} | ||
for(int i=1;i<k;i++) | ||
{ | ||
value=player.front(); | ||
player.erase(player.begin()); | ||
player.push_back(value); | ||
} | ||
player.erase(player.begin()); | ||
} | ||
return player.front(); | ||
|
||
} | ||
}; |