id int64 1 3.58k | problem_description stringlengths 516 21.8k | instruction int64 0 3 | solution_c dict |
|---|---|---|---|
486 | <p>You are given an integer array <code>nums</code>. Two players are playing a game with this array: player 1 and player 2.</p>
<p>Player 1 and player 2 take turns, with player 1 starting first. Both players start the game with a score of <code>0</code>. At each turn, the player takes one of the numbers from either end of the array (i.e., <code>nums[0]</code> or <code>nums[nums.length - 1]</code>) which reduces the size of the array by <code>1</code>. The player adds the chosen number to their score. The game ends when there are no more elements in the array.</p>
<p>Return <code>true</code> if Player 1 can win the game. If the scores of both players are equal, then player 1 is still the winner, and you should also return <code>true</code>. You may assume that both players are playing optimally.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,5,2]
<strong>Output:</strong> false
<strong>Explanation:</strong> Initially, player 1 can choose between 1 and 2.
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).
So, final score of player 1 is 1 + 2 = 3, and player 2 is 5.
Hence, player 1 will never be the winner and you need to return false.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,5,233,7]
<strong>Output:</strong> true
<strong>Explanation:</strong> Player 1 first chooses 1. Then player 2 has to choose between 5 and 7. No matter which number player 2 choose, player 1 can choose 233.
Finally, player 1 has more score (234) than player 2 (12), so you need to return True representing player1 can win.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 20</code></li>
<li><code>0 <= nums[i] <= 10<sup>7</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int solve(vector<int>& nums,int i,int j,vector<vector<int>>& dp){\n if(i>j){\n return 0;\n }\n if(dp[i][j]!=-1){\n return dp[i][j];\n }\n int take_i=nums[i]+min(solve(nums,i+2,j,dp),solve(nums,i+1,j-1,dp));\n int take_j=nums[j]+min(solve(nums,i+1,j-1,dp),solve(nums,i,j-2,dp));\n return dp[i][j]=max(take_i,take_j);\n }\n bool predictTheWinner(vector<int>& nums) {\n int n=nums.size();\n vector<vector<int>> dp(n+1,vector<int>(n+1,-1));\n int sum=0;\n for(int i=0;i<nums.size();i++){\n sum += nums[i];\n }\n int score=solve(nums,0,nums.size()-1,dp); \n int a=sum-score;\n if(score>=a){\n return true;\n }\n return false;\n }\n};",
"memory": "9200"
} |
486 | <p>You are given an integer array <code>nums</code>. Two players are playing a game with this array: player 1 and player 2.</p>
<p>Player 1 and player 2 take turns, with player 1 starting first. Both players start the game with a score of <code>0</code>. At each turn, the player takes one of the numbers from either end of the array (i.e., <code>nums[0]</code> or <code>nums[nums.length - 1]</code>) which reduces the size of the array by <code>1</code>. The player adds the chosen number to their score. The game ends when there are no more elements in the array.</p>
<p>Return <code>true</code> if Player 1 can win the game. If the scores of both players are equal, then player 1 is still the winner, and you should also return <code>true</code>. You may assume that both players are playing optimally.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,5,2]
<strong>Output:</strong> false
<strong>Explanation:</strong> Initially, player 1 can choose between 1 and 2.
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).
So, final score of player 1 is 1 + 2 = 3, and player 2 is 5.
Hence, player 1 will never be the winner and you need to return false.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,5,233,7]
<strong>Output:</strong> true
<strong>Explanation:</strong> Player 1 first chooses 1. Then player 2 has to choose between 5 and 7. No matter which number player 2 choose, player 1 can choose 233.
Finally, player 1 has more score (234) than player 2 (12), so you need to return True representing player1 can win.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 20</code></li>
<li><code>0 <= nums[i] <= 10<sup>7</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int dfs(int i,int j,vector<int>& nums,int turn,vector<vector<int>> &dp){\n if(i>j) return 0;\n if(dp[i][j]!=-1) return dp[i][j];\n int ans=0;\n\n if(turn==0) ans=max(nums[i]+dfs(i+1,j,nums,1,dp),nums[j]+dfs(i,j-1,nums,1,dp));\n else\n ans=min(-nums[i]+dfs(i+1,j,nums,0,dp),-nums[j]+dfs(i,j-1,nums,0,dp));\n return dp[i][j]=ans;\n }\n bool predictTheWinner(vector<int>& nums) {\n int n=nums.size();\n vector<vector<int>>dp(n,vector<int>(n,-1));\n return dfs(0,n-1,nums,0,dp)>=0;\n }\n};",
"memory": "9200"
} |
486 | <p>You are given an integer array <code>nums</code>. Two players are playing a game with this array: player 1 and player 2.</p>
<p>Player 1 and player 2 take turns, with player 1 starting first. Both players start the game with a score of <code>0</code>. At each turn, the player takes one of the numbers from either end of the array (i.e., <code>nums[0]</code> or <code>nums[nums.length - 1]</code>) which reduces the size of the array by <code>1</code>. The player adds the chosen number to their score. The game ends when there are no more elements in the array.</p>
<p>Return <code>true</code> if Player 1 can win the game. If the scores of both players are equal, then player 1 is still the winner, and you should also return <code>true</code>. You may assume that both players are playing optimally.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,5,2]
<strong>Output:</strong> false
<strong>Explanation:</strong> Initially, player 1 can choose between 1 and 2.
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).
So, final score of player 1 is 1 + 2 = 3, and player 2 is 5.
Hence, player 1 will never be the winner and you need to return false.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,5,233,7]
<strong>Output:</strong> true
<strong>Explanation:</strong> Player 1 first chooses 1. Then player 2 has to choose between 5 and 7. No matter which number player 2 choose, player 1 can choose 233.
Finally, player 1 has more score (234) than player 2 (12), so you need to return True representing player1 can win.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 20</code></li>
<li><code>0 <= nums[i] <= 10<sup>7</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n bool predictTheWinner(vector<int>& nums) {\n int n=nums.size();\n \n vector<vector<int>> dp(n,vector<int>(n,0));\n for(int i=0;i<n;i++){\n dp[i][i]=nums[i];\n }\n for(int i=n-2;i>=0;i--){\n for(int j=i+1;j<n;j++){\n dp[i][j] = max(nums[i] - dp[i+1][j], nums[j] - dp[i][j-1]);\n }\n }\n \n return dp[0][n-1] >= 0;\n }\n};",
"memory": "9300"
} |
486 | <p>You are given an integer array <code>nums</code>. Two players are playing a game with this array: player 1 and player 2.</p>
<p>Player 1 and player 2 take turns, with player 1 starting first. Both players start the game with a score of <code>0</code>. At each turn, the player takes one of the numbers from either end of the array (i.e., <code>nums[0]</code> or <code>nums[nums.length - 1]</code>) which reduces the size of the array by <code>1</code>. The player adds the chosen number to their score. The game ends when there are no more elements in the array.</p>
<p>Return <code>true</code> if Player 1 can win the game. If the scores of both players are equal, then player 1 is still the winner, and you should also return <code>true</code>. You may assume that both players are playing optimally.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,5,2]
<strong>Output:</strong> false
<strong>Explanation:</strong> Initially, player 1 can choose between 1 and 2.
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).
So, final score of player 1 is 1 + 2 = 3, and player 2 is 5.
Hence, player 1 will never be the winner and you need to return false.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,5,233,7]
<strong>Output:</strong> true
<strong>Explanation:</strong> Player 1 first chooses 1. Then player 2 has to choose between 5 and 7. No matter which number player 2 choose, player 1 can choose 233.
Finally, player 1 has more score (234) than player 2 (12), so you need to return True representing player1 can win.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 20</code></li>
<li><code>0 <= nums[i] <= 10<sup>7</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n bool predictTheWinner(vector<int>& nums) {\n int n = nums.size();\n vector<vector<int>> dp(n, vector<int>(n, 0));\n \n for (int i = 0; i < n; i++) {\n dp[i][i] = nums[i];\n }\n \n for (int len = 2; len <= n; len++) {\n for (int i = 0; i <= n - len; i++) {\n int j = i + len - 1; \n int op1 = nums[i] + min((i + 2 <= j ? dp[i+2][j] : 0), (i + 1 <= j - 1 ? dp[i+1][j-1] : 0));\n int op2 = nums[j] + min((i <= j - 2 ? dp[i][j-2] : 0), (i + 1 <= j - 1 ? dp[i+1][j-1] : 0));\n \n dp[i][j] = max(op1, op2);\n }\n }\n \n int totalSum = accumulate(nums.begin(), nums.end(), 0);\n int player1 = dp[0][n-1];\n return player1 >= totalSum - player1;\n }\n};\n",
"memory": "9300"
} |
486 | <p>You are given an integer array <code>nums</code>. Two players are playing a game with this array: player 1 and player 2.</p>
<p>Player 1 and player 2 take turns, with player 1 starting first. Both players start the game with a score of <code>0</code>. At each turn, the player takes one of the numbers from either end of the array (i.e., <code>nums[0]</code> or <code>nums[nums.length - 1]</code>) which reduces the size of the array by <code>1</code>. The player adds the chosen number to their score. The game ends when there are no more elements in the array.</p>
<p>Return <code>true</code> if Player 1 can win the game. If the scores of both players are equal, then player 1 is still the winner, and you should also return <code>true</code>. You may assume that both players are playing optimally.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,5,2]
<strong>Output:</strong> false
<strong>Explanation:</strong> Initially, player 1 can choose between 1 and 2.
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).
So, final score of player 1 is 1 + 2 = 3, and player 2 is 5.
Hence, player 1 will never be the winner and you need to return false.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,5,233,7]
<strong>Output:</strong> true
<strong>Explanation:</strong> Player 1 first chooses 1. Then player 2 has to choose between 5 and 7. No matter which number player 2 choose, player 1 can choose 233.
Finally, player 1 has more score (234) than player 2 (12), so you need to return True representing player1 can win.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 20</code></li>
<li><code>0 <= nums[i] <= 10<sup>7</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n\n int memFunc(vector<int>& nums, vector<vector<int>> &dp, int s, int e){\n if(s > e){\n return 0; \n }\n\n if(dp[s][e] != -1) return dp[s][e] ; \n\n dp[s][e] = max(nums[s] - memFunc(nums, dp, s+1, e), nums[e] - memFunc(nums, dp, s, e-1)) ; \n\n return dp[s][e] ; \n }\n\n bool predictTheWinner(vector<int>& nums) {\n int n = nums.size(); \n vector<vector<int>> dp(n, vector<int>(n, -1)) ; \n\n if(n%2 == 1) return memFunc(nums, dp, 0, n-1) >= 0 ; //if scores equal then also p1 winner so true... \n else return true ; \n // Always true for even size array...... \n }\n};",
"memory": "9400"
} |
486 | <p>You are given an integer array <code>nums</code>. Two players are playing a game with this array: player 1 and player 2.</p>
<p>Player 1 and player 2 take turns, with player 1 starting first. Both players start the game with a score of <code>0</code>. At each turn, the player takes one of the numbers from either end of the array (i.e., <code>nums[0]</code> or <code>nums[nums.length - 1]</code>) which reduces the size of the array by <code>1</code>. The player adds the chosen number to their score. The game ends when there are no more elements in the array.</p>
<p>Return <code>true</code> if Player 1 can win the game. If the scores of both players are equal, then player 1 is still the winner, and you should also return <code>true</code>. You may assume that both players are playing optimally.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,5,2]
<strong>Output:</strong> false
<strong>Explanation:</strong> Initially, player 1 can choose between 1 and 2.
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).
So, final score of player 1 is 1 + 2 = 3, and player 2 is 5.
Hence, player 1 will never be the winner and you need to return false.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,5,233,7]
<strong>Output:</strong> true
<strong>Explanation:</strong> Player 1 first chooses 1. Then player 2 has to choose between 5 and 7. No matter which number player 2 choose, player 1 can choose 233.
Finally, player 1 has more score (234) than player 2 (12), so you need to return True representing player1 can win.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 20</code></li>
<li><code>0 <= nums[i] <= 10<sup>7</sup></code></li>
</ul>
| 3 | {
"code": "// Recursion tarika 1\nclass Solution1 {\npublic:\n // Ye function player1 ka score nikalega, min-max game strategy leke,\n\n long long solve(vector<int>& nums, int i, int j, vector<vector<long long>>& mem) {\n if (i > j) return 0;\n\n // 1 ne i liya, phir 2 agar i+1 lega to solve(i+2, j) call hoga, aur agar j lega to\n // solve(i+1, j-1) (Next call) call hoga \n\n // 1 ne j liya, phir 2 agar i lega to solve(i+1, j-1) call hoga, aur agar j-1 lega to\n // solve(i, j-2) (Next call) call hoga \n\n if (mem[i][j] != -1) return mem[i][j];\n\n long long takeI = nums[i] + min(solve(nums, i+2, j, mem), solve(nums, i+1, j-1, mem));\n long long takeJ = nums[j] + min(solve(nums, i+1, j-1, mem), solve(nums, i, j-2, mem));\n \n return mem[i][j] = max(takeI, takeJ);\n }\n bool predictTheWinner(vector<int>& nums) {\n long long sum = 0;\n for (int num: nums) sum += num;\n\n int n = nums.size();\n vector<vector<long long>> mem(n+1, vector<long long>(n+1, -1));\n long long player1Score = solve(nums, 0, nums.size() -1, mem);\n long long player2Score = sum - player1Score;\n if (player1Score >= player2Score) return true;\n else return false;\n }\n};\n\n\n\n// Recursion tarika 2\nclass Solution {\npublic:\n // Ye function player1 aur player2 ka difference nikalega, min-max game strategy leke,\n // Yaha pe player1 ke score ko lete jayenge aur usme se player2 ka score minus karte jayenge\n\n long long solve(vector<int>& nums, int i, int j, vector<vector<long long>>& mem) {\n if (i > j) return 0;\n\n if (mem[i][j] != -1) return mem[i][j];\n\n // 1 ne agar i liya to uska score le liya phir 2 jo hai wo (i+1) ya j me kuch lega to uska score minus kar denge\n // Same for agar 1 ne j liya\n\n long long takeI = nums[i] - solve(nums, i+1, j, mem);\n long long takeJ = nums[j] - solve(nums, i, j-1, mem);\n \n return mem[i][j] = max(takeI, takeJ);\n }\n\n bool predictTheWinner(vector<int>& nums) {\n \n int n = nums.size();\n vector<vector<long long>> mem(n+1, vector<long long>(n+1, -1));\n return solve(nums, 0, nums.size() -1, mem) >=0;\n }\n};",
"memory": "9500"
} |
486 | <p>You are given an integer array <code>nums</code>. Two players are playing a game with this array: player 1 and player 2.</p>
<p>Player 1 and player 2 take turns, with player 1 starting first. Both players start the game with a score of <code>0</code>. At each turn, the player takes one of the numbers from either end of the array (i.e., <code>nums[0]</code> or <code>nums[nums.length - 1]</code>) which reduces the size of the array by <code>1</code>. The player adds the chosen number to their score. The game ends when there are no more elements in the array.</p>
<p>Return <code>true</code> if Player 1 can win the game. If the scores of both players are equal, then player 1 is still the winner, and you should also return <code>true</code>. You may assume that both players are playing optimally.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,5,2]
<strong>Output:</strong> false
<strong>Explanation:</strong> Initially, player 1 can choose between 1 and 2.
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).
So, final score of player 1 is 1 + 2 = 3, and player 2 is 5.
Hence, player 1 will never be the winner and you need to return false.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,5,233,7]
<strong>Output:</strong> true
<strong>Explanation:</strong> Player 1 first chooses 1. Then player 2 has to choose between 5 and 7. No matter which number player 2 choose, player 1 can choose 233.
Finally, player 1 has more score (234) than player 2 (12), so you need to return True representing player1 can win.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 20</code></li>
<li><code>0 <= nums[i] <= 10<sup>7</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n/*OPTIMAL GAME STRATEGY CONCEPT:\nwhen its your turn do your best \nwhen its others turn expect least from him */\n\n// RECURSION 2 : storing the difference of p1 and whatever the p2 returns and if this difference is >=0 p1 wins\n#define ll long long int\n/*ll f(int s,int e,vector<int>& nums,vector<vector<ll>>&dp){\n if(s>e) return 0;// exhausted the nums\n if(s==e) return nums[s];// onle one ele left\n\nif(dp[s][e]!=-1) return dp[s][e];\n\n ll take_s=0,take_e=0;\n take_s=nums[s] - f(s+1,e,nums,dp);\n take_e=nums[e] - f(s,e-1,nums,dp);\n\n return dp[s][e]=max(take_s,take_e);\n} */\n bool predictTheWinner(vector<int>& nums) {\n int n=nums.size();\n \n // vector<vector<ll>>dp(n+1,vector<ll>(n+1,-1));\n // return f(0,n-1,nums,dp) >=0;\n //tabulation/Bottom up\n vector<vector<ll>>dp(n+2,vector<ll>(n+2,0));\n for(int s=n;s>=1;s--){\n for(int e=1;e<=n;e++){\n if(s==e) {\n dp[s][e]=nums[s-1];\n continue;\n }\n ll take_s=0,take_e=0;\n take_s=nums[s-1] - dp[s+1][e];\n take_e=nums[e-1] - dp[s][e-1];\n dp[s][e]=max(take_s,take_e);\n }\n }\n return dp[1][n]>=0; \n }\n};",
"memory": "9600"
} |
486 | <p>You are given an integer array <code>nums</code>. Two players are playing a game with this array: player 1 and player 2.</p>
<p>Player 1 and player 2 take turns, with player 1 starting first. Both players start the game with a score of <code>0</code>. At each turn, the player takes one of the numbers from either end of the array (i.e., <code>nums[0]</code> or <code>nums[nums.length - 1]</code>) which reduces the size of the array by <code>1</code>. The player adds the chosen number to their score. The game ends when there are no more elements in the array.</p>
<p>Return <code>true</code> if Player 1 can win the game. If the scores of both players are equal, then player 1 is still the winner, and you should also return <code>true</code>. You may assume that both players are playing optimally.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,5,2]
<strong>Output:</strong> false
<strong>Explanation:</strong> Initially, player 1 can choose between 1 and 2.
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).
So, final score of player 1 is 1 + 2 = 3, and player 2 is 5.
Hence, player 1 will never be the winner and you need to return false.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,5,233,7]
<strong>Output:</strong> true
<strong>Explanation:</strong> Player 1 first chooses 1. Then player 2 has to choose between 5 and 7. No matter which number player 2 choose, player 1 can choose 233.
Finally, player 1 has more score (234) than player 2 (12), so you need to return True representing player1 can win.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 20</code></li>
<li><code>0 <= nums[i] <= 10<sup>7</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n\n bool fun(vector<int>& nums,int o,int sa,int sb,int i,int j,int n,vector<vector<vector<int>>>&dp,int sum){\n if( i>=n || j<0)return sa>=sb;\n \n if(sa>=sum)return true;\n if(i>j){\n if(sa>=sb)return true;\n else return false;\n \n }\n if(dp[o][i][j]!=-1)return dp[o][i][j];\n if(o==0){\n bool a=fun(nums,1,sa+nums[i],sb,i+1,j,n,dp,sum);\n bool b=fun(nums,1,sa+nums[j],sb,i,j-1,n,dp,sum);\n return (a || b);\n }\n else{\n bool a=fun(nums,0,sa,sb+nums[i],i+1,j,n,dp,sum);\n bool b=fun(nums,0,sa,sb+nums[j],i,j-1,n,dp,sum);\n return (a && b);\n }\n }\n bool predictTheWinner(vector<int>& nums) {\n \n int n=nums.size();\n int sum=0;\n for(auto i:nums){\n sum+=i;\n }\n vector<vector<vector<int>>>dp(2,vector<vector<int>>(n,vector<int>(n,-1)));\n int sa=0,sb=0,i=0,j=n-1;\n return fun(nums,0,sa,sb,i,j,n,dp,sum/2+1);\n \n }\n};",
"memory": "9600"
} |
486 | <p>You are given an integer array <code>nums</code>. Two players are playing a game with this array: player 1 and player 2.</p>
<p>Player 1 and player 2 take turns, with player 1 starting first. Both players start the game with a score of <code>0</code>. At each turn, the player takes one of the numbers from either end of the array (i.e., <code>nums[0]</code> or <code>nums[nums.length - 1]</code>) which reduces the size of the array by <code>1</code>. The player adds the chosen number to their score. The game ends when there are no more elements in the array.</p>
<p>Return <code>true</code> if Player 1 can win the game. If the scores of both players are equal, then player 1 is still the winner, and you should also return <code>true</code>. You may assume that both players are playing optimally.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,5,2]
<strong>Output:</strong> false
<strong>Explanation:</strong> Initially, player 1 can choose between 1 and 2.
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).
So, final score of player 1 is 1 + 2 = 3, and player 2 is 5.
Hence, player 1 will never be the winner and you need to return false.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,5,233,7]
<strong>Output:</strong> true
<strong>Explanation:</strong> Player 1 first chooses 1. Then player 2 has to choose between 5 and 7. No matter which number player 2 choose, player 1 can choose 233.
Finally, player 1 has more score (234) than player 2 (12), so you need to return True representing player1 can win.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 20</code></li>
<li><code>0 <= nums[i] <= 10<sup>7</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n\n using pii = pair<int, int>;\n bool predictTheWinner(vector<int>& nums) {\n int n = nums.size();\n vector<vector<pii>> p1(n + 1, vector<pii>(n + 1, make_pair(0, 0)));\n vector<vector<pii>> p2(n + 1, vector<pii>(n + 1, make_pair(0, 0)));\n p1[n - 1][n] = make_pair(nums[n-1], 0);\n p2[n - 1][n] = make_pair(0, nums[n-1]);\n for (int i = n - 1; i >= 0; --i) {\n for (int j = i + 1; j <= n; ++j) {\n auto picksLeft = make_pair(nums[i] + p2[i + 1][j].first, p2[i + 1][j].second);\n auto picksRight = make_pair(nums[j - 1] + p2[i][j - 1].first, p2[i][j - 1].second);\n if (picksLeft.first >= picksRight.first) {\n p1[i][j] = picksLeft;\n } else {\n p1[i][j] = picksRight;\n }\n \n picksLeft = make_pair(p1[i + 1][j].first, nums[i] + p1[i + 1][j].second);\n picksRight = make_pair(p1[i][j - 1].first, nums[j - 1] + p1[i][j - 1].second);\n \n if (picksLeft.second >= picksRight.second) {\n p2[i][j] = picksLeft;\n } else {\n p2[i][j] = picksRight;\n }\n }\n }\n\n return p1[0][n].first >= p1[0][n].second;\n }\n\n};",
"memory": "9700"
} |
486 | <p>You are given an integer array <code>nums</code>. Two players are playing a game with this array: player 1 and player 2.</p>
<p>Player 1 and player 2 take turns, with player 1 starting first. Both players start the game with a score of <code>0</code>. At each turn, the player takes one of the numbers from either end of the array (i.e., <code>nums[0]</code> or <code>nums[nums.length - 1]</code>) which reduces the size of the array by <code>1</code>. The player adds the chosen number to their score. The game ends when there are no more elements in the array.</p>
<p>Return <code>true</code> if Player 1 can win the game. If the scores of both players are equal, then player 1 is still the winner, and you should also return <code>true</code>. You may assume that both players are playing optimally.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,5,2]
<strong>Output:</strong> false
<strong>Explanation:</strong> Initially, player 1 can choose between 1 and 2.
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).
So, final score of player 1 is 1 + 2 = 3, and player 2 is 5.
Hence, player 1 will never be the winner and you need to return false.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,5,233,7]
<strong>Output:</strong> true
<strong>Explanation:</strong> Player 1 first chooses 1. Then player 2 has to choose between 5 and 7. No matter which number player 2 choose, player 1 can choose 233.
Finally, player 1 has more score (234) than player 2 (12), so you need to return True representing player1 can win.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 20</code></li>
<li><code>0 <= nums[i] <= 10<sup>7</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n bool predictTheWinner(vector<int>& nums) {\n int n = nums.size();\n int f[n][n];\n memset(f, 0, sizeof(f));\n function<int(int, int)> dfs = [&](int i, int j) -> int {\n if (i > j) {\n return 0;\n }\n if (f[i][j]) {\n return f[i][j];\n }\n return f[i][j] = max(nums[i] - dfs(i + 1, j), nums[j] - dfs(i, j - 1));\n };\n return dfs(0, n - 1) >= 0;\n }\n};",
"memory": "9800"
} |
486 | <p>You are given an integer array <code>nums</code>. Two players are playing a game with this array: player 1 and player 2.</p>
<p>Player 1 and player 2 take turns, with player 1 starting first. Both players start the game with a score of <code>0</code>. At each turn, the player takes one of the numbers from either end of the array (i.e., <code>nums[0]</code> or <code>nums[nums.length - 1]</code>) which reduces the size of the array by <code>1</code>. The player adds the chosen number to their score. The game ends when there are no more elements in the array.</p>
<p>Return <code>true</code> if Player 1 can win the game. If the scores of both players are equal, then player 1 is still the winner, and you should also return <code>true</code>. You may assume that both players are playing optimally.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,5,2]
<strong>Output:</strong> false
<strong>Explanation:</strong> Initially, player 1 can choose between 1 and 2.
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).
So, final score of player 1 is 1 + 2 = 3, and player 2 is 5.
Hence, player 1 will never be the winner and you need to return false.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,5,233,7]
<strong>Output:</strong> true
<strong>Explanation:</strong> Player 1 first chooses 1. Then player 2 has to choose between 5 and 7. No matter which number player 2 choose, player 1 can choose 233.
Finally, player 1 has more score (234) than player 2 (12), so you need to return True representing player1 can win.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 20</code></li>
<li><code>0 <= nums[i] <= 10<sup>7</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n bool predictTheWinner(vector<int>& nums) {\n // Observations:\n // - We can simulate the state of the game by (scoreA, who, from, to)\n // - The initial state would be: (0, A, 0, n-1)\n // - The final state would be: (scoreA, n % 2 ? Alice : Bob, k, k)\n //\n // Approach 1: Backtracking\n // if from > to the game is done: return true if existing any scenarios that scoreA >= scoreB\n // take front for current player\n // take back to current playler\n // Time: O(2 ** nums.length) ~ 1e6\n // Space: O(nums.length)\n //\n\n int total_score = accumulate(nums.begin(), nums.end(), 0);\n\n function<bool(int, bool, int, int)> backtrack = [&](int score, bool is_a_turn, int from, int to) {\n if (score * 2 >= total_score) return true;\n if (from > to) return false;\n if (is_a_turn) {\n return backtrack(score + nums[from], !is_a_turn, from + 1, to)\n || backtrack(score + nums[to], !is_a_turn, from, to - 1);\n } else {\n return backtrack(score, !is_a_turn, from + 1, to)\n && backtrack(score, !is_a_turn, from, to - 1);\n }\n };\n\n return backtrack(0, true, 0, nums.size() - 1);\n }\n};",
"memory": "10000"
} |
486 | <p>You are given an integer array <code>nums</code>. Two players are playing a game with this array: player 1 and player 2.</p>
<p>Player 1 and player 2 take turns, with player 1 starting first. Both players start the game with a score of <code>0</code>. At each turn, the player takes one of the numbers from either end of the array (i.e., <code>nums[0]</code> or <code>nums[nums.length - 1]</code>) which reduces the size of the array by <code>1</code>. The player adds the chosen number to their score. The game ends when there are no more elements in the array.</p>
<p>Return <code>true</code> if Player 1 can win the game. If the scores of both players are equal, then player 1 is still the winner, and you should also return <code>true</code>. You may assume that both players are playing optimally.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,5,2]
<strong>Output:</strong> false
<strong>Explanation:</strong> Initially, player 1 can choose between 1 and 2.
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).
So, final score of player 1 is 1 + 2 = 3, and player 2 is 5.
Hence, player 1 will never be the winner and you need to return false.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,5,233,7]
<strong>Output:</strong> true
<strong>Explanation:</strong> Player 1 first chooses 1. Then player 2 has to choose between 5 and 7. No matter which number player 2 choose, player 1 can choose 233.
Finally, player 1 has more score (234) than player 2 (12), so you need to return True representing player1 can win.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 20</code></li>
<li><code>0 <= nums[i] <= 10<sup>7</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\nprivate:\n int solve(vector<int>& nums, int i, int j, bool player1,\n vector<vector<vector<int>>>& dp) {\n if (i > j) {\n return 0;\n }\n if (dp[i][j][player1] != -1)\n return dp[i][j][player1];\n\n if (player1) {\n int takeFromStart1 = nums[i] + solve(nums, i + 1, j, false, dp);\n int takeFromEnd1 = nums[j] + solve(nums, i, j - 1, false, dp);\n return dp[i][j][player1] = max(takeFromStart1, takeFromEnd1);\n } else {\n int takeFromStart2 = -nums[i] + solve(nums, i + 1, j, true, dp);\n int takeFromEnd2 = -nums[j] + solve(nums, i, j - 1, true, dp);\n return dp[i][j][player1] = min(takeFromStart2, takeFromEnd2);\n }\n }\n\npublic:\n bool predictTheWinner(vector<int>& nums) {\n int n = nums.size();\n vector<vector<vector<int>>> dp(\n n, vector<vector<int>>(n, vector<int>(2, -1)));\n int ans = solve(nums, 0, nums.size() - 1, true, dp);\n return ans >= 0;\n }\n};",
"memory": "10100"
} |
486 | <p>You are given an integer array <code>nums</code>. Two players are playing a game with this array: player 1 and player 2.</p>
<p>Player 1 and player 2 take turns, with player 1 starting first. Both players start the game with a score of <code>0</code>. At each turn, the player takes one of the numbers from either end of the array (i.e., <code>nums[0]</code> or <code>nums[nums.length - 1]</code>) which reduces the size of the array by <code>1</code>. The player adds the chosen number to their score. The game ends when there are no more elements in the array.</p>
<p>Return <code>true</code> if Player 1 can win the game. If the scores of both players are equal, then player 1 is still the winner, and you should also return <code>true</code>. You may assume that both players are playing optimally.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,5,2]
<strong>Output:</strong> false
<strong>Explanation:</strong> Initially, player 1 can choose between 1 and 2.
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).
So, final score of player 1 is 1 + 2 = 3, and player 2 is 5.
Hence, player 1 will never be the winner and you need to return false.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,5,233,7]
<strong>Output:</strong> true
<strong>Explanation:</strong> Player 1 first chooses 1. Then player 2 has to choose between 5 and 7. No matter which number player 2 choose, player 1 can choose 233.
Finally, player 1 has more score (234) than player 2 (12), so you need to return True representing player1 can win.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 20</code></li>
<li><code>0 <= nums[i] <= 10<sup>7</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\n long long f(int i, int j, bool turn, vector<int> &nums, vector<vector<vector<long long>>> &dp)\n {\n if(i == j) \n {\n if(turn) return nums[i];\n return 0;\n }\n if(dp[i][j][turn] != -1) return dp[i][j][turn];\n if(turn)\n {\n return dp[i][j][turn] = max(nums[i] + f(i + 1, j, false, nums, dp), \n nums[j] + f(i, j - 1, false, nums, dp));\n }\n else\n {\n return dp[i][j][turn] = min(0 + f(i + 1, j, true, nums, dp), \n 0 + f(i, j - 1, true, nums, dp));\n }\n return 0;\n }\npublic:\n bool predictTheWinner(vector<int>& nums) {\n long long sum = accumulate(nums.begin(), nums.end(), 0);\n int n = nums.size();\n vector<vector<vector<long long>>>dp(n, vector<vector<long long>>(n , vector<long long>(2, -1)));\n long long player1_score = f(0, nums.size() - 1, 1, nums, dp);\n return player1_score >= sum - player1_score;\n }\n};",
"memory": "10200"
} |
486 | <p>You are given an integer array <code>nums</code>. Two players are playing a game with this array: player 1 and player 2.</p>
<p>Player 1 and player 2 take turns, with player 1 starting first. Both players start the game with a score of <code>0</code>. At each turn, the player takes one of the numbers from either end of the array (i.e., <code>nums[0]</code> or <code>nums[nums.length - 1]</code>) which reduces the size of the array by <code>1</code>. The player adds the chosen number to their score. The game ends when there are no more elements in the array.</p>
<p>Return <code>true</code> if Player 1 can win the game. If the scores of both players are equal, then player 1 is still the winner, and you should also return <code>true</code>. You may assume that both players are playing optimally.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,5,2]
<strong>Output:</strong> false
<strong>Explanation:</strong> Initially, player 1 can choose between 1 and 2.
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).
So, final score of player 1 is 1 + 2 = 3, and player 2 is 5.
Hence, player 1 will never be the winner and you need to return false.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,5,233,7]
<strong>Output:</strong> true
<strong>Explanation:</strong> Player 1 first chooses 1. Then player 2 has to choose between 5 and 7. No matter which number player 2 choose, player 1 can choose 233.
Finally, player 1 has more score (234) than player 2 (12), so you need to return True representing player1 can win.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 20</code></li>
<li><code>0 <= nums[i] <= 10<sup>7</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\n long long f(int i, int j, bool turn, vector<int> &nums, vector<vector<vector<long long>>> &dp)\n {\n if(i == j) \n {\n if(turn) return nums[i];\n return 0;\n }\n if(dp[i][j][turn] != -1) return dp[i][j][turn];\n if(turn)\n {\n return dp[i][j][turn] = max(nums[i] + f(i + 1, j, false, nums, dp), \n nums[j] + f(i, j - 1, false, nums, dp));\n }\n else\n {\n return dp[i][j][turn] = min(0 + f(i + 1, j, true, nums, dp), \n 0 + f(i, j - 1, true, nums, dp));\n }\n return 0;\n }\npublic:\n bool predictTheWinner(vector<int>& nums) {\n long long sum = accumulate(nums.begin(), nums.end(), 0);\n int n = nums.size();\n vector<vector<vector<long long>>>dp(n, vector<vector<long long>>(n , vector<long long>(2, -1)));\n long long player1_score = f(0, n - 1, 1, nums, dp);\n return player1_score >= sum - player1_score;\n }\n};\n\n\n\n",
"memory": "10200"
} |
486 | <p>You are given an integer array <code>nums</code>. Two players are playing a game with this array: player 1 and player 2.</p>
<p>Player 1 and player 2 take turns, with player 1 starting first. Both players start the game with a score of <code>0</code>. At each turn, the player takes one of the numbers from either end of the array (i.e., <code>nums[0]</code> or <code>nums[nums.length - 1]</code>) which reduces the size of the array by <code>1</code>. The player adds the chosen number to their score. The game ends when there are no more elements in the array.</p>
<p>Return <code>true</code> if Player 1 can win the game. If the scores of both players are equal, then player 1 is still the winner, and you should also return <code>true</code>. You may assume that both players are playing optimally.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,5,2]
<strong>Output:</strong> false
<strong>Explanation:</strong> Initially, player 1 can choose between 1 and 2.
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).
So, final score of player 1 is 1 + 2 = 3, and player 2 is 5.
Hence, player 1 will never be the winner and you need to return false.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,5,233,7]
<strong>Output:</strong> true
<strong>Explanation:</strong> Player 1 first chooses 1. Then player 2 has to choose between 5 and 7. No matter which number player 2 choose, player 1 can choose 233.
Finally, player 1 has more score (234) than player 2 (12), so you need to return True representing player1 can win.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 20</code></li>
<li><code>0 <= nums[i] <= 10<sup>7</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int solve(vector<int>&nums,int i,int j,int turn,vector<vector<vector<int>>>&dp){\n if(i > j){\n return 0;\n }\n\n if(dp[i][j][turn] != -1){\n return dp[i][j][turn];\n }\n\n if(turn == 0){\n int ans1 = nums[i] + solve(nums,i+1,j,1-turn,dp);\n int ans2 = nums[j] + solve(nums,i,j-1,1-turn,dp);\n \n dp[i][j][turn] = max(ans1,ans2);\n return dp[i][j][turn];\n }\n else{\n int ans1 = 0 + solve(nums,i+1,j,1-turn,dp);\n int ans2 = 0 + solve(nums,i,j-1,1-turn,dp);\n\n dp[i][j][turn] = min(ans1,ans2);\n return dp[i][j][turn];\n }\n }\n\n bool predictTheWinner(vector<int>& nums) {\n int sum = 0;\n for(int i=0;i<nums.size();i++){\n sum+=nums[i];\n }\n\n vector<vector<vector<int>>>dp(nums.size(),vector<vector<int>>(nums.size(),vector<int>(2,-1)));\n\n int ans = solve(nums,0,nums.size()-1,0,dp);\n\n return ans >= sum - ans;\n }\n};",
"memory": "10300"
} |
486 | <p>You are given an integer array <code>nums</code>. Two players are playing a game with this array: player 1 and player 2.</p>
<p>Player 1 and player 2 take turns, with player 1 starting first. Both players start the game with a score of <code>0</code>. At each turn, the player takes one of the numbers from either end of the array (i.e., <code>nums[0]</code> or <code>nums[nums.length - 1]</code>) which reduces the size of the array by <code>1</code>. The player adds the chosen number to their score. The game ends when there are no more elements in the array.</p>
<p>Return <code>true</code> if Player 1 can win the game. If the scores of both players are equal, then player 1 is still the winner, and you should also return <code>true</code>. You may assume that both players are playing optimally.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,5,2]
<strong>Output:</strong> false
<strong>Explanation:</strong> Initially, player 1 can choose between 1 and 2.
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).
So, final score of player 1 is 1 + 2 = 3, and player 2 is 5.
Hence, player 1 will never be the winner and you need to return false.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,5,233,7]
<strong>Output:</strong> true
<strong>Explanation:</strong> Player 1 first chooses 1. Then player 2 has to choose between 5 and 7. No matter which number player 2 choose, player 1 can choose 233.
Finally, player 1 has more score (234) than player 2 (12), so you need to return True representing player1 can win.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 20</code></li>
<li><code>0 <= nums[i] <= 10<sup>7</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n long long helper(vector<int>& nums, int turn, int l, int r, vector<vector<vector<int>>>& dp){\n if(l>r){\n return 0;\n }\n if(dp[l][r][turn]!=-1) return dp[l][r][turn];\n long long ans;\n if(turn==1){\n ans = max(nums[l] + helper(nums, 0, l+1, r, dp), nums[r] + helper(nums, 0, l, r-1, dp) );\n }\n else{\n ans = min( -1*nums[l] + helper(nums, 1, l+1, r, dp), -1*nums[r] + helper(nums, 1, l, r-1, dp) );\n }\n return dp[l][r][turn] = ans;\n }\n\n bool predictTheWinner(vector<int>& nums) {\n vector<vector<vector<int>>> dp(nums.size(), vector<vector<int>>(nums.size(), vector<int>(2, -1)));\n return helper(nums, 1, 0, nums.size()-1, dp)>=0;\n }\n};",
"memory": "10300"
} |
2,727 | <p>You are given a <strong>0-indexed</strong> array of strings <code>details</code>. Each element of <code>details</code> provides information about a given passenger compressed into a string of length <code>15</code>. The system is such that:</p>
<ul>
<li>The first ten characters consist of the phone number of passengers.</li>
<li>The next character denotes the gender of the person.</li>
<li>The following two characters are used to indicate the age of the person.</li>
<li>The last two characters determine the seat allotted to that person.</li>
</ul>
<p>Return <em>the number of passengers who are <strong>strictly </strong><strong>more than 60 years old</strong>.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> details = ["7868190130M7522","5303914400F9211","9273338290F4010"]
<strong>Output:</strong> 2
<strong>Explanation:</strong> The passengers at indices 0, 1, and 2 have ages 75, 92, and 40. Thus, there are 2 people who are over 60 years old.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> details = ["1313579440F2036","2921522980M5644"]
<strong>Output:</strong> 0
<strong>Explanation:</strong> None of the passengers are older than 60.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= details.length <= 100</code></li>
<li><code>details[i].length == 15</code></li>
<li><code>details[i] consists of digits from '0' to '9'.</code></li>
<li><code>details[i][10] is either 'M' or 'F' or 'O'.</code></li>
<li>The phone numbers and seat numbers of the passengers are distinct.</li>
</ul>
| 0 | {
"code": "\nauto init = []() {\n cin.tie(nullptr)->sync_with_stdio(false);\n freopen(\"user.out\", \"w\", stdout);\n\n char agearr[3];\n string line;\n while (std::getline(std::cin, line)) {\n std::istringstream input(line);\n int cnt = 0;\n input.ignore(13);\n while (!input.eof()) {\n input.get(agearr, 3);\n if (agearr[0] > '6' || (agearr[0] == '6' && agearr[1] > '0'))\n ++cnt;\n input.ignore(16);\n }\n cout << cnt << '\\n';\n }\n \n\n exit(0);\n return 0;\n}();\n\nclass Solution {\npublic:\n int countSeniors(vector<string>& details) { return 0; }\n};",
"memory": "8000"
} |
2,727 | <p>You are given a <strong>0-indexed</strong> array of strings <code>details</code>. Each element of <code>details</code> provides information about a given passenger compressed into a string of length <code>15</code>. The system is such that:</p>
<ul>
<li>The first ten characters consist of the phone number of passengers.</li>
<li>The next character denotes the gender of the person.</li>
<li>The following two characters are used to indicate the age of the person.</li>
<li>The last two characters determine the seat allotted to that person.</li>
</ul>
<p>Return <em>the number of passengers who are <strong>strictly </strong><strong>more than 60 years old</strong>.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> details = ["7868190130M7522","5303914400F9211","9273338290F4010"]
<strong>Output:</strong> 2
<strong>Explanation:</strong> The passengers at indices 0, 1, and 2 have ages 75, 92, and 40. Thus, there are 2 people who are over 60 years old.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> details = ["1313579440F2036","2921522980M5644"]
<strong>Output:</strong> 0
<strong>Explanation:</strong> None of the passengers are older than 60.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= details.length <= 100</code></li>
<li><code>details[i].length == 15</code></li>
<li><code>details[i] consists of digits from '0' to '9'.</code></li>
<li><code>details[i][10] is either 'M' or 'F' or 'O'.</code></li>
<li>The phone numbers and seat numbers of the passengers are distinct.</li>
</ul>
| 0 | {
"code": "\nauto init = []() {\n cin.tie(nullptr)->sync_with_stdio(false);\n freopen(\"user.out\", \"w\", stdout);\n\n char agearr[3];\n string line;\n while (std::getline(std::cin, line)) {\n std::istringstream input(line);\n int cnt = 0;\n input.ignore(13);\n while (!input.eof()) {\n input.get(agearr, 3);\n if (agearr[0] > '6' || (agearr[0] == '6' && agearr[1] > '0'))\n ++cnt;\n input.ignore(16);\n }\n cout << cnt << '\\n';\n }\n \n\n exit(0);\n return 0;\n}();\n\nclass Solution {\npublic:\n int countSeniors(vector<string>& details) { return 0; }\n};",
"memory": "8100"
} |
2,727 | <p>You are given a <strong>0-indexed</strong> array of strings <code>details</code>. Each element of <code>details</code> provides information about a given passenger compressed into a string of length <code>15</code>. The system is such that:</p>
<ul>
<li>The first ten characters consist of the phone number of passengers.</li>
<li>The next character denotes the gender of the person.</li>
<li>The following two characters are used to indicate the age of the person.</li>
<li>The last two characters determine the seat allotted to that person.</li>
</ul>
<p>Return <em>the number of passengers who are <strong>strictly </strong><strong>more than 60 years old</strong>.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> details = ["7868190130M7522","5303914400F9211","9273338290F4010"]
<strong>Output:</strong> 2
<strong>Explanation:</strong> The passengers at indices 0, 1, and 2 have ages 75, 92, and 40. Thus, there are 2 people who are over 60 years old.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> details = ["1313579440F2036","2921522980M5644"]
<strong>Output:</strong> 0
<strong>Explanation:</strong> None of the passengers are older than 60.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= details.length <= 100</code></li>
<li><code>details[i].length == 15</code></li>
<li><code>details[i] consists of digits from '0' to '9'.</code></li>
<li><code>details[i][10] is either 'M' or 'F' or 'O'.</code></li>
<li>The phone numbers and seat numbers of the passengers are distinct.</li>
</ul>
| 0 | {
"code": "#pragma GCC optimize(\"O3,unroll-loops\")\n#pragma GCC target(\"avx2,bmi,bmi2,lzcnt,popcnt\")\n\nstatic const bool Booster = [](){\n std::ios_base::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::cout.tie(nullptr);\n return true;\n}();\n\nvoid parse_input_and_solve(std::ofstream& out, std::string& s) {\n s.erase(s.begin());\n s.pop_back();\n istringstream iss(s);\n string w;\n int cnt = 0;\n while(getline(iss, w, ',')) {\n int num = (w[12] - 48) * 10 + (w[13] - 48);\n if(num > 60) cnt++;\n }\n out << cnt << endl;\n}\n\nbool Solve = [](){\n std::ofstream out(\"user.out\");\n for (std::string s; std::getline(std::cin, s);) {\n parse_input_and_solve(out, s);\n }\n out.flush();\n exit(0);\n return true;\n}();\n\n\nclass Solution {\npublic:\n int countSeniors(vector<string>& det) {\n int cnt = 0;\n for(auto& t : det) {\n if(t[11] - 48 <= 5) continue;\n if(t[11] - 48 >= 6) cnt++;\n }\n return cnt;\n }\n};",
"memory": "8400"
} |
2,727 | <p>You are given a <strong>0-indexed</strong> array of strings <code>details</code>. Each element of <code>details</code> provides information about a given passenger compressed into a string of length <code>15</code>. The system is such that:</p>
<ul>
<li>The first ten characters consist of the phone number of passengers.</li>
<li>The next character denotes the gender of the person.</li>
<li>The following two characters are used to indicate the age of the person.</li>
<li>The last two characters determine the seat allotted to that person.</li>
</ul>
<p>Return <em>the number of passengers who are <strong>strictly </strong><strong>more than 60 years old</strong>.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> details = ["7868190130M7522","5303914400F9211","9273338290F4010"]
<strong>Output:</strong> 2
<strong>Explanation:</strong> The passengers at indices 0, 1, and 2 have ages 75, 92, and 40. Thus, there are 2 people who are over 60 years old.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> details = ["1313579440F2036","2921522980M5644"]
<strong>Output:</strong> 0
<strong>Explanation:</strong> None of the passengers are older than 60.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= details.length <= 100</code></li>
<li><code>details[i].length == 15</code></li>
<li><code>details[i] consists of digits from '0' to '9'.</code></li>
<li><code>details[i][10] is either 'M' or 'F' or 'O'.</code></li>
<li>The phone numbers and seat numbers of the passengers are distinct.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int countSeniors(vector<string>& details) {\n int ans = 0;\n for (auto& x : details) {\n int age = stoi(x.substr(11, 2));\n ans += age > 60;\n }\n return ans;\n }\n};",
"memory": "17200"
} |
2,727 | <p>You are given a <strong>0-indexed</strong> array of strings <code>details</code>. Each element of <code>details</code> provides information about a given passenger compressed into a string of length <code>15</code>. The system is such that:</p>
<ul>
<li>The first ten characters consist of the phone number of passengers.</li>
<li>The next character denotes the gender of the person.</li>
<li>The following two characters are used to indicate the age of the person.</li>
<li>The last two characters determine the seat allotted to that person.</li>
</ul>
<p>Return <em>the number of passengers who are <strong>strictly </strong><strong>more than 60 years old</strong>.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> details = ["7868190130M7522","5303914400F9211","9273338290F4010"]
<strong>Output:</strong> 2
<strong>Explanation:</strong> The passengers at indices 0, 1, and 2 have ages 75, 92, and 40. Thus, there are 2 people who are over 60 years old.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> details = ["1313579440F2036","2921522980M5644"]
<strong>Output:</strong> 0
<strong>Explanation:</strong> None of the passengers are older than 60.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= details.length <= 100</code></li>
<li><code>details[i].length == 15</code></li>
<li><code>details[i] consists of digits from '0' to '9'.</code></li>
<li><code>details[i][10] is either 'M' or 'F' or 'O'.</code></li>
<li>The phone numbers and seat numbers of the passengers are distinct.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int countSeniors(vector<string>& details) {\n int count = 0;\n int age = 0;\n for(string& str: details){\n age = ((str[11]) - '0') * 10 + str[12] - '0';\n \n age > 60? count++: NULL;\n }\n return count;\n }\n};",
"memory": "17300"
} |
2,727 | <p>You are given a <strong>0-indexed</strong> array of strings <code>details</code>. Each element of <code>details</code> provides information about a given passenger compressed into a string of length <code>15</code>. The system is such that:</p>
<ul>
<li>The first ten characters consist of the phone number of passengers.</li>
<li>The next character denotes the gender of the person.</li>
<li>The following two characters are used to indicate the age of the person.</li>
<li>The last two characters determine the seat allotted to that person.</li>
</ul>
<p>Return <em>the number of passengers who are <strong>strictly </strong><strong>more than 60 years old</strong>.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> details = ["7868190130M7522","5303914400F9211","9273338290F4010"]
<strong>Output:</strong> 2
<strong>Explanation:</strong> The passengers at indices 0, 1, and 2 have ages 75, 92, and 40. Thus, there are 2 people who are over 60 years old.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> details = ["1313579440F2036","2921522980M5644"]
<strong>Output:</strong> 0
<strong>Explanation:</strong> None of the passengers are older than 60.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= details.length <= 100</code></li>
<li><code>details[i].length == 15</code></li>
<li><code>details[i] consists of digits from '0' to '9'.</code></li>
<li><code>details[i][10] is either 'M' or 'F' or 'O'.</code></li>
<li>The phone numbers and seat numbers of the passengers are distinct.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int countSeniors(vector<string>& details) {\n int count =0;\n for(string& s : details){\n int age = (s[11] - '0')*10 + (s[12] - '0');\n if( age > 60)\n ++count;\n }\n return count;\n }\n};",
"memory": "17400"
} |
2,727 | <p>You are given a <strong>0-indexed</strong> array of strings <code>details</code>. Each element of <code>details</code> provides information about a given passenger compressed into a string of length <code>15</code>. The system is such that:</p>
<ul>
<li>The first ten characters consist of the phone number of passengers.</li>
<li>The next character denotes the gender of the person.</li>
<li>The following two characters are used to indicate the age of the person.</li>
<li>The last two characters determine the seat allotted to that person.</li>
</ul>
<p>Return <em>the number of passengers who are <strong>strictly </strong><strong>more than 60 years old</strong>.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> details = ["7868190130M7522","5303914400F9211","9273338290F4010"]
<strong>Output:</strong> 2
<strong>Explanation:</strong> The passengers at indices 0, 1, and 2 have ages 75, 92, and 40. Thus, there are 2 people who are over 60 years old.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> details = ["1313579440F2036","2921522980M5644"]
<strong>Output:</strong> 0
<strong>Explanation:</strong> None of the passengers are older than 60.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= details.length <= 100</code></li>
<li><code>details[i].length == 15</code></li>
<li><code>details[i] consists of digits from '0' to '9'.</code></li>
<li><code>details[i][10] is either 'M' or 'F' or 'O'.</code></li>
<li>The phone numbers and seat numbers of the passengers are distinct.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int countSeniors(vector<string>& v) {\nint c=0;\n for(int i=0;i<v.size();i++){\n int n=v[i].size();\n if((v[i][n-4]=='6'&&v[i][n-3]>='1')||(v[i][n-4]>='7')){\n c++;\n }\n }\n return c;\n }\n};",
"memory": "17400"
} |
2,727 | <p>You are given a <strong>0-indexed</strong> array of strings <code>details</code>. Each element of <code>details</code> provides information about a given passenger compressed into a string of length <code>15</code>. The system is such that:</p>
<ul>
<li>The first ten characters consist of the phone number of passengers.</li>
<li>The next character denotes the gender of the person.</li>
<li>The following two characters are used to indicate the age of the person.</li>
<li>The last two characters determine the seat allotted to that person.</li>
</ul>
<p>Return <em>the number of passengers who are <strong>strictly </strong><strong>more than 60 years old</strong>.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> details = ["7868190130M7522","5303914400F9211","9273338290F4010"]
<strong>Output:</strong> 2
<strong>Explanation:</strong> The passengers at indices 0, 1, and 2 have ages 75, 92, and 40. Thus, there are 2 people who are over 60 years old.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> details = ["1313579440F2036","2921522980M5644"]
<strong>Output:</strong> 0
<strong>Explanation:</strong> None of the passengers are older than 60.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= details.length <= 100</code></li>
<li><code>details[i].length == 15</code></li>
<li><code>details[i] consists of digits from '0' to '9'.</code></li>
<li><code>details[i][10] is either 'M' or 'F' or 'O'.</code></li>
<li>The phone numbers and seat numbers of the passengers are distinct.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int countSeniors(vector<string>& v) {\n int count = 0; \n for(auto &x : v){\n int age = stoi(x.substr(11,2)) ;\n if(age > 60){\n count+=1 ;\n }\n }\n return count ;\n }\n};",
"memory": "17500"
} |
2,727 | <p>You are given a <strong>0-indexed</strong> array of strings <code>details</code>. Each element of <code>details</code> provides information about a given passenger compressed into a string of length <code>15</code>. The system is such that:</p>
<ul>
<li>The first ten characters consist of the phone number of passengers.</li>
<li>The next character denotes the gender of the person.</li>
<li>The following two characters are used to indicate the age of the person.</li>
<li>The last two characters determine the seat allotted to that person.</li>
</ul>
<p>Return <em>the number of passengers who are <strong>strictly </strong><strong>more than 60 years old</strong>.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> details = ["7868190130M7522","5303914400F9211","9273338290F4010"]
<strong>Output:</strong> 2
<strong>Explanation:</strong> The passengers at indices 0, 1, and 2 have ages 75, 92, and 40. Thus, there are 2 people who are over 60 years old.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> details = ["1313579440F2036","2921522980M5644"]
<strong>Output:</strong> 0
<strong>Explanation:</strong> None of the passengers are older than 60.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= details.length <= 100</code></li>
<li><code>details[i].length == 15</code></li>
<li><code>details[i] consists of digits from '0' to '9'.</code></li>
<li><code>details[i][10] is either 'M' or 'F' or 'O'.</code></li>
<li>The phone numbers and seat numbers of the passengers are distinct.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int countSeniors(vector<string>& details) {\n int count =0;\n for(string& s : details){\n int age = (s[11] - '0')*10 + (s[12] - '0');\n if( age > 60)\n ++count;\n }\n return count;\n }\n};",
"memory": "17500"
} |
2,727 | <p>You are given a <strong>0-indexed</strong> array of strings <code>details</code>. Each element of <code>details</code> provides information about a given passenger compressed into a string of length <code>15</code>. The system is such that:</p>
<ul>
<li>The first ten characters consist of the phone number of passengers.</li>
<li>The next character denotes the gender of the person.</li>
<li>The following two characters are used to indicate the age of the person.</li>
<li>The last two characters determine the seat allotted to that person.</li>
</ul>
<p>Return <em>the number of passengers who are <strong>strictly </strong><strong>more than 60 years old</strong>.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> details = ["7868190130M7522","5303914400F9211","9273338290F4010"]
<strong>Output:</strong> 2
<strong>Explanation:</strong> The passengers at indices 0, 1, and 2 have ages 75, 92, and 40. Thus, there are 2 people who are over 60 years old.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> details = ["1313579440F2036","2921522980M5644"]
<strong>Output:</strong> 0
<strong>Explanation:</strong> None of the passengers are older than 60.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= details.length <= 100</code></li>
<li><code>details[i].length == 15</code></li>
<li><code>details[i] consists of digits from '0' to '9'.</code></li>
<li><code>details[i][10] is either 'M' or 'F' or 'O'.</code></li>
<li>The phone numbers and seat numbers of the passengers are distinct.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int countSeniors(vector<string>& details) {\n\n int n=details.size();\n int count=0;\n for(int i=0;i<n;i++)\n {\n string x=details[i];\n cout <<x.substr(11,2)<<endl;\n int age = stoi(x.substr(11,2));\n if(age>60)\n {\n count++;\n }\n\n } \n return count;\n }\n};",
"memory": "17600"
} |
2,727 | <p>You are given a <strong>0-indexed</strong> array of strings <code>details</code>. Each element of <code>details</code> provides information about a given passenger compressed into a string of length <code>15</code>. The system is such that:</p>
<ul>
<li>The first ten characters consist of the phone number of passengers.</li>
<li>The next character denotes the gender of the person.</li>
<li>The following two characters are used to indicate the age of the person.</li>
<li>The last two characters determine the seat allotted to that person.</li>
</ul>
<p>Return <em>the number of passengers who are <strong>strictly </strong><strong>more than 60 years old</strong>.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> details = ["7868190130M7522","5303914400F9211","9273338290F4010"]
<strong>Output:</strong> 2
<strong>Explanation:</strong> The passengers at indices 0, 1, and 2 have ages 75, 92, and 40. Thus, there are 2 people who are over 60 years old.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> details = ["1313579440F2036","2921522980M5644"]
<strong>Output:</strong> 0
<strong>Explanation:</strong> None of the passengers are older than 60.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= details.length <= 100</code></li>
<li><code>details[i].length == 15</code></li>
<li><code>details[i] consists of digits from '0' to '9'.</code></li>
<li><code>details[i][10] is either 'M' or 'F' or 'O'.</code></li>
<li>The phone numbers and seat numbers of the passengers are distinct.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int countSeniors(vector<string>& details) {\n int ans=0;\n for(int i=0;i<details.size();i++){\n int x=details[i][11]-48;\n int y=details[i][12]-48;\n int s=0;\n s=s*10 + x;\n s=s*10 + y;\n if(s>60)ans++;\n }\n return ans;\n }\n};",
"memory": "17600"
} |
2,727 | <p>You are given a <strong>0-indexed</strong> array of strings <code>details</code>. Each element of <code>details</code> provides information about a given passenger compressed into a string of length <code>15</code>. The system is such that:</p>
<ul>
<li>The first ten characters consist of the phone number of passengers.</li>
<li>The next character denotes the gender of the person.</li>
<li>The following two characters are used to indicate the age of the person.</li>
<li>The last two characters determine the seat allotted to that person.</li>
</ul>
<p>Return <em>the number of passengers who are <strong>strictly </strong><strong>more than 60 years old</strong>.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> details = ["7868190130M7522","5303914400F9211","9273338290F4010"]
<strong>Output:</strong> 2
<strong>Explanation:</strong> The passengers at indices 0, 1, and 2 have ages 75, 92, and 40. Thus, there are 2 people who are over 60 years old.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> details = ["1313579440F2036","2921522980M5644"]
<strong>Output:</strong> 0
<strong>Explanation:</strong> None of the passengers are older than 60.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= details.length <= 100</code></li>
<li><code>details[i].length == 15</code></li>
<li><code>details[i] consists of digits from '0' to '9'.</code></li>
<li><code>details[i][10] is either 'M' or 'F' or 'O'.</code></li>
<li>The phone numbers and seat numbers of the passengers are distinct.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int countSeniors(vector<string>& details) {\n int count = 0 ;\n for (int i=0; i<details.size(); i++){\n string detail = details[i] ;\n int age = (detail[11]-'0')*10 + (detail[12]-'0') ;\n if (age > 60) count++ ;\n }\n return count ;\n }\n};",
"memory": "17700"
} |
2,727 | <p>You are given a <strong>0-indexed</strong> array of strings <code>details</code>. Each element of <code>details</code> provides information about a given passenger compressed into a string of length <code>15</code>. The system is such that:</p>
<ul>
<li>The first ten characters consist of the phone number of passengers.</li>
<li>The next character denotes the gender of the person.</li>
<li>The following two characters are used to indicate the age of the person.</li>
<li>The last two characters determine the seat allotted to that person.</li>
</ul>
<p>Return <em>the number of passengers who are <strong>strictly </strong><strong>more than 60 years old</strong>.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> details = ["7868190130M7522","5303914400F9211","9273338290F4010"]
<strong>Output:</strong> 2
<strong>Explanation:</strong> The passengers at indices 0, 1, and 2 have ages 75, 92, and 40. Thus, there are 2 people who are over 60 years old.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> details = ["1313579440F2036","2921522980M5644"]
<strong>Output:</strong> 0
<strong>Explanation:</strong> None of the passengers are older than 60.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= details.length <= 100</code></li>
<li><code>details[i].length == 15</code></li>
<li><code>details[i] consists of digits from '0' to '9'.</code></li>
<li><code>details[i][10] is either 'M' or 'F' or 'O'.</code></li>
<li>The phone numbers and seat numbers of the passengers are distinct.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int countSeniors(vector<string>& details) {\n int i,c=0;\n for (i=0;i<details.size();i=i+1)\n {\n if (stoi(details[i].substr(11,2))>60)\n {\n c=c+1;\n }\n }\n return c;\n }\n};",
"memory": "17700"
} |
1,951 | <p>There are <code>n</code> friends that are playing a game. The friends are sitting in a circle and are numbered from <code>1</code> to <code>n</code> in <strong>clockwise order</strong>. More formally, moving clockwise from the <code>i<sup>th</sup></code> friend brings you to the <code>(i+1)<sup>th</sup></code> friend for <code>1 <= i < n</code>, and moving clockwise from the <code>n<sup>th</sup></code> friend brings you to the <code>1<sup>st</sup></code> friend.</p>
<p>The rules of the game are as follows:</p>
<ol>
<li><strong>Start</strong> at the <code>1<sup>st</sup></code> friend.</li>
<li>Count the next <code>k</code> friends in the clockwise direction <strong>including</strong> the friend you started at. The counting wraps around the circle and may count some friends more than once.</li>
<li>The last friend you counted leaves the circle and loses the game.</li>
<li>If there is still more than one friend in the circle, go back to step <code>2</code> <strong>starting</strong> from the friend <strong>immediately clockwise</strong> of the friend who just lost and repeat.</li>
<li>Else, the last friend in the circle wins the game.</li>
</ol>
<p>Given the number of friends, <code>n</code>, and an integer <code>k</code>, return <em>the winner of the game</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/25/ic234-q2-ex11.png" style="width: 500px; height: 345px;" />
<pre>
<strong>Input:</strong> n = 5, k = 2
<strong>Output:</strong> 3
<strong>Explanation:</strong> Here are the steps of the game:
1) Start at friend 1.
2) Count 2 friends clockwise, which are friends 1 and 2.
3) Friend 2 leaves the circle. Next start is friend 3.
4) Count 2 friends clockwise, which are friends 3 and 4.
5) Friend 4 leaves the circle. Next start is friend 5.
6) Count 2 friends clockwise, which are friends 5 and 1.
7) Friend 1 leaves the circle. Next start is friend 3.
8) Count 2 friends clockwise, which are friends 3 and 5.
9) Friend 5 leaves the circle. Only friend 3 is left, so they are the winner.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 6, k = 5
<strong>Output:</strong> 1
<strong>Explanation:</strong> The friends leave in this order: 5, 4, 6, 2, 3. The winner is friend 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= n <= 500</code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<p>Could you solve this problem in linear time with constant space?</p>
| 0 | {
"code": "class Solution {\npublic:\n int findTheWinner(int n, int k) {\n if (n==1) return 1;\n return (findTheWinner(n-1,k)+k-1)%n+1;\n }\n};",
"memory": "7200"
} |
1,951 | <p>There are <code>n</code> friends that are playing a game. The friends are sitting in a circle and are numbered from <code>1</code> to <code>n</code> in <strong>clockwise order</strong>. More formally, moving clockwise from the <code>i<sup>th</sup></code> friend brings you to the <code>(i+1)<sup>th</sup></code> friend for <code>1 <= i < n</code>, and moving clockwise from the <code>n<sup>th</sup></code> friend brings you to the <code>1<sup>st</sup></code> friend.</p>
<p>The rules of the game are as follows:</p>
<ol>
<li><strong>Start</strong> at the <code>1<sup>st</sup></code> friend.</li>
<li>Count the next <code>k</code> friends in the clockwise direction <strong>including</strong> the friend you started at. The counting wraps around the circle and may count some friends more than once.</li>
<li>The last friend you counted leaves the circle and loses the game.</li>
<li>If there is still more than one friend in the circle, go back to step <code>2</code> <strong>starting</strong> from the friend <strong>immediately clockwise</strong> of the friend who just lost and repeat.</li>
<li>Else, the last friend in the circle wins the game.</li>
</ol>
<p>Given the number of friends, <code>n</code>, and an integer <code>k</code>, return <em>the winner of the game</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/25/ic234-q2-ex11.png" style="width: 500px; height: 345px;" />
<pre>
<strong>Input:</strong> n = 5, k = 2
<strong>Output:</strong> 3
<strong>Explanation:</strong> Here are the steps of the game:
1) Start at friend 1.
2) Count 2 friends clockwise, which are friends 1 and 2.
3) Friend 2 leaves the circle. Next start is friend 3.
4) Count 2 friends clockwise, which are friends 3 and 4.
5) Friend 4 leaves the circle. Next start is friend 5.
6) Count 2 friends clockwise, which are friends 5 and 1.
7) Friend 1 leaves the circle. Next start is friend 3.
8) Count 2 friends clockwise, which are friends 3 and 5.
9) Friend 5 leaves the circle. Only friend 3 is left, so they are the winner.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 6, k = 5
<strong>Output:</strong> 1
<strong>Explanation:</strong> The friends leave in this order: 5, 4, 6, 2, 3. The winner is friend 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= n <= 500</code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<p>Could you solve this problem in linear time with constant space?</p>
| 0 | {
"code": "class Solution {\npublic:\n int helper(int n,int k){\n if(n==1) return 0;\n return (helper(n-1,k) + k)%n;\n }\n\n int findTheWinner(int n, int k) {\n return helper(n,k)+1;\n }\n};",
"memory": "7200"
} |
1,951 | <p>There are <code>n</code> friends that are playing a game. The friends are sitting in a circle and are numbered from <code>1</code> to <code>n</code> in <strong>clockwise order</strong>. More formally, moving clockwise from the <code>i<sup>th</sup></code> friend brings you to the <code>(i+1)<sup>th</sup></code> friend for <code>1 <= i < n</code>, and moving clockwise from the <code>n<sup>th</sup></code> friend brings you to the <code>1<sup>st</sup></code> friend.</p>
<p>The rules of the game are as follows:</p>
<ol>
<li><strong>Start</strong> at the <code>1<sup>st</sup></code> friend.</li>
<li>Count the next <code>k</code> friends in the clockwise direction <strong>including</strong> the friend you started at. The counting wraps around the circle and may count some friends more than once.</li>
<li>The last friend you counted leaves the circle and loses the game.</li>
<li>If there is still more than one friend in the circle, go back to step <code>2</code> <strong>starting</strong> from the friend <strong>immediately clockwise</strong> of the friend who just lost and repeat.</li>
<li>Else, the last friend in the circle wins the game.</li>
</ol>
<p>Given the number of friends, <code>n</code>, and an integer <code>k</code>, return <em>the winner of the game</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/25/ic234-q2-ex11.png" style="width: 500px; height: 345px;" />
<pre>
<strong>Input:</strong> n = 5, k = 2
<strong>Output:</strong> 3
<strong>Explanation:</strong> Here are the steps of the game:
1) Start at friend 1.
2) Count 2 friends clockwise, which are friends 1 and 2.
3) Friend 2 leaves the circle. Next start is friend 3.
4) Count 2 friends clockwise, which are friends 3 and 4.
5) Friend 4 leaves the circle. Next start is friend 5.
6) Count 2 friends clockwise, which are friends 5 and 1.
7) Friend 1 leaves the circle. Next start is friend 3.
8) Count 2 friends clockwise, which are friends 3 and 5.
9) Friend 5 leaves the circle. Only friend 3 is left, so they are the winner.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 6, k = 5
<strong>Output:</strong> 1
<strong>Explanation:</strong> The friends leave in this order: 5, 4, 6, 2, 3. The winner is friend 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= n <= 500</code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<p>Could you solve this problem in linear time with constant space?</p>
| 0 | {
"code": "class Solution {\npublic:\n\n int findTheWinnerQueue(int n, int k) {\n std::queue<int> q;\n for (int i = 1; i <= n; i++) {\n q.push(i);\n }\n\n while (q.size() > 1) {\n for (int i = 1; i < k; i++) {\n q.push(q.front());\n q.pop();\n }\n\n // std::cout << \"POP: \" << q.front() << std::endl;\n q.pop();\n }\n\n return q.front();\n }\n\n int findTheWinnerMath(int n, int k) {\n auto result = 0;\n for (int i = 2; i <= n; i++) {\n //std::cout << \"i: \" << i - 1 << \" \" << result << std::endl;\n result = (result + k) % i;\n }\n return result + 1;\n }\n\n int findTheWinner(int n, int k) {\n return findTheWinnerMath(n, k); \n }\n};",
"memory": "7300"
} |
1,951 | <p>There are <code>n</code> friends that are playing a game. The friends are sitting in a circle and are numbered from <code>1</code> to <code>n</code> in <strong>clockwise order</strong>. More formally, moving clockwise from the <code>i<sup>th</sup></code> friend brings you to the <code>(i+1)<sup>th</sup></code> friend for <code>1 <= i < n</code>, and moving clockwise from the <code>n<sup>th</sup></code> friend brings you to the <code>1<sup>st</sup></code> friend.</p>
<p>The rules of the game are as follows:</p>
<ol>
<li><strong>Start</strong> at the <code>1<sup>st</sup></code> friend.</li>
<li>Count the next <code>k</code> friends in the clockwise direction <strong>including</strong> the friend you started at. The counting wraps around the circle and may count some friends more than once.</li>
<li>The last friend you counted leaves the circle and loses the game.</li>
<li>If there is still more than one friend in the circle, go back to step <code>2</code> <strong>starting</strong> from the friend <strong>immediately clockwise</strong> of the friend who just lost and repeat.</li>
<li>Else, the last friend in the circle wins the game.</li>
</ol>
<p>Given the number of friends, <code>n</code>, and an integer <code>k</code>, return <em>the winner of the game</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/25/ic234-q2-ex11.png" style="width: 500px; height: 345px;" />
<pre>
<strong>Input:</strong> n = 5, k = 2
<strong>Output:</strong> 3
<strong>Explanation:</strong> Here are the steps of the game:
1) Start at friend 1.
2) Count 2 friends clockwise, which are friends 1 and 2.
3) Friend 2 leaves the circle. Next start is friend 3.
4) Count 2 friends clockwise, which are friends 3 and 4.
5) Friend 4 leaves the circle. Next start is friend 5.
6) Count 2 friends clockwise, which are friends 5 and 1.
7) Friend 1 leaves the circle. Next start is friend 3.
8) Count 2 friends clockwise, which are friends 3 and 5.
9) Friend 5 leaves the circle. Only friend 3 is left, so they are the winner.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 6, k = 5
<strong>Output:</strong> 1
<strong>Explanation:</strong> The friends leave in this order: 5, 4, 6, 2, 3. The winner is friend 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= n <= 500</code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<p>Could you solve this problem in linear time with constant space?</p>
| 0 | {
"code": "class Solution {\npublic:\n\n int winner(int n, int k) {\n if(n==1){\n return 0;\n }\n\n return (winner(n-1,k)+k)%n; \n }\n int findTheWinner(int n, int k){\n return winner(n,k)+1;\n }\n};",
"memory": "7300"
} |
1,951 | <p>There are <code>n</code> friends that are playing a game. The friends are sitting in a circle and are numbered from <code>1</code> to <code>n</code> in <strong>clockwise order</strong>. More formally, moving clockwise from the <code>i<sup>th</sup></code> friend brings you to the <code>(i+1)<sup>th</sup></code> friend for <code>1 <= i < n</code>, and moving clockwise from the <code>n<sup>th</sup></code> friend brings you to the <code>1<sup>st</sup></code> friend.</p>
<p>The rules of the game are as follows:</p>
<ol>
<li><strong>Start</strong> at the <code>1<sup>st</sup></code> friend.</li>
<li>Count the next <code>k</code> friends in the clockwise direction <strong>including</strong> the friend you started at. The counting wraps around the circle and may count some friends more than once.</li>
<li>The last friend you counted leaves the circle and loses the game.</li>
<li>If there is still more than one friend in the circle, go back to step <code>2</code> <strong>starting</strong> from the friend <strong>immediately clockwise</strong> of the friend who just lost and repeat.</li>
<li>Else, the last friend in the circle wins the game.</li>
</ol>
<p>Given the number of friends, <code>n</code>, and an integer <code>k</code>, return <em>the winner of the game</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/25/ic234-q2-ex11.png" style="width: 500px; height: 345px;" />
<pre>
<strong>Input:</strong> n = 5, k = 2
<strong>Output:</strong> 3
<strong>Explanation:</strong> Here are the steps of the game:
1) Start at friend 1.
2) Count 2 friends clockwise, which are friends 1 and 2.
3) Friend 2 leaves the circle. Next start is friend 3.
4) Count 2 friends clockwise, which are friends 3 and 4.
5) Friend 4 leaves the circle. Next start is friend 5.
6) Count 2 friends clockwise, which are friends 5 and 1.
7) Friend 1 leaves the circle. Next start is friend 3.
8) Count 2 friends clockwise, which are friends 3 and 5.
9) Friend 5 leaves the circle. Only friend 3 is left, so they are the winner.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 6, k = 5
<strong>Output:</strong> 1
<strong>Explanation:</strong> The friends leave in this order: 5, 4, 6, 2, 3. The winner is friend 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= n <= 500</code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<p>Could you solve this problem in linear time with constant space?</p>
| 0 | {
"code": "class Solution {\npublic:\n int findTheWinner(int n, int k) {\n // f(n, k) = (f(n-1, k) + k) mod n\n int survivor = 0;\n for (int i = 2; i <= n; ++i) {\n survivor = (survivor + k) % i;\n }\n\n return ++survivor;\n }\n};",
"memory": "7400"
} |
1,951 | <p>There are <code>n</code> friends that are playing a game. The friends are sitting in a circle and are numbered from <code>1</code> to <code>n</code> in <strong>clockwise order</strong>. More formally, moving clockwise from the <code>i<sup>th</sup></code> friend brings you to the <code>(i+1)<sup>th</sup></code> friend for <code>1 <= i < n</code>, and moving clockwise from the <code>n<sup>th</sup></code> friend brings you to the <code>1<sup>st</sup></code> friend.</p>
<p>The rules of the game are as follows:</p>
<ol>
<li><strong>Start</strong> at the <code>1<sup>st</sup></code> friend.</li>
<li>Count the next <code>k</code> friends in the clockwise direction <strong>including</strong> the friend you started at. The counting wraps around the circle and may count some friends more than once.</li>
<li>The last friend you counted leaves the circle and loses the game.</li>
<li>If there is still more than one friend in the circle, go back to step <code>2</code> <strong>starting</strong> from the friend <strong>immediately clockwise</strong> of the friend who just lost and repeat.</li>
<li>Else, the last friend in the circle wins the game.</li>
</ol>
<p>Given the number of friends, <code>n</code>, and an integer <code>k</code>, return <em>the winner of the game</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/25/ic234-q2-ex11.png" style="width: 500px; height: 345px;" />
<pre>
<strong>Input:</strong> n = 5, k = 2
<strong>Output:</strong> 3
<strong>Explanation:</strong> Here are the steps of the game:
1) Start at friend 1.
2) Count 2 friends clockwise, which are friends 1 and 2.
3) Friend 2 leaves the circle. Next start is friend 3.
4) Count 2 friends clockwise, which are friends 3 and 4.
5) Friend 4 leaves the circle. Next start is friend 5.
6) Count 2 friends clockwise, which are friends 5 and 1.
7) Friend 1 leaves the circle. Next start is friend 3.
8) Count 2 friends clockwise, which are friends 3 and 5.
9) Friend 5 leaves the circle. Only friend 3 is left, so they are the winner.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 6, k = 5
<strong>Output:</strong> 1
<strong>Explanation:</strong> The friends leave in this order: 5, 4, 6, 2, 3. The winner is friend 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= n <= 500</code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<p>Could you solve this problem in linear time with constant space?</p>
| 0 | {
"code": "class Solution {\npublic:\n int findTheWinner(int n, int k) {\n int winner=0;\n int i;\n for(i=1;i<=n;i++){\n winner=(winner+k)%i;\n \n\n }\n return winner+1;\n \n }\n};",
"memory": "7400"
} |
1,951 | <p>There are <code>n</code> friends that are playing a game. The friends are sitting in a circle and are numbered from <code>1</code> to <code>n</code> in <strong>clockwise order</strong>. More formally, moving clockwise from the <code>i<sup>th</sup></code> friend brings you to the <code>(i+1)<sup>th</sup></code> friend for <code>1 <= i < n</code>, and moving clockwise from the <code>n<sup>th</sup></code> friend brings you to the <code>1<sup>st</sup></code> friend.</p>
<p>The rules of the game are as follows:</p>
<ol>
<li><strong>Start</strong> at the <code>1<sup>st</sup></code> friend.</li>
<li>Count the next <code>k</code> friends in the clockwise direction <strong>including</strong> the friend you started at. The counting wraps around the circle and may count some friends more than once.</li>
<li>The last friend you counted leaves the circle and loses the game.</li>
<li>If there is still more than one friend in the circle, go back to step <code>2</code> <strong>starting</strong> from the friend <strong>immediately clockwise</strong> of the friend who just lost and repeat.</li>
<li>Else, the last friend in the circle wins the game.</li>
</ol>
<p>Given the number of friends, <code>n</code>, and an integer <code>k</code>, return <em>the winner of the game</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/25/ic234-q2-ex11.png" style="width: 500px; height: 345px;" />
<pre>
<strong>Input:</strong> n = 5, k = 2
<strong>Output:</strong> 3
<strong>Explanation:</strong> Here are the steps of the game:
1) Start at friend 1.
2) Count 2 friends clockwise, which are friends 1 and 2.
3) Friend 2 leaves the circle. Next start is friend 3.
4) Count 2 friends clockwise, which are friends 3 and 4.
5) Friend 4 leaves the circle. Next start is friend 5.
6) Count 2 friends clockwise, which are friends 5 and 1.
7) Friend 1 leaves the circle. Next start is friend 3.
8) Count 2 friends clockwise, which are friends 3 and 5.
9) Friend 5 leaves the circle. Only friend 3 is left, so they are the winner.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 6, k = 5
<strong>Output:</strong> 1
<strong>Explanation:</strong> The friends leave in this order: 5, 4, 6, 2, 3. The winner is friend 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= n <= 500</code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<p>Could you solve this problem in linear time with constant space?</p>
| 0 | {
"code": "class Solution {\npublic:\nint winner(int n, int k)\n{\n if (n == 1)\n {\n // If only 1 member is left, it is treated as 0\n return 0;\n }\n // competitors are of range 0...n-1\n\n int x = winner(n - 1, k);\n int y = (x + k) % n;\n\n return y;\n}\nint findTheWinner(int n, int k)\n{\n return winner(n, k) + 1; // 1 is added as it is 1 based numbers\n}\n};",
"memory": "7500"
} |
1,951 | <p>There are <code>n</code> friends that are playing a game. The friends are sitting in a circle and are numbered from <code>1</code> to <code>n</code> in <strong>clockwise order</strong>. More formally, moving clockwise from the <code>i<sup>th</sup></code> friend brings you to the <code>(i+1)<sup>th</sup></code> friend for <code>1 <= i < n</code>, and moving clockwise from the <code>n<sup>th</sup></code> friend brings you to the <code>1<sup>st</sup></code> friend.</p>
<p>The rules of the game are as follows:</p>
<ol>
<li><strong>Start</strong> at the <code>1<sup>st</sup></code> friend.</li>
<li>Count the next <code>k</code> friends in the clockwise direction <strong>including</strong> the friend you started at. The counting wraps around the circle and may count some friends more than once.</li>
<li>The last friend you counted leaves the circle and loses the game.</li>
<li>If there is still more than one friend in the circle, go back to step <code>2</code> <strong>starting</strong> from the friend <strong>immediately clockwise</strong> of the friend who just lost and repeat.</li>
<li>Else, the last friend in the circle wins the game.</li>
</ol>
<p>Given the number of friends, <code>n</code>, and an integer <code>k</code>, return <em>the winner of the game</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/25/ic234-q2-ex11.png" style="width: 500px; height: 345px;" />
<pre>
<strong>Input:</strong> n = 5, k = 2
<strong>Output:</strong> 3
<strong>Explanation:</strong> Here are the steps of the game:
1) Start at friend 1.
2) Count 2 friends clockwise, which are friends 1 and 2.
3) Friend 2 leaves the circle. Next start is friend 3.
4) Count 2 friends clockwise, which are friends 3 and 4.
5) Friend 4 leaves the circle. Next start is friend 5.
6) Count 2 friends clockwise, which are friends 5 and 1.
7) Friend 1 leaves the circle. Next start is friend 3.
8) Count 2 friends clockwise, which are friends 3 and 5.
9) Friend 5 leaves the circle. Only friend 3 is left, so they are the winner.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 6, k = 5
<strong>Output:</strong> 1
<strong>Explanation:</strong> The friends leave in this order: 5, 4, 6, 2, 3. The winner is friend 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= n <= 500</code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<p>Could you solve this problem in linear time with constant space?</p>
| 0 | {
"code": "class Solution {\npublic:\n static int findTheWinner(int n, int k) {\n if (n==1) return 1;\n return (findTheWinner(n-1, k)+(k-1))%n+1;\n }\n};\n\n\n\n\nauto init = []() {\n ios::sync_with_stdio(0);\n cin.tie(0);\n cout.tie(0);\n return 'c';\n}();",
"memory": "7500"
} |
1,951 | <p>There are <code>n</code> friends that are playing a game. The friends are sitting in a circle and are numbered from <code>1</code> to <code>n</code> in <strong>clockwise order</strong>. More formally, moving clockwise from the <code>i<sup>th</sup></code> friend brings you to the <code>(i+1)<sup>th</sup></code> friend for <code>1 <= i < n</code>, and moving clockwise from the <code>n<sup>th</sup></code> friend brings you to the <code>1<sup>st</sup></code> friend.</p>
<p>The rules of the game are as follows:</p>
<ol>
<li><strong>Start</strong> at the <code>1<sup>st</sup></code> friend.</li>
<li>Count the next <code>k</code> friends in the clockwise direction <strong>including</strong> the friend you started at. The counting wraps around the circle and may count some friends more than once.</li>
<li>The last friend you counted leaves the circle and loses the game.</li>
<li>If there is still more than one friend in the circle, go back to step <code>2</code> <strong>starting</strong> from the friend <strong>immediately clockwise</strong> of the friend who just lost and repeat.</li>
<li>Else, the last friend in the circle wins the game.</li>
</ol>
<p>Given the number of friends, <code>n</code>, and an integer <code>k</code>, return <em>the winner of the game</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/25/ic234-q2-ex11.png" style="width: 500px; height: 345px;" />
<pre>
<strong>Input:</strong> n = 5, k = 2
<strong>Output:</strong> 3
<strong>Explanation:</strong> Here are the steps of the game:
1) Start at friend 1.
2) Count 2 friends clockwise, which are friends 1 and 2.
3) Friend 2 leaves the circle. Next start is friend 3.
4) Count 2 friends clockwise, which are friends 3 and 4.
5) Friend 4 leaves the circle. Next start is friend 5.
6) Count 2 friends clockwise, which are friends 5 and 1.
7) Friend 1 leaves the circle. Next start is friend 3.
8) Count 2 friends clockwise, which are friends 3 and 5.
9) Friend 5 leaves the circle. Only friend 3 is left, so they are the winner.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 6, k = 5
<strong>Output:</strong> 1
<strong>Explanation:</strong> The friends leave in this order: 5, 4, 6, 2, 3. The winner is friend 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= n <= 500</code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<p>Could you solve this problem in linear time with constant space?</p>
| 1 | {
"code": "class Solution {\n int find(int n, int k){\n\nif(n==1) return 0;\nreturn (find(n-1,k)+k)%n;\n }\npublic:\n int findTheWinner(int n, int k) {\n return find(n,k)+1;\n }\n};",
"memory": "7600"
} |
1,951 | <p>There are <code>n</code> friends that are playing a game. The friends are sitting in a circle and are numbered from <code>1</code> to <code>n</code> in <strong>clockwise order</strong>. More formally, moving clockwise from the <code>i<sup>th</sup></code> friend brings you to the <code>(i+1)<sup>th</sup></code> friend for <code>1 <= i < n</code>, and moving clockwise from the <code>n<sup>th</sup></code> friend brings you to the <code>1<sup>st</sup></code> friend.</p>
<p>The rules of the game are as follows:</p>
<ol>
<li><strong>Start</strong> at the <code>1<sup>st</sup></code> friend.</li>
<li>Count the next <code>k</code> friends in the clockwise direction <strong>including</strong> the friend you started at. The counting wraps around the circle and may count some friends more than once.</li>
<li>The last friend you counted leaves the circle and loses the game.</li>
<li>If there is still more than one friend in the circle, go back to step <code>2</code> <strong>starting</strong> from the friend <strong>immediately clockwise</strong> of the friend who just lost and repeat.</li>
<li>Else, the last friend in the circle wins the game.</li>
</ol>
<p>Given the number of friends, <code>n</code>, and an integer <code>k</code>, return <em>the winner of the game</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/25/ic234-q2-ex11.png" style="width: 500px; height: 345px;" />
<pre>
<strong>Input:</strong> n = 5, k = 2
<strong>Output:</strong> 3
<strong>Explanation:</strong> Here are the steps of the game:
1) Start at friend 1.
2) Count 2 friends clockwise, which are friends 1 and 2.
3) Friend 2 leaves the circle. Next start is friend 3.
4) Count 2 friends clockwise, which are friends 3 and 4.
5) Friend 4 leaves the circle. Next start is friend 5.
6) Count 2 friends clockwise, which are friends 5 and 1.
7) Friend 1 leaves the circle. Next start is friend 3.
8) Count 2 friends clockwise, which are friends 3 and 5.
9) Friend 5 leaves the circle. Only friend 3 is left, so they are the winner.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 6, k = 5
<strong>Output:</strong> 1
<strong>Explanation:</strong> The friends leave in this order: 5, 4, 6, 2, 3. The winner is friend 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= n <= 500</code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<p>Could you solve this problem in linear time with constant space?</p>
| 1 | {
"code": "class Solution {\npublic:\n int findTheWinner(int n, int k) {\n vector<int> temp(n,1);\n int i = 0;\n int count = 0;\n int p = n-1;\n while(p>0){\n if(temp[i]==1){count++;}\n if(count == k){\n temp[i]=0;\n count = 0;\n p--;\n }\n i = (i+1)%n;\n \n }\n int ans ;\n for(int i=0;i<n;i++){\n if(temp[i]==1){\n ans = i;\n break;\n }\n }\n return ans+1;\n \n }\n};",
"memory": "7600"
} |
1,951 | <p>There are <code>n</code> friends that are playing a game. The friends are sitting in a circle and are numbered from <code>1</code> to <code>n</code> in <strong>clockwise order</strong>. More formally, moving clockwise from the <code>i<sup>th</sup></code> friend brings you to the <code>(i+1)<sup>th</sup></code> friend for <code>1 <= i < n</code>, and moving clockwise from the <code>n<sup>th</sup></code> friend brings you to the <code>1<sup>st</sup></code> friend.</p>
<p>The rules of the game are as follows:</p>
<ol>
<li><strong>Start</strong> at the <code>1<sup>st</sup></code> friend.</li>
<li>Count the next <code>k</code> friends in the clockwise direction <strong>including</strong> the friend you started at. The counting wraps around the circle and may count some friends more than once.</li>
<li>The last friend you counted leaves the circle and loses the game.</li>
<li>If there is still more than one friend in the circle, go back to step <code>2</code> <strong>starting</strong> from the friend <strong>immediately clockwise</strong> of the friend who just lost and repeat.</li>
<li>Else, the last friend in the circle wins the game.</li>
</ol>
<p>Given the number of friends, <code>n</code>, and an integer <code>k</code>, return <em>the winner of the game</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/25/ic234-q2-ex11.png" style="width: 500px; height: 345px;" />
<pre>
<strong>Input:</strong> n = 5, k = 2
<strong>Output:</strong> 3
<strong>Explanation:</strong> Here are the steps of the game:
1) Start at friend 1.
2) Count 2 friends clockwise, which are friends 1 and 2.
3) Friend 2 leaves the circle. Next start is friend 3.
4) Count 2 friends clockwise, which are friends 3 and 4.
5) Friend 4 leaves the circle. Next start is friend 5.
6) Count 2 friends clockwise, which are friends 5 and 1.
7) Friend 1 leaves the circle. Next start is friend 3.
8) Count 2 friends clockwise, which are friends 3 and 5.
9) Friend 5 leaves the circle. Only friend 3 is left, so they are the winner.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 6, k = 5
<strong>Output:</strong> 1
<strong>Explanation:</strong> The friends leave in this order: 5, 4, 6, 2, 3. The winner is friend 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= n <= 500</code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<p>Could you solve this problem in linear time with constant space?</p>
| 1 | {
"code": "class Solution {\npublic:\nvoid friendEliminated(int *friends, int index)\n{\n friends[index] = INT32_MAX;\n}\nint numPlayers(int *friends, int size)\n{\n int count = 0, index;\n for (int i = 0; i < size; i++)\n {\n if (friends[i] != INT32_MAX)\n {\n index = friends[i];\n count++;\n }\n if (count >= 2)\n return -1;\n }\n return index;\n}\nint getIndex(int *friends, int index, int k, int size)\n{\n int count = 0, temp = index, ans;\n while (count < k)\n {\n if (friends[temp] != INT32_MAX)\n {\n ans = temp;\n temp = (temp + 1) % size;\n count++;\n }\n else\n temp = (temp + 1) % size;\n }\n return ans;\n}\n int findTheWinner(int n, int k) {\n int *friends = new int[n];\n\n for (int i = 0; i < n; i++)\n {\n friends[i] = i + 1;\n }\n int index = 0;\n while (numPlayers(friends, n) == -1)\n {\n index = getIndex(friends,index,k,n);\n friendEliminated(friends, index);\n }\n return numPlayers(friends, n);\n }\n};",
"memory": "7700"
} |
1,951 | <p>There are <code>n</code> friends that are playing a game. The friends are sitting in a circle and are numbered from <code>1</code> to <code>n</code> in <strong>clockwise order</strong>. More formally, moving clockwise from the <code>i<sup>th</sup></code> friend brings you to the <code>(i+1)<sup>th</sup></code> friend for <code>1 <= i < n</code>, and moving clockwise from the <code>n<sup>th</sup></code> friend brings you to the <code>1<sup>st</sup></code> friend.</p>
<p>The rules of the game are as follows:</p>
<ol>
<li><strong>Start</strong> at the <code>1<sup>st</sup></code> friend.</li>
<li>Count the next <code>k</code> friends in the clockwise direction <strong>including</strong> the friend you started at. The counting wraps around the circle and may count some friends more than once.</li>
<li>The last friend you counted leaves the circle and loses the game.</li>
<li>If there is still more than one friend in the circle, go back to step <code>2</code> <strong>starting</strong> from the friend <strong>immediately clockwise</strong> of the friend who just lost and repeat.</li>
<li>Else, the last friend in the circle wins the game.</li>
</ol>
<p>Given the number of friends, <code>n</code>, and an integer <code>k</code>, return <em>the winner of the game</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/25/ic234-q2-ex11.png" style="width: 500px; height: 345px;" />
<pre>
<strong>Input:</strong> n = 5, k = 2
<strong>Output:</strong> 3
<strong>Explanation:</strong> Here are the steps of the game:
1) Start at friend 1.
2) Count 2 friends clockwise, which are friends 1 and 2.
3) Friend 2 leaves the circle. Next start is friend 3.
4) Count 2 friends clockwise, which are friends 3 and 4.
5) Friend 4 leaves the circle. Next start is friend 5.
6) Count 2 friends clockwise, which are friends 5 and 1.
7) Friend 1 leaves the circle. Next start is friend 3.
8) Count 2 friends clockwise, which are friends 3 and 5.
9) Friend 5 leaves the circle. Only friend 3 is left, so they are the winner.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 6, k = 5
<strong>Output:</strong> 1
<strong>Explanation:</strong> The friends leave in this order: 5, 4, 6, 2, 3. The winner is friend 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= n <= 500</code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<p>Could you solve this problem in linear time with constant space?</p>
| 1 | {
"code": "class Solution {\npublic:\n int findTheWinner(int n, int k) {\n int start=0;\n vector<int>v(n,-1);\n for(int i=0;i<n;i++)\n {\n v[i]=i+1;\n }\n while(v.size()!=1)\n {\n start=(start+k-1)%v.size();\n v.erase(v.begin()+start);\n } \n return v[0];\n }\n};",
"memory": "7700"
} |
1,951 | <p>There are <code>n</code> friends that are playing a game. The friends are sitting in a circle and are numbered from <code>1</code> to <code>n</code> in <strong>clockwise order</strong>. More formally, moving clockwise from the <code>i<sup>th</sup></code> friend brings you to the <code>(i+1)<sup>th</sup></code> friend for <code>1 <= i < n</code>, and moving clockwise from the <code>n<sup>th</sup></code> friend brings you to the <code>1<sup>st</sup></code> friend.</p>
<p>The rules of the game are as follows:</p>
<ol>
<li><strong>Start</strong> at the <code>1<sup>st</sup></code> friend.</li>
<li>Count the next <code>k</code> friends in the clockwise direction <strong>including</strong> the friend you started at. The counting wraps around the circle and may count some friends more than once.</li>
<li>The last friend you counted leaves the circle and loses the game.</li>
<li>If there is still more than one friend in the circle, go back to step <code>2</code> <strong>starting</strong> from the friend <strong>immediately clockwise</strong> of the friend who just lost and repeat.</li>
<li>Else, the last friend in the circle wins the game.</li>
</ol>
<p>Given the number of friends, <code>n</code>, and an integer <code>k</code>, return <em>the winner of the game</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/25/ic234-q2-ex11.png" style="width: 500px; height: 345px;" />
<pre>
<strong>Input:</strong> n = 5, k = 2
<strong>Output:</strong> 3
<strong>Explanation:</strong> Here are the steps of the game:
1) Start at friend 1.
2) Count 2 friends clockwise, which are friends 1 and 2.
3) Friend 2 leaves the circle. Next start is friend 3.
4) Count 2 friends clockwise, which are friends 3 and 4.
5) Friend 4 leaves the circle. Next start is friend 5.
6) Count 2 friends clockwise, which are friends 5 and 1.
7) Friend 1 leaves the circle. Next start is friend 3.
8) Count 2 friends clockwise, which are friends 3 and 5.
9) Friend 5 leaves the circle. Only friend 3 is left, so they are the winner.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 6, k = 5
<strong>Output:</strong> 1
<strong>Explanation:</strong> The friends leave in this order: 5, 4, 6, 2, 3. The winner is friend 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= n <= 500</code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<p>Could you solve this problem in linear time with constant space?</p>
| 1 | {
"code": "void solve(vector<int>& personArr,int k,int n,int index){\n // Base case \n if(personArr.size() == 1) return ;\n\n\n // Recurssive call \n \n int y=personArr.size();\n index=(index+(k-1)) % y ;\n \n \n personArr.erase(personArr.begin()+index);\n // for(int i=0;i<personArr.size();i++){\n // cout<<personArr[i]<<\" \";\n // }\n cout<<endl;\n solve(personArr,k,n,index);\n \n}\nclass Solution {\npublic:\n int findTheWinner(int n, int k) {\n // n-> total number of people in a circle \n //k-> after how many we will have to delete \n int index=0;\n vector<int>personArr;\n for(int i=1;i<=n;i++){\n personArr.push_back(i);\n }\n solve(personArr,k,n,index);\n return personArr[0];\n\n }\n};",
"memory": "7800"
} |
1,951 | <p>There are <code>n</code> friends that are playing a game. The friends are sitting in a circle and are numbered from <code>1</code> to <code>n</code> in <strong>clockwise order</strong>. More formally, moving clockwise from the <code>i<sup>th</sup></code> friend brings you to the <code>(i+1)<sup>th</sup></code> friend for <code>1 <= i < n</code>, and moving clockwise from the <code>n<sup>th</sup></code> friend brings you to the <code>1<sup>st</sup></code> friend.</p>
<p>The rules of the game are as follows:</p>
<ol>
<li><strong>Start</strong> at the <code>1<sup>st</sup></code> friend.</li>
<li>Count the next <code>k</code> friends in the clockwise direction <strong>including</strong> the friend you started at. The counting wraps around the circle and may count some friends more than once.</li>
<li>The last friend you counted leaves the circle and loses the game.</li>
<li>If there is still more than one friend in the circle, go back to step <code>2</code> <strong>starting</strong> from the friend <strong>immediately clockwise</strong> of the friend who just lost and repeat.</li>
<li>Else, the last friend in the circle wins the game.</li>
</ol>
<p>Given the number of friends, <code>n</code>, and an integer <code>k</code>, return <em>the winner of the game</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/25/ic234-q2-ex11.png" style="width: 500px; height: 345px;" />
<pre>
<strong>Input:</strong> n = 5, k = 2
<strong>Output:</strong> 3
<strong>Explanation:</strong> Here are the steps of the game:
1) Start at friend 1.
2) Count 2 friends clockwise, which are friends 1 and 2.
3) Friend 2 leaves the circle. Next start is friend 3.
4) Count 2 friends clockwise, which are friends 3 and 4.
5) Friend 4 leaves the circle. Next start is friend 5.
6) Count 2 friends clockwise, which are friends 5 and 1.
7) Friend 1 leaves the circle. Next start is friend 3.
8) Count 2 friends clockwise, which are friends 3 and 5.
9) Friend 5 leaves the circle. Only friend 3 is left, so they are the winner.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 6, k = 5
<strong>Output:</strong> 1
<strong>Explanation:</strong> The friends leave in this order: 5, 4, 6, 2, 3. The winner is friend 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= n <= 500</code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<p>Could you solve this problem in linear time with constant space?</p>
| 1 | {
"code": "void solve(vector<int>& personArr,int k,int n,int index){\n // Base case \n if(personArr.size() == 1) return ;\n\n\n // Recurssive call \n \n int y=personArr.size();\n index=(index+(k-1)) % y ;\n \n \n personArr.erase(personArr.begin()+index);\n // for(int i=0;i<personArr.size();i++){\n // cout<<personArr[i]<<\" \";\n // }\n cout<<endl;\n solve(personArr,k,n,index);\n \n}\nclass Solution {\npublic:\n int findTheWinner(int n, int k) {\n // n-> total number of people in a circle \n //k-> after how many we will have to delete \n int index=0;\n vector<int>personArr;\n for(int i=1;i<=n;i++){\n personArr.push_back(i);\n }\n solve(personArr,k,n,index);\n return personArr[0];\n\n }\n};",
"memory": "7800"
} |
1,951 | <p>There are <code>n</code> friends that are playing a game. The friends are sitting in a circle and are numbered from <code>1</code> to <code>n</code> in <strong>clockwise order</strong>. More formally, moving clockwise from the <code>i<sup>th</sup></code> friend brings you to the <code>(i+1)<sup>th</sup></code> friend for <code>1 <= i < n</code>, and moving clockwise from the <code>n<sup>th</sup></code> friend brings you to the <code>1<sup>st</sup></code> friend.</p>
<p>The rules of the game are as follows:</p>
<ol>
<li><strong>Start</strong> at the <code>1<sup>st</sup></code> friend.</li>
<li>Count the next <code>k</code> friends in the clockwise direction <strong>including</strong> the friend you started at. The counting wraps around the circle and may count some friends more than once.</li>
<li>The last friend you counted leaves the circle and loses the game.</li>
<li>If there is still more than one friend in the circle, go back to step <code>2</code> <strong>starting</strong> from the friend <strong>immediately clockwise</strong> of the friend who just lost and repeat.</li>
<li>Else, the last friend in the circle wins the game.</li>
</ol>
<p>Given the number of friends, <code>n</code>, and an integer <code>k</code>, return <em>the winner of the game</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/25/ic234-q2-ex11.png" style="width: 500px; height: 345px;" />
<pre>
<strong>Input:</strong> n = 5, k = 2
<strong>Output:</strong> 3
<strong>Explanation:</strong> Here are the steps of the game:
1) Start at friend 1.
2) Count 2 friends clockwise, which are friends 1 and 2.
3) Friend 2 leaves the circle. Next start is friend 3.
4) Count 2 friends clockwise, which are friends 3 and 4.
5) Friend 4 leaves the circle. Next start is friend 5.
6) Count 2 friends clockwise, which are friends 5 and 1.
7) Friend 1 leaves the circle. Next start is friend 3.
8) Count 2 friends clockwise, which are friends 3 and 5.
9) Friend 5 leaves the circle. Only friend 3 is left, so they are the winner.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 6, k = 5
<strong>Output:</strong> 1
<strong>Explanation:</strong> The friends leave in this order: 5, 4, 6, 2, 3. The winner is friend 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= n <= 500</code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<p>Could you solve this problem in linear time with constant space?</p>
| 2 | {
"code": "void solve(vector<int>& personArr,int k,int n,int index){\n // Base case \n if(personArr.size() == 1) return ;\n\n\n // Recurssive call \n \n int y=personArr.size();\n index=(index+(k-1)) % y ;\n \n \n personArr.erase(personArr.begin()+index);\n // for(int i=0;i<personArr.size();i++){\n // cout<<personArr[i]<<\" \";\n // }\n cout<<endl;\n solve(personArr,k,n,index);\n \n}\nclass Solution {\npublic:\n int findTheWinner(int n, int k) {\n // n-> total number of people in a circle \n //k-> after how many we will have to delete \n int index=0;\n vector<int>personArr;\n for(int i=1;i<=n;i++){\n personArr.push_back(i);\n }\n solve(personArr,k,n,index);\n return personArr[0];\n\n }\n};",
"memory": "7900"
} |
1,951 | <p>There are <code>n</code> friends that are playing a game. The friends are sitting in a circle and are numbered from <code>1</code> to <code>n</code> in <strong>clockwise order</strong>. More formally, moving clockwise from the <code>i<sup>th</sup></code> friend brings you to the <code>(i+1)<sup>th</sup></code> friend for <code>1 <= i < n</code>, and moving clockwise from the <code>n<sup>th</sup></code> friend brings you to the <code>1<sup>st</sup></code> friend.</p>
<p>The rules of the game are as follows:</p>
<ol>
<li><strong>Start</strong> at the <code>1<sup>st</sup></code> friend.</li>
<li>Count the next <code>k</code> friends in the clockwise direction <strong>including</strong> the friend you started at. The counting wraps around the circle and may count some friends more than once.</li>
<li>The last friend you counted leaves the circle and loses the game.</li>
<li>If there is still more than one friend in the circle, go back to step <code>2</code> <strong>starting</strong> from the friend <strong>immediately clockwise</strong> of the friend who just lost and repeat.</li>
<li>Else, the last friend in the circle wins the game.</li>
</ol>
<p>Given the number of friends, <code>n</code>, and an integer <code>k</code>, return <em>the winner of the game</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/25/ic234-q2-ex11.png" style="width: 500px; height: 345px;" />
<pre>
<strong>Input:</strong> n = 5, k = 2
<strong>Output:</strong> 3
<strong>Explanation:</strong> Here are the steps of the game:
1) Start at friend 1.
2) Count 2 friends clockwise, which are friends 1 and 2.
3) Friend 2 leaves the circle. Next start is friend 3.
4) Count 2 friends clockwise, which are friends 3 and 4.
5) Friend 4 leaves the circle. Next start is friend 5.
6) Count 2 friends clockwise, which are friends 5 and 1.
7) Friend 1 leaves the circle. Next start is friend 3.
8) Count 2 friends clockwise, which are friends 3 and 5.
9) Friend 5 leaves the circle. Only friend 3 is left, so they are the winner.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 6, k = 5
<strong>Output:</strong> 1
<strong>Explanation:</strong> The friends leave in this order: 5, 4, 6, 2, 3. The winner is friend 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= n <= 500</code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<p>Could you solve this problem in linear time with constant space?</p>
| 2 | {
"code": "class Solution {\npublic:\n int findTheWinner(int n, int k) {\n vector<int> arr;\n for(int i=1;i<=n;i++)\n {\n arr.push_back(i);\n }\n for(int i=0;arr.size()!=1;)\n {\n i=(i+k-1)%arr.size();\n arr.erase(arr.begin()+i);\n\n }\n return arr[0];\n }\n};",
"memory": "7900"
} |
1,951 | <p>There are <code>n</code> friends that are playing a game. The friends are sitting in a circle and are numbered from <code>1</code> to <code>n</code> in <strong>clockwise order</strong>. More formally, moving clockwise from the <code>i<sup>th</sup></code> friend brings you to the <code>(i+1)<sup>th</sup></code> friend for <code>1 <= i < n</code>, and moving clockwise from the <code>n<sup>th</sup></code> friend brings you to the <code>1<sup>st</sup></code> friend.</p>
<p>The rules of the game are as follows:</p>
<ol>
<li><strong>Start</strong> at the <code>1<sup>st</sup></code> friend.</li>
<li>Count the next <code>k</code> friends in the clockwise direction <strong>including</strong> the friend you started at. The counting wraps around the circle and may count some friends more than once.</li>
<li>The last friend you counted leaves the circle and loses the game.</li>
<li>If there is still more than one friend in the circle, go back to step <code>2</code> <strong>starting</strong> from the friend <strong>immediately clockwise</strong> of the friend who just lost and repeat.</li>
<li>Else, the last friend in the circle wins the game.</li>
</ol>
<p>Given the number of friends, <code>n</code>, and an integer <code>k</code>, return <em>the winner of the game</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/25/ic234-q2-ex11.png" style="width: 500px; height: 345px;" />
<pre>
<strong>Input:</strong> n = 5, k = 2
<strong>Output:</strong> 3
<strong>Explanation:</strong> Here are the steps of the game:
1) Start at friend 1.
2) Count 2 friends clockwise, which are friends 1 and 2.
3) Friend 2 leaves the circle. Next start is friend 3.
4) Count 2 friends clockwise, which are friends 3 and 4.
5) Friend 4 leaves the circle. Next start is friend 5.
6) Count 2 friends clockwise, which are friends 5 and 1.
7) Friend 1 leaves the circle. Next start is friend 3.
8) Count 2 friends clockwise, which are friends 3 and 5.
9) Friend 5 leaves the circle. Only friend 3 is left, so they are the winner.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 6, k = 5
<strong>Output:</strong> 1
<strong>Explanation:</strong> The friends leave in this order: 5, 4, 6, 2, 3. The winner is friend 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= n <= 500</code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<p>Could you solve this problem in linear time with constant space?</p>
| 2 | {
"code": "class Solution {\npublic:\n int findTheWinner(int n, int k) {\n // Create a vector of friends numbered from 1 to n\n vector<int> friends;\n for (int i = 1; i <= n; i++) {\n friends.push_back(i);\n }\n \n int startIndex = 0;\n \n // Continue until only one friend remains\n while (friends.size() > 1) {\n // Calculate the index of the friend to be removed\n int removeIndex = (startIndex + k - 1) % friends.size();\n \n // Remove the friend\n friends.erase(friends.begin() + removeIndex);\n \n // Update the start index for the next round\n startIndex = removeIndex;\n }\n \n // Return the last remaining friend\n return friends[0];\n }\n};",
"memory": "8000"
} |
1,951 | <p>There are <code>n</code> friends that are playing a game. The friends are sitting in a circle and are numbered from <code>1</code> to <code>n</code> in <strong>clockwise order</strong>. More formally, moving clockwise from the <code>i<sup>th</sup></code> friend brings you to the <code>(i+1)<sup>th</sup></code> friend for <code>1 <= i < n</code>, and moving clockwise from the <code>n<sup>th</sup></code> friend brings you to the <code>1<sup>st</sup></code> friend.</p>
<p>The rules of the game are as follows:</p>
<ol>
<li><strong>Start</strong> at the <code>1<sup>st</sup></code> friend.</li>
<li>Count the next <code>k</code> friends in the clockwise direction <strong>including</strong> the friend you started at. The counting wraps around the circle and may count some friends more than once.</li>
<li>The last friend you counted leaves the circle and loses the game.</li>
<li>If there is still more than one friend in the circle, go back to step <code>2</code> <strong>starting</strong> from the friend <strong>immediately clockwise</strong> of the friend who just lost and repeat.</li>
<li>Else, the last friend in the circle wins the game.</li>
</ol>
<p>Given the number of friends, <code>n</code>, and an integer <code>k</code>, return <em>the winner of the game</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/25/ic234-q2-ex11.png" style="width: 500px; height: 345px;" />
<pre>
<strong>Input:</strong> n = 5, k = 2
<strong>Output:</strong> 3
<strong>Explanation:</strong> Here are the steps of the game:
1) Start at friend 1.
2) Count 2 friends clockwise, which are friends 1 and 2.
3) Friend 2 leaves the circle. Next start is friend 3.
4) Count 2 friends clockwise, which are friends 3 and 4.
5) Friend 4 leaves the circle. Next start is friend 5.
6) Count 2 friends clockwise, which are friends 5 and 1.
7) Friend 1 leaves the circle. Next start is friend 3.
8) Count 2 friends clockwise, which are friends 3 and 5.
9) Friend 5 leaves the circle. Only friend 3 is left, so they are the winner.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 6, k = 5
<strong>Output:</strong> 1
<strong>Explanation:</strong> The friends leave in this order: 5, 4, 6, 2, 3. The winner is friend 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= n <= 500</code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<p>Could you solve this problem in linear time with constant space?</p>
| 2 | {
"code": "class Solution {\n\n struct ListNode {\n int val;\n ListNode *next;\n ListNode() : val(0), next(nullptr) {}\n ListNode(int x) : val(x), next(nullptr) {}\n ListNode(int x, ListNode *next) : val(x), next(next) {}\n };\n \npublic:\n int solve(ListNode* curr,ListNode* prev,int k){\n\n if(prev == curr){\n return curr->val;\n }\n int savek = k;\n while(k--){\n prev = prev->next;\n curr = curr->next;\n }\n prev->next = curr->next;\n curr = prev->next;\n \n return solve(curr,prev,savek);\n }\n int findTheWinner(int n, int k) {\n \n ListNode* head = new ListNode(1);\n ListNode* temp = head;\n\n for(int i = 2; i <= n ; i++){\n \n ListNode* node = new ListNode(i);\n temp->next = node;\n temp = temp->next;\n \n }\n temp->next = head;\n\n ListNode* prev = temp, *curr = head;\n\n return solve(curr,prev,k-1);\n }\n};",
"memory": "8100"
} |
1,951 | <p>There are <code>n</code> friends that are playing a game. The friends are sitting in a circle and are numbered from <code>1</code> to <code>n</code> in <strong>clockwise order</strong>. More formally, moving clockwise from the <code>i<sup>th</sup></code> friend brings you to the <code>(i+1)<sup>th</sup></code> friend for <code>1 <= i < n</code>, and moving clockwise from the <code>n<sup>th</sup></code> friend brings you to the <code>1<sup>st</sup></code> friend.</p>
<p>The rules of the game are as follows:</p>
<ol>
<li><strong>Start</strong> at the <code>1<sup>st</sup></code> friend.</li>
<li>Count the next <code>k</code> friends in the clockwise direction <strong>including</strong> the friend you started at. The counting wraps around the circle and may count some friends more than once.</li>
<li>The last friend you counted leaves the circle and loses the game.</li>
<li>If there is still more than one friend in the circle, go back to step <code>2</code> <strong>starting</strong> from the friend <strong>immediately clockwise</strong> of the friend who just lost and repeat.</li>
<li>Else, the last friend in the circle wins the game.</li>
</ol>
<p>Given the number of friends, <code>n</code>, and an integer <code>k</code>, return <em>the winner of the game</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/25/ic234-q2-ex11.png" style="width: 500px; height: 345px;" />
<pre>
<strong>Input:</strong> n = 5, k = 2
<strong>Output:</strong> 3
<strong>Explanation:</strong> Here are the steps of the game:
1) Start at friend 1.
2) Count 2 friends clockwise, which are friends 1 and 2.
3) Friend 2 leaves the circle. Next start is friend 3.
4) Count 2 friends clockwise, which are friends 3 and 4.
5) Friend 4 leaves the circle. Next start is friend 5.
6) Count 2 friends clockwise, which are friends 5 and 1.
7) Friend 1 leaves the circle. Next start is friend 3.
8) Count 2 friends clockwise, which are friends 3 and 5.
9) Friend 5 leaves the circle. Only friend 3 is left, so they are the winner.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 6, k = 5
<strong>Output:</strong> 1
<strong>Explanation:</strong> The friends leave in this order: 5, 4, 6, 2, 3. The winner is friend 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= n <= 500</code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<p>Could you solve this problem in linear time with constant space?</p>
| 2 | {
"code": "class Solution {\npublic:\n struct ListNode {\n int val;\n ListNode *next;\n ListNode(int x) : val(x), next(NULL) {}\n };\n void solve(ListNode* temp, int &i, int k, int n)\n {\n // edge case\n if(n==1) return;\n\n // 1 case hm solve krenge\n int count=k-1;\n while(count>1)\n {\n temp=temp->next;\n count--;\n }\n ListNode* todel=temp->next;\n temp->next=todel->next;\n delete todel;\n i=temp->next->val;\n\n // baki case recursion sambhal lega\n solve(temp->next,i,k,n-1);\n }\n int findTheWinner(int n, int k) {\n if(n==1) return 1;\n if(k==1) return n;\n vector<int> v;\n ListNode* root=new ListNode(1);\n ListNode* temp1=root;\n for(int i=2;i<=n;i++)\n {\n temp1->next=new ListNode(i);\n temp1=temp1->next;\n }\n temp1->next=root;\n int i=1;\n ListNode* temp=root;\n solve(temp,i,k,n);\n\n return i;\n }\n};",
"memory": "8200"
} |
1,951 | <p>There are <code>n</code> friends that are playing a game. The friends are sitting in a circle and are numbered from <code>1</code> to <code>n</code> in <strong>clockwise order</strong>. More formally, moving clockwise from the <code>i<sup>th</sup></code> friend brings you to the <code>(i+1)<sup>th</sup></code> friend for <code>1 <= i < n</code>, and moving clockwise from the <code>n<sup>th</sup></code> friend brings you to the <code>1<sup>st</sup></code> friend.</p>
<p>The rules of the game are as follows:</p>
<ol>
<li><strong>Start</strong> at the <code>1<sup>st</sup></code> friend.</li>
<li>Count the next <code>k</code> friends in the clockwise direction <strong>including</strong> the friend you started at. The counting wraps around the circle and may count some friends more than once.</li>
<li>The last friend you counted leaves the circle and loses the game.</li>
<li>If there is still more than one friend in the circle, go back to step <code>2</code> <strong>starting</strong> from the friend <strong>immediately clockwise</strong> of the friend who just lost and repeat.</li>
<li>Else, the last friend in the circle wins the game.</li>
</ol>
<p>Given the number of friends, <code>n</code>, and an integer <code>k</code>, return <em>the winner of the game</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/25/ic234-q2-ex11.png" style="width: 500px; height: 345px;" />
<pre>
<strong>Input:</strong> n = 5, k = 2
<strong>Output:</strong> 3
<strong>Explanation:</strong> Here are the steps of the game:
1) Start at friend 1.
2) Count 2 friends clockwise, which are friends 1 and 2.
3) Friend 2 leaves the circle. Next start is friend 3.
4) Count 2 friends clockwise, which are friends 3 and 4.
5) Friend 4 leaves the circle. Next start is friend 5.
6) Count 2 friends clockwise, which are friends 5 and 1.
7) Friend 1 leaves the circle. Next start is friend 3.
8) Count 2 friends clockwise, which are friends 3 and 5.
9) Friend 5 leaves the circle. Only friend 3 is left, so they are the winner.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 6, k = 5
<strong>Output:</strong> 1
<strong>Explanation:</strong> The friends leave in this order: 5, 4, 6, 2, 3. The winner is friend 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= n <= 500</code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<p>Could you solve this problem in linear time with constant space?</p>
| 2 | {
"code": "class Solution {\npublic:\n class Node {\n public:\n int val;\n Node *prev, *next;\n Node(int v) {\n val = v;\n }\n };\n\n int findTheWinner(int n, int k) {\n Node *head = new Node(1);\n Node *curr = head;\n\n for(int i=2; i<=n; i++) {\n Node *newNode = new Node(i);\n curr->next = newNode;\n newNode->prev = curr;\n curr = newNode;\n }\n\n curr->next = head;\n head->prev = curr;\n curr = head;\n\n while(curr->next != curr) {\n for(int i=1; i<k; i++) {\n curr = curr->next;\n }\n\n Node *next = curr->next;\n\n curr->prev->next = curr->next;\n curr->next->prev = curr->prev;\n curr->prev = nullptr;\n curr->next = nullptr;\n\n curr = next;\n }\n\n return curr->val;\n }\n};",
"memory": "8300"
} |
1,951 | <p>There are <code>n</code> friends that are playing a game. The friends are sitting in a circle and are numbered from <code>1</code> to <code>n</code> in <strong>clockwise order</strong>. More formally, moving clockwise from the <code>i<sup>th</sup></code> friend brings you to the <code>(i+1)<sup>th</sup></code> friend for <code>1 <= i < n</code>, and moving clockwise from the <code>n<sup>th</sup></code> friend brings you to the <code>1<sup>st</sup></code> friend.</p>
<p>The rules of the game are as follows:</p>
<ol>
<li><strong>Start</strong> at the <code>1<sup>st</sup></code> friend.</li>
<li>Count the next <code>k</code> friends in the clockwise direction <strong>including</strong> the friend you started at. The counting wraps around the circle and may count some friends more than once.</li>
<li>The last friend you counted leaves the circle and loses the game.</li>
<li>If there is still more than one friend in the circle, go back to step <code>2</code> <strong>starting</strong> from the friend <strong>immediately clockwise</strong> of the friend who just lost and repeat.</li>
<li>Else, the last friend in the circle wins the game.</li>
</ol>
<p>Given the number of friends, <code>n</code>, and an integer <code>k</code>, return <em>the winner of the game</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/25/ic234-q2-ex11.png" style="width: 500px; height: 345px;" />
<pre>
<strong>Input:</strong> n = 5, k = 2
<strong>Output:</strong> 3
<strong>Explanation:</strong> Here are the steps of the game:
1) Start at friend 1.
2) Count 2 friends clockwise, which are friends 1 and 2.
3) Friend 2 leaves the circle. Next start is friend 3.
4) Count 2 friends clockwise, which are friends 3 and 4.
5) Friend 4 leaves the circle. Next start is friend 5.
6) Count 2 friends clockwise, which are friends 5 and 1.
7) Friend 1 leaves the circle. Next start is friend 3.
8) Count 2 friends clockwise, which are friends 3 and 5.
9) Friend 5 leaves the circle. Only friend 3 is left, so they are the winner.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 6, k = 5
<strong>Output:</strong> 1
<strong>Explanation:</strong> The friends leave in this order: 5, 4, 6, 2, 3. The winner is friend 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= n <= 500</code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<p>Could you solve this problem in linear time with constant space?</p>
| 2 | {
"code": "class Solution {\npublic:\n int findTheWinner(int n, int k) {\n list<int> friends;\n for(int i=1;i<=n;i++){\n friends.push_back(i);\n }\n auto it=friends.begin();\n while(friends.size()>1){\n for(int i=0;i<k-1;++i){\n ++it;\n if(it == friends.end()){\n it=friends.begin();\n }\n }\n it=friends.erase(it);\n if(it==friends.end()){\n it=friends.begin();\n }\n }\n return friends.front();\n }\n};",
"memory": "8400"
} |
1,951 | <p>There are <code>n</code> friends that are playing a game. The friends are sitting in a circle and are numbered from <code>1</code> to <code>n</code> in <strong>clockwise order</strong>. More formally, moving clockwise from the <code>i<sup>th</sup></code> friend brings you to the <code>(i+1)<sup>th</sup></code> friend for <code>1 <= i < n</code>, and moving clockwise from the <code>n<sup>th</sup></code> friend brings you to the <code>1<sup>st</sup></code> friend.</p>
<p>The rules of the game are as follows:</p>
<ol>
<li><strong>Start</strong> at the <code>1<sup>st</sup></code> friend.</li>
<li>Count the next <code>k</code> friends in the clockwise direction <strong>including</strong> the friend you started at. The counting wraps around the circle and may count some friends more than once.</li>
<li>The last friend you counted leaves the circle and loses the game.</li>
<li>If there is still more than one friend in the circle, go back to step <code>2</code> <strong>starting</strong> from the friend <strong>immediately clockwise</strong> of the friend who just lost and repeat.</li>
<li>Else, the last friend in the circle wins the game.</li>
</ol>
<p>Given the number of friends, <code>n</code>, and an integer <code>k</code>, return <em>the winner of the game</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/25/ic234-q2-ex11.png" style="width: 500px; height: 345px;" />
<pre>
<strong>Input:</strong> n = 5, k = 2
<strong>Output:</strong> 3
<strong>Explanation:</strong> Here are the steps of the game:
1) Start at friend 1.
2) Count 2 friends clockwise, which are friends 1 and 2.
3) Friend 2 leaves the circle. Next start is friend 3.
4) Count 2 friends clockwise, which are friends 3 and 4.
5) Friend 4 leaves the circle. Next start is friend 5.
6) Count 2 friends clockwise, which are friends 5 and 1.
7) Friend 1 leaves the circle. Next start is friend 3.
8) Count 2 friends clockwise, which are friends 3 and 5.
9) Friend 5 leaves the circle. Only friend 3 is left, so they are the winner.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 6, k = 5
<strong>Output:</strong> 1
<strong>Explanation:</strong> The friends leave in this order: 5, 4, 6, 2, 3. The winner is friend 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= n <= 500</code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<p>Could you solve this problem in linear time with constant space?</p>
| 2 | {
"code": "class Solution {\npublic:\n int findTheWinner(int n, int k) {\n if(k==1 || n==1) \n return n;\n set<int> s;\n for(int i=1;i<=n;i++){\n s.insert(i);\n }\n auto it=s.begin();\n while(s.size()>1){\n for(int steps=1;steps<k;steps++){\n it++;\n if(it==s.end())\n it=s.begin();\n }\n auto nit=next(it);\n if(nit==s.end())\n nit=s.begin();\n \n s.erase(it);\n it=nit;\n }\n return *s.begin();\n\n }\n};",
"memory": "8500"
} |
1,951 | <p>There are <code>n</code> friends that are playing a game. The friends are sitting in a circle and are numbered from <code>1</code> to <code>n</code> in <strong>clockwise order</strong>. More formally, moving clockwise from the <code>i<sup>th</sup></code> friend brings you to the <code>(i+1)<sup>th</sup></code> friend for <code>1 <= i < n</code>, and moving clockwise from the <code>n<sup>th</sup></code> friend brings you to the <code>1<sup>st</sup></code> friend.</p>
<p>The rules of the game are as follows:</p>
<ol>
<li><strong>Start</strong> at the <code>1<sup>st</sup></code> friend.</li>
<li>Count the next <code>k</code> friends in the clockwise direction <strong>including</strong> the friend you started at. The counting wraps around the circle and may count some friends more than once.</li>
<li>The last friend you counted leaves the circle and loses the game.</li>
<li>If there is still more than one friend in the circle, go back to step <code>2</code> <strong>starting</strong> from the friend <strong>immediately clockwise</strong> of the friend who just lost and repeat.</li>
<li>Else, the last friend in the circle wins the game.</li>
</ol>
<p>Given the number of friends, <code>n</code>, and an integer <code>k</code>, return <em>the winner of the game</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/25/ic234-q2-ex11.png" style="width: 500px; height: 345px;" />
<pre>
<strong>Input:</strong> n = 5, k = 2
<strong>Output:</strong> 3
<strong>Explanation:</strong> Here are the steps of the game:
1) Start at friend 1.
2) Count 2 friends clockwise, which are friends 1 and 2.
3) Friend 2 leaves the circle. Next start is friend 3.
4) Count 2 friends clockwise, which are friends 3 and 4.
5) Friend 4 leaves the circle. Next start is friend 5.
6) Count 2 friends clockwise, which are friends 5 and 1.
7) Friend 1 leaves the circle. Next start is friend 3.
8) Count 2 friends clockwise, which are friends 3 and 5.
9) Friend 5 leaves the circle. Only friend 3 is left, so they are the winner.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 6, k = 5
<strong>Output:</strong> 1
<strong>Explanation:</strong> The friends leave in this order: 5, 4, 6, 2, 3. The winner is friend 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= n <= 500</code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<p>Could you solve this problem in linear time with constant space?</p>
| 2 | {
"code": "class Solution {\npublic:\n int winner(vector<bool>&arr,int index,int n,int &k)\n {\n if(n==1)\n return index+1;\n\n int kill=k-1%n;\n\n for(int i=1;i<=kill;i++)\n {\n index=(index+1)%(arr.size()); \n while(arr[index])\n {\n index=(index+1)%(arr.size());\n }\n\n \n }\n arr[index]=1;\n\n while(arr[index])\n {\n index=(index+1)%(arr.size());\n }\n \n return winner(arr,index,n-1,k);\n\n\n \n }\n int findTheWinner(int n, int k) \n {\n vector<bool> arr(n,0);\n return winner(arr,0,n,k);\n \n }\n};",
"memory": "8600"
} |
1,951 | <p>There are <code>n</code> friends that are playing a game. The friends are sitting in a circle and are numbered from <code>1</code> to <code>n</code> in <strong>clockwise order</strong>. More formally, moving clockwise from the <code>i<sup>th</sup></code> friend brings you to the <code>(i+1)<sup>th</sup></code> friend for <code>1 <= i < n</code>, and moving clockwise from the <code>n<sup>th</sup></code> friend brings you to the <code>1<sup>st</sup></code> friend.</p>
<p>The rules of the game are as follows:</p>
<ol>
<li><strong>Start</strong> at the <code>1<sup>st</sup></code> friend.</li>
<li>Count the next <code>k</code> friends in the clockwise direction <strong>including</strong> the friend you started at. The counting wraps around the circle and may count some friends more than once.</li>
<li>The last friend you counted leaves the circle and loses the game.</li>
<li>If there is still more than one friend in the circle, go back to step <code>2</code> <strong>starting</strong> from the friend <strong>immediately clockwise</strong> of the friend who just lost and repeat.</li>
<li>Else, the last friend in the circle wins the game.</li>
</ol>
<p>Given the number of friends, <code>n</code>, and an integer <code>k</code>, return <em>the winner of the game</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/25/ic234-q2-ex11.png" style="width: 500px; height: 345px;" />
<pre>
<strong>Input:</strong> n = 5, k = 2
<strong>Output:</strong> 3
<strong>Explanation:</strong> Here are the steps of the game:
1) Start at friend 1.
2) Count 2 friends clockwise, which are friends 1 and 2.
3) Friend 2 leaves the circle. Next start is friend 3.
4) Count 2 friends clockwise, which are friends 3 and 4.
5) Friend 4 leaves the circle. Next start is friend 5.
6) Count 2 friends clockwise, which are friends 5 and 1.
7) Friend 1 leaves the circle. Next start is friend 3.
8) Count 2 friends clockwise, which are friends 3 and 5.
9) Friend 5 leaves the circle. Only friend 3 is left, so they are the winner.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 6, k = 5
<strong>Output:</strong> 1
<strong>Explanation:</strong> The friends leave in this order: 5, 4, 6, 2, 3. The winner is friend 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= n <= 500</code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<p>Could you solve this problem in linear time with constant space?</p>
| 2 | {
"code": "class Solution {\npublic:\n int winner(vector<bool>&arr,int index,int n,int &k)\n {\n if(n==1)\n return index+1;\n\n int kill=k-1%n;\n\n for(int i=1;i<=kill;i++)\n {\n index=(index+1)%(arr.size()); \n while(arr[index])\n {\n index=(index+1)%(arr.size());\n }\n\n \n }\n arr[index]=1;\n\n while(arr[index])\n {\n index=(index+1)%(arr.size());\n }\n \n return winner(arr,index,n-1,k);\n\n\n \n }\n int findTheWinner(int n, int k) \n {\n vector<bool> arr(n,0);\n return winner(arr,0,n,k);\n \n }\n};",
"memory": "8600"
} |
1,951 | <p>There are <code>n</code> friends that are playing a game. The friends are sitting in a circle and are numbered from <code>1</code> to <code>n</code> in <strong>clockwise order</strong>. More formally, moving clockwise from the <code>i<sup>th</sup></code> friend brings you to the <code>(i+1)<sup>th</sup></code> friend for <code>1 <= i < n</code>, and moving clockwise from the <code>n<sup>th</sup></code> friend brings you to the <code>1<sup>st</sup></code> friend.</p>
<p>The rules of the game are as follows:</p>
<ol>
<li><strong>Start</strong> at the <code>1<sup>st</sup></code> friend.</li>
<li>Count the next <code>k</code> friends in the clockwise direction <strong>including</strong> the friend you started at. The counting wraps around the circle and may count some friends more than once.</li>
<li>The last friend you counted leaves the circle and loses the game.</li>
<li>If there is still more than one friend in the circle, go back to step <code>2</code> <strong>starting</strong> from the friend <strong>immediately clockwise</strong> of the friend who just lost and repeat.</li>
<li>Else, the last friend in the circle wins the game.</li>
</ol>
<p>Given the number of friends, <code>n</code>, and an integer <code>k</code>, return <em>the winner of the game</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/25/ic234-q2-ex11.png" style="width: 500px; height: 345px;" />
<pre>
<strong>Input:</strong> n = 5, k = 2
<strong>Output:</strong> 3
<strong>Explanation:</strong> Here are the steps of the game:
1) Start at friend 1.
2) Count 2 friends clockwise, which are friends 1 and 2.
3) Friend 2 leaves the circle. Next start is friend 3.
4) Count 2 friends clockwise, which are friends 3 and 4.
5) Friend 4 leaves the circle. Next start is friend 5.
6) Count 2 friends clockwise, which are friends 5 and 1.
7) Friend 1 leaves the circle. Next start is friend 3.
8) Count 2 friends clockwise, which are friends 3 and 5.
9) Friend 5 leaves the circle. Only friend 3 is left, so they are the winner.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 6, k = 5
<strong>Output:</strong> 1
<strong>Explanation:</strong> The friends leave in this order: 5, 4, 6, 2, 3. The winner is friend 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= n <= 500</code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<p>Could you solve this problem in linear time with constant space?</p>
| 2 | {
"code": "class Solution {\npublic:\n int winner(vector<bool>&arr,int index,int n,int &k)\n {\n if(n==1)\n return index+1;\n\n int kill=(k-1)%n;\n\n while(kill--)\n {\n index=(index+1)%(arr.size()); \n while(arr[index])\n {\n index=(index+1)%(arr.size());\n }\n\n \n }\n arr[index]=1;\n\n while(arr[index])\n {\n index=(index+1)%(arr.size());\n }\n \n return winner(arr,index,n-1,k);\n\n\n \n }\n int findTheWinner(int n, int k) \n {\n vector<bool> arr(n,0);\n return winner(arr,0,n,k);\n \n }\n};",
"memory": "8700"
} |
1,951 | <p>There are <code>n</code> friends that are playing a game. The friends are sitting in a circle and are numbered from <code>1</code> to <code>n</code> in <strong>clockwise order</strong>. More formally, moving clockwise from the <code>i<sup>th</sup></code> friend brings you to the <code>(i+1)<sup>th</sup></code> friend for <code>1 <= i < n</code>, and moving clockwise from the <code>n<sup>th</sup></code> friend brings you to the <code>1<sup>st</sup></code> friend.</p>
<p>The rules of the game are as follows:</p>
<ol>
<li><strong>Start</strong> at the <code>1<sup>st</sup></code> friend.</li>
<li>Count the next <code>k</code> friends in the clockwise direction <strong>including</strong> the friend you started at. The counting wraps around the circle and may count some friends more than once.</li>
<li>The last friend you counted leaves the circle and loses the game.</li>
<li>If there is still more than one friend in the circle, go back to step <code>2</code> <strong>starting</strong> from the friend <strong>immediately clockwise</strong> of the friend who just lost and repeat.</li>
<li>Else, the last friend in the circle wins the game.</li>
</ol>
<p>Given the number of friends, <code>n</code>, and an integer <code>k</code>, return <em>the winner of the game</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/25/ic234-q2-ex11.png" style="width: 500px; height: 345px;" />
<pre>
<strong>Input:</strong> n = 5, k = 2
<strong>Output:</strong> 3
<strong>Explanation:</strong> Here are the steps of the game:
1) Start at friend 1.
2) Count 2 friends clockwise, which are friends 1 and 2.
3) Friend 2 leaves the circle. Next start is friend 3.
4) Count 2 friends clockwise, which are friends 3 and 4.
5) Friend 4 leaves the circle. Next start is friend 5.
6) Count 2 friends clockwise, which are friends 5 and 1.
7) Friend 1 leaves the circle. Next start is friend 3.
8) Count 2 friends clockwise, which are friends 3 and 5.
9) Friend 5 leaves the circle. Only friend 3 is left, so they are the winner.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 6, k = 5
<strong>Output:</strong> 1
<strong>Explanation:</strong> The friends leave in this order: 5, 4, 6, 2, 3. The winner is friend 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= n <= 500</code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<p>Could you solve this problem in linear time with constant space?</p>
| 2 | {
"code": "class Solution {\npublic:\n int findTheWinner(int n, int k) {\n set<int> s;\n for(int i=1;i<=n;i++)\n {\n s.insert(i);\n }\n int c=0;\n int i=1;\n while(s.size()>1)\n {\n if(i>n)\n {\n i=i-n;\n }\n if(s.find(i)!=s.end())\n {\n c++;\n if(c==k)\n {\n s.erase(i);\n c=0;\n }\n }\n i++;\n }\n int res;\n for(auto x:s)\n {\n res=x;\n }\n return res;\n \n }\n};",
"memory": "8700"
} |
1,951 | <p>There are <code>n</code> friends that are playing a game. The friends are sitting in a circle and are numbered from <code>1</code> to <code>n</code> in <strong>clockwise order</strong>. More formally, moving clockwise from the <code>i<sup>th</sup></code> friend brings you to the <code>(i+1)<sup>th</sup></code> friend for <code>1 <= i < n</code>, and moving clockwise from the <code>n<sup>th</sup></code> friend brings you to the <code>1<sup>st</sup></code> friend.</p>
<p>The rules of the game are as follows:</p>
<ol>
<li><strong>Start</strong> at the <code>1<sup>st</sup></code> friend.</li>
<li>Count the next <code>k</code> friends in the clockwise direction <strong>including</strong> the friend you started at. The counting wraps around the circle and may count some friends more than once.</li>
<li>The last friend you counted leaves the circle and loses the game.</li>
<li>If there is still more than one friend in the circle, go back to step <code>2</code> <strong>starting</strong> from the friend <strong>immediately clockwise</strong> of the friend who just lost and repeat.</li>
<li>Else, the last friend in the circle wins the game.</li>
</ol>
<p>Given the number of friends, <code>n</code>, and an integer <code>k</code>, return <em>the winner of the game</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/25/ic234-q2-ex11.png" style="width: 500px; height: 345px;" />
<pre>
<strong>Input:</strong> n = 5, k = 2
<strong>Output:</strong> 3
<strong>Explanation:</strong> Here are the steps of the game:
1) Start at friend 1.
2) Count 2 friends clockwise, which are friends 1 and 2.
3) Friend 2 leaves the circle. Next start is friend 3.
4) Count 2 friends clockwise, which are friends 3 and 4.
5) Friend 4 leaves the circle. Next start is friend 5.
6) Count 2 friends clockwise, which are friends 5 and 1.
7) Friend 1 leaves the circle. Next start is friend 3.
8) Count 2 friends clockwise, which are friends 3 and 5.
9) Friend 5 leaves the circle. Only friend 3 is left, so they are the winner.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 6, k = 5
<strong>Output:</strong> 1
<strong>Explanation:</strong> The friends leave in this order: 5, 4, 6, 2, 3. The winner is friend 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= n <= 500</code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<p>Could you solve this problem in linear time with constant space?</p>
| 2 | {
"code": "class Solution {\npublic:\n int winner(vector<bool>&arr,int index,int n,int &k)\n {\n if(n==1)\n return index+1;\n\n int kill=(k-1)%n;\n\n while(kill--)\n {\n index=(index+1)%(arr.size()); \n while(arr[index])\n {\n index=(index+1)%(arr.size());\n }\n\n \n }\n arr[index]=1;\n\n while(arr[index])\n {\n index=(index+1)%(arr.size());\n }\n \n return winner(arr,index,n-1,k);\n\n\n \n }\n int findTheWinner(int n, int k) \n {\n vector<bool> arr(n,0);\n return winner(arr,0,n,k);\n \n }\n};",
"memory": "8800"
} |
1,951 | <p>There are <code>n</code> friends that are playing a game. The friends are sitting in a circle and are numbered from <code>1</code> to <code>n</code> in <strong>clockwise order</strong>. More formally, moving clockwise from the <code>i<sup>th</sup></code> friend brings you to the <code>(i+1)<sup>th</sup></code> friend for <code>1 <= i < n</code>, and moving clockwise from the <code>n<sup>th</sup></code> friend brings you to the <code>1<sup>st</sup></code> friend.</p>
<p>The rules of the game are as follows:</p>
<ol>
<li><strong>Start</strong> at the <code>1<sup>st</sup></code> friend.</li>
<li>Count the next <code>k</code> friends in the clockwise direction <strong>including</strong> the friend you started at. The counting wraps around the circle and may count some friends more than once.</li>
<li>The last friend you counted leaves the circle and loses the game.</li>
<li>If there is still more than one friend in the circle, go back to step <code>2</code> <strong>starting</strong> from the friend <strong>immediately clockwise</strong> of the friend who just lost and repeat.</li>
<li>Else, the last friend in the circle wins the game.</li>
</ol>
<p>Given the number of friends, <code>n</code>, and an integer <code>k</code>, return <em>the winner of the game</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/25/ic234-q2-ex11.png" style="width: 500px; height: 345px;" />
<pre>
<strong>Input:</strong> n = 5, k = 2
<strong>Output:</strong> 3
<strong>Explanation:</strong> Here are the steps of the game:
1) Start at friend 1.
2) Count 2 friends clockwise, which are friends 1 and 2.
3) Friend 2 leaves the circle. Next start is friend 3.
4) Count 2 friends clockwise, which are friends 3 and 4.
5) Friend 4 leaves the circle. Next start is friend 5.
6) Count 2 friends clockwise, which are friends 5 and 1.
7) Friend 1 leaves the circle. Next start is friend 3.
8) Count 2 friends clockwise, which are friends 3 and 5.
9) Friend 5 leaves the circle. Only friend 3 is left, so they are the winner.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 6, k = 5
<strong>Output:</strong> 1
<strong>Explanation:</strong> The friends leave in this order: 5, 4, 6, 2, 3. The winner is friend 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= n <= 500</code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<p>Could you solve this problem in linear time with constant space?</p>
| 2 | {
"code": "class Solution {\npublic:\n int winner(vector<bool>&arr,int index,int n,int &k)\n {\n if(n==1)\n return index+1;\n\n int kill=(k-1)%n;\n\n for(int i=0;i<kill;i++)\n {\n index=(index+1)%(arr.size()); \n while(arr[index])\n {\n index=(index+1)%(arr.size());\n }\n\n \n }\n arr[index]=1;\n\n while(arr[index])\n {\n index=(index+1)%(arr.size());\n }\n \n return winner(arr,index,n-1,k);\n\n\n \n }\n int findTheWinner(int n, int k) \n {\n vector<bool> arr(n,0);\n return winner(arr,0,n,k);\n \n }\n};",
"memory": "8800"
} |
1,951 | <p>There are <code>n</code> friends that are playing a game. The friends are sitting in a circle and are numbered from <code>1</code> to <code>n</code> in <strong>clockwise order</strong>. More formally, moving clockwise from the <code>i<sup>th</sup></code> friend brings you to the <code>(i+1)<sup>th</sup></code> friend for <code>1 <= i < n</code>, and moving clockwise from the <code>n<sup>th</sup></code> friend brings you to the <code>1<sup>st</sup></code> friend.</p>
<p>The rules of the game are as follows:</p>
<ol>
<li><strong>Start</strong> at the <code>1<sup>st</sup></code> friend.</li>
<li>Count the next <code>k</code> friends in the clockwise direction <strong>including</strong> the friend you started at. The counting wraps around the circle and may count some friends more than once.</li>
<li>The last friend you counted leaves the circle and loses the game.</li>
<li>If there is still more than one friend in the circle, go back to step <code>2</code> <strong>starting</strong> from the friend <strong>immediately clockwise</strong> of the friend who just lost and repeat.</li>
<li>Else, the last friend in the circle wins the game.</li>
</ol>
<p>Given the number of friends, <code>n</code>, and an integer <code>k</code>, return <em>the winner of the game</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/25/ic234-q2-ex11.png" style="width: 500px; height: 345px;" />
<pre>
<strong>Input:</strong> n = 5, k = 2
<strong>Output:</strong> 3
<strong>Explanation:</strong> Here are the steps of the game:
1) Start at friend 1.
2) Count 2 friends clockwise, which are friends 1 and 2.
3) Friend 2 leaves the circle. Next start is friend 3.
4) Count 2 friends clockwise, which are friends 3 and 4.
5) Friend 4 leaves the circle. Next start is friend 5.
6) Count 2 friends clockwise, which are friends 5 and 1.
7) Friend 1 leaves the circle. Next start is friend 3.
8) Count 2 friends clockwise, which are friends 3 and 5.
9) Friend 5 leaves the circle. Only friend 3 is left, so they are the winner.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 6, k = 5
<strong>Output:</strong> 1
<strong>Explanation:</strong> The friends leave in this order: 5, 4, 6, 2, 3. The winner is friend 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= n <= 500</code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<p>Could you solve this problem in linear time with constant space?</p>
| 2 | {
"code": "class Solution {\npublic:\n int winner(vector<bool>&arr,int index,int n,int &k)\n {\n if(n==1)\n return index+1;\n\n int kill=(k-1)%n;\n\n for(int i=1;i<=kill;i++)\n {\n index=(index+1)%(arr.size()); \n while(arr[index])\n {\n index=(index+1)%(arr.size());\n }\n\n \n }\n arr[index]=1;\n\n while(arr[index])\n {\n index=(index+1)%(arr.size());\n }\n \n return winner(arr,index,n-1,k);\n\n\n \n }\n int findTheWinner(int n, int k) \n {\n vector<bool> arr(n,0);\n return winner(arr,0,n,k);\n \n }\n};",
"memory": "8900"
} |
1,951 | <p>There are <code>n</code> friends that are playing a game. The friends are sitting in a circle and are numbered from <code>1</code> to <code>n</code> in <strong>clockwise order</strong>. More formally, moving clockwise from the <code>i<sup>th</sup></code> friend brings you to the <code>(i+1)<sup>th</sup></code> friend for <code>1 <= i < n</code>, and moving clockwise from the <code>n<sup>th</sup></code> friend brings you to the <code>1<sup>st</sup></code> friend.</p>
<p>The rules of the game are as follows:</p>
<ol>
<li><strong>Start</strong> at the <code>1<sup>st</sup></code> friend.</li>
<li>Count the next <code>k</code> friends in the clockwise direction <strong>including</strong> the friend you started at. The counting wraps around the circle and may count some friends more than once.</li>
<li>The last friend you counted leaves the circle and loses the game.</li>
<li>If there is still more than one friend in the circle, go back to step <code>2</code> <strong>starting</strong> from the friend <strong>immediately clockwise</strong> of the friend who just lost and repeat.</li>
<li>Else, the last friend in the circle wins the game.</li>
</ol>
<p>Given the number of friends, <code>n</code>, and an integer <code>k</code>, return <em>the winner of the game</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/25/ic234-q2-ex11.png" style="width: 500px; height: 345px;" />
<pre>
<strong>Input:</strong> n = 5, k = 2
<strong>Output:</strong> 3
<strong>Explanation:</strong> Here are the steps of the game:
1) Start at friend 1.
2) Count 2 friends clockwise, which are friends 1 and 2.
3) Friend 2 leaves the circle. Next start is friend 3.
4) Count 2 friends clockwise, which are friends 3 and 4.
5) Friend 4 leaves the circle. Next start is friend 5.
6) Count 2 friends clockwise, which are friends 5 and 1.
7) Friend 1 leaves the circle. Next start is friend 3.
8) Count 2 friends clockwise, which are friends 3 and 5.
9) Friend 5 leaves the circle. Only friend 3 is left, so they are the winner.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 6, k = 5
<strong>Output:</strong> 1
<strong>Explanation:</strong> The friends leave in this order: 5, 4, 6, 2, 3. The winner is friend 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= n <= 500</code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<p>Could you solve this problem in linear time with constant space?</p>
| 2 | {
"code": "class Solution {\npublic:\n\n int winner( vector<bool>&person , int n , int index , int person_left , int k )\n {\n if(person_left==1)\n { \n for(int i=0;i<n;i++)\n if(person[i]==0)\n return i;\n }\n\n //find the position of Kill;\n\n int kill=(k-1)%person_left;\n while(kill--)\n\n {\n index=(index+1)%n;\n while(person[index]==1)\n index=(index+1)%n;//Skip the elemenated person\n }\n person[index]=1;\n //Next alive person\n while(person[index]==1)\n index=(index+1)%n;\n \n return winner(person,n,index,person_left-1,k);\n \n }\n int findTheWinner(int n, int k) {\n vector<bool>person(n,0);\n return winner(person,n,0,n,k)+1;\n }\n};",
"memory": "8900"
} |
1,951 | <p>There are <code>n</code> friends that are playing a game. The friends are sitting in a circle and are numbered from <code>1</code> to <code>n</code> in <strong>clockwise order</strong>. More formally, moving clockwise from the <code>i<sup>th</sup></code> friend brings you to the <code>(i+1)<sup>th</sup></code> friend for <code>1 <= i < n</code>, and moving clockwise from the <code>n<sup>th</sup></code> friend brings you to the <code>1<sup>st</sup></code> friend.</p>
<p>The rules of the game are as follows:</p>
<ol>
<li><strong>Start</strong> at the <code>1<sup>st</sup></code> friend.</li>
<li>Count the next <code>k</code> friends in the clockwise direction <strong>including</strong> the friend you started at. The counting wraps around the circle and may count some friends more than once.</li>
<li>The last friend you counted leaves the circle and loses the game.</li>
<li>If there is still more than one friend in the circle, go back to step <code>2</code> <strong>starting</strong> from the friend <strong>immediately clockwise</strong> of the friend who just lost and repeat.</li>
<li>Else, the last friend in the circle wins the game.</li>
</ol>
<p>Given the number of friends, <code>n</code>, and an integer <code>k</code>, return <em>the winner of the game</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/25/ic234-q2-ex11.png" style="width: 500px; height: 345px;" />
<pre>
<strong>Input:</strong> n = 5, k = 2
<strong>Output:</strong> 3
<strong>Explanation:</strong> Here are the steps of the game:
1) Start at friend 1.
2) Count 2 friends clockwise, which are friends 1 and 2.
3) Friend 2 leaves the circle. Next start is friend 3.
4) Count 2 friends clockwise, which are friends 3 and 4.
5) Friend 4 leaves the circle. Next start is friend 5.
6) Count 2 friends clockwise, which are friends 5 and 1.
7) Friend 1 leaves the circle. Next start is friend 3.
8) Count 2 friends clockwise, which are friends 3 and 5.
9) Friend 5 leaves the circle. Only friend 3 is left, so they are the winner.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 6, k = 5
<strong>Output:</strong> 1
<strong>Explanation:</strong> The friends leave in this order: 5, 4, 6, 2, 3. The winner is friend 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= n <= 500</code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<p>Could you solve this problem in linear time with constant space?</p>
| 2 | {
"code": "class Solution {\npublic:\n int findTheWinner(int n, int k) {\n \n std::vector<int> friends;\n for(int i = 1; i <=n; i++){\n friends.insert(friends.end(), i);\n }\n int pos = 0;\n\n while(friends.size() > 1){\n pos = pos + k - 1;\n pos = pos % friends.size();\n friends.erase(std::next(friends.begin(), pos));\n if(pos == friends.size()){\n pos = 0;\n }\n std::cout << std::endl;\n }\n return friends[0];\n }\n};",
"memory": "9000"
} |
1,951 | <p>There are <code>n</code> friends that are playing a game. The friends are sitting in a circle and are numbered from <code>1</code> to <code>n</code> in <strong>clockwise order</strong>. More formally, moving clockwise from the <code>i<sup>th</sup></code> friend brings you to the <code>(i+1)<sup>th</sup></code> friend for <code>1 <= i < n</code>, and moving clockwise from the <code>n<sup>th</sup></code> friend brings you to the <code>1<sup>st</sup></code> friend.</p>
<p>The rules of the game are as follows:</p>
<ol>
<li><strong>Start</strong> at the <code>1<sup>st</sup></code> friend.</li>
<li>Count the next <code>k</code> friends in the clockwise direction <strong>including</strong> the friend you started at. The counting wraps around the circle and may count some friends more than once.</li>
<li>The last friend you counted leaves the circle and loses the game.</li>
<li>If there is still more than one friend in the circle, go back to step <code>2</code> <strong>starting</strong> from the friend <strong>immediately clockwise</strong> of the friend who just lost and repeat.</li>
<li>Else, the last friend in the circle wins the game.</li>
</ol>
<p>Given the number of friends, <code>n</code>, and an integer <code>k</code>, return <em>the winner of the game</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/25/ic234-q2-ex11.png" style="width: 500px; height: 345px;" />
<pre>
<strong>Input:</strong> n = 5, k = 2
<strong>Output:</strong> 3
<strong>Explanation:</strong> Here are the steps of the game:
1) Start at friend 1.
2) Count 2 friends clockwise, which are friends 1 and 2.
3) Friend 2 leaves the circle. Next start is friend 3.
4) Count 2 friends clockwise, which are friends 3 and 4.
5) Friend 4 leaves the circle. Next start is friend 5.
6) Count 2 friends clockwise, which are friends 5 and 1.
7) Friend 1 leaves the circle. Next start is friend 3.
8) Count 2 friends clockwise, which are friends 3 and 5.
9) Friend 5 leaves the circle. Only friend 3 is left, so they are the winner.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 6, k = 5
<strong>Output:</strong> 1
<strong>Explanation:</strong> The friends leave in this order: 5, 4, 6, 2, 3. The winner is friend 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= n <= 500</code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<p>Could you solve this problem in linear time with constant space?</p>
| 2 | {
"code": "class Solution {\npublic:\n int findTheWinner(int n, int k) {\n \n std::vector<int> friends;\n for(int i = 1; i <=n; i++){\n friends.insert(friends.end(), i);\n }\n int pos = 0;\n\n while(friends.size() > 1){\n pos = pos + k - 1;\n pos = pos % friends.size();\n friends.erase(std::next(friends.begin(), pos));\n }\n return friends[0];\n }\n};",
"memory": "9000"
} |
1,951 | <p>There are <code>n</code> friends that are playing a game. The friends are sitting in a circle and are numbered from <code>1</code> to <code>n</code> in <strong>clockwise order</strong>. More formally, moving clockwise from the <code>i<sup>th</sup></code> friend brings you to the <code>(i+1)<sup>th</sup></code> friend for <code>1 <= i < n</code>, and moving clockwise from the <code>n<sup>th</sup></code> friend brings you to the <code>1<sup>st</sup></code> friend.</p>
<p>The rules of the game are as follows:</p>
<ol>
<li><strong>Start</strong> at the <code>1<sup>st</sup></code> friend.</li>
<li>Count the next <code>k</code> friends in the clockwise direction <strong>including</strong> the friend you started at. The counting wraps around the circle and may count some friends more than once.</li>
<li>The last friend you counted leaves the circle and loses the game.</li>
<li>If there is still more than one friend in the circle, go back to step <code>2</code> <strong>starting</strong> from the friend <strong>immediately clockwise</strong> of the friend who just lost and repeat.</li>
<li>Else, the last friend in the circle wins the game.</li>
</ol>
<p>Given the number of friends, <code>n</code>, and an integer <code>k</code>, return <em>the winner of the game</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/25/ic234-q2-ex11.png" style="width: 500px; height: 345px;" />
<pre>
<strong>Input:</strong> n = 5, k = 2
<strong>Output:</strong> 3
<strong>Explanation:</strong> Here are the steps of the game:
1) Start at friend 1.
2) Count 2 friends clockwise, which are friends 1 and 2.
3) Friend 2 leaves the circle. Next start is friend 3.
4) Count 2 friends clockwise, which are friends 3 and 4.
5) Friend 4 leaves the circle. Next start is friend 5.
6) Count 2 friends clockwise, which are friends 5 and 1.
7) Friend 1 leaves the circle. Next start is friend 3.
8) Count 2 friends clockwise, which are friends 3 and 5.
9) Friend 5 leaves the circle. Only friend 3 is left, so they are the winner.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 6, k = 5
<strong>Output:</strong> 1
<strong>Explanation:</strong> The friends leave in this order: 5, 4, 6, 2, 3. The winner is friend 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= n <= 500</code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<p>Could you solve this problem in linear time with constant space?</p>
| 2 | {
"code": "class Solution {\n\npublic:\n int findTheWinner(int n, int k) {\n vector<int> li;\n for(int i=1;i<n+1;i++){\n li.push_back(i);\n }\n std::function<int(int,int)> recur = [&](int totp,int currp){\n if (totp<2){\n return li[0];\n }\n int rm=(currp+k-1)%totp;\n li.erase(li.begin() + rm);\n totp--;\n currp=rm%totp;\n return recur(totp,currp);\n };\n\n return recur(n,0);\n }\n};\n\n",
"memory": "9100"
} |
1,951 | <p>There are <code>n</code> friends that are playing a game. The friends are sitting in a circle and are numbered from <code>1</code> to <code>n</code> in <strong>clockwise order</strong>. More formally, moving clockwise from the <code>i<sup>th</sup></code> friend brings you to the <code>(i+1)<sup>th</sup></code> friend for <code>1 <= i < n</code>, and moving clockwise from the <code>n<sup>th</sup></code> friend brings you to the <code>1<sup>st</sup></code> friend.</p>
<p>The rules of the game are as follows:</p>
<ol>
<li><strong>Start</strong> at the <code>1<sup>st</sup></code> friend.</li>
<li>Count the next <code>k</code> friends in the clockwise direction <strong>including</strong> the friend you started at. The counting wraps around the circle and may count some friends more than once.</li>
<li>The last friend you counted leaves the circle and loses the game.</li>
<li>If there is still more than one friend in the circle, go back to step <code>2</code> <strong>starting</strong> from the friend <strong>immediately clockwise</strong> of the friend who just lost and repeat.</li>
<li>Else, the last friend in the circle wins the game.</li>
</ol>
<p>Given the number of friends, <code>n</code>, and an integer <code>k</code>, return <em>the winner of the game</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/25/ic234-q2-ex11.png" style="width: 500px; height: 345px;" />
<pre>
<strong>Input:</strong> n = 5, k = 2
<strong>Output:</strong> 3
<strong>Explanation:</strong> Here are the steps of the game:
1) Start at friend 1.
2) Count 2 friends clockwise, which are friends 1 and 2.
3) Friend 2 leaves the circle. Next start is friend 3.
4) Count 2 friends clockwise, which are friends 3 and 4.
5) Friend 4 leaves the circle. Next start is friend 5.
6) Count 2 friends clockwise, which are friends 5 and 1.
7) Friend 1 leaves the circle. Next start is friend 3.
8) Count 2 friends clockwise, which are friends 3 and 5.
9) Friend 5 leaves the circle. Only friend 3 is left, so they are the winner.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 6, k = 5
<strong>Output:</strong> 1
<strong>Explanation:</strong> The friends leave in this order: 5, 4, 6, 2, 3. The winner is friend 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= n <= 500</code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<p>Could you solve this problem in linear time with constant space?</p>
| 2 | {
"code": "class Solution {\n int winner(vector<bool>&play,int &count,int &total,int k)\n{\n if(total==play.size()-1)\n {\n for(int i=0;i<play.size();i++)\n {\n if(play[i]==0)\n return i+1;\n }\n }\n \n for(int i=0;i<play.size();i++)\n {\n if(play[i]==0)\n count++;\n \n if(count==k)\n { \n play[i]=1;\n count=0;\n total++;\n }\n \n }\n return winner(play,count,total,k);\n}\npublic:\n int findTheWinner(int n, int k) {\n if(k==1)\n return n;\n vector<bool>play(n,0);\n int count=0,total=0;\n return winner(play,count,total,k);\n }\n};",
"memory": "9300"
} |
1,951 | <p>There are <code>n</code> friends that are playing a game. The friends are sitting in a circle and are numbered from <code>1</code> to <code>n</code> in <strong>clockwise order</strong>. More formally, moving clockwise from the <code>i<sup>th</sup></code> friend brings you to the <code>(i+1)<sup>th</sup></code> friend for <code>1 <= i < n</code>, and moving clockwise from the <code>n<sup>th</sup></code> friend brings you to the <code>1<sup>st</sup></code> friend.</p>
<p>The rules of the game are as follows:</p>
<ol>
<li><strong>Start</strong> at the <code>1<sup>st</sup></code> friend.</li>
<li>Count the next <code>k</code> friends in the clockwise direction <strong>including</strong> the friend you started at. The counting wraps around the circle and may count some friends more than once.</li>
<li>The last friend you counted leaves the circle and loses the game.</li>
<li>If there is still more than one friend in the circle, go back to step <code>2</code> <strong>starting</strong> from the friend <strong>immediately clockwise</strong> of the friend who just lost and repeat.</li>
<li>Else, the last friend in the circle wins the game.</li>
</ol>
<p>Given the number of friends, <code>n</code>, and an integer <code>k</code>, return <em>the winner of the game</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/25/ic234-q2-ex11.png" style="width: 500px; height: 345px;" />
<pre>
<strong>Input:</strong> n = 5, k = 2
<strong>Output:</strong> 3
<strong>Explanation:</strong> Here are the steps of the game:
1) Start at friend 1.
2) Count 2 friends clockwise, which are friends 1 and 2.
3) Friend 2 leaves the circle. Next start is friend 3.
4) Count 2 friends clockwise, which are friends 3 and 4.
5) Friend 4 leaves the circle. Next start is friend 5.
6) Count 2 friends clockwise, which are friends 5 and 1.
7) Friend 1 leaves the circle. Next start is friend 3.
8) Count 2 friends clockwise, which are friends 3 and 5.
9) Friend 5 leaves the circle. Only friend 3 is left, so they are the winner.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 6, k = 5
<strong>Output:</strong> 1
<strong>Explanation:</strong> The friends leave in this order: 5, 4, 6, 2, 3. The winner is friend 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= n <= 500</code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<p>Could you solve this problem in linear time with constant space?</p>
| 2 | {
"code": "class Solution {\npublic:\n int findTheWinner(int n, int k) {\n std::unordered_set<int> losers;\n int start = 1;\n while (losers.size() < n) {\n int count = 0;\n int next_start = start;\n for (; count < k; next_start = (next_start + 1) % n) {\n if (losers.find(next_start) != losers.end()) {\n continue;\n }\n start = next_start;\n ++count;\n }\n losers.insert(start);\n }\n return start == 0 ? n : start;\n }\n};",
"memory": "9700"
} |
1,951 | <p>There are <code>n</code> friends that are playing a game. The friends are sitting in a circle and are numbered from <code>1</code> to <code>n</code> in <strong>clockwise order</strong>. More formally, moving clockwise from the <code>i<sup>th</sup></code> friend brings you to the <code>(i+1)<sup>th</sup></code> friend for <code>1 <= i < n</code>, and moving clockwise from the <code>n<sup>th</sup></code> friend brings you to the <code>1<sup>st</sup></code> friend.</p>
<p>The rules of the game are as follows:</p>
<ol>
<li><strong>Start</strong> at the <code>1<sup>st</sup></code> friend.</li>
<li>Count the next <code>k</code> friends in the clockwise direction <strong>including</strong> the friend you started at. The counting wraps around the circle and may count some friends more than once.</li>
<li>The last friend you counted leaves the circle and loses the game.</li>
<li>If there is still more than one friend in the circle, go back to step <code>2</code> <strong>starting</strong> from the friend <strong>immediately clockwise</strong> of the friend who just lost and repeat.</li>
<li>Else, the last friend in the circle wins the game.</li>
</ol>
<p>Given the number of friends, <code>n</code>, and an integer <code>k</code>, return <em>the winner of the game</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/25/ic234-q2-ex11.png" style="width: 500px; height: 345px;" />
<pre>
<strong>Input:</strong> n = 5, k = 2
<strong>Output:</strong> 3
<strong>Explanation:</strong> Here are the steps of the game:
1) Start at friend 1.
2) Count 2 friends clockwise, which are friends 1 and 2.
3) Friend 2 leaves the circle. Next start is friend 3.
4) Count 2 friends clockwise, which are friends 3 and 4.
5) Friend 4 leaves the circle. Next start is friend 5.
6) Count 2 friends clockwise, which are friends 5 and 1.
7) Friend 1 leaves the circle. Next start is friend 3.
8) Count 2 friends clockwise, which are friends 3 and 5.
9) Friend 5 leaves the circle. Only friend 3 is left, so they are the winner.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 6, k = 5
<strong>Output:</strong> 1
<strong>Explanation:</strong> The friends leave in this order: 5, 4, 6, 2, 3. The winner is friend 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= n <= 500</code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<p>Could you solve this problem in linear time with constant space?</p>
| 2 | {
"code": "class Solution {\npublic:\n int findTheWinner(int n, int k) {\n unordered_set<int> out;\n int idx = 0;\n while (out.size() < n-1) {\n int tmp = 0;\n while (tmp < k) {\n if (out.count(idx) ==0) {\n if (++tmp == k)\n out.insert(idx);\n }\n if (++idx >=n) idx=0;\n }\n \n }\n for (int i=0;i< n;i++) {\n if (out.count(i) ==0 )\n return i+1;\n }\n return 0;\n }\n};",
"memory": "9800"
} |
1,951 | <p>There are <code>n</code> friends that are playing a game. The friends are sitting in a circle and are numbered from <code>1</code> to <code>n</code> in <strong>clockwise order</strong>. More formally, moving clockwise from the <code>i<sup>th</sup></code> friend brings you to the <code>(i+1)<sup>th</sup></code> friend for <code>1 <= i < n</code>, and moving clockwise from the <code>n<sup>th</sup></code> friend brings you to the <code>1<sup>st</sup></code> friend.</p>
<p>The rules of the game are as follows:</p>
<ol>
<li><strong>Start</strong> at the <code>1<sup>st</sup></code> friend.</li>
<li>Count the next <code>k</code> friends in the clockwise direction <strong>including</strong> the friend you started at. The counting wraps around the circle and may count some friends more than once.</li>
<li>The last friend you counted leaves the circle and loses the game.</li>
<li>If there is still more than one friend in the circle, go back to step <code>2</code> <strong>starting</strong> from the friend <strong>immediately clockwise</strong> of the friend who just lost and repeat.</li>
<li>Else, the last friend in the circle wins the game.</li>
</ol>
<p>Given the number of friends, <code>n</code>, and an integer <code>k</code>, return <em>the winner of the game</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/25/ic234-q2-ex11.png" style="width: 500px; height: 345px;" />
<pre>
<strong>Input:</strong> n = 5, k = 2
<strong>Output:</strong> 3
<strong>Explanation:</strong> Here are the steps of the game:
1) Start at friend 1.
2) Count 2 friends clockwise, which are friends 1 and 2.
3) Friend 2 leaves the circle. Next start is friend 3.
4) Count 2 friends clockwise, which are friends 3 and 4.
5) Friend 4 leaves the circle. Next start is friend 5.
6) Count 2 friends clockwise, which are friends 5 and 1.
7) Friend 1 leaves the circle. Next start is friend 3.
8) Count 2 friends clockwise, which are friends 3 and 5.
9) Friend 5 leaves the circle. Only friend 3 is left, so they are the winner.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 6, k = 5
<strong>Output:</strong> 1
<strong>Explanation:</strong> The friends leave in this order: 5, 4, 6, 2, 3. The winner is friend 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= n <= 500</code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<p>Could you solve this problem in linear time with constant space?</p>
| 2 | {
"code": "class Solution {\npublic:\n int findTheWinner(int n, int k) {\n unordered_map<int,int> m;\n int s = 0;\n int cnt = 0;\n while(m.size()<n-1){\n int x = s%n+1;\n if(m.find(x)==m.end()) cnt++;\n if(cnt==k){\n cout<<x<<endl;\n m[x] = 1;\n cnt=0;\n }\n s++;\n }\n for(int i=1;i<=n;i++) if(!m[i]) return i;\n return 1;\n }\n};",
"memory": "9900"
} |
1,951 | <p>There are <code>n</code> friends that are playing a game. The friends are sitting in a circle and are numbered from <code>1</code> to <code>n</code> in <strong>clockwise order</strong>. More formally, moving clockwise from the <code>i<sup>th</sup></code> friend brings you to the <code>(i+1)<sup>th</sup></code> friend for <code>1 <= i < n</code>, and moving clockwise from the <code>n<sup>th</sup></code> friend brings you to the <code>1<sup>st</sup></code> friend.</p>
<p>The rules of the game are as follows:</p>
<ol>
<li><strong>Start</strong> at the <code>1<sup>st</sup></code> friend.</li>
<li>Count the next <code>k</code> friends in the clockwise direction <strong>including</strong> the friend you started at. The counting wraps around the circle and may count some friends more than once.</li>
<li>The last friend you counted leaves the circle and loses the game.</li>
<li>If there is still more than one friend in the circle, go back to step <code>2</code> <strong>starting</strong> from the friend <strong>immediately clockwise</strong> of the friend who just lost and repeat.</li>
<li>Else, the last friend in the circle wins the game.</li>
</ol>
<p>Given the number of friends, <code>n</code>, and an integer <code>k</code>, return <em>the winner of the game</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/25/ic234-q2-ex11.png" style="width: 500px; height: 345px;" />
<pre>
<strong>Input:</strong> n = 5, k = 2
<strong>Output:</strong> 3
<strong>Explanation:</strong> Here are the steps of the game:
1) Start at friend 1.
2) Count 2 friends clockwise, which are friends 1 and 2.
3) Friend 2 leaves the circle. Next start is friend 3.
4) Count 2 friends clockwise, which are friends 3 and 4.
5) Friend 4 leaves the circle. Next start is friend 5.
6) Count 2 friends clockwise, which are friends 5 and 1.
7) Friend 1 leaves the circle. Next start is friend 3.
8) Count 2 friends clockwise, which are friends 3 and 5.
9) Friend 5 leaves the circle. Only friend 3 is left, so they are the winner.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 6, k = 5
<strong>Output:</strong> 1
<strong>Explanation:</strong> The friends leave in this order: 5, 4, 6, 2, 3. The winner is friend 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= n <= 500</code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<p>Could you solve this problem in linear time with constant space?</p>
| 2 | {
"code": "class Solution {\npublic:\n int findTheWinner(int n, int k) {\n int N=n;\n vector<int>V;\n for(int i=1;i<=n;i++)\n {\n V.push_back(i);\n }\n map<int,int>M;\n int count=0;\n // int start=k-1;\n // while(1)\n // {\n // if(M.size()<n-1)\n // {\n int ans;\n if(k!=1)\n {\n while(1)\n {\n for(int i=0;i<N;i++)\n {\n if(M.find(V[i])==M.end())\n {\n count++;\n if(count%(k)==0)\n {\n M[V[i]]++;\n cout<<V[i]<<\" \";\n count=0;\n if(M.size()==n-1)\n {\n break;\n }\n }\n }\n else\n {\n continue;\n }\n if(i==N-1)\n {\n i=-1;\n }\n }\n if(M.size()==N-1){break;}\n }\n cout<<endl;\n for(int i=1;i<=N;i++)\n {\n if(M.find(i)==M.end())\n {\n ans=i;\n break;\n }\n }\n for(auto it : M)\n {\n cout<<it.first<<\" \";\n }\n cout<<endl;\n cout<<M.size()<<endl;\n }\n else\n {\n ans=N;\n } \n // else\n // {\n // break;\n // }\n // }\n return ans;\n }\n};",
"memory": "11300"
} |
1,951 | <p>There are <code>n</code> friends that are playing a game. The friends are sitting in a circle and are numbered from <code>1</code> to <code>n</code> in <strong>clockwise order</strong>. More formally, moving clockwise from the <code>i<sup>th</sup></code> friend brings you to the <code>(i+1)<sup>th</sup></code> friend for <code>1 <= i < n</code>, and moving clockwise from the <code>n<sup>th</sup></code> friend brings you to the <code>1<sup>st</sup></code> friend.</p>
<p>The rules of the game are as follows:</p>
<ol>
<li><strong>Start</strong> at the <code>1<sup>st</sup></code> friend.</li>
<li>Count the next <code>k</code> friends in the clockwise direction <strong>including</strong> the friend you started at. The counting wraps around the circle and may count some friends more than once.</li>
<li>The last friend you counted leaves the circle and loses the game.</li>
<li>If there is still more than one friend in the circle, go back to step <code>2</code> <strong>starting</strong> from the friend <strong>immediately clockwise</strong> of the friend who just lost and repeat.</li>
<li>Else, the last friend in the circle wins the game.</li>
</ol>
<p>Given the number of friends, <code>n</code>, and an integer <code>k</code>, return <em>the winner of the game</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/25/ic234-q2-ex11.png" style="width: 500px; height: 345px;" />
<pre>
<strong>Input:</strong> n = 5, k = 2
<strong>Output:</strong> 3
<strong>Explanation:</strong> Here are the steps of the game:
1) Start at friend 1.
2) Count 2 friends clockwise, which are friends 1 and 2.
3) Friend 2 leaves the circle. Next start is friend 3.
4) Count 2 friends clockwise, which are friends 3 and 4.
5) Friend 4 leaves the circle. Next start is friend 5.
6) Count 2 friends clockwise, which are friends 5 and 1.
7) Friend 1 leaves the circle. Next start is friend 3.
8) Count 2 friends clockwise, which are friends 3 and 5.
9) Friend 5 leaves the circle. Only friend 3 is left, so they are the winner.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 6, k = 5
<strong>Output:</strong> 1
<strong>Explanation:</strong> The friends leave in this order: 5, 4, 6, 2, 3. The winner is friend 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= n <= 500</code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<p>Could you solve this problem in linear time with constant space?</p>
| 2 | {
"code": "#include <ext/pb_ds/assoc_container.hpp>\n#include <ext/pb_ds/tree_policy.hpp>\n\nusing namespace __gnu_pbds;\n#define ll long long\n\n\ntemplate<class T> using ordered_set =tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update> ;\n\nclass Solution {\npublic:\n int findTheWinner(int n, int k) \n {\n ordered_set<int> q1;\n for(int j=1;j<=n;j++)\n {\n q1.insert(j);\n }\n ll posi=(k-1)%n;\n vector<ll>vec1;\n while(!q1.empty())\n {\n ll ele=*q1.find_by_order(posi);\n // cout<<ele<<\" \";\n q1.erase(ele);\n vec1.push_back(ele);\n if(!q1.empty())\n {\n posi=(posi+k-1)%(q1.size());\n }\n }\n return vec1[vec1.size()-1];\n // cout<<endl;\n }\n};",
"memory": "11600"
} |
1,951 | <p>There are <code>n</code> friends that are playing a game. The friends are sitting in a circle and are numbered from <code>1</code> to <code>n</code> in <strong>clockwise order</strong>. More formally, moving clockwise from the <code>i<sup>th</sup></code> friend brings you to the <code>(i+1)<sup>th</sup></code> friend for <code>1 <= i < n</code>, and moving clockwise from the <code>n<sup>th</sup></code> friend brings you to the <code>1<sup>st</sup></code> friend.</p>
<p>The rules of the game are as follows:</p>
<ol>
<li><strong>Start</strong> at the <code>1<sup>st</sup></code> friend.</li>
<li>Count the next <code>k</code> friends in the clockwise direction <strong>including</strong> the friend you started at. The counting wraps around the circle and may count some friends more than once.</li>
<li>The last friend you counted leaves the circle and loses the game.</li>
<li>If there is still more than one friend in the circle, go back to step <code>2</code> <strong>starting</strong> from the friend <strong>immediately clockwise</strong> of the friend who just lost and repeat.</li>
<li>Else, the last friend in the circle wins the game.</li>
</ol>
<p>Given the number of friends, <code>n</code>, and an integer <code>k</code>, return <em>the winner of the game</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/25/ic234-q2-ex11.png" style="width: 500px; height: 345px;" />
<pre>
<strong>Input:</strong> n = 5, k = 2
<strong>Output:</strong> 3
<strong>Explanation:</strong> Here are the steps of the game:
1) Start at friend 1.
2) Count 2 friends clockwise, which are friends 1 and 2.
3) Friend 2 leaves the circle. Next start is friend 3.
4) Count 2 friends clockwise, which are friends 3 and 4.
5) Friend 4 leaves the circle. Next start is friend 5.
6) Count 2 friends clockwise, which are friends 5 and 1.
7) Friend 1 leaves the circle. Next start is friend 3.
8) Count 2 friends clockwise, which are friends 3 and 5.
9) Friend 5 leaves the circle. Only friend 3 is left, so they are the winner.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 6, k = 5
<strong>Output:</strong> 1
<strong>Explanation:</strong> The friends leave in this order: 5, 4, 6, 2, 3. The winner is friend 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= n <= 500</code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<p>Could you solve this problem in linear time with constant space?</p>
| 2 | {
"code": "class Solution {\npublic:\n int findTheWinner(int n, int k) {\n deque<int> dq;\n for(int i=1;i<=n;i++)\n {\n dq.push_back(i);\n }\n int i=0;\n while(dq.size()!=1)\n {\n i=(i+k-1)%dq.size();\n dq.erase(dq.begin()+i);\n \n }\n return dq[0];\n }\n};",
"memory": "12200"
} |
1,951 | <p>There are <code>n</code> friends that are playing a game. The friends are sitting in a circle and are numbered from <code>1</code> to <code>n</code> in <strong>clockwise order</strong>. More formally, moving clockwise from the <code>i<sup>th</sup></code> friend brings you to the <code>(i+1)<sup>th</sup></code> friend for <code>1 <= i < n</code>, and moving clockwise from the <code>n<sup>th</sup></code> friend brings you to the <code>1<sup>st</sup></code> friend.</p>
<p>The rules of the game are as follows:</p>
<ol>
<li><strong>Start</strong> at the <code>1<sup>st</sup></code> friend.</li>
<li>Count the next <code>k</code> friends in the clockwise direction <strong>including</strong> the friend you started at. The counting wraps around the circle and may count some friends more than once.</li>
<li>The last friend you counted leaves the circle and loses the game.</li>
<li>If there is still more than one friend in the circle, go back to step <code>2</code> <strong>starting</strong> from the friend <strong>immediately clockwise</strong> of the friend who just lost and repeat.</li>
<li>Else, the last friend in the circle wins the game.</li>
</ol>
<p>Given the number of friends, <code>n</code>, and an integer <code>k</code>, return <em>the winner of the game</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/25/ic234-q2-ex11.png" style="width: 500px; height: 345px;" />
<pre>
<strong>Input:</strong> n = 5, k = 2
<strong>Output:</strong> 3
<strong>Explanation:</strong> Here are the steps of the game:
1) Start at friend 1.
2) Count 2 friends clockwise, which are friends 1 and 2.
3) Friend 2 leaves the circle. Next start is friend 3.
4) Count 2 friends clockwise, which are friends 3 and 4.
5) Friend 4 leaves the circle. Next start is friend 5.
6) Count 2 friends clockwise, which are friends 5 and 1.
7) Friend 1 leaves the circle. Next start is friend 3.
8) Count 2 friends clockwise, which are friends 3 and 5.
9) Friend 5 leaves the circle. Only friend 3 is left, so they are the winner.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 6, k = 5
<strong>Output:</strong> 1
<strong>Explanation:</strong> The friends leave in this order: 5, 4, 6, 2, 3. The winner is friend 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= n <= 500</code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<p>Could you solve this problem in linear time with constant space?</p>
| 2 | {
"code": "class Solution {\npublic:\n int findTheWinner(int n, int k) {\n deque<int> dq;\n for(int i=1;i<=n;i++)\n {\n dq.push_back(i);\n }\n int i=0;\n while(dq.size()!=1)\n {\n i=(i+k-1)%dq.size();\n dq.erase(dq.begin()+i);\n \n }\n return dq[0];\n }\n};",
"memory": "12200"
} |
1,951 | <p>There are <code>n</code> friends that are playing a game. The friends are sitting in a circle and are numbered from <code>1</code> to <code>n</code> in <strong>clockwise order</strong>. More formally, moving clockwise from the <code>i<sup>th</sup></code> friend brings you to the <code>(i+1)<sup>th</sup></code> friend for <code>1 <= i < n</code>, and moving clockwise from the <code>n<sup>th</sup></code> friend brings you to the <code>1<sup>st</sup></code> friend.</p>
<p>The rules of the game are as follows:</p>
<ol>
<li><strong>Start</strong> at the <code>1<sup>st</sup></code> friend.</li>
<li>Count the next <code>k</code> friends in the clockwise direction <strong>including</strong> the friend you started at. The counting wraps around the circle and may count some friends more than once.</li>
<li>The last friend you counted leaves the circle and loses the game.</li>
<li>If there is still more than one friend in the circle, go back to step <code>2</code> <strong>starting</strong> from the friend <strong>immediately clockwise</strong> of the friend who just lost and repeat.</li>
<li>Else, the last friend in the circle wins the game.</li>
</ol>
<p>Given the number of friends, <code>n</code>, and an integer <code>k</code>, return <em>the winner of the game</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/25/ic234-q2-ex11.png" style="width: 500px; height: 345px;" />
<pre>
<strong>Input:</strong> n = 5, k = 2
<strong>Output:</strong> 3
<strong>Explanation:</strong> Here are the steps of the game:
1) Start at friend 1.
2) Count 2 friends clockwise, which are friends 1 and 2.
3) Friend 2 leaves the circle. Next start is friend 3.
4) Count 2 friends clockwise, which are friends 3 and 4.
5) Friend 4 leaves the circle. Next start is friend 5.
6) Count 2 friends clockwise, which are friends 5 and 1.
7) Friend 1 leaves the circle. Next start is friend 3.
8) Count 2 friends clockwise, which are friends 3 and 5.
9) Friend 5 leaves the circle. Only friend 3 is left, so they are the winner.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 6, k = 5
<strong>Output:</strong> 1
<strong>Explanation:</strong> The friends leave in this order: 5, 4, 6, 2, 3. The winner is friend 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= n <= 500</code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<p>Could you solve this problem in linear time with constant space?</p>
| 2 | {
"code": "class Solution {\npublic:\nint winner(vector<bool>person,int n,int index,int person_left,int k)\n{\n if(person_left==1)\n {\n for(int i=0;i<n;i++)\n {\n if(person[i]==0)\n {\n return i+1;\n }\n }\n }\n\n int eliminate=(k-1)%person_left;\n\n while(eliminate--)\n {\n index=(index+1)%n;\n while(person[index]==true)\n {\n index=(index+1)%n;\n }\n \n }\n person[index]=1;\n\n while(person[index]==true)\n {\n index=(index+1)%n;\n }\n return winner(person,n,index,person_left-1,k);\n}\n int findTheWinner(int n, int k) {\n vector<bool>person(n,false);\n return winner(person,n,0,n,k);\n }\n};",
"memory": "12600"
} |
1,951 | <p>There are <code>n</code> friends that are playing a game. The friends are sitting in a circle and are numbered from <code>1</code> to <code>n</code> in <strong>clockwise order</strong>. More formally, moving clockwise from the <code>i<sup>th</sup></code> friend brings you to the <code>(i+1)<sup>th</sup></code> friend for <code>1 <= i < n</code>, and moving clockwise from the <code>n<sup>th</sup></code> friend brings you to the <code>1<sup>st</sup></code> friend.</p>
<p>The rules of the game are as follows:</p>
<ol>
<li><strong>Start</strong> at the <code>1<sup>st</sup></code> friend.</li>
<li>Count the next <code>k</code> friends in the clockwise direction <strong>including</strong> the friend you started at. The counting wraps around the circle and may count some friends more than once.</li>
<li>The last friend you counted leaves the circle and loses the game.</li>
<li>If there is still more than one friend in the circle, go back to step <code>2</code> <strong>starting</strong> from the friend <strong>immediately clockwise</strong> of the friend who just lost and repeat.</li>
<li>Else, the last friend in the circle wins the game.</li>
</ol>
<p>Given the number of friends, <code>n</code>, and an integer <code>k</code>, return <em>the winner of the game</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/25/ic234-q2-ex11.png" style="width: 500px; height: 345px;" />
<pre>
<strong>Input:</strong> n = 5, k = 2
<strong>Output:</strong> 3
<strong>Explanation:</strong> Here are the steps of the game:
1) Start at friend 1.
2) Count 2 friends clockwise, which are friends 1 and 2.
3) Friend 2 leaves the circle. Next start is friend 3.
4) Count 2 friends clockwise, which are friends 3 and 4.
5) Friend 4 leaves the circle. Next start is friend 5.
6) Count 2 friends clockwise, which are friends 5 and 1.
7) Friend 1 leaves the circle. Next start is friend 3.
8) Count 2 friends clockwise, which are friends 3 and 5.
9) Friend 5 leaves the circle. Only friend 3 is left, so they are the winner.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 6, k = 5
<strong>Output:</strong> 1
<strong>Explanation:</strong> The friends leave in this order: 5, 4, 6, 2, 3. The winner is friend 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= n <= 500</code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<p>Could you solve this problem in linear time with constant space?</p>
| 2 | {
"code": "class Solution {\npublic:\n int winner(vector<bool>person, int n ,int index, int person_left, int k)\n {\n if(person_left==1)\n {\n for(int i=0; i<n; i++)\n {\n if(person[i]==0)\n return i;\n }\n }\n int kill=(k-1)%person_left;\n while(kill--){\n index=(index+1)%n;\n\n while(person[index]==1)\n index=(index+1)%n; \n\n }\n person[index]=1;\n while(person[index]==1)\n index= (index+1)%n;\n\n return winner(person, n, index,person_left-1,k);\n\n }\n int findTheWinner(int n, int k) {\n vector<bool>person(n, 0);\n return winner(person, n, 0,n ,k)+1;\n }\n};",
"memory": "12700"
} |
1,951 | <p>There are <code>n</code> friends that are playing a game. The friends are sitting in a circle and are numbered from <code>1</code> to <code>n</code> in <strong>clockwise order</strong>. More formally, moving clockwise from the <code>i<sup>th</sup></code> friend brings you to the <code>(i+1)<sup>th</sup></code> friend for <code>1 <= i < n</code>, and moving clockwise from the <code>n<sup>th</sup></code> friend brings you to the <code>1<sup>st</sup></code> friend.</p>
<p>The rules of the game are as follows:</p>
<ol>
<li><strong>Start</strong> at the <code>1<sup>st</sup></code> friend.</li>
<li>Count the next <code>k</code> friends in the clockwise direction <strong>including</strong> the friend you started at. The counting wraps around the circle and may count some friends more than once.</li>
<li>The last friend you counted leaves the circle and loses the game.</li>
<li>If there is still more than one friend in the circle, go back to step <code>2</code> <strong>starting</strong> from the friend <strong>immediately clockwise</strong> of the friend who just lost and repeat.</li>
<li>Else, the last friend in the circle wins the game.</li>
</ol>
<p>Given the number of friends, <code>n</code>, and an integer <code>k</code>, return <em>the winner of the game</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/25/ic234-q2-ex11.png" style="width: 500px; height: 345px;" />
<pre>
<strong>Input:</strong> n = 5, k = 2
<strong>Output:</strong> 3
<strong>Explanation:</strong> Here are the steps of the game:
1) Start at friend 1.
2) Count 2 friends clockwise, which are friends 1 and 2.
3) Friend 2 leaves the circle. Next start is friend 3.
4) Count 2 friends clockwise, which are friends 3 and 4.
5) Friend 4 leaves the circle. Next start is friend 5.
6) Count 2 friends clockwise, which are friends 5 and 1.
7) Friend 1 leaves the circle. Next start is friend 3.
8) Count 2 friends clockwise, which are friends 3 and 5.
9) Friend 5 leaves the circle. Only friend 3 is left, so they are the winner.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 6, k = 5
<strong>Output:</strong> 1
<strong>Explanation:</strong> The friends leave in this order: 5, 4, 6, 2, 3. The winner is friend 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= n <= 500</code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<p>Could you solve this problem in linear time with constant space?</p>
| 2 | {
"code": "class Solution {\npublic:\n int winner(vector<bool>player,int n, int k, int index,int left){\n if(left == 1)\n {\n for(int i = 0; i < n; i++)\n if(player[i] == 0)\n return i;\n }\n int kill = (k-1)%left;\n while(kill--){\n index = (index + 1) % n; \n while(player[index])\n index = (index + 1) % n; \n }\n player[index] = 1;\n while(player[index])\n index = (index + 1) % n;\n return winner(player,n,k,index,left-1);\n }\n int findTheWinner(int n, int k) {\n vector<bool>player(n,0);\n return winner(player,n,k,0,n) + 1;\n }\n};",
"memory": "12800"
} |
1,951 | <p>There are <code>n</code> friends that are playing a game. The friends are sitting in a circle and are numbered from <code>1</code> to <code>n</code> in <strong>clockwise order</strong>. More formally, moving clockwise from the <code>i<sup>th</sup></code> friend brings you to the <code>(i+1)<sup>th</sup></code> friend for <code>1 <= i < n</code>, and moving clockwise from the <code>n<sup>th</sup></code> friend brings you to the <code>1<sup>st</sup></code> friend.</p>
<p>The rules of the game are as follows:</p>
<ol>
<li><strong>Start</strong> at the <code>1<sup>st</sup></code> friend.</li>
<li>Count the next <code>k</code> friends in the clockwise direction <strong>including</strong> the friend you started at. The counting wraps around the circle and may count some friends more than once.</li>
<li>The last friend you counted leaves the circle and loses the game.</li>
<li>If there is still more than one friend in the circle, go back to step <code>2</code> <strong>starting</strong> from the friend <strong>immediately clockwise</strong> of the friend who just lost and repeat.</li>
<li>Else, the last friend in the circle wins the game.</li>
</ol>
<p>Given the number of friends, <code>n</code>, and an integer <code>k</code>, return <em>the winner of the game</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/25/ic234-q2-ex11.png" style="width: 500px; height: 345px;" />
<pre>
<strong>Input:</strong> n = 5, k = 2
<strong>Output:</strong> 3
<strong>Explanation:</strong> Here are the steps of the game:
1) Start at friend 1.
2) Count 2 friends clockwise, which are friends 1 and 2.
3) Friend 2 leaves the circle. Next start is friend 3.
4) Count 2 friends clockwise, which are friends 3 and 4.
5) Friend 4 leaves the circle. Next start is friend 5.
6) Count 2 friends clockwise, which are friends 5 and 1.
7) Friend 1 leaves the circle. Next start is friend 3.
8) Count 2 friends clockwise, which are friends 3 and 5.
9) Friend 5 leaves the circle. Only friend 3 is left, so they are the winner.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 6, k = 5
<strong>Output:</strong> 1
<strong>Explanation:</strong> The friends leave in this order: 5, 4, 6, 2, 3. The winner is friend 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= n <= 500</code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<p>Could you solve this problem in linear time with constant space?</p>
| 2 | {
"code": "class Solution {\npublic:\n int winner(vector<bool>player,int n, int k, int index,int left){\n if(left == 1)\n {\n for(int i = 0; i < n; i++)\n if(player[i] == 0)\n return i;\n }\n int kill = (k-1)%left;\n while(kill--){\n index = (index + 1) % n; \n while(player[index])\n index = (index + 1) % n; \n }\n player[index] = 1;\n while(player[index])\n index = (index + 1) % n;\n return winner(player,n,k,index,left-1);\n }\n int findTheWinner(int n, int k) {\n vector<bool>player(n,0);\n return winner(player,n,k,0,n) + 1;\n }\n};",
"memory": "12900"
} |
1,951 | <p>There are <code>n</code> friends that are playing a game. The friends are sitting in a circle and are numbered from <code>1</code> to <code>n</code> in <strong>clockwise order</strong>. More formally, moving clockwise from the <code>i<sup>th</sup></code> friend brings you to the <code>(i+1)<sup>th</sup></code> friend for <code>1 <= i < n</code>, and moving clockwise from the <code>n<sup>th</sup></code> friend brings you to the <code>1<sup>st</sup></code> friend.</p>
<p>The rules of the game are as follows:</p>
<ol>
<li><strong>Start</strong> at the <code>1<sup>st</sup></code> friend.</li>
<li>Count the next <code>k</code> friends in the clockwise direction <strong>including</strong> the friend you started at. The counting wraps around the circle and may count some friends more than once.</li>
<li>The last friend you counted leaves the circle and loses the game.</li>
<li>If there is still more than one friend in the circle, go back to step <code>2</code> <strong>starting</strong> from the friend <strong>immediately clockwise</strong> of the friend who just lost and repeat.</li>
<li>Else, the last friend in the circle wins the game.</li>
</ol>
<p>Given the number of friends, <code>n</code>, and an integer <code>k</code>, return <em>the winner of the game</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/25/ic234-q2-ex11.png" style="width: 500px; height: 345px;" />
<pre>
<strong>Input:</strong> n = 5, k = 2
<strong>Output:</strong> 3
<strong>Explanation:</strong> Here are the steps of the game:
1) Start at friend 1.
2) Count 2 friends clockwise, which are friends 1 and 2.
3) Friend 2 leaves the circle. Next start is friend 3.
4) Count 2 friends clockwise, which are friends 3 and 4.
5) Friend 4 leaves the circle. Next start is friend 5.
6) Count 2 friends clockwise, which are friends 5 and 1.
7) Friend 1 leaves the circle. Next start is friend 3.
8) Count 2 friends clockwise, which are friends 3 and 5.
9) Friend 5 leaves the circle. Only friend 3 is left, so they are the winner.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 6, k = 5
<strong>Output:</strong> 1
<strong>Explanation:</strong> The friends leave in this order: 5, 4, 6, 2, 3. The winner is friend 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= n <= 500</code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<p>Could you solve this problem in linear time with constant space?</p>
| 2 | {
"code": "class Solution {\npublic:\n int findTheWinner(int n, int k) {\n queue<int> q;\n for (int i = 1; i <=n; i++) {\n q.push(i);\n }\n while(n>1){\n for(int i=0;i<(k%n-1+n)%n;i++){\n q.push(q.front());\n q.pop(); \n }\n q.pop();\n n--;\n }\n return q.front();\n }\n};",
"memory": "14400"
} |
1,951 | <p>There are <code>n</code> friends that are playing a game. The friends are sitting in a circle and are numbered from <code>1</code> to <code>n</code> in <strong>clockwise order</strong>. More formally, moving clockwise from the <code>i<sup>th</sup></code> friend brings you to the <code>(i+1)<sup>th</sup></code> friend for <code>1 <= i < n</code>, and moving clockwise from the <code>n<sup>th</sup></code> friend brings you to the <code>1<sup>st</sup></code> friend.</p>
<p>The rules of the game are as follows:</p>
<ol>
<li><strong>Start</strong> at the <code>1<sup>st</sup></code> friend.</li>
<li>Count the next <code>k</code> friends in the clockwise direction <strong>including</strong> the friend you started at. The counting wraps around the circle and may count some friends more than once.</li>
<li>The last friend you counted leaves the circle and loses the game.</li>
<li>If there is still more than one friend in the circle, go back to step <code>2</code> <strong>starting</strong> from the friend <strong>immediately clockwise</strong> of the friend who just lost and repeat.</li>
<li>Else, the last friend in the circle wins the game.</li>
</ol>
<p>Given the number of friends, <code>n</code>, and an integer <code>k</code>, return <em>the winner of the game</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/25/ic234-q2-ex11.png" style="width: 500px; height: 345px;" />
<pre>
<strong>Input:</strong> n = 5, k = 2
<strong>Output:</strong> 3
<strong>Explanation:</strong> Here are the steps of the game:
1) Start at friend 1.
2) Count 2 friends clockwise, which are friends 1 and 2.
3) Friend 2 leaves the circle. Next start is friend 3.
4) Count 2 friends clockwise, which are friends 3 and 4.
5) Friend 4 leaves the circle. Next start is friend 5.
6) Count 2 friends clockwise, which are friends 5 and 1.
7) Friend 1 leaves the circle. Next start is friend 3.
8) Count 2 friends clockwise, which are friends 3 and 5.
9) Friend 5 leaves the circle. Only friend 3 is left, so they are the winner.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 6, k = 5
<strong>Output:</strong> 1
<strong>Explanation:</strong> The friends leave in this order: 5, 4, 6, 2, 3. The winner is friend 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= n <= 500</code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<p>Could you solve this problem in linear time with constant space?</p>
| 2 | {
"code": "class Solution {\npublic:\n int findTheWinner(int n, int k) {\n queue<int> q;\n for(int i = 1; i <= n; i++)\n q.push(i);\n while(q.size() > 1){\n int m = k % q.size();\n if(m == 0) m = q.size();\n int i = 1;\n while(i < m){\n q.push(q.front());\n q.pop();\n i++;\n }\n q.pop();\n }\n return q.front();\n }\n};",
"memory": "14500"
} |
1,951 | <p>There are <code>n</code> friends that are playing a game. The friends are sitting in a circle and are numbered from <code>1</code> to <code>n</code> in <strong>clockwise order</strong>. More formally, moving clockwise from the <code>i<sup>th</sup></code> friend brings you to the <code>(i+1)<sup>th</sup></code> friend for <code>1 <= i < n</code>, and moving clockwise from the <code>n<sup>th</sup></code> friend brings you to the <code>1<sup>st</sup></code> friend.</p>
<p>The rules of the game are as follows:</p>
<ol>
<li><strong>Start</strong> at the <code>1<sup>st</sup></code> friend.</li>
<li>Count the next <code>k</code> friends in the clockwise direction <strong>including</strong> the friend you started at. The counting wraps around the circle and may count some friends more than once.</li>
<li>The last friend you counted leaves the circle and loses the game.</li>
<li>If there is still more than one friend in the circle, go back to step <code>2</code> <strong>starting</strong> from the friend <strong>immediately clockwise</strong> of the friend who just lost and repeat.</li>
<li>Else, the last friend in the circle wins the game.</li>
</ol>
<p>Given the number of friends, <code>n</code>, and an integer <code>k</code>, return <em>the winner of the game</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/25/ic234-q2-ex11.png" style="width: 500px; height: 345px;" />
<pre>
<strong>Input:</strong> n = 5, k = 2
<strong>Output:</strong> 3
<strong>Explanation:</strong> Here are the steps of the game:
1) Start at friend 1.
2) Count 2 friends clockwise, which are friends 1 and 2.
3) Friend 2 leaves the circle. Next start is friend 3.
4) Count 2 friends clockwise, which are friends 3 and 4.
5) Friend 4 leaves the circle. Next start is friend 5.
6) Count 2 friends clockwise, which are friends 5 and 1.
7) Friend 1 leaves the circle. Next start is friend 3.
8) Count 2 friends clockwise, which are friends 3 and 5.
9) Friend 5 leaves the circle. Only friend 3 is left, so they are the winner.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 6, k = 5
<strong>Output:</strong> 1
<strong>Explanation:</strong> The friends leave in this order: 5, 4, 6, 2, 3. The winner is friend 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= n <= 500</code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<p>Could you solve this problem in linear time with constant space?</p>
| 2 | {
"code": "class Solution {\npublic:\n int findTheWinner(int n, int k) {\n queue<int> q;\n for(int i = 1; i <= n; i++){\n q.push(i);\n }\n int a = k;\n while(q.size() >= 2){\n if(k % q.size() == 0){\n a = k;\n }\n else{\n a = k % q.size();\n }\n for(int i = 1; i <= a - 1; i++){\n int elem = q.front();\n q.pop();\n q.push(elem);\n }\n q.pop();\n }\n return q.front();\n }\n};",
"memory": "14600"
} |
1,951 | <p>There are <code>n</code> friends that are playing a game. The friends are sitting in a circle and are numbered from <code>1</code> to <code>n</code> in <strong>clockwise order</strong>. More formally, moving clockwise from the <code>i<sup>th</sup></code> friend brings you to the <code>(i+1)<sup>th</sup></code> friend for <code>1 <= i < n</code>, and moving clockwise from the <code>n<sup>th</sup></code> friend brings you to the <code>1<sup>st</sup></code> friend.</p>
<p>The rules of the game are as follows:</p>
<ol>
<li><strong>Start</strong> at the <code>1<sup>st</sup></code> friend.</li>
<li>Count the next <code>k</code> friends in the clockwise direction <strong>including</strong> the friend you started at. The counting wraps around the circle and may count some friends more than once.</li>
<li>The last friend you counted leaves the circle and loses the game.</li>
<li>If there is still more than one friend in the circle, go back to step <code>2</code> <strong>starting</strong> from the friend <strong>immediately clockwise</strong> of the friend who just lost and repeat.</li>
<li>Else, the last friend in the circle wins the game.</li>
</ol>
<p>Given the number of friends, <code>n</code>, and an integer <code>k</code>, return <em>the winner of the game</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/25/ic234-q2-ex11.png" style="width: 500px; height: 345px;" />
<pre>
<strong>Input:</strong> n = 5, k = 2
<strong>Output:</strong> 3
<strong>Explanation:</strong> Here are the steps of the game:
1) Start at friend 1.
2) Count 2 friends clockwise, which are friends 1 and 2.
3) Friend 2 leaves the circle. Next start is friend 3.
4) Count 2 friends clockwise, which are friends 3 and 4.
5) Friend 4 leaves the circle. Next start is friend 5.
6) Count 2 friends clockwise, which are friends 5 and 1.
7) Friend 1 leaves the circle. Next start is friend 3.
8) Count 2 friends clockwise, which are friends 3 and 5.
9) Friend 5 leaves the circle. Only friend 3 is left, so they are the winner.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 6, k = 5
<strong>Output:</strong> 1
<strong>Explanation:</strong> The friends leave in this order: 5, 4, 6, 2, 3. The winner is friend 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= n <= 500</code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<p>Could you solve this problem in linear time with constant space?</p>
| 2 | {
"code": "class Solution {\npublic:\n int findTheWinner(int n, int k) {\n queue<int> q;\n for(int i = 1; i <= n; i++){\n q.push(i);\n }\n int a = k;\n while(q.size() >= 2){\n /*if(k > q.size()){\n a = k % q.size();\n }*/\n if(k % q.size() != 0){\n a = k % q.size();\n }\n else{\n a = k;\n }\n for(int i = 1; i <= a - 1; i++){\n int elem = q.front();\n q.pop();\n q.push(elem);\n }\n q.pop();\n }\n return q.front();\n }\n};",
"memory": "14700"
} |
1,951 | <p>There are <code>n</code> friends that are playing a game. The friends are sitting in a circle and are numbered from <code>1</code> to <code>n</code> in <strong>clockwise order</strong>. More formally, moving clockwise from the <code>i<sup>th</sup></code> friend brings you to the <code>(i+1)<sup>th</sup></code> friend for <code>1 <= i < n</code>, and moving clockwise from the <code>n<sup>th</sup></code> friend brings you to the <code>1<sup>st</sup></code> friend.</p>
<p>The rules of the game are as follows:</p>
<ol>
<li><strong>Start</strong> at the <code>1<sup>st</sup></code> friend.</li>
<li>Count the next <code>k</code> friends in the clockwise direction <strong>including</strong> the friend you started at. The counting wraps around the circle and may count some friends more than once.</li>
<li>The last friend you counted leaves the circle and loses the game.</li>
<li>If there is still more than one friend in the circle, go back to step <code>2</code> <strong>starting</strong> from the friend <strong>immediately clockwise</strong> of the friend who just lost and repeat.</li>
<li>Else, the last friend in the circle wins the game.</li>
</ol>
<p>Given the number of friends, <code>n</code>, and an integer <code>k</code>, return <em>the winner of the game</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/25/ic234-q2-ex11.png" style="width: 500px; height: 345px;" />
<pre>
<strong>Input:</strong> n = 5, k = 2
<strong>Output:</strong> 3
<strong>Explanation:</strong> Here are the steps of the game:
1) Start at friend 1.
2) Count 2 friends clockwise, which are friends 1 and 2.
3) Friend 2 leaves the circle. Next start is friend 3.
4) Count 2 friends clockwise, which are friends 3 and 4.
5) Friend 4 leaves the circle. Next start is friend 5.
6) Count 2 friends clockwise, which are friends 5 and 1.
7) Friend 1 leaves the circle. Next start is friend 3.
8) Count 2 friends clockwise, which are friends 3 and 5.
9) Friend 5 leaves the circle. Only friend 3 is left, so they are the winner.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 6, k = 5
<strong>Output:</strong> 1
<strong>Explanation:</strong> The friends leave in this order: 5, 4, 6, 2, 3. The winner is friend 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= n <= 500</code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<p>Could you solve this problem in linear time with constant space?</p>
| 2 | {
"code": "class Solution {\npublic:\n int findTheWinner(int n, int k) {\n queue<int> q;\n for (int i = 0; i<n; i++) {q.push(i+1);}\n while(q.size() > 1) {\n int c = 0;\n while (c < (k-1) % q.size()) {\n q.push(q.front());\n cout << q.back();\n q.pop();\n c++;\n }\n q.pop(); \n }\n return q.front();\n }\n};",
"memory": "15600"
} |
1,951 | <p>There are <code>n</code> friends that are playing a game. The friends are sitting in a circle and are numbered from <code>1</code> to <code>n</code> in <strong>clockwise order</strong>. More formally, moving clockwise from the <code>i<sup>th</sup></code> friend brings you to the <code>(i+1)<sup>th</sup></code> friend for <code>1 <= i < n</code>, and moving clockwise from the <code>n<sup>th</sup></code> friend brings you to the <code>1<sup>st</sup></code> friend.</p>
<p>The rules of the game are as follows:</p>
<ol>
<li><strong>Start</strong> at the <code>1<sup>st</sup></code> friend.</li>
<li>Count the next <code>k</code> friends in the clockwise direction <strong>including</strong> the friend you started at. The counting wraps around the circle and may count some friends more than once.</li>
<li>The last friend you counted leaves the circle and loses the game.</li>
<li>If there is still more than one friend in the circle, go back to step <code>2</code> <strong>starting</strong> from the friend <strong>immediately clockwise</strong> of the friend who just lost and repeat.</li>
<li>Else, the last friend in the circle wins the game.</li>
</ol>
<p>Given the number of friends, <code>n</code>, and an integer <code>k</code>, return <em>the winner of the game</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/25/ic234-q2-ex11.png" style="width: 500px; height: 345px;" />
<pre>
<strong>Input:</strong> n = 5, k = 2
<strong>Output:</strong> 3
<strong>Explanation:</strong> Here are the steps of the game:
1) Start at friend 1.
2) Count 2 friends clockwise, which are friends 1 and 2.
3) Friend 2 leaves the circle. Next start is friend 3.
4) Count 2 friends clockwise, which are friends 3 and 4.
5) Friend 4 leaves the circle. Next start is friend 5.
6) Count 2 friends clockwise, which are friends 5 and 1.
7) Friend 1 leaves the circle. Next start is friend 3.
8) Count 2 friends clockwise, which are friends 3 and 5.
9) Friend 5 leaves the circle. Only friend 3 is left, so they are the winner.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 6, k = 5
<strong>Output:</strong> 1
<strong>Explanation:</strong> The friends leave in this order: 5, 4, 6, 2, 3. The winner is friend 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= n <= 500</code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<p>Could you solve this problem in linear time with constant space?</p>
| 2 | {
"code": "class Solution {\npublic:\n int findTheWinner(int n, int k) {\n queue<int> q;\n for (int i = 0; i<n; i++) {q.push(i+1);}\n while(q.size() > 1) {\n int c = k;\n short s = q.size();\n while (0 < (c-1) % s) {\n q.push(q.front());\n cout << q.back();\n q.pop();\n c--;\n }\n q.pop(); \n }\n return q.front();\n }\n};",
"memory": "15700"
} |
1,951 | <p>There are <code>n</code> friends that are playing a game. The friends are sitting in a circle and are numbered from <code>1</code> to <code>n</code> in <strong>clockwise order</strong>. More formally, moving clockwise from the <code>i<sup>th</sup></code> friend brings you to the <code>(i+1)<sup>th</sup></code> friend for <code>1 <= i < n</code>, and moving clockwise from the <code>n<sup>th</sup></code> friend brings you to the <code>1<sup>st</sup></code> friend.</p>
<p>The rules of the game are as follows:</p>
<ol>
<li><strong>Start</strong> at the <code>1<sup>st</sup></code> friend.</li>
<li>Count the next <code>k</code> friends in the clockwise direction <strong>including</strong> the friend you started at. The counting wraps around the circle and may count some friends more than once.</li>
<li>The last friend you counted leaves the circle and loses the game.</li>
<li>If there is still more than one friend in the circle, go back to step <code>2</code> <strong>starting</strong> from the friend <strong>immediately clockwise</strong> of the friend who just lost and repeat.</li>
<li>Else, the last friend in the circle wins the game.</li>
</ol>
<p>Given the number of friends, <code>n</code>, and an integer <code>k</code>, return <em>the winner of the game</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/25/ic234-q2-ex11.png" style="width: 500px; height: 345px;" />
<pre>
<strong>Input:</strong> n = 5, k = 2
<strong>Output:</strong> 3
<strong>Explanation:</strong> Here are the steps of the game:
1) Start at friend 1.
2) Count 2 friends clockwise, which are friends 1 and 2.
3) Friend 2 leaves the circle. Next start is friend 3.
4) Count 2 friends clockwise, which are friends 3 and 4.
5) Friend 4 leaves the circle. Next start is friend 5.
6) Count 2 friends clockwise, which are friends 5 and 1.
7) Friend 1 leaves the circle. Next start is friend 3.
8) Count 2 friends clockwise, which are friends 3 and 5.
9) Friend 5 leaves the circle. Only friend 3 is left, so they are the winner.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 6, k = 5
<strong>Output:</strong> 1
<strong>Explanation:</strong> The friends leave in this order: 5, 4, 6, 2, 3. The winner is friend 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= n <= 500</code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<p>Could you solve this problem in linear time with constant space?</p>
| 2 | {
"code": "class Solution {\npublic:\n int findTheWinner(int n, int k) {\n queue<int> q;\n for (int i = 0; i<n; i++) {q.push(i+1);}\n while(q.size() > 1) {\n int c = 0;\n short s = q.size();\n while (c < (k-1) % s) {\n q.push(q.front());\n cout << q.back();\n q.pop();\n c++;\n }\n q.pop(); \n }\n return q.front();\n }\n};",
"memory": "15800"
} |
1,951 | <p>There are <code>n</code> friends that are playing a game. The friends are sitting in a circle and are numbered from <code>1</code> to <code>n</code> in <strong>clockwise order</strong>. More formally, moving clockwise from the <code>i<sup>th</sup></code> friend brings you to the <code>(i+1)<sup>th</sup></code> friend for <code>1 <= i < n</code>, and moving clockwise from the <code>n<sup>th</sup></code> friend brings you to the <code>1<sup>st</sup></code> friend.</p>
<p>The rules of the game are as follows:</p>
<ol>
<li><strong>Start</strong> at the <code>1<sup>st</sup></code> friend.</li>
<li>Count the next <code>k</code> friends in the clockwise direction <strong>including</strong> the friend you started at. The counting wraps around the circle and may count some friends more than once.</li>
<li>The last friend you counted leaves the circle and loses the game.</li>
<li>If there is still more than one friend in the circle, go back to step <code>2</code> <strong>starting</strong> from the friend <strong>immediately clockwise</strong> of the friend who just lost and repeat.</li>
<li>Else, the last friend in the circle wins the game.</li>
</ol>
<p>Given the number of friends, <code>n</code>, and an integer <code>k</code>, return <em>the winner of the game</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/25/ic234-q2-ex11.png" style="width: 500px; height: 345px;" />
<pre>
<strong>Input:</strong> n = 5, k = 2
<strong>Output:</strong> 3
<strong>Explanation:</strong> Here are the steps of the game:
1) Start at friend 1.
2) Count 2 friends clockwise, which are friends 1 and 2.
3) Friend 2 leaves the circle. Next start is friend 3.
4) Count 2 friends clockwise, which are friends 3 and 4.
5) Friend 4 leaves the circle. Next start is friend 5.
6) Count 2 friends clockwise, which are friends 5 and 1.
7) Friend 1 leaves the circle. Next start is friend 3.
8) Count 2 friends clockwise, which are friends 3 and 5.
9) Friend 5 leaves the circle. Only friend 3 is left, so they are the winner.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 6, k = 5
<strong>Output:</strong> 1
<strong>Explanation:</strong> The friends leave in this order: 5, 4, 6, 2, 3. The winner is friend 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= n <= 500</code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<p>Could you solve this problem in linear time with constant space?</p>
| 2 | {
"code": "class Solution {\npublic:\n int findTheWinner(int n, int k) {\n queue<int> q;\n for (int i = 0; i<n; i++) {q.push(i+1);}\n while(q.size() > 1) {\n short c = k;\n short s = q.size();\n while (0 < (c-1) % s) {\n q.push(q.front());\n cout << q.back();\n q.pop();\n c--;\n }\n q.pop(); \n }\n return q.front();\n }\n};",
"memory": "15800"
} |
1,951 | <p>There are <code>n</code> friends that are playing a game. The friends are sitting in a circle and are numbered from <code>1</code> to <code>n</code> in <strong>clockwise order</strong>. More formally, moving clockwise from the <code>i<sup>th</sup></code> friend brings you to the <code>(i+1)<sup>th</sup></code> friend for <code>1 <= i < n</code>, and moving clockwise from the <code>n<sup>th</sup></code> friend brings you to the <code>1<sup>st</sup></code> friend.</p>
<p>The rules of the game are as follows:</p>
<ol>
<li><strong>Start</strong> at the <code>1<sup>st</sup></code> friend.</li>
<li>Count the next <code>k</code> friends in the clockwise direction <strong>including</strong> the friend you started at. The counting wraps around the circle and may count some friends more than once.</li>
<li>The last friend you counted leaves the circle and loses the game.</li>
<li>If there is still more than one friend in the circle, go back to step <code>2</code> <strong>starting</strong> from the friend <strong>immediately clockwise</strong> of the friend who just lost and repeat.</li>
<li>Else, the last friend in the circle wins the game.</li>
</ol>
<p>Given the number of friends, <code>n</code>, and an integer <code>k</code>, return <em>the winner of the game</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/25/ic234-q2-ex11.png" style="width: 500px; height: 345px;" />
<pre>
<strong>Input:</strong> n = 5, k = 2
<strong>Output:</strong> 3
<strong>Explanation:</strong> Here are the steps of the game:
1) Start at friend 1.
2) Count 2 friends clockwise, which are friends 1 and 2.
3) Friend 2 leaves the circle. Next start is friend 3.
4) Count 2 friends clockwise, which are friends 3 and 4.
5) Friend 4 leaves the circle. Next start is friend 5.
6) Count 2 friends clockwise, which are friends 5 and 1.
7) Friend 1 leaves the circle. Next start is friend 3.
8) Count 2 friends clockwise, which are friends 3 and 5.
9) Friend 5 leaves the circle. Only friend 3 is left, so they are the winner.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 6, k = 5
<strong>Output:</strong> 1
<strong>Explanation:</strong> The friends leave in this order: 5, 4, 6, 2, 3. The winner is friend 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= n <= 500</code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<p>Could you solve this problem in linear time with constant space?</p>
| 2 | {
"code": "//Burak Emre Polat\nclass Solution {\npublic:\n int findTheWinner(int n, int k) {\n queue<short> q;\n for (short i = 1; i<=n; i++) {q.push(i);}\n while(q.size() > 1) {\n for (short i = 1; i < k ; i++) {\n q.push(q.front());\n q.pop();\n }\n q.pop(); \n }\n return q.front();\n }\n};",
"memory": "16600"
} |
1,951 | <p>There are <code>n</code> friends that are playing a game. The friends are sitting in a circle and are numbered from <code>1</code> to <code>n</code> in <strong>clockwise order</strong>. More formally, moving clockwise from the <code>i<sup>th</sup></code> friend brings you to the <code>(i+1)<sup>th</sup></code> friend for <code>1 <= i < n</code>, and moving clockwise from the <code>n<sup>th</sup></code> friend brings you to the <code>1<sup>st</sup></code> friend.</p>
<p>The rules of the game are as follows:</p>
<ol>
<li><strong>Start</strong> at the <code>1<sup>st</sup></code> friend.</li>
<li>Count the next <code>k</code> friends in the clockwise direction <strong>including</strong> the friend you started at. The counting wraps around the circle and may count some friends more than once.</li>
<li>The last friend you counted leaves the circle and loses the game.</li>
<li>If there is still more than one friend in the circle, go back to step <code>2</code> <strong>starting</strong> from the friend <strong>immediately clockwise</strong> of the friend who just lost and repeat.</li>
<li>Else, the last friend in the circle wins the game.</li>
</ol>
<p>Given the number of friends, <code>n</code>, and an integer <code>k</code>, return <em>the winner of the game</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/25/ic234-q2-ex11.png" style="width: 500px; height: 345px;" />
<pre>
<strong>Input:</strong> n = 5, k = 2
<strong>Output:</strong> 3
<strong>Explanation:</strong> Here are the steps of the game:
1) Start at friend 1.
2) Count 2 friends clockwise, which are friends 1 and 2.
3) Friend 2 leaves the circle. Next start is friend 3.
4) Count 2 friends clockwise, which are friends 3 and 4.
5) Friend 4 leaves the circle. Next start is friend 5.
6) Count 2 friends clockwise, which are friends 5 and 1.
7) Friend 1 leaves the circle. Next start is friend 3.
8) Count 2 friends clockwise, which are friends 3 and 5.
9) Friend 5 leaves the circle. Only friend 3 is left, so they are the winner.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 6, k = 5
<strong>Output:</strong> 1
<strong>Explanation:</strong> The friends leave in this order: 5, 4, 6, 2, 3. The winner is friend 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= n <= 500</code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<p>Could you solve this problem in linear time with constant space?</p>
| 2 | {
"code": "//Burak Emre Polat\nclass Solution {\npublic:\n int findTheWinner(int n, int k) {\n queue<short> q;\n for (short i = 1; i<=n; i++) {q.push(i);}\n while(q.size() > 1) {\n for (short i = 1; i < k ; i++) {\n q.push(q.front());\n q.pop();\n }\n q.pop(); \n }\n return q.front();\n }\n};",
"memory": "16800"
} |
1,951 | <p>There are <code>n</code> friends that are playing a game. The friends are sitting in a circle and are numbered from <code>1</code> to <code>n</code> in <strong>clockwise order</strong>. More formally, moving clockwise from the <code>i<sup>th</sup></code> friend brings you to the <code>(i+1)<sup>th</sup></code> friend for <code>1 <= i < n</code>, and moving clockwise from the <code>n<sup>th</sup></code> friend brings you to the <code>1<sup>st</sup></code> friend.</p>
<p>The rules of the game are as follows:</p>
<ol>
<li><strong>Start</strong> at the <code>1<sup>st</sup></code> friend.</li>
<li>Count the next <code>k</code> friends in the clockwise direction <strong>including</strong> the friend you started at. The counting wraps around the circle and may count some friends more than once.</li>
<li>The last friend you counted leaves the circle and loses the game.</li>
<li>If there is still more than one friend in the circle, go back to step <code>2</code> <strong>starting</strong> from the friend <strong>immediately clockwise</strong> of the friend who just lost and repeat.</li>
<li>Else, the last friend in the circle wins the game.</li>
</ol>
<p>Given the number of friends, <code>n</code>, and an integer <code>k</code>, return <em>the winner of the game</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/25/ic234-q2-ex11.png" style="width: 500px; height: 345px;" />
<pre>
<strong>Input:</strong> n = 5, k = 2
<strong>Output:</strong> 3
<strong>Explanation:</strong> Here are the steps of the game:
1) Start at friend 1.
2) Count 2 friends clockwise, which are friends 1 and 2.
3) Friend 2 leaves the circle. Next start is friend 3.
4) Count 2 friends clockwise, which are friends 3 and 4.
5) Friend 4 leaves the circle. Next start is friend 5.
6) Count 2 friends clockwise, which are friends 5 and 1.
7) Friend 1 leaves the circle. Next start is friend 3.
8) Count 2 friends clockwise, which are friends 3 and 5.
9) Friend 5 leaves the circle. Only friend 3 is left, so they are the winner.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 6, k = 5
<strong>Output:</strong> 1
<strong>Explanation:</strong> The friends leave in this order: 5, 4, 6, 2, 3. The winner is friend 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= n <= 500</code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<p>Could you solve this problem in linear time with constant space?</p>
| 2 | {
"code": "class Solution {\npublic:\n int findTheWinner(int n, int k) {\n deque <int> dq;\n for(int i=1;i<=n;i++){\n dq.push_back(i);\n }\n int p=0;\n while(dq.size()!=1){\n int h=dq.front();\n dq.pop_front();\n p++;\n p%=k;\n if(p!=0){\n dq.push_back(h);\n }\n\n }\n\n return dq.front();\n }\n};",
"memory": "25700"
} |
1,951 | <p>There are <code>n</code> friends that are playing a game. The friends are sitting in a circle and are numbered from <code>1</code> to <code>n</code> in <strong>clockwise order</strong>. More formally, moving clockwise from the <code>i<sup>th</sup></code> friend brings you to the <code>(i+1)<sup>th</sup></code> friend for <code>1 <= i < n</code>, and moving clockwise from the <code>n<sup>th</sup></code> friend brings you to the <code>1<sup>st</sup></code> friend.</p>
<p>The rules of the game are as follows:</p>
<ol>
<li><strong>Start</strong> at the <code>1<sup>st</sup></code> friend.</li>
<li>Count the next <code>k</code> friends in the clockwise direction <strong>including</strong> the friend you started at. The counting wraps around the circle and may count some friends more than once.</li>
<li>The last friend you counted leaves the circle and loses the game.</li>
<li>If there is still more than one friend in the circle, go back to step <code>2</code> <strong>starting</strong> from the friend <strong>immediately clockwise</strong> of the friend who just lost and repeat.</li>
<li>Else, the last friend in the circle wins the game.</li>
</ol>
<p>Given the number of friends, <code>n</code>, and an integer <code>k</code>, return <em>the winner of the game</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/25/ic234-q2-ex11.png" style="width: 500px; height: 345px;" />
<pre>
<strong>Input:</strong> n = 5, k = 2
<strong>Output:</strong> 3
<strong>Explanation:</strong> Here are the steps of the game:
1) Start at friend 1.
2) Count 2 friends clockwise, which are friends 1 and 2.
3) Friend 2 leaves the circle. Next start is friend 3.
4) Count 2 friends clockwise, which are friends 3 and 4.
5) Friend 4 leaves the circle. Next start is friend 5.
6) Count 2 friends clockwise, which are friends 5 and 1.
7) Friend 1 leaves the circle. Next start is friend 3.
8) Count 2 friends clockwise, which are friends 3 and 5.
9) Friend 5 leaves the circle. Only friend 3 is left, so they are the winner.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 6, k = 5
<strong>Output:</strong> 1
<strong>Explanation:</strong> The friends leave in this order: 5, 4, 6, 2, 3. The winner is friend 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= n <= 500</code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<p>Could you solve this problem in linear time with constant space?</p>
| 2 | {
"code": "class Solution {\npublic:\n int findTheWinner(int n, int k) {\n queue<int>q;\n for(int i=1;i<=n;i++)q.push(i);\n while(q.size()>1){\n int val=k;\n while(--val){\n q.push(q.front());\n q.pop();\n }\n q.pop();\n }\n return q.front();\n }\n};",
"memory": "25800"
} |
1,951 | <p>There are <code>n</code> friends that are playing a game. The friends are sitting in a circle and are numbered from <code>1</code> to <code>n</code> in <strong>clockwise order</strong>. More formally, moving clockwise from the <code>i<sup>th</sup></code> friend brings you to the <code>(i+1)<sup>th</sup></code> friend for <code>1 <= i < n</code>, and moving clockwise from the <code>n<sup>th</sup></code> friend brings you to the <code>1<sup>st</sup></code> friend.</p>
<p>The rules of the game are as follows:</p>
<ol>
<li><strong>Start</strong> at the <code>1<sup>st</sup></code> friend.</li>
<li>Count the next <code>k</code> friends in the clockwise direction <strong>including</strong> the friend you started at. The counting wraps around the circle and may count some friends more than once.</li>
<li>The last friend you counted leaves the circle and loses the game.</li>
<li>If there is still more than one friend in the circle, go back to step <code>2</code> <strong>starting</strong> from the friend <strong>immediately clockwise</strong> of the friend who just lost and repeat.</li>
<li>Else, the last friend in the circle wins the game.</li>
</ol>
<p>Given the number of friends, <code>n</code>, and an integer <code>k</code>, return <em>the winner of the game</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/25/ic234-q2-ex11.png" style="width: 500px; height: 345px;" />
<pre>
<strong>Input:</strong> n = 5, k = 2
<strong>Output:</strong> 3
<strong>Explanation:</strong> Here are the steps of the game:
1) Start at friend 1.
2) Count 2 friends clockwise, which are friends 1 and 2.
3) Friend 2 leaves the circle. Next start is friend 3.
4) Count 2 friends clockwise, which are friends 3 and 4.
5) Friend 4 leaves the circle. Next start is friend 5.
6) Count 2 friends clockwise, which are friends 5 and 1.
7) Friend 1 leaves the circle. Next start is friend 3.
8) Count 2 friends clockwise, which are friends 3 and 5.
9) Friend 5 leaves the circle. Only friend 3 is left, so they are the winner.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 6, k = 5
<strong>Output:</strong> 1
<strong>Explanation:</strong> The friends leave in this order: 5, 4, 6, 2, 3. The winner is friend 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= n <= 500</code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<p>Could you solve this problem in linear time with constant space?</p>
| 2 | {
"code": "class Solution {\npublic:\n\n int findTheWinnerQueue(int n, int k) {\n std::queue<int> q;\n for (int i = 1; i <= n; i++) {\n q.push(i);\n }\n\n while (q.size() > 1) {\n for (int i = 1; i < k; i++) {\n q.push(q.front());\n q.pop();\n }\n\n std::cout << \"POP: \" << q.front() << std::endl;\n q.pop();\n }\n\n return q.front();\n }\n\n int findTheWinner(int n, int k) {\n return findTheWinnerQueue(n, k); \n }\n};",
"memory": "25800"
} |
1,951 | <p>There are <code>n</code> friends that are playing a game. The friends are sitting in a circle and are numbered from <code>1</code> to <code>n</code> in <strong>clockwise order</strong>. More formally, moving clockwise from the <code>i<sup>th</sup></code> friend brings you to the <code>(i+1)<sup>th</sup></code> friend for <code>1 <= i < n</code>, and moving clockwise from the <code>n<sup>th</sup></code> friend brings you to the <code>1<sup>st</sup></code> friend.</p>
<p>The rules of the game are as follows:</p>
<ol>
<li><strong>Start</strong> at the <code>1<sup>st</sup></code> friend.</li>
<li>Count the next <code>k</code> friends in the clockwise direction <strong>including</strong> the friend you started at. The counting wraps around the circle and may count some friends more than once.</li>
<li>The last friend you counted leaves the circle and loses the game.</li>
<li>If there is still more than one friend in the circle, go back to step <code>2</code> <strong>starting</strong> from the friend <strong>immediately clockwise</strong> of the friend who just lost and repeat.</li>
<li>Else, the last friend in the circle wins the game.</li>
</ol>
<p>Given the number of friends, <code>n</code>, and an integer <code>k</code>, return <em>the winner of the game</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/25/ic234-q2-ex11.png" style="width: 500px; height: 345px;" />
<pre>
<strong>Input:</strong> n = 5, k = 2
<strong>Output:</strong> 3
<strong>Explanation:</strong> Here are the steps of the game:
1) Start at friend 1.
2) Count 2 friends clockwise, which are friends 1 and 2.
3) Friend 2 leaves the circle. Next start is friend 3.
4) Count 2 friends clockwise, which are friends 3 and 4.
5) Friend 4 leaves the circle. Next start is friend 5.
6) Count 2 friends clockwise, which are friends 5 and 1.
7) Friend 1 leaves the circle. Next start is friend 3.
8) Count 2 friends clockwise, which are friends 3 and 5.
9) Friend 5 leaves the circle. Only friend 3 is left, so they are the winner.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 6, k = 5
<strong>Output:</strong> 1
<strong>Explanation:</strong> The friends leave in this order: 5, 4, 6, 2, 3. The winner is friend 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= n <= 500</code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<p>Could you solve this problem in linear time with constant space?</p>
| 2 | {
"code": "#include <queue>\nusing namespace std;\n\nclass Solution {\npublic:\n int findTheWinner(int n, int k) {\n queue<int> s;\n \n // Populate the queue with players from 1 to n\n for(int i = 1; i <= n; i++) {\n s.push(i);\n }\n \n // Simulate the elimination process\n while(s.size() > 1) {\n // Move the first k-1 players to the back of the queue\n for(int i = 1; i < k; i++) {\n s.push(s.front());\n s.pop();\n }\n // Eliminate the k-th player\n s.pop();\n }\n \n // The last remaining player is the winner\n return s.front();\n }\n};\n",
"memory": "25900"
} |
1,951 | <p>There are <code>n</code> friends that are playing a game. The friends are sitting in a circle and are numbered from <code>1</code> to <code>n</code> in <strong>clockwise order</strong>. More formally, moving clockwise from the <code>i<sup>th</sup></code> friend brings you to the <code>(i+1)<sup>th</sup></code> friend for <code>1 <= i < n</code>, and moving clockwise from the <code>n<sup>th</sup></code> friend brings you to the <code>1<sup>st</sup></code> friend.</p>
<p>The rules of the game are as follows:</p>
<ol>
<li><strong>Start</strong> at the <code>1<sup>st</sup></code> friend.</li>
<li>Count the next <code>k</code> friends in the clockwise direction <strong>including</strong> the friend you started at. The counting wraps around the circle and may count some friends more than once.</li>
<li>The last friend you counted leaves the circle and loses the game.</li>
<li>If there is still more than one friend in the circle, go back to step <code>2</code> <strong>starting</strong> from the friend <strong>immediately clockwise</strong> of the friend who just lost and repeat.</li>
<li>Else, the last friend in the circle wins the game.</li>
</ol>
<p>Given the number of friends, <code>n</code>, and an integer <code>k</code>, return <em>the winner of the game</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/25/ic234-q2-ex11.png" style="width: 500px; height: 345px;" />
<pre>
<strong>Input:</strong> n = 5, k = 2
<strong>Output:</strong> 3
<strong>Explanation:</strong> Here are the steps of the game:
1) Start at friend 1.
2) Count 2 friends clockwise, which are friends 1 and 2.
3) Friend 2 leaves the circle. Next start is friend 3.
4) Count 2 friends clockwise, which are friends 3 and 4.
5) Friend 4 leaves the circle. Next start is friend 5.
6) Count 2 friends clockwise, which are friends 5 and 1.
7) Friend 1 leaves the circle. Next start is friend 3.
8) Count 2 friends clockwise, which are friends 3 and 5.
9) Friend 5 leaves the circle. Only friend 3 is left, so they are the winner.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 6, k = 5
<strong>Output:</strong> 1
<strong>Explanation:</strong> The friends leave in this order: 5, 4, 6, 2, 3. The winner is friend 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= n <= 500</code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<p>Could you solve this problem in linear time with constant space?</p>
| 3 | {
"code": "class Solution {\npublic:\n \n int findTheWinner(int n, int k) {\n queue<int> q;\n for(int i=1; i<=n; i++){\n q.push(i);\n }\n while(q.size()>1){\n for(int i=1; i<k; i++){\n q.push(q.front());\n q.pop();\n }\n q.pop();\n }\n return q.front();\n\n }\n};",
"memory": "26000"
} |
1,951 | <p>There are <code>n</code> friends that are playing a game. The friends are sitting in a circle and are numbered from <code>1</code> to <code>n</code> in <strong>clockwise order</strong>. More formally, moving clockwise from the <code>i<sup>th</sup></code> friend brings you to the <code>(i+1)<sup>th</sup></code> friend for <code>1 <= i < n</code>, and moving clockwise from the <code>n<sup>th</sup></code> friend brings you to the <code>1<sup>st</sup></code> friend.</p>
<p>The rules of the game are as follows:</p>
<ol>
<li><strong>Start</strong> at the <code>1<sup>st</sup></code> friend.</li>
<li>Count the next <code>k</code> friends in the clockwise direction <strong>including</strong> the friend you started at. The counting wraps around the circle and may count some friends more than once.</li>
<li>The last friend you counted leaves the circle and loses the game.</li>
<li>If there is still more than one friend in the circle, go back to step <code>2</code> <strong>starting</strong> from the friend <strong>immediately clockwise</strong> of the friend who just lost and repeat.</li>
<li>Else, the last friend in the circle wins the game.</li>
</ol>
<p>Given the number of friends, <code>n</code>, and an integer <code>k</code>, return <em>the winner of the game</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/25/ic234-q2-ex11.png" style="width: 500px; height: 345px;" />
<pre>
<strong>Input:</strong> n = 5, k = 2
<strong>Output:</strong> 3
<strong>Explanation:</strong> Here are the steps of the game:
1) Start at friend 1.
2) Count 2 friends clockwise, which are friends 1 and 2.
3) Friend 2 leaves the circle. Next start is friend 3.
4) Count 2 friends clockwise, which are friends 3 and 4.
5) Friend 4 leaves the circle. Next start is friend 5.
6) Count 2 friends clockwise, which are friends 5 and 1.
7) Friend 1 leaves the circle. Next start is friend 3.
8) Count 2 friends clockwise, which are friends 3 and 5.
9) Friend 5 leaves the circle. Only friend 3 is left, so they are the winner.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 6, k = 5
<strong>Output:</strong> 1
<strong>Explanation:</strong> The friends leave in this order: 5, 4, 6, 2, 3. The winner is friend 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= n <= 500</code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<p>Could you solve this problem in linear time with constant space?</p>
| 3 | {
"code": "class Solution {\npublic:\n queue<int>q;\n int findTheWinner(int n, int k)\n {\n for(int i=1;i<=n;i++)\n {\n q.push(i);\n }\n\n int cnt=0;\n while(q.size()>1)\n {\n int v=q.front();\n q.pop();\n cnt++;\n\n if(cnt==k) cnt=0;\n else\n q.push(v); \n } \n return q.front(); \n }\n\n};",
"memory": "26000"
} |
1,252 | <p>Given a palindromic string of lowercase English letters <code>palindrome</code>, replace <strong>exactly one</strong> character with any lowercase English letter so that the resulting string is <strong>not</strong> a palindrome and that it is the <strong>lexicographically smallest</strong> one possible.</p>
<p>Return <em>the resulting string. If there is no way to replace a character to make it not a palindrome, return an <strong>empty string</strong>.</em></p>
<p>A string <code>a</code> is lexicographically smaller than a string <code>b</code> (of the same length) if in the first position where <code>a</code> and <code>b</code> differ, <code>a</code> has a character strictly smaller than the corresponding character in <code>b</code>. For example, <code>"abcc"</code> is lexicographically smaller than <code>"abcd"</code> because the first position they differ is at the fourth character, and <code>'c'</code> is smaller than <code>'d'</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> palindrome = "abccba"
<strong>Output:</strong> "aaccba"
<strong>Explanation:</strong> There are many ways to make "abccba" not a palindrome, such as "<u>z</u>bccba", "a<u>a</u>ccba", and "ab<u>a</u>cba".
Of all the ways, "aaccba" is the lexicographically smallest.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> palindrome = "a"
<strong>Output:</strong> ""
<strong>Explanation:</strong> There is no way to replace a single character to make "a" not a palindrome, so return an empty string.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= palindrome.length <= 1000</code></li>
<li><code>palindrome</code> consists of only lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n string breakPalindrome(string p) {\n int n=p.size();\n if(n==1) return \"\";\n //cout << 'z'-'a'<<endl;\n for(int i=0;i<n/2;i++){\n if(p[i]-'a'!=0) {\n p[i]='a';\n return p;\n }\n }\n for(int i=n-1;i>=0;i--){\n if(p[i]-'a'<=25) {\n p[i]='b';\n return p;\n }\n }\n return \"\";\n }\n};",
"memory": "7400"
} |
1,252 | <p>Given a palindromic string of lowercase English letters <code>palindrome</code>, replace <strong>exactly one</strong> character with any lowercase English letter so that the resulting string is <strong>not</strong> a palindrome and that it is the <strong>lexicographically smallest</strong> one possible.</p>
<p>Return <em>the resulting string. If there is no way to replace a character to make it not a palindrome, return an <strong>empty string</strong>.</em></p>
<p>A string <code>a</code> is lexicographically smaller than a string <code>b</code> (of the same length) if in the first position where <code>a</code> and <code>b</code> differ, <code>a</code> has a character strictly smaller than the corresponding character in <code>b</code>. For example, <code>"abcc"</code> is lexicographically smaller than <code>"abcd"</code> because the first position they differ is at the fourth character, and <code>'c'</code> is smaller than <code>'d'</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> palindrome = "abccba"
<strong>Output:</strong> "aaccba"
<strong>Explanation:</strong> There are many ways to make "abccba" not a palindrome, such as "<u>z</u>bccba", "a<u>a</u>ccba", and "ab<u>a</u>cba".
Of all the ways, "aaccba" is the lexicographically smallest.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> palindrome = "a"
<strong>Output:</strong> ""
<strong>Explanation:</strong> There is no way to replace a single character to make "a" not a palindrome, so return an empty string.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= palindrome.length <= 1000</code></li>
<li><code>palindrome</code> consists of only lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n string breakPalindrome(string palindrome) {\n if (palindrome.size() == 1) {\n return \"\";\n }\n\n for (int i = 0; i < palindrome.size(); ++i) {\n char& c = palindrome[i];\n if (c != 'a' && !(palindrome.size() % 2 == 1 && i == palindrome.size() / 2)) {\n c = 'a';\n return palindrome;\n }\n }\n\n palindrome.back() = 'b';\n return palindrome;\n }\n};",
"memory": "7500"
} |
1,252 | <p>Given a palindromic string of lowercase English letters <code>palindrome</code>, replace <strong>exactly one</strong> character with any lowercase English letter so that the resulting string is <strong>not</strong> a palindrome and that it is the <strong>lexicographically smallest</strong> one possible.</p>
<p>Return <em>the resulting string. If there is no way to replace a character to make it not a palindrome, return an <strong>empty string</strong>.</em></p>
<p>A string <code>a</code> is lexicographically smaller than a string <code>b</code> (of the same length) if in the first position where <code>a</code> and <code>b</code> differ, <code>a</code> has a character strictly smaller than the corresponding character in <code>b</code>. For example, <code>"abcc"</code> is lexicographically smaller than <code>"abcd"</code> because the first position they differ is at the fourth character, and <code>'c'</code> is smaller than <code>'d'</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> palindrome = "abccba"
<strong>Output:</strong> "aaccba"
<strong>Explanation:</strong> There are many ways to make "abccba" not a palindrome, such as "<u>z</u>bccba", "a<u>a</u>ccba", and "ab<u>a</u>cba".
Of all the ways, "aaccba" is the lexicographically smallest.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> palindrome = "a"
<strong>Output:</strong> ""
<strong>Explanation:</strong> There is no way to replace a single character to make "a" not a palindrome, so return an empty string.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= palindrome.length <= 1000</code></li>
<li><code>palindrome</code> consists of only lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n string breakPalindrome(string palindrome) {\n int length = palindrome.size();\n \n if (length == 1) { \n return \"\";\n }\n \n for (int i = 0; i < length / 2; i++) {\n if (palindrome[i] != 'a') {\n palindrome[i] = 'a';\n return palindrome;\n }\n }\n \n palindrome[length - 1] = 'b';\n return palindrome;\n }\n};",
"memory": "7500"
} |
1,252 | <p>Given a palindromic string of lowercase English letters <code>palindrome</code>, replace <strong>exactly one</strong> character with any lowercase English letter so that the resulting string is <strong>not</strong> a palindrome and that it is the <strong>lexicographically smallest</strong> one possible.</p>
<p>Return <em>the resulting string. If there is no way to replace a character to make it not a palindrome, return an <strong>empty string</strong>.</em></p>
<p>A string <code>a</code> is lexicographically smaller than a string <code>b</code> (of the same length) if in the first position where <code>a</code> and <code>b</code> differ, <code>a</code> has a character strictly smaller than the corresponding character in <code>b</code>. For example, <code>"abcc"</code> is lexicographically smaller than <code>"abcd"</code> because the first position they differ is at the fourth character, and <code>'c'</code> is smaller than <code>'d'</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> palindrome = "abccba"
<strong>Output:</strong> "aaccba"
<strong>Explanation:</strong> There are many ways to make "abccba" not a palindrome, such as "<u>z</u>bccba", "a<u>a</u>ccba", and "ab<u>a</u>cba".
Of all the ways, "aaccba" is the lexicographically smallest.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> palindrome = "a"
<strong>Output:</strong> ""
<strong>Explanation:</strong> There is no way to replace a single character to make "a" not a palindrome, so return an empty string.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= palindrome.length <= 1000</code></li>
<li><code>palindrome</code> consists of only lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n string breakPalindrome(string palindrome) {\n if (palindrome.size() == 1)\n {\n return \"\";\n }\n\n // We only have to look up to halfway into the palindrome, not inclusive of center character\n\n // Then we want to change the first non-a character to a, if possible\n for(int i = 0; i < palindrome.size() / 2; i++)\n {\n const char currentChar = palindrome[i];\n if (currentChar != 'a')\n {\n palindrome[i] = 'a';\n return palindrome;\n }\n }\n\n // All characters must have been 'a'. We replace the last character \n palindrome[palindrome.size() - 1] = 'b';\n return palindrome;\n\n return \"\";\n }\n};",
"memory": "7600"
} |
1,252 | <p>Given a palindromic string of lowercase English letters <code>palindrome</code>, replace <strong>exactly one</strong> character with any lowercase English letter so that the resulting string is <strong>not</strong> a palindrome and that it is the <strong>lexicographically smallest</strong> one possible.</p>
<p>Return <em>the resulting string. If there is no way to replace a character to make it not a palindrome, return an <strong>empty string</strong>.</em></p>
<p>A string <code>a</code> is lexicographically smaller than a string <code>b</code> (of the same length) if in the first position where <code>a</code> and <code>b</code> differ, <code>a</code> has a character strictly smaller than the corresponding character in <code>b</code>. For example, <code>"abcc"</code> is lexicographically smaller than <code>"abcd"</code> because the first position they differ is at the fourth character, and <code>'c'</code> is smaller than <code>'d'</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> palindrome = "abccba"
<strong>Output:</strong> "aaccba"
<strong>Explanation:</strong> There are many ways to make "abccba" not a palindrome, such as "<u>z</u>bccba", "a<u>a</u>ccba", and "ab<u>a</u>cba".
Of all the ways, "aaccba" is the lexicographically smallest.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> palindrome = "a"
<strong>Output:</strong> ""
<strong>Explanation:</strong> There is no way to replace a single character to make "a" not a palindrome, so return an empty string.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= palindrome.length <= 1000</code></li>
<li><code>palindrome</code> consists of only lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n string breakPalindrome(string palindrome) {\n int length = palindrome.size();\n \n if (length == 1) { \n return \"\";\n }\n \n for (int i = 0; i < length / 2; i++) {\n if (palindrome[i] != 'a') {\n palindrome[i] = 'a';\n return palindrome;\n }\n }\n \n palindrome[length - 1] = 'b';\n return palindrome;\n }\n};",
"memory": "7600"
} |
1,252 | <p>Given a palindromic string of lowercase English letters <code>palindrome</code>, replace <strong>exactly one</strong> character with any lowercase English letter so that the resulting string is <strong>not</strong> a palindrome and that it is the <strong>lexicographically smallest</strong> one possible.</p>
<p>Return <em>the resulting string. If there is no way to replace a character to make it not a palindrome, return an <strong>empty string</strong>.</em></p>
<p>A string <code>a</code> is lexicographically smaller than a string <code>b</code> (of the same length) if in the first position where <code>a</code> and <code>b</code> differ, <code>a</code> has a character strictly smaller than the corresponding character in <code>b</code>. For example, <code>"abcc"</code> is lexicographically smaller than <code>"abcd"</code> because the first position they differ is at the fourth character, and <code>'c'</code> is smaller than <code>'d'</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> palindrome = "abccba"
<strong>Output:</strong> "aaccba"
<strong>Explanation:</strong> There are many ways to make "abccba" not a palindrome, such as "<u>z</u>bccba", "a<u>a</u>ccba", and "ab<u>a</u>cba".
Of all the ways, "aaccba" is the lexicographically smallest.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> palindrome = "a"
<strong>Output:</strong> ""
<strong>Explanation:</strong> There is no way to replace a single character to make "a" not a palindrome, so return an empty string.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= palindrome.length <= 1000</code></li>
<li><code>palindrome</code> consists of only lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n string breakPalindrome(string palindrome) {\n int n = palindrome.size();\n if(n == 1)\n return \"\";\n \n int idx = -1;\n for(int i=0; i<n/2; i++) {\n if(palindrome[i] != 'a') {\n palindrome[i] = 'a';\n return palindrome;\n } \n }\n palindrome[n-1] = 'b';\n return palindrome;\n }\n};",
"memory": "7700"
} |
1,252 | <p>Given a palindromic string of lowercase English letters <code>palindrome</code>, replace <strong>exactly one</strong> character with any lowercase English letter so that the resulting string is <strong>not</strong> a palindrome and that it is the <strong>lexicographically smallest</strong> one possible.</p>
<p>Return <em>the resulting string. If there is no way to replace a character to make it not a palindrome, return an <strong>empty string</strong>.</em></p>
<p>A string <code>a</code> is lexicographically smaller than a string <code>b</code> (of the same length) if in the first position where <code>a</code> and <code>b</code> differ, <code>a</code> has a character strictly smaller than the corresponding character in <code>b</code>. For example, <code>"abcc"</code> is lexicographically smaller than <code>"abcd"</code> because the first position they differ is at the fourth character, and <code>'c'</code> is smaller than <code>'d'</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> palindrome = "abccba"
<strong>Output:</strong> "aaccba"
<strong>Explanation:</strong> There are many ways to make "abccba" not a palindrome, such as "<u>z</u>bccba", "a<u>a</u>ccba", and "ab<u>a</u>cba".
Of all the ways, "aaccba" is the lexicographically smallest.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> palindrome = "a"
<strong>Output:</strong> ""
<strong>Explanation:</strong> There is no way to replace a single character to make "a" not a palindrome, so return an empty string.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= palindrome.length <= 1000</code></li>
<li><code>palindrome</code> consists of only lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n string breakPalindrome(string palindrome) {\n int length = palindrome.size();\n \n if (length == 1) { \n return \"\";\n }\n \n for (int i = 0; i < length / 2; i++) {\n if (palindrome[i] != 'a') {\n palindrome[i] = 'a';\n return palindrome;\n }\n }\n \n palindrome[length - 1] = 'b';\n return palindrome;\n }\n};",
"memory": "7700"
} |
1,253 | <p>A <strong>matrix diagonal</strong> is a diagonal line of cells starting from some cell in either the topmost row or leftmost column and going in the bottom-right direction until reaching the matrix's end. For example, the <strong>matrix diagonal</strong> starting from <code>mat[2][0]</code>, where <code>mat</code> is a <code>6 x 3</code> matrix, includes cells <code>mat[2][0]</code>, <code>mat[3][1]</code>, and <code>mat[4][2]</code>.</p>
<p>Given an <code>m x n</code> matrix <code>mat</code> of integers, sort each <strong>matrix diagonal</strong> in ascending order and return <em>the resulting matrix</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/01/21/1482_example_1_2.png" style="width: 500px; height: 198px;" />
<pre>
<strong>Input:</strong> mat = [[3,3,1,1],[2,2,1,2],[1,1,1,2]]
<strong>Output:</strong> [[1,1,1,1],[1,2,2,2],[1,2,3,3]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> mat = [[11,25,66,1,69,7],[23,55,17,45,15,52],[75,31,36,44,58,8],[22,27,33,25,68,4],[84,28,14,11,5,50]]
<strong>Output:</strong> [[5,17,4,1,52,7],[11,11,25,45,8,69],[14,23,25,44,58,15],[22,27,31,36,50,66],[84,28,75,33,55,68]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == mat.length</code></li>
<li><code>n == mat[i].length</code></li>
<li><code>1 <= m, n <= 100</code></li>
<li><code>1 <= mat[i][j] <= 100</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<vector<int>> diagonalSort(vector<vector<int>>& mat) {\n int n = mat.size();\n int m = mat[0].size();\n\n \n for (int i = 0; i < n; i++) {\n int j = i, k = 0; \n bool swapped;\n do {\n swapped = false;\n \n j = i; \n k = 0;\n for (; j + 1 < n && k + 1 < m; j++, k++) {\n if (mat[j][k] > mat[j + 1][k + 1]) { \n swap(mat[j][k], mat[j + 1][k + 1]);\n swapped = true;\n }\n }\n } while (swapped);\n }\n\n \n for (int i = 1; i < m; i++) {\n int j = 0, k = i; \n bool swapped;\n do {\n swapped = false;\n \n j = 0; \n k = i;\n for (; j + 1 < n && k + 1 < m; j++, k++) {\n if (mat[j][k] > mat[j + 1][k + 1]) { \n swap(mat[j][k], mat[j + 1][k + 1]);\n swapped = true;\n }\n }\n } while (swapped);\n }\n\n return mat;\n }\n};\n",
"memory": "11100"
} |
1,253 | <p>A <strong>matrix diagonal</strong> is a diagonal line of cells starting from some cell in either the topmost row or leftmost column and going in the bottom-right direction until reaching the matrix's end. For example, the <strong>matrix diagonal</strong> starting from <code>mat[2][0]</code>, where <code>mat</code> is a <code>6 x 3</code> matrix, includes cells <code>mat[2][0]</code>, <code>mat[3][1]</code>, and <code>mat[4][2]</code>.</p>
<p>Given an <code>m x n</code> matrix <code>mat</code> of integers, sort each <strong>matrix diagonal</strong> in ascending order and return <em>the resulting matrix</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/01/21/1482_example_1_2.png" style="width: 500px; height: 198px;" />
<pre>
<strong>Input:</strong> mat = [[3,3,1,1],[2,2,1,2],[1,1,1,2]]
<strong>Output:</strong> [[1,1,1,1],[1,2,2,2],[1,2,3,3]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> mat = [[11,25,66,1,69,7],[23,55,17,45,15,52],[75,31,36,44,58,8],[22,27,33,25,68,4],[84,28,14,11,5,50]]
<strong>Output:</strong> [[5,17,4,1,52,7],[11,11,25,45,8,69],[14,23,25,44,58,15],[22,27,31,36,50,66],[84,28,75,33,55,68]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == mat.length</code></li>
<li><code>n == mat[i].length</code></li>
<li><code>1 <= m, n <= 100</code></li>
<li><code>1 <= mat[i][j] <= 100</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<vector<int>> diagonalSort(vector<vector<int>>& mat){\n for(int k=0; k + 1 < mat.size(); k++){\n for(int i=0; i + 1 <mat.size();i++){\n for(int j=0; j + 1<mat[i].size();j++){\n if(mat[i][j] > mat[i + 1][j + 1]){\n swap(mat[i][j], mat[i + 1][j + 1]);\n }\n }\n }\n }\n return mat;\n }\n}; \n \n \n \n \n",
"memory": "11200"
} |
1,253 | <p>A <strong>matrix diagonal</strong> is a diagonal line of cells starting from some cell in either the topmost row or leftmost column and going in the bottom-right direction until reaching the matrix's end. For example, the <strong>matrix diagonal</strong> starting from <code>mat[2][0]</code>, where <code>mat</code> is a <code>6 x 3</code> matrix, includes cells <code>mat[2][0]</code>, <code>mat[3][1]</code>, and <code>mat[4][2]</code>.</p>
<p>Given an <code>m x n</code> matrix <code>mat</code> of integers, sort each <strong>matrix diagonal</strong> in ascending order and return <em>the resulting matrix</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/01/21/1482_example_1_2.png" style="width: 500px; height: 198px;" />
<pre>
<strong>Input:</strong> mat = [[3,3,1,1],[2,2,1,2],[1,1,1,2]]
<strong>Output:</strong> [[1,1,1,1],[1,2,2,2],[1,2,3,3]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> mat = [[11,25,66,1,69,7],[23,55,17,45,15,52],[75,31,36,44,58,8],[22,27,33,25,68,4],[84,28,14,11,5,50]]
<strong>Output:</strong> [[5,17,4,1,52,7],[11,11,25,45,8,69],[14,23,25,44,58,15],[22,27,31,36,50,66],[84,28,75,33,55,68]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == mat.length</code></li>
<li><code>n == mat[i].length</code></li>
<li><code>1 <= m, n <= 100</code></li>
<li><code>1 <= mat[i][j] <= 100</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<vector<int>> diagonalSort(vector<vector<int>>& mat) {\n int m = mat.size();\n int n = mat[0].size();\n bool bubble = true;\n while (bubble) {\n bubble = false;\n for (int i = 0; i < m - 1; i++) {\n for (int j = 0; j < n - 1; j++) {\n int a = mat[i][j];\n int b = mat[i + 1][j + 1];\n if (b < a) {\n mat[i][j] = b;\n mat[i + 1][j + 1] = a;\n bubble = true;\n }\n }\n }\n }\n return mat;\n }\n};",
"memory": "11300"
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.