id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
1,617
<p>Alice and Bob take turns playing a game, with Alice starting first.</p> <p>Initially, there are <code>n</code> stones in a pile. On each player&#39;s turn, that player makes a <em>move</em> consisting of removing <strong>any</strong> non-zero <strong>square number</strong> of stones in the pile.</p> <p>Also, if a ...
2
{ "code": "class Solution {\npublic:\n vector<int> dp; // -1 --> not touched yet, 0 --> loss state, 1 --> win state\n\n // Recursive function to determine if the current state is a winning state\n int f(int n) {\n if (n < 0) return 0;\n // If DP already evaluated the state, then return the stat...
1,617
<p>Alice and Bob take turns playing a game, with Alice starting first.</p> <p>Initially, there are <code>n</code> stones in a pile. On each player&#39;s turn, that player makes a <em>move</em> consisting of removing <strong>any</strong> non-zero <strong>square number</strong> of stones in the pile.</p> <p>Also, if a ...
2
{ "code": "class Solution {\npublic:\n vector<int> dp; // For memoization\n\n bool f(int i) {\n if(i == 0) return false; // If no stones are left, current player loses\n\n if(dp[i] != -1) return dp[i]; // Return memoized result\n\n // Try every square number (j*j) that can be removed\n ...
1,617
<p>Alice and Bob take turns playing a game, with Alice starting first.</p> <p>Initially, there are <code>n</code> stones in a pile. On each player&#39;s turn, that player makes a <em>move</em> consisting of removing <strong>any</strong> non-zero <strong>square number</strong> of stones in the pile.</p> <p>Also, if a ...
2
{ "code": "class Solution {\nprivate:\n int dp[101010][2];\n\n int calc(int n, int b) {\n if (n < 0) {\n return 0; // Lose\n }\n if (dp[n][b] != -1) {\n return dp[n][b];\n }\n int ans = 0; // Default lose\n for (int i = 1; i * i <= n; ++i) {\n ...
1,617
<p>Alice and Bob take turns playing a game, with Alice starting first.</p> <p>Initially, there are <code>n</code> stones in a pile. On each player&#39;s turn, that player makes a <em>move</em> consisting of removing <strong>any</strong> non-zero <strong>square number</strong> of stones in the pile.</p> <p>Also, if a ...
2
{ "code": "class Solution {\npublic:\n bool winnerSquareGame(int n) {\n vector<int> dp(n + 1);\n dp[0] = 1;\n return check(dp, n); \n }\n\nprivate:\n bool check(vector<int>& dp, int n) {\n if (dp[n] != 0) {\n return dp[n] == 2;\n }\n for (int i = 1;...
1,617
<p>Alice and Bob take turns playing a game, with Alice starting first.</p> <p>Initially, there are <code>n</code> stones in a pile. On each player&#39;s turn, that player makes a <em>move</em> consisting of removing <strong>any</strong> non-zero <strong>square number</strong> of stones in the pile.</p> <p>Also, if a ...
2
{ "code": "class Solution {\npublic:\n bool f(int n, vector<int>& memo) {\n if (n == 0) return false;\n \n if (memo[n] != -1) return memo[n];\n\n int i = 1;\n while (i * i <= n) {\n if (!f(n - i * i, memo)) {\n memo[n] = true;\n return tru...
1,617
<p>Alice and Bob take turns playing a game, with Alice starting first.</p> <p>Initially, there are <code>n</code> stones in a pile. On each player&#39;s turn, that player makes a <em>move</em> consisting of removing <strong>any</strong> non-zero <strong>square number</strong> of stones in the pile.</p> <p>Also, if a ...
2
{ "code": "class Solution {\npublic:\n bool decide(int n, vector<int> &dp) {\n if(n == 0) {\n return false;\n }\n if(dp[n] != -1) {\n return dp[n];\n }\n for(int i = 1; i * i <= n; i++) {\n if(!decide(n - i * i, dp)) {\n return dp[n...
1,617
<p>Alice and Bob take turns playing a game, with Alice starting first.</p> <p>Initially, there are <code>n</code> stones in a pile. On each player&#39;s turn, that player makes a <em>move</em> consisting of removing <strong>any</strong> non-zero <strong>square number</strong> of stones in the pile.</p> <p>Also, if a ...
2
{ "code": "class Solution {\n int solve(int n, vector<int>& dp)\n {\n // Base Case\n // if(n < 0)\n // {\n // return 0;\n // }\n\n if(dp[n] != -1)\n {\n return dp[n];\n }\n\n for(int i=1;i*i <= n ;i++)\n {\n if(solve...
1,617
<p>Alice and Bob take turns playing a game, with Alice starting first.</p> <p>Initially, there are <code>n</code> stones in a pile. On each player&#39;s turn, that player makes a <em>move</em> consisting of removing <strong>any</strong> non-zero <strong>square number</strong> of stones in the pile.</p> <p>Also, if a ...
3
{ "code": "class Solution {\npublic:\n bool solve(int n, vector<int>& dp){// can anyone win when having n stones and it's his/her own turn\n if(n == 0){\n return false;\n }\n\n if(dp[n] != -1)\n return dp[n];\n \n for (int i = 1; i*i <= n; i++) {\n ...
1,617
<p>Alice and Bob take turns playing a game, with Alice starting first.</p> <p>Initially, there are <code>n</code> stones in a pile. On each player&#39;s turn, that player makes a <em>move</em> consisting of removing <strong>any</strong> non-zero <strong>square number</strong> of stones in the pile.</p> <p>Also, if a ...
3
{ "code": "class Solution {\npublic:\nint dp[100001][2];\n bool find(int n,int t)\n {\n if(n==0) return !t;\n if(n==1) return t;\n int x=sqrt(n);\n if((x*x)==n) return t;\n if(dp[n][t]!=-1) return dp[n][t];\n int k=1;bool ans=false;\n if(t==0) ans=true;\n ...
1,617
<p>Alice and Bob take turns playing a game, with Alice starting first.</p> <p>Initially, there are <code>n</code> stones in a pile. On each player&#39;s turn, that player makes a <em>move</em> consisting of removing <strong>any</strong> non-zero <strong>square number</strong> of stones in the pile.</p> <p>Also, if a ...
3
{ "code": "class Solution {\npublic:\n int dp[1000001];\n\n int func(int n){\n if(n<=0) return 0;\n if(dp[n]!=-1) return dp[n];\n\n for(int i=1;i*i<=n;i++){\n if(func(n-i*i) == 0){\n return dp[n] = 1;\n }\n }\n return dp[n] = 0;\n }\n\n ...
1,617
<p>Alice and Bob take turns playing a game, with Alice starting first.</p> <p>Initially, there are <code>n</code> stones in a pile. On each player&#39;s turn, that player makes a <em>move</em> consisting of removing <strong>any</strong> non-zero <strong>square number</strong> of stones in the pile.</p> <p>Also, if a ...
3
{ "code": "class Solution {\npublic:\n int dp[1000000];\n int solve(int n)\n {\n if(n <= 0) return 0;\n if(dp[n] != -1) return dp[n];\n for(int i=1;i*i<=n ;i++)\n {\n if(solve(n - i*i) == 0) return dp[n] = 1;\n }\n return dp[n] = 0;\n }\n bool winner...
1,617
<p>Alice and Bob take turns playing a game, with Alice starting first.</p> <p>Initially, there are <code>n</code> stones in a pile. On each player&#39;s turn, that player makes a <em>move</em> consisting of removing <strong>any</strong> non-zero <strong>square number</strong> of stones in the pile.</p> <p>Also, if a ...
3
{ "code": "class Solution {\npublic:\n bool solve(int n, int cnt, vector<int>& dp){\n if(n == 0){\n return (n % 2);\n }\n\n if(dp[n] != -1)\n return dp[n];\n \n for (int i = 1; i*i <= n; i++) {\n if(i*i > n)\n break;\n if...
1,617
<p>Alice and Bob take turns playing a game, with Alice starting first.</p> <p>Initially, there are <code>n</code> stones in a pile. On each player&#39;s turn, that player makes a <em>move</em> consisting of removing <strong>any</strong> non-zero <strong>square number</strong> of stones in the pile.</p> <p>Also, if a ...
3
{ "code": "class Solution {\n\nint dp[1000001];\nint winner(int n){\n\n if(n == 0) return 0;\n if(dp[n] != -1) return dp[n];\n\n bool ans = 0;\n for(int i = 1; i*i <= n; i++){\n bool temp = winner(n-(i*i));\n if(temp == 0) ans = 1;\n }\n return dp[n] = ans;\n}\npublic:\n bool winner...
1,617
<p>Alice and Bob take turns playing a game, with Alice starting first.</p> <p>Initially, there are <code>n</code> stones in a pile. On each player&#39;s turn, that player makes a <em>move</em> consisting of removing <strong>any</strong> non-zero <strong>square number</strong> of stones in the pile.</p> <p>Also, if a ...
3
{ "code": "class Solution {\npublic:\n int dp[1000001];\n int solve(int n)\n {\n \n if(n<=0)return 0;\n if(dp[n]!=-1)return dp[n];\n for(int i=1;i*i<=n;i++)\n {\n if(solve(n-i*i)==0)return dp[n] = 1;\n }\n return 0;\n }\n bool winnerSquareGame(...
1,617
<p>Alice and Bob take turns playing a game, with Alice starting first.</p> <p>Initially, there are <code>n</code> stones in a pile. On each player&#39;s turn, that player makes a <em>move</em> consisting of removing <strong>any</strong> non-zero <strong>square number</strong> of stones in the pile.</p> <p>Also, if a ...
3
{ "code": "class Solution {\npublic:\n vector<int>sq;\n void makesqrt(int n){\n for(int i = 1; i<=sqrt(n); i++){\n sq.push_back(i * i);\n }\n }\n bool winnerSquareGame(int n) {\n makesqrt(n);\n // for(auto it : sq)cout<<it<<\" \";\n // cout<<endl;\...
1,617
<p>Alice and Bob take turns playing a game, with Alice starting first.</p> <p>Initially, there are <code>n</code> stones in a pile. On each player&#39;s turn, that player makes a <em>move</em> consisting of removing <strong>any</strong> non-zero <strong>square number</strong> of stones in the pile.</p> <p>Also, if a ...
3
{ "code": "class Solution {\npublic:\n bool Recurssion(int isAlice, int n,vector<vector<int>>&dp) {\n // if last is alice then false\n if (n <= 0 && isAlice)\n return false;\n if (n <= 0 && !isAlice)\n return true;\n if(dp[isAlice][n]!=-1) return dp[isAlice][n];\n\...
1,617
<p>Alice and Bob take turns playing a game, with Alice starting first.</p> <p>Initially, there are <code>n</code> stones in a pile. On each player&#39;s turn, that player makes a <em>move</em> consisting of removing <strong>any</strong> non-zero <strong>square number</strong> of stones in the pile.</p> <p>Also, if a ...
3
{ "code": "class Solution {\n vector<int> vec;\n\n bool solve(int n,vector<int> &dp) {\n if(n <= 0) return false;\n if(dp[n] != -1) return dp[n] == 1;\n\n for(int i=0;i<vec.size();i++) {\n if(vec[i] > n) break;\n\n if(!solve(n-vec[i],dp)) {\n dp[n] = 1;\...
1,617
<p>Alice and Bob take turns playing a game, with Alice starting first.</p> <p>Initially, there are <code>n</code> stones in a pile. On each player&#39;s turn, that player makes a <em>move</em> consisting of removing <strong>any</strong> non-zero <strong>square number</strong> of stones in the pile.</p> <p>Also, if a ...
3
{ "code": "class Solution {\nprivate:\n vector<int> generate_squares(int n){\n vector<int> squares;\n for(int i = 1; i * i <= n; i++)\n squares.push_back(i*i);\n \n return squares;\n }\n int calculate(int n, vector<int> &dp, vector<int> &squares){\n if(n == 0)\n ...
1,617
<p>Alice and Bob take turns playing a game, with Alice starting first.</p> <p>Initially, there are <code>n</code> stones in a pile. On each player&#39;s turn, that player makes a <em>move</em> consisting of removing <strong>any</strong> non-zero <strong>square number</strong> of stones in the pile.</p> <p>Also, if a ...
3
{ "code": "class Solution {\npublic:\n int dp[2][100001];\n bool solve(int n,int par,vector<int>&v)\n { \n if(par==1&&n==0)\n {\n return true;\n }\n if(dp[par][n]!=-1)return dp[par][n];\n \n bool ans=false;\n if(par==1)ans=true;\n for(int j=0...
1,617
<p>Alice and Bob take turns playing a game, with Alice starting first.</p> <p>Initially, there are <code>n</code> stones in a pile. On each player&#39;s turn, that player makes a <em>move</em> consisting of removing <strong>any</strong> non-zero <strong>square number</strong> of stones in the pile.</p> <p>Also, if a ...
3
{ "code": "class Solution {\npublic:\n int dp[2][100001];\n bool solve(int n,int par,vector<int>&v)\n { \n if(par==1&&n==0)\n {\n return true;\n }\n if(dp[par][n]!=-1)return dp[par][n];\n \n bool ans=false;\n if(par==1)ans=true;\n for(int j=0...
1,617
<p>Alice and Bob take turns playing a game, with Alice starting first.</p> <p>Initially, there are <code>n</code> stones in a pile. On each player&#39;s turn, that player makes a <em>move</em> consisting of removing <strong>any</strong> non-zero <strong>square number</strong> of stones in the pile.</p> <p>Also, if a ...
3
{ "code": "class Solution {\npublic:\n int solve(int turn, int n, vector<vector<int>> &dp) {\n if(turn == 1 && n == 0) return 0;\n if(turn == 0 && n == 0) return 1;\n\n if(dp[turn][n] != -1) return dp[turn][n];\n\n if(turn) {\n int ans = INT_MIN;\n for(int i = 1; (...
1,617
<p>Alice and Bob take turns playing a game, with Alice starting first.</p> <p>Initially, there are <code>n</code> stones in a pile. On each player&#39;s turn, that player makes a <em>move</em> consisting of removing <strong>any</strong> non-zero <strong>square number</strong> of stones in the pile.</p> <p>Also, if a ...
3
{ "code": "class Solution {\npublic:\n int solve(int turn, int n, vector<vector<int>> &dp) {\n if(turn == 1 && n == 0) return 0;\n if(turn == 0 && n == 0) return 1;\n\n if(dp[turn][n] != -1) return dp[turn][n];\n\n if(turn) {\n int ans = false;\n for(int i = 1; (i ...
1,617
<p>Alice and Bob take turns playing a game, with Alice starting first.</p> <p>Initially, there are <code>n</code> stones in a pile. On each player&#39;s turn, that player makes a <em>move</em> consisting of removing <strong>any</strong> non-zero <strong>square number</strong> of stones in the pile.</p> <p>Also, if a ...
3
{ "code": "class Solution {\npublic:\n bool dp[2][100001];\n map<pair<int,int>,int>m;\n bool dfs2(int& i, int& n){\n if(n==0){\n return true;\n }\n\n if(m.count({1,n})){\n return dp[1][n];\n }\n\n int g = sqrt(n);\n\n bool ans = false;\n\n ...
1,617
<p>Alice and Bob take turns playing a game, with Alice starting first.</p> <p>Initially, there are <code>n</code> stones in a pile. On each player&#39;s turn, that player makes a <em>move</em> consisting of removing <strong>any</strong> non-zero <strong>square number</strong> of stones in the pile.</p> <p>Also, if a ...
3
{ "code": "class Solution {\npublic:\n\n vector<vector<int>> dp;\n \n bool solve(int n,int turn){\n\n bool ans = false;\n if(turn==0){\n ans = true;\n }\n\n if(dp[turn][n]!=-1){\n return dp[turn][n];\n }\n\n for(int i=1;i*i<=n;i++){\n ...
1,617
<p>Alice and Bob take turns playing a game, with Alice starting first.</p> <p>Initially, there are <code>n</code> stones in a pile. On each player&#39;s turn, that player makes a <em>move</em> consisting of removing <strong>any</strong> non-zero <strong>square number</strong> of stones in the pile.</p> <p>Also, if a ...
3
{ "code": "class Solution {\npublic:\n bool winnerSquareGame(int n) {\n vector memo(2, vector<int>(n+1, -1));\n return doesAliceWin(n, 0, memo);\n }\nprivate:\n bool doesAliceWin(int n, int turn, vector<vector<int>>& memo) {\n if (n == 0) return turn ? true : false;\n if (memo[tur...
1,617
<p>Alice and Bob take turns playing a game, with Alice starting first.</p> <p>Initially, there are <code>n</code> stones in a pile. On each player&#39;s turn, that player makes a <em>move</em> consisting of removing <strong>any</strong> non-zero <strong>square number</strong> of stones in the pile.</p> <p>Also, if a ...
3
{ "code": "class Solution {\n\nint dp[1000001][2];\nint winner(int n, int t){\n\n if(n == 0) return 0;\n if(dp[n][t] != -1) return dp[n][t];\n\n if(t == 0){\n bool ans = 0;\n for(int i = 1; i*i <= n; i++){\n bool temp = winner(n-(i*i), !t);\n if(temp == 0) ans = 1;\n ...
1,617
<p>Alice and Bob take turns playing a game, with Alice starting first.</p> <p>Initially, there are <code>n</code> stones in a pile. On each player&#39;s turn, that player makes a <em>move</em> consisting of removing <strong>any</strong> non-zero <strong>square number</strong> of stones in the pile.</p> <p>Also, if a ...
3
{ "code": "class Solution {\npublic:\n bool winnerSquareGame(int n) {\n vector<int> g(n+1);\n int ni = 0;\n for (int i=1; i<=n; ++i) {\n if ((ni+1)*(ni+1) == i) ++ni;\n vector<bool> check(ni+1);\n for (int j=1; j*j<=i; ++j) if (g[i-j*j] <= ni) check[g[i-j*j]] =...
1,617
<p>Alice and Bob take turns playing a game, with Alice starting first.</p> <p>Initially, there are <code>n</code> stones in a pile. On each player&#39;s turn, that player makes a <em>move</em> consisting of removing <strong>any</strong> non-zero <strong>square number</strong> of stones in the pile.</p> <p>Also, if a ...
3
{ "code": "class Solution {\npublic:\n bool winnerSquareGame(int n) {\n vector<vector<int>>dp(n+1,vector<int>(2,0));\n for(int i=1;i<=n;i++)\n {\n for(int j=1;j<=sqrt(i);j++)\n {\n if(dp[i-(j*j)][0]==0)\n {\n dp[i][1]=1;\n ...
1,617
<p>Alice and Bob take turns playing a game, with Alice starting first.</p> <p>Initially, there are <code>n</code> stones in a pile. On each player&#39;s turn, that player makes a <em>move</em> consisting of removing <strong>any</strong> non-zero <strong>square number</strong> of stones in the pile.</p> <p>Also, if a ...
3
{ "code": "class Solution {\npublic:\n // bool recursion(int n,int t,vector<vector<int>> &dp){\n // if(n==0)\n // return t;\n // if(dp[n][t]!=-1)\n // return dp[n][t];\n // if(t==0){\n // bool ans=false;\n // for(int i=1;i<=sqrt(n);++i){\n // ...
1,617
<p>Alice and Bob take turns playing a game, with Alice starting first.</p> <p>Initially, there are <code>n</code> stones in a pile. On each player&#39;s turn, that player makes a <em>move</em> consisting of removing <strong>any</strong> non-zero <strong>square number</strong> of stones in the pile.</p> <p>Also, if a ...
3
{ "code": "class Solution {\npublic:\n bool winnerSquareGame(int n) {\n // memset(dp, -1, sizeof(dp));\n vector<vector<int>> dp(n+1, vector<int> (2, -1));\n for(int i = 0; i<= n; i++){\n for(int j = 0; j < 2;j++){\n dp[i][j] = (j ? true : false);\n for(...
1,617
<p>Alice and Bob take turns playing a game, with Alice starting first.</p> <p>Initially, there are <code>n</code> stones in a pile. On each player&#39;s turn, that player makes a <em>move</em> consisting of removing <strong>any</strong> non-zero <strong>square number</strong> of stones in the pile.</p> <p>Also, if a ...
3
{ "code": "class Solution {\n vector<vector<int>> dp;\n bool solve(int n,bool turn)\n {\n if(n==0)\n return !turn;\n if(dp[n][turn]!=-1)\n return dp[n][turn];\n for(int i=sqrt(n);i>=1;i--)\n {\n\n if(solve(n-i*i,!turn)==turn)\n return dp[n][turn...
1,617
<p>Alice and Bob take turns playing a game, with Alice starting first.</p> <p>Initially, there are <code>n</code> stones in a pile. On each player&#39;s turn, that player makes a <em>move</em> consisting of removing <strong>any</strong> non-zero <strong>square number</strong> of stones in the pile.</p> <p>Also, if a ...
3
{ "code": "class Solution {\npublic:\n bool winnerSquareGame(int n) {\n vector<vector<int>> dp(n+1,vector<int>(2,0));\n dp[0][0]=1;\n dp[0][1]=0;\n bool flag;\n for(int i=1;i<=n;i++)\n {\n // turn -> 0 alice\n flag=false;\n for(int j=sqrt(i...
1,617
<p>Alice and Bob take turns playing a game, with Alice starting first.</p> <p>Initially, there are <code>n</code> stones in a pile. On each player&#39;s turn, that player makes a <em>move</em> consisting of removing <strong>any</strong> non-zero <strong>square number</strong> of stones in the pile.</p> <p>Also, if a ...
3
{ "code": "#include <vector>\n#include <cmath>\n\nclass Solution {\npublic:\n std::vector<std::optional<bool>> dp; // Optional to handle null values in C++\n\n Solution() : dp(100000 + 1, std::nullopt) {} // Initialize dp with nullopt (equivalent to null)\n\n bool winnerSquareGame(int n) {\n if (dp[n]...
1,617
<p>Alice and Bob take turns playing a game, with Alice starting first.</p> <p>Initially, there are <code>n</code> stones in a pile. On each player&#39;s turn, that player makes a <em>move</em> consisting of removing <strong>any</strong> non-zero <strong>square number</strong> of stones in the pile.</p> <p>Also, if a ...
3
{ "code": "class Solution {\npublic:\nbool isSquareNumber(int n) {\n if (n < 0) {\n return false;\n }\n int sqrt_n = static_cast<int>(sqrt(n));\n return sqrt_n * sqrt_n == n;\n }\n\n \n bool winnerSquareGame(int n) {\n unordered_map<int,bool> um;\n um[1]=true;\n um[2]=f...
1,617
<p>Alice and Bob take turns playing a game, with Alice starting first.</p> <p>Initially, there are <code>n</code> stones in a pile. On each player&#39;s turn, that player makes a <em>move</em> consisting of removing <strong>any</strong> non-zero <strong>square number</strong> of stones in the pile.</p> <p>Also, if a ...
3
{ "code": "class Solution {\npublic:\n bool winnerSquareGame(int n) {\n vector<int> v;\n unordered_map<int,int> mp;\n for(int i=1;i<=sqrt(n+1);i++) {\n v.push_back(i*i);\n mp[i*i]=1;\n }\n vector<int> dp(n+1,-1);\n dp[0]=1;\n dp[1]=1;\n ...
1,617
<p>Alice and Bob take turns playing a game, with Alice starting first.</p> <p>Initially, there are <code>n</code> stones in a pile. On each player&#39;s turn, that player makes a <em>move</em> consisting of removing <strong>any</strong> non-zero <strong>square number</strong> of stones in the pile.</p> <p>Also, if a ...
3
{ "code": "class Solution {\npublic:\n\n int func(int num,int fl,vector<vector<int>>& dp){\n\n if(num==0){\n if(fl==0) return 0;\n return 1;\n }\n\n if(dp[num][fl]!=-1) return dp[num][fl];\n\n if(fl==0){\n int ans=0;\n for(int i=1;(i*i)<=num;i+...
1,617
<p>Alice and Bob take turns playing a game, with Alice starting first.</p> <p>Initially, there are <code>n</code> stones in a pile. On each player&#39;s turn, that player makes a <em>move</em> consisting of removing <strong>any</strong> non-zero <strong>square number</strong> of stones in the pile.</p> <p>Also, if a ...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> dp;\n bool solve(int n , int turn){\n if(n == 0){\n return turn == 2;\n }\n\n if(dp[n][turn] != -1) return dp[n][turn];\n\n bool ans = false;\n if(turn == 2){\n ans = true;\n for(int i=...
1,617
<p>Alice and Bob take turns playing a game, with Alice starting first.</p> <p>Initially, there are <code>n</code> stones in a pile. On each player&#39;s turn, that player makes a <em>move</em> consisting of removing <strong>any</strong> non-zero <strong>square number</strong> of stones in the pile.</p> <p>Also, if a ...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> dp;\n bool f(int n, int alice){\n if(dp[n][alice]!=-1) return dp[n][alice];\n if(n==0 or n==2) return dp[n][alice] = !alice;\n if(n==1) return dp[n][alice] = alice;\n int x = sqrt(n);\n bool ans = false;\n if(al...
1,617
<p>Alice and Bob take turns playing a game, with Alice starting first.</p> <p>Initially, there are <code>n</code> stones in a pile. On each player&#39;s turn, that player makes a <em>move</em> consisting of removing <strong>any</strong> non-zero <strong>square number</strong> of stones in the pile.</p> <p>Also, if a ...
3
{ "code": "class Solution {\npublic:\n bool winnerSquareGame(int n) {\n vector<vector<bool>> dp(n+1,vector<bool>(2,0));\n dp[0][0]=0;\n dp[0][1]=1;\n for(int i=1;i<=n;i++){\n for(int j=0;j<=1;j++){\n int answer=j;\n for(int k=1;k<=sqrt(i);k++){\n...
1,617
<p>Alice and Bob take turns playing a game, with Alice starting first.</p> <p>Initially, there are <code>n</code> stones in a pile. On each player&#39;s turn, that player makes a <em>move</em> consisting of removing <strong>any</strong> non-zero <strong>square number</strong> of stones in the pile.</p> <p>Also, if a ...
3
{ "code": "class Solution {\npublic:\n bool winnerSquareGame(int n) {\n vector<int> perfectSquares;\n\n for(int num = 1; num * num <= n; ++num)\n perfectSquares.push_back(num * num);\n \n vector<vector<bool>> dp(n + 1, vector<bool>(2, false));\n\n for(int num = 1; ...
1,617
<p>Alice and Bob take turns playing a game, with Alice starting first.</p> <p>Initially, there are <code>n</code> stones in a pile. On each player&#39;s turn, that player makes a <em>move</em> consisting of removing <strong>any</strong> non-zero <strong>square number</strong> of stones in the pile.</p> <p>Also, if a ...
3
{ "code": "class Solution {\npublic:\n bool winnerSquareGame(int n) {\n vector<int> perfectSquares;\n\n for(int num = 1; num * num <= n; ++num)\n perfectSquares.push_back(num * num);\n \n vector<vector<bool>> dp(n + 1, vector<bool>(2, false));\n\n for(int num = 1; ...
1,617
<p>Alice and Bob take turns playing a game, with Alice starting first.</p> <p>Initially, there are <code>n</code> stones in a pile. On each player&#39;s turn, that player makes a <em>move</em> consisting of removing <strong>any</strong> non-zero <strong>square number</strong> of stones in the pile.</p> <p>Also, if a ...
3
{ "code": "\nclass Solution {\npublic:\n // Method to check whether Alice can win the game or not, using 2D tabulation - O(N*sqrt(N)) & O(N)\n bool winnerSquareGame(int n) {\n vector<int> perfectSquares;\n\n // Store all the perfect square numbers lying within 1 to n\n for(int num = 1; num ...
1,617
<p>Alice and Bob take turns playing a game, with Alice starting first.</p> <p>Initially, there are <code>n</code> stones in a pile. On each player&#39;s turn, that player makes a <em>move</em> consisting of removing <strong>any</strong> non-zero <strong>square number</strong> of stones in the pile.</p> <p>Also, if a ...
3
{ "code": "class Solution {\npublic:\n bool winnerSquareGame(int n) {\n vector<int> perfectSquares;\n\n for(int num = 1; num * num <= n; ++num)\n perfectSquares.push_back(num * num);\n \n vector<vector<bool>> dp(n + 1, vector<bool>(2, false));\n\n for(int num = 1; ...
1,617
<p>Alice and Bob take turns playing a game, with Alice starting first.</p> <p>Initially, there are <code>n</code> stones in a pile. On each player&#39;s turn, that player makes a <em>move</em> consisting of removing <strong>any</strong> non-zero <strong>square number</strong> of stones in the pile.</p> <p>Also, if a ...
3
{ "code": "class Solution {\npublic:\n bool winnerSquareGame(int n) {\n vector<int> perfectSquares;\n\n for(int num = 1; num * num <= n; ++num)\n perfectSquares.push_back(num * num);\n \n vector<vector<bool>> dp(n + 1, vector<bool>(2, false));\n\n for(int num = 1; ...
1,617
<p>Alice and Bob take turns playing a game, with Alice starting first.</p> <p>Initially, there are <code>n</code> stones in a pile. On each player&#39;s turn, that player makes a <em>move</em> consisting of removing <strong>any</strong> non-zero <strong>square number</strong> of stones in the pile.</p> <p>Also, if a ...
3
{ "code": "class Solution {\nprivate:\n bool f(int n, int turn, vector<vector<int>>& dp) {\n int x = sqrt(n);\n // cout<<x<<\" \";\n if (x*x == n) {\n if (turn == 1) return 1;\n else return 0;\n }\n if (dp[n][turn] != -1) return dp[n][turn];\n if (tur...
1,617
<p>Alice and Bob take turns playing a game, with Alice starting first.</p> <p>Initially, there are <code>n</code> stones in a pile. On each player&#39;s turn, that player makes a <em>move</em> consisting of removing <strong>any</strong> non-zero <strong>square number</strong> of stones in the pile.</p> <p>Also, if a ...
3
{ "code": "class Solution {\npublic:\n bool solve(int n,int turn,vector<vector<int>>&dp){\n \n if(turn == 1 && n==0)return true;\n if(turn == 0 && n==0)return false;\n\n if(dp[n][turn]!=-1)return dp[n][turn];\n\n bool ans=(turn == 0)?false:true;\n\n for(int i=1;i*i<...
1,617
<p>Alice and Bob take turns playing a game, with Alice starting first.</p> <p>Initially, there are <code>n</code> stones in a pile. On each player&#39;s turn, that player makes a <em>move</em> consisting of removing <strong>any</strong> non-zero <strong>square number</strong> of stones in the pile.</p> <p>Also, if a ...
3
{ "code": "class Solution {\npublic:\n\n int solve(int left, bool alice,vector<vector<int>>& dp){\n if(left<=0)return false;\n if(dp[left][alice]!=-1)return dp[left][alice];\n for(int i=1;i*i<=left;i++){\n bool curr=solve(left-(i*i),!alice,dp);\n if(curr==0)return dp[left...
1,617
<p>Alice and Bob take turns playing a game, with Alice starting first.</p> <p>Initially, there are <code>n</code> stones in a pile. On each player&#39;s turn, that player makes a <em>move</em> consisting of removing <strong>any</strong> non-zero <strong>square number</strong> of stones in the pile.</p> <p>Also, if a ...
3
{ "code": "class Solution {\npublic:\n bool helper(int n,int j,vector<vector<int>> &dp){\n if(n==0){\n return false;\n }\n if(dp[n][j]!=-1){\n return dp[n][j];\n }\n for(int i=1;i*i<=n;i++){\n if(j==0){\n if(!helper(n-i*i,1,dp)) ret...
1,617
<p>Alice and Bob take turns playing a game, with Alice starting first.</p> <p>Initially, there are <code>n</code> stones in a pile. On each player&#39;s turn, that player makes a <em>move</em> consisting of removing <strong>any</strong> non-zero <strong>square number</strong> of stones in the pile.</p> <p>Also, if a ...
3
{ "code": "class Solution {\n int memo(int num,int turn,vector<vector<int>>& dp){\n //basecase\n if(num==0){\n return turn==1;\n }\n //cache check\n if(dp[num][turn]!=-1) return dp[num][turn];\n //compute\n int res;\n if(turn==0){ //alice\n ...
1,617
<p>Alice and Bob take turns playing a game, with Alice starting first.</p> <p>Initially, there are <code>n</code> stones in a pile. On each player&#39;s turn, that player makes a <em>move</em> consisting of removing <strong>any</strong> non-zero <strong>square number</strong> of stones in the pile.</p> <p>Also, if a ...
3
{ "code": "class Solution {\npublic:\n int func (int i,int turn ,vector <vector <int > > & dp){\n if (i==0){\n if (turn==1)return 1;\n else return 0;\n }\n if (dp[i][turn]!=-1)return dp[i][turn];\n int ans=0;\n if (turn==0){\n for (int j=1;j*j<=i;...
1,617
<p>Alice and Bob take turns playing a game, with Alice starting first.</p> <p>Initially, there are <code>n</code> stones in a pile. On each player&#39;s turn, that player makes a <em>move</em> consisting of removing <strong>any</strong> non-zero <strong>square number</strong> of stones in the pile.</p> <p>Also, if a ...
3
{ "code": "class Solution {\npublic:\n bool solve(int n,int turn,vector<vector<int>> &dp)\n {\n if(n<=0)\n return turn;\n\n if(dp[n][turn]!=-1)\n return dp[n][turn];\n\n bool ans=(turn==1);\n if(turn==0)\n {\n for(int i=1;i*i<=n;i++)\n {\n ...
1,617
<p>Alice and Bob take turns playing a game, with Alice starting first.</p> <p>Initially, there are <code>n</code> stones in a pile. On each player&#39;s turn, that player makes a <em>move</em> consisting of removing <strong>any</strong> non-zero <strong>square number</strong> of stones in the pile.</p> <p>Also, if a ...
3
{ "code": "// Memoization\nclass Solution {\n public:\n static vector<int> power;\n Solution()\n {\n int n=100001;\n int i=1;\n int lastSquare=1;\n while((i*i)<=n)\n {\n power.push_back(i*i);\n i++;\n }\n }\n \n int help(int n,int tu...
1,617
<p>Alice and Bob take turns playing a game, with Alice starting first.</p> <p>Initially, there are <code>n</code> stones in a pile. On each player&#39;s turn, that player makes a <em>move</em> consisting of removing <strong>any</strong> non-zero <strong>square number</strong> of stones in the pile.</p> <p>Also, if a ...
3
{ "code": "class Solution {\npublic:\nunordered_map<int , unordered_map<int , int>>check ; \nint solve(int n , int chance){\n if(check[n].count(chance))\n return check[n][chance] ; \n int temp = 0 ; \n if(n==0){\n return check[n][chance] = 1 ; \n }\n if(chance==0){\n int m = sqrt(n) ...
1,617
<p>Alice and Bob take turns playing a game, with Alice starting first.</p> <p>Initially, there are <code>n</code> stones in a pile. On each player&#39;s turn, that player makes a <em>move</em> consisting of removing <strong>any</strong> non-zero <strong>square number</strong> of stones in the pile.</p> <p>Also, if a ...
3
{ "code": "class Solution {\npublic:\nunordered_map<int , unordered_map<int , int>>check ; \nint solve(int n , int chance){\n if(check[n].count(chance))\n return check[n][chance] ; \n int temp = 0 ; \n if(n==0){\n return check[n][chance] = 1 ; \n }\n if(chance==0){\n int m = sqrt(n) ...
1,617
<p>Alice and Bob take turns playing a game, with Alice starting first.</p> <p>Initially, there are <code>n</code> stones in a pile. On each player&#39;s turn, that player makes a <em>move</em> consisting of removing <strong>any</strong> non-zero <strong>square number</strong> of stones in the pile.</p> <p>Also, if a ...
3
{ "code": "class Solution {\npublic:\n bool solve(int n, int turn, vector<vector<int>>& dp) {\n if (n == 0) {\n return false;\n }\n\n if (dp[n][turn] != -1) {\n return dp[n][turn];\n }\n\n bool canWin = false;\n for (int i = 1; i * i <= n; ++i) {\n\n ...
2,469
<p>You are given an integer array <code>nums</code> of length <code>n</code>, and an integer array <code>queries</code> of length <code>m</code>.</p> <p>Return <em>an array </em><code>answer</code><em> of length </em><code>m</code><em> where </em><code>answer[i]</code><em> is the <strong>maximum</strong> size of a <st...
0
{ "code": "class Solution {\npublic:\n vector<int> answerQueries(vector<int>& nums, vector<int>& queries) {\n int n=nums.size();\n int m=queries.size();\n vector<int> ans(m);\n sort(nums.begin(),nums.end()); //n*logn\n // nums[0] as it is\n for(int i=1;i<n;i++){ ...
2,469
<p>You are given an integer array <code>nums</code> of length <code>n</code>, and an integer array <code>queries</code> of length <code>m</code>.</p> <p>Return <em>an array </em><code>answer</code><em> of length </em><code>m</code><em> where </em><code>answer[i]</code><em> is the <strong>maximum</strong> size of a <st...
0
{ "code": "class Solution {\npublic:\n vector<int> answerQueries(vector<int>& nums, vector<int>& queries) {\n int n=nums.size();\n int m=queries.size();\n vector<int> ans(m);\n sort(nums.begin(),nums.end());\n for(int i=1;i<n;i++){\n nums[i] += nums[i-1];\n }\n ...
2,469
<p>You are given an integer array <code>nums</code> of length <code>n</code>, and an integer array <code>queries</code> of length <code>m</code>.</p> <p>Return <em>an array </em><code>answer</code><em> of length </em><code>m</code><em> where </em><code>answer[i]</code><em> is the <strong>maximum</strong> size of a <st...
0
{ "code": "class Solution {\npublic:\n vector<int> answerQueries(vector<int>& nums, vector<int>& queries) {\n int n = nums.size();\n int m = queries.size();\n vector<int> ans(m);\n sort(nums.begin(),nums.end());\n for(int i = 1 ; i<n ; i++){ nums[i]=nums[i-1]+nums[i];\n...
2,469
<p>You are given an integer array <code>nums</code> of length <code>n</code>, and an integer array <code>queries</code> of length <code>m</code>.</p> <p>Return <em>an array </em><code>answer</code><em> of length </em><code>m</code><em> where </em><code>answer[i]</code><em> is the <strong>maximum</strong> size of a <st...
0
{ "code": "class Solution {\npublic:\n vector<int> answerQueries(vector<int>& nums, vector<int>& queries) {\n int n=nums.size();\n int m=queries.size();\n vector<int>ans(m);\n // sort nums\n sort(nums.begin(),nums.end());\n // make nums prefix sum\n for(int i=1;i<n;...
2,469
<p>You are given an integer array <code>nums</code> of length <code>n</code>, and an integer array <code>queries</code> of length <code>m</code>.</p> <p>Return <em>an array </em><code>answer</code><em> of length </em><code>m</code><em> where </em><code>answer[i]</code><em> is the <strong>maximum</strong> size of a <st...
0
{ "code": "class Solution {\npublic:\n vector<int> answerQueries(vector<int>& nums, vector<int>& queries) {\n int n = nums.size();\n int m = queries.size();\n vector<int> ans(m);\n sort(nums.begin(),nums.end());\n for(int i = 1 ; i<n ; i++){\n nums[i]=nums[i-1]+nums[i];...
2,469
<p>You are given an integer array <code>nums</code> of length <code>n</code>, and an integer array <code>queries</code> of length <code>m</code>.</p> <p>Return <em>an array </em><code>answer</code><em> of length </em><code>m</code><em> where </em><code>answer[i]</code><em> is the <strong>maximum</strong> size of a <st...
0
{ "code": "class Solution {\npublic:\n vector<int> answerQueries(vector<int>& nums, vector<int>& queries) {\n // this wil be solved by (sorting, binary searching, and prefix sum)\n int n = nums.size();\n int m = queries.size();\n vector<int>ans(m);\n //sort\n sort(nums.begin()...
2,469
<p>You are given an integer array <code>nums</code> of length <code>n</code>, and an integer array <code>queries</code> of length <code>m</code>.</p> <p>Return <em>an array </em><code>answer</code><em> of length </em><code>m</code><em> where </em><code>answer[i]</code><em> is the <strong>maximum</strong> size of a <st...
0
{ "code": "class Solution {\npublic:\n vector<int> answerQueries(vector<int>& nums, vector<int>& queries) {\n int n = nums.size();\n int m = queries.size();\n vector<int> ans(m);\n sort(nums.begin(),nums.end());\n for(int i = 1 ; i<n ; i++){\n nums[i]=nums[i-1]+nums[i];...
2,469
<p>You are given an integer array <code>nums</code> of length <code>n</code>, and an integer array <code>queries</code> of length <code>m</code>.</p> <p>Return <em>an array </em><code>answer</code><em> of length </em><code>m</code><em> where </em><code>answer[i]</code><em> is the <strong>maximum</strong> size of a <st...
0
{ "code": "class Solution {\npublic:\n vector<int> answerQueries(vector<int>& nums, vector<int>& queries) {\n int n = nums.size();\n int m = queries.size();\n vector<int> ans(m);\n sort(nums.begin(),nums.end());\n for(int i = 1 ; i<n ; i++){nums[i]=nums[i-1]+nums[i];\n }\n...
2,469
<p>You are given an integer array <code>nums</code> of length <code>n</code>, and an integer array <code>queries</code> of length <code>m</code>.</p> <p>Return <em>an array </em><code>answer</code><em> of length </em><code>m</code><em> where </em><code>answer[i]</code><em> is the <strong>maximum</strong> size of a <st...
1
{ "code": "class Solution {\npublic:\n vector<int> answerQueries(vector<int>& nums, vector<int>& queries) {\n int n=nums.size();\n int m=queries.size();\n sort(nums.begin(),nums.end());\n vector<int>ans;\n int x=0;\n for(int i=0;i<n;i++)\n {\n x=x+...
2,469
<p>You are given an integer array <code>nums</code> of length <code>n</code>, and an integer array <code>queries</code> of length <code>m</code>.</p> <p>Return <em>an array </em><code>answer</code><em> of length </em><code>m</code><em> where </em><code>answer[i]</code><em> is the <strong>maximum</strong> size of a <st...
1
{ "code": "// //Approach-1\n// //Brute Force : O(m*n)\n// class Solution {\n// public:\n \n// vector<int> answerQueries(vector<int>& nums, vector<int>& queries) {\n// int n = nums.size();\n \n// sort(begin(nums), end(nums));\n \n// int m = queries.size();\n// vecto...
2,469
<p>You are given an integer array <code>nums</code> of length <code>n</code>, and an integer array <code>queries</code> of length <code>m</code>.</p> <p>Return <em>an array </em><code>answer</code><em> of length </em><code>m</code><em> where </em><code>answer[i]</code><em> is the <strong>maximum</strong> size of a <st...
2
{ "code": "class Solution {\npublic:\n vector<int> answerQueries(vector<int>& nums, vector<int>& queries) {\n int n = nums.size();\n vector<int> answer;\n vector<int> sums(n + 1);\n sort(nums.begin(), nums.end());\n for (int i = 0; i < n; i++) sums[i + 1] = sums[i] + nums[i];\n ...
2,469
<p>You are given an integer array <code>nums</code> of length <code>n</code>, and an integer array <code>queries</code> of length <code>m</code>.</p> <p>Return <em>an array </em><code>answer</code><em> of length </em><code>m</code><em> where </em><code>answer[i]</code><em> is the <strong>maximum</strong> size of a <st...
2
{ "code": "class Solution {\npublic:\n vector<int> answerQueries(vector<int>& nums, vector<int>& queries) {\n sort(nums.begin(), nums.end());\n vector<int> result;\n\n for (int limit : queries) {\n int currentSum = 0;\n int count = 0;\n\n for (int num : nums) {\n if (curr...
2,469
<p>You are given an integer array <code>nums</code> of length <code>n</code>, and an integer array <code>queries</code> of length <code>m</code>.</p> <p>Return <em>an array </em><code>answer</code><em> of length </em><code>m</code><em> where </em><code>answer[i]</code><em> is the <strong>maximum</strong> size of a <st...
3
{ "code": "class Solution {\npublic:\n vector<int> answerQueries(vector<int> A, vector<int>& queries) {\n sort(A.begin(), A.end());\n vector<int> res;\n for (int i = 1; i < A.size(); ++i)\n A[i] += A[i - 1];\n for (int& q: queries)\n res.push_back(upper_bound(A.beg...
2,469
<p>You are given an integer array <code>nums</code> of length <code>n</code>, and an integer array <code>queries</code> of length <code>m</code>.</p> <p>Return <em>an array </em><code>answer</code><em> of length </em><code>m</code><em> where </em><code>answer[i]</code><em> is the <strong>maximum</strong> size of a <st...
3
{ "code": "class Solution {\nprivate:\n int findSubsequenceSize(vector<int>& preSum, int target, int l, int r) {\n\n int ind = -1;\n while (l < r) {\n int mid = (l + r) / 2;\n\n if (preSum[mid] <= target) {\n ind = mid;\n l = mid + 1;\n }...
2,469
<p>You are given an integer array <code>nums</code> of length <code>n</code>, and an integer array <code>queries</code> of length <code>m</code>.</p> <p>Return <em>an array </em><code>answer</code><em> of length </em><code>m</code><em> where </em><code>answer[i]</code><em> is the <strong>maximum</strong> size of a <st...
3
{ "code": "class Solution {\npublic:\n\n int bns(vector<int> &pref, int sum) {\n int n = pref.size();\n int low = 0;\n int high = n-1;\n\n while(low <= high) {\n int mid = low+(high-low);\n if(pref[mid] == sum) return mid;\n\n else if(pref[mid] > sum) hi...
2,469
<p>You are given an integer array <code>nums</code> of length <code>n</code>, and an integer array <code>queries</code> of length <code>m</code>.</p> <p>Return <em>an array </em><code>answer</code><em> of length </em><code>m</code><em> where </em><code>answer[i]</code><em> is the <strong>maximum</strong> size of a <st...
3
{ "code": "class Solution {\npublic:\n vector<int> answerQueries(vector<int>& nums, vector<int>& queries) {\n sort(nums.begin(), nums.end());\n vector<int> ans;\n vector<int> prefix(nums.size(), nums[0]);\n \n for(int i=1; i<nums.size(); i++)\n prefix[i] = prefix[i-1]+nums...
2,469
<p>You are given an integer array <code>nums</code> of length <code>n</code>, and an integer array <code>queries</code> of length <code>m</code>.</p> <p>Return <em>an array </em><code>answer</code><em> of length </em><code>m</code><em> where </em><code>answer[i]</code><em> is the <strong>maximum</strong> size of a <st...
3
{ "code": "class Solution {\npublic:\n vector<int> answerQueries(vector<int>& nums, vector<int>& queries) {\n vector<int> myVec(nums);\n\t\tvector<int> result;\n\t\tvector<int> prefix;\n\t\tsort(myVec.begin(), myVec.end());\n\t\tint prefSum = 0;\n\t\tfor (int num : myVec)\n\t\t{\n\t\t\tprefSum += num;\n\t\t...
2,469
<p>You are given an integer array <code>nums</code> of length <code>n</code>, and an integer array <code>queries</code> of length <code>m</code>.</p> <p>Return <em>an array </em><code>answer</code><em> of length </em><code>m</code><em> where </em><code>answer[i]</code><em> is the <strong>maximum</strong> size of a <st...
3
{ "code": "class Solution {\npublic:\n vector<int> answerQueries(vector<int>& nums, vector<int>& queries) {\n sort(nums.begin(), nums.end());\n vector<pair<int,int>> v;\n int n = nums.size(), m = queries.size();\n for (int i=0;i<m; i++) {\n v.push_back({queries[i], i});\n ...
2,470
<p>You are given a string <code>s</code>, which contains stars <code>*</code>.</p> <p>In one operation, you can:</p> <ul> <li>Choose a star in <code>s</code>.</li> <li>Remove the closest <strong>non-star</strong> character to its <strong>left</strong>, as well as remove the star itself.</li> </ul> <p>Return <em>th...
0
{ "code": "class Solution {\npublic:\n string removeStars(string& s) {\n int k = 0;\n\n for (int i = 1; i < s.size(); i++) {\n if (s[i] == '*') {\n --k;\n }\n else {\n s[++k] = s[i];\n }\n }\n\n s.resize(++k);\n\n...
2,470
<p>You are given a string <code>s</code>, which contains stars <code>*</code>.</p> <p>In one operation, you can:</p> <ul> <li>Choose a star in <code>s</code>.</li> <li>Remove the closest <strong>non-star</strong> character to its <strong>left</strong>, as well as remove the star itself.</li> </ul> <p>Return <em>th...
0
{ "code": "class Solution {\npublic:\n string removeStars(string& s) {\n int k = 0;\n\n for (int i = 1; i < s.size(); i++) {\n if (s[i] == '*') {\n --k;\n }\n else {\n s[++k] = s[i];\n }\n }\n\n s.resize(++k);\n\n...
2,470
<p>You are given a string <code>s</code>, which contains stars <code>*</code>.</p> <p>In one operation, you can:</p> <ul> <li>Choose a star in <code>s</code>.</li> <li>Remove the closest <strong>non-star</strong> character to its <strong>left</strong>, as well as remove the star itself.</li> </ul> <p>Return <em>th...
0
{ "code": "auto __untie_cin = cin.tie(nullptr);\nauto __unsync_ios_stdio = ios_base::sync_with_stdio(false);\n#define endl \"\\n\"\n\nclass Solution {\npublic:\n string removeStars(string& s) {\n int k = 0;\n\n for (int i = 1; i < s.size(); i++) {\n if (s[i] == '*') {\n --k;...
2,470
<p>You are given a string <code>s</code>, which contains stars <code>*</code>.</p> <p>In one operation, you can:</p> <ul> <li>Choose a star in <code>s</code>.</li> <li>Remove the closest <strong>non-star</strong> character to its <strong>left</strong>, as well as remove the star itself.</li> </ul> <p>Return <em>th...
0
{ "code": "auto __untie_cin = cin.tie(nullptr);\nauto __unsync_ios_stdio = ios_base::sync_with_stdio(false);\n#define endl \"\\n\"\n\nclass Solution {\npublic:\n string removeStars(string& s) {\n int k = 0;\n\n for (int i = 1; i < s.size(); i++) {\n if (s[i] == '*') {\n --k;...
2,470
<p>You are given a string <code>s</code>, which contains stars <code>*</code>.</p> <p>In one operation, you can:</p> <ul> <li>Choose a star in <code>s</code>.</li> <li>Remove the closest <strong>non-star</strong> character to its <strong>left</strong>, as well as remove the star itself.</li> </ul> <p>Return <em>th...
0
{ "code": "#include <string>\n\nclass Solution {\npublic:\n string removeStars(string& s) {\n int writeIndex = 0;\n\n for (char c : s) {\n if (c != '*') {\n s[writeIndex++] = c;\n } else if (writeIndex > 0) {\n writeIndex--; // Remove the previous c...
2,470
<p>You are given a string <code>s</code>, which contains stars <code>*</code>.</p> <p>In one operation, you can:</p> <ul> <li>Choose a star in <code>s</code>.</li> <li>Remove the closest <strong>non-star</strong> character to its <strong>left</strong>, as well as remove the star itself.</li> </ul> <p>Return <em>th...
0
{ "code": "class Solution {\npublic:\n string removeStars(string& s) {\n for(int idx{0}; idx < s.size(); idx++){\n if(s[idx] == '*'){\n s.erase((s.begin() + (idx - 1)),(s.begin() + idx + 1));\n idx -= 2;\n }\n }\n return s;\n }\n};", "me...
2,470
<p>You are given a string <code>s</code>, which contains stars <code>*</code>.</p> <p>In one operation, you can:</p> <ul> <li>Choose a star in <code>s</code>.</li> <li>Remove the closest <strong>non-star</strong> character to its <strong>left</strong>, as well as remove the star itself.</li> </ul> <p>Return <em>th...
0
{ "code": "class Solution {\npublic:\n string removeStars(string& s) {\n int i = 0;\n for (char& c: s){\n if (c != '*') s[i++] = c;\n else --i;\n }\n return s.substr(0, i);\n }\n};\n", "memory": "21538" }
2,470
<p>You are given a string <code>s</code>, which contains stars <code>*</code>.</p> <p>In one operation, you can:</p> <ul> <li>Choose a star in <code>s</code>.</li> <li>Remove the closest <strong>non-star</strong> character to its <strong>left</strong>, as well as remove the star itself.</li> </ul> <p>Return <em>th...
0
{ "code": "class Solution {\npublic:\n string removeStars(string& s) {\n cin.tie(0);\n cout.tie(0);\n ios::sync_with_stdio(0);\n int i = 0;\n for (char& c: s){\n if (c != '*') s[i++] = c;\n else --i;\n }\n return s.substr(0, i);\n }\n};\n", "memory": "21538" }
2,470
<p>You are given a string <code>s</code>, which contains stars <code>*</code>.</p> <p>In one operation, you can:</p> <ul> <li>Choose a star in <code>s</code>.</li> <li>Remove the closest <strong>non-star</strong> character to its <strong>left</strong>, as well as remove the star itself.</li> </ul> <p>Return <em>th...
0
{ "code": "class Solution {\npublic:\n string removeStars(string &s) {\n int i=0;\n for(auto k:s){\n if(k!='*')s[i++]=k;\n else i--;\n }\n return s.substr(0,i);\n }\n};\n\nstatic auto _ = []() {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nul...
2,470
<p>You are given a string <code>s</code>, which contains stars <code>*</code>.</p> <p>In one operation, you can:</p> <ul> <li>Choose a star in <code>s</code>.</li> <li>Remove the closest <strong>non-star</strong> character to its <strong>left</strong>, as well as remove the star itself.</li> </ul> <p>Return <em>th...
0
{ "code": "static auto _ = []() {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n return true;\n}();\n\n#pragma -O3\n\nclass Solution {\npublic:\n string removeStars(string& s) {\n int i = 0;\n for (char& c: s){\n if (c != '*') s[i++] = c;\n else -...
2,470
<p>You are given a string <code>s</code>, which contains stars <code>*</code>.</p> <p>In one operation, you can:</p> <ul> <li>Choose a star in <code>s</code>.</li> <li>Remove the closest <strong>non-star</strong> character to its <strong>left</strong>, as well as remove the star itself.</li> </ul> <p>Return <em>th...
0
{ "code": "class Solution {\npublic:\n std::string removeStars(std::string& s) {\n std::string ans;\n ans.reserve(s.size());\n int stars = 0;\n for (auto it =s.rbegin(); it != s.rend(); ++it) {\n if (*it == '*')\n ++stars;\n else if (stars > 0)\n ...
2,470
<p>You are given a string <code>s</code>, which contains stars <code>*</code>.</p> <p>In one operation, you can:</p> <ul> <li>Choose a star in <code>s</code>.</li> <li>Remove the closest <strong>non-star</strong> character to its <strong>left</strong>, as well as remove the star itself.</li> </ul> <p>Return <em>th...
0
{ "code": "class Solution {\npublic:\n std::string removeStars(std::string& s) {\n std::string ans;\n ans.reserve(s.size());\n int stars = 0;\n for (auto it =s.rbegin(); it != s.rend(); ++it) {\n if (*it == '*')\n ++stars;\n else if (stars > 0)\n ...
2,470
<p>You are given a string <code>s</code>, which contains stars <code>*</code>.</p> <p>In one operation, you can:</p> <ul> <li>Choose a star in <code>s</code>.</li> <li>Remove the closest <strong>non-star</strong> character to its <strong>left</strong>, as well as remove the star itself.</li> </ul> <p>Return <em>th...
0
{ "code": "class Solution {\npublic:\n std::string removeStars(std::string& s) {\n std::string ans;\n ans.reserve(s.size());\n int stars = 0;\n for (auto it =s.rbegin(); it != s.rend(); ++it) {\n if (*it == '*')\n ++stars;\n else if (stars > 0)\n ...
2,470
<p>You are given a string <code>s</code>, which contains stars <code>*</code>.</p> <p>In one operation, you can:</p> <ul> <li>Choose a star in <code>s</code>.</li> <li>Remove the closest <strong>non-star</strong> character to its <strong>left</strong>, as well as remove the star itself.</li> </ul> <p>Return <em>th...
0
{ "code": "class Solution {\npublic:\n std::string removeStars(std::string& s) {\n std::string ans;\n ans.reserve(s.size());\n int stars = 0;\n for (auto it =s.rbegin(); it != s.rend(); ++it) {\n if (*it == '*')\n ++stars;\n else if (stars > 0)\n ...
2,470
<p>You are given a string <code>s</code>, which contains stars <code>*</code>.</p> <p>In one operation, you can:</p> <ul> <li>Choose a star in <code>s</code>.</li> <li>Remove the closest <strong>non-star</strong> character to its <strong>left</strong>, as well as remove the star itself.</li> </ul> <p>Return <em>th...
0
{ "code": "class Solution {\npublic:\n std::string removeStars(std::string& s) {\n std::string ans;\n ans.reserve(s.size());\n int stars = 0;\n for (auto it =s.rbegin(); it != s.rend(); ++it) {\n if (*it == '*')\n ++stars;\n else if (stars > 0)\n ...
2,470
<p>You are given a string <code>s</code>, which contains stars <code>*</code>.</p> <p>In one operation, you can:</p> <ul> <li>Choose a star in <code>s</code>.</li> <li>Remove the closest <strong>non-star</strong> character to its <strong>left</strong>, as well as remove the star itself.</li> </ul> <p>Return <em>th...
0
{ "code": "class Solution {\npublic:\n std::string removeStars(std::string& s) {\n std::string ans;\n ans.reserve(s.size());\n int stars = 0;\n for (auto it =s.rbegin(); it != s.rend(); ++it) {\n if (*it == '*')\n ++stars;\n else if (stars > 0)\n ...
2,470
<p>You are given a string <code>s</code>, which contains stars <code>*</code>.</p> <p>In one operation, you can:</p> <ul> <li>Choose a star in <code>s</code>.</li> <li>Remove the closest <strong>non-star</strong> character to its <strong>left</strong>, as well as remove the star itself.</li> </ul> <p>Return <em>th...
0
{ "code": "class Solution\n{\npublic:\n std::string removeStars(const std::string& s)\n {\n std::size_t length = s.length(),\n count = 0;\n\n std::string result;\n\n for (auto iter = s.rbegin(); iter != s.rend(); ++iter)\n if (*iter == '*')\n ++c...
2,470
<p>You are given a string <code>s</code>, which contains stars <code>*</code>.</p> <p>In one operation, you can:</p> <ul> <li>Choose a star in <code>s</code>.</li> <li>Remove the closest <strong>non-star</strong> character to its <strong>left</strong>, as well as remove the star itself.</li> </ul> <p>Return <em>th...
0
{ "code": "class Solution {\npublic:\n std::string removeStars(const std::string& s) {\n int n = s.size();\n std::string result;\n result.reserve(n); // Reserve space to optimize performance\n \n for (int j = 0; j < n; j++) {\n if (s[j] == '*') {\n if (!...
2,470
<p>You are given a string <code>s</code>, which contains stars <code>*</code>.</p> <p>In one operation, you can:</p> <ul> <li>Choose a star in <code>s</code>.</li> <li>Remove the closest <strong>non-star</strong> character to its <strong>left</strong>, as well as remove the star itself.</li> </ul> <p>Return <em>th...
0
{ "code": "class Solution {\npublic:\n string removeStars(string& s) {\n string stack{};\n for(auto const& character: s){\n if(character == '*'){stack.pop_back();}\n else{stack.push_back(character);}\n }\n return stack;\n }\n};", "memory": "22633" }
2,470
<p>You are given a string <code>s</code>, which contains stars <code>*</code>.</p> <p>In one operation, you can:</p> <ul> <li>Choose a star in <code>s</code>.</li> <li>Remove the closest <strong>non-star</strong> character to its <strong>left</strong>, as well as remove the star itself.</li> </ul> <p>Return <em>th...
0
{ "code": "#include <string>\n\nclass Solution {\npublic:\n std::string removeStars(const std::string& s) {\n std::string result; // Use a string to simulate stack behavior\n\n for (char ch : s) {\n if (ch != '*') {\n result.push_back(ch); // Add character to the end of th...
2,470
<p>You are given a string <code>s</code>, which contains stars <code>*</code>.</p> <p>In one operation, you can:</p> <ul> <li>Choose a star in <code>s</code>.</li> <li>Remove the closest <strong>non-star</strong> character to its <strong>left</strong>, as well as remove the star itself.</li> </ul> <p>Return <em>th...
0
{ "code": "class Solution {\npublic:\n string removeStars(string& str) {\n string ans=\"\";\n for(char ch:str)\n {\n if(ch=='*' && !str.empty())\n ans.pop_back();\n else\n ans.push_back(ch);\n }\n return ans;\n }\n};", "mem...
2,470
<p>You are given a string <code>s</code>, which contains stars <code>*</code>.</p> <p>In one operation, you can:</p> <ul> <li>Choose a star in <code>s</code>.</li> <li>Remove the closest <strong>non-star</strong> character to its <strong>left</strong>, as well as remove the star itself.</li> </ul> <p>Return <em>th...
0
{ "code": "class Solution {\npublic:\n string removeStars(string &s) {\n string ans;\n for(auto &val: s) {\n if(val == '*') ans.pop_back();\n else ans.push_back(val);\n }\n return ans;\n }\n};", "memory": "23181" }
2,470
<p>You are given a string <code>s</code>, which contains stars <code>*</code>.</p> <p>In one operation, you can:</p> <ul> <li>Choose a star in <code>s</code>.</li> <li>Remove the closest <strong>non-star</strong> character to its <strong>left</strong>, as well as remove the star itself.</li> </ul> <p>Return <em>th...
0
{ "code": "class Solution {\npublic:\n string removeStars(string& s) {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n string ret;\n for (auto c : s) {\n if (c == '*') {\n ret.pop_back();\n } else {\n ...
2,470
<p>You are given a string <code>s</code>, which contains stars <code>*</code>.</p> <p>In one operation, you can:</p> <ul> <li>Choose a star in <code>s</code>.</li> <li>Remove the closest <strong>non-star</strong> character to its <strong>left</strong>, as well as remove the star itself.</li> </ul> <p>Return <em>th...
0
{ "code": "class Solution {\npublic:\n string removeStars(string &s) {\n stack<char>st;\n for(auto it:s){\n if(it =='*')st.pop();\n else st.push(it);\n }\n s.clear();\n while(!st.empty()){\n s.insert(s.begin(),st.top());\n st.pop();\n ...
2,470
<p>You are given a string <code>s</code>, which contains stars <code>*</code>.</p> <p>In one operation, you can:</p> <ul> <li>Choose a star in <code>s</code>.</li> <li>Remove the closest <strong>non-star</strong> character to its <strong>left</strong>, as well as remove the star itself.</li> </ul> <p>Return <em>th...
0
{ "code": "class Solution {\npublic:\n string removeStars(string &s) {\n stack<char>st;\n for(auto it:s){\n if(it =='*')st.pop();\n else st.push(it);\n }\n s = \"\";\n while(!st.empty()){\n s.insert(s.begin(),st.top());\n st.pop();\n ...
2,470
<p>You are given a string <code>s</code>, which contains stars <code>*</code>.</p> <p>In one operation, you can:</p> <ul> <li>Choose a star in <code>s</code>.</li> <li>Remove the closest <strong>non-star</strong> character to its <strong>left</strong>, as well as remove the star itself.</li> </ul> <p>Return <em>th...
0
{ "code": "class Solution {\npublic:\n string removeStars(const string& s) {\n deque<char> closest_non_star;\n\n for(auto c : s)\n if (c != '*') closest_non_star.push_back(c);\n else closest_non_star.pop_back();\n \n return string(closest_non_star.begin(), closest_...
2,470
<p>You are given a string <code>s</code>, which contains stars <code>*</code>.</p> <p>In one operation, you can:</p> <ul> <li>Choose a star in <code>s</code>.</li> <li>Remove the closest <strong>non-star</strong> character to its <strong>left</strong>, as well as remove the star itself.</li> </ul> <p>Return <em>th...
0
{ "code": "class Solution {\npublic:\n Solution() {\n std::ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n }\n\n std::string removeStars(const std::string &s) {\n\n std::stack<char> stk;\n\n for (auto c : s) {\n if (c == '*')\n stk.pop();\n else\n ...