id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
2,606
<p>You are given a <strong>0-indexed</strong> <code>m x n</code> binary matrix <code>grid</code>.</p> <p>A <strong>0-indexed</strong> <code>m x n</code> difference matrix <code>diff</code> is created with the following procedure:</p> <ul> <li>Let the number of ones in the <code>i<sup>th</sup></code> row be <code>one...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> onesMinusZeros(vector<vector<int>>& grid) {\n int rows=grid.size();\n int cols=grid[0].size();\n\n vector<vector<int>> ans;\n\n vector<int> rowsum1;\n vector<int> rowsum0;\n vector<int> colsum1;\n vector<int...
2,606
<p>You are given a <strong>0-indexed</strong> <code>m x n</code> binary matrix <code>grid</code>.</p> <p>A <strong>0-indexed</strong> <code>m x n</code> difference matrix <code>diff</code> is created with the following procedure:</p> <ul> <li>Let the number of ones in the <code>i<sup>th</sup></code> row be <code>one...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> onesMinusZeros(vector<vector<int>>& grid) {\n int n=grid.size();\n int m=grid[0].size();\n vector<vector<pair<int,int>>>sum(n,vector<pair<int,int>>(m));\n for(int i=0;i<n;i++){\n int s=0;\n for(int j=0;j<m;...
2,606
<p>You are given a <strong>0-indexed</strong> <code>m x n</code> binary matrix <code>grid</code>.</p> <p>A <strong>0-indexed</strong> <code>m x n</code> difference matrix <code>diff</code> is created with the following procedure:</p> <ul> <li>Let the number of ones in the <code>i<sup>th</sup></code> row be <code>one...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> onesMinusZeros(vector<vector<int>>& grid) \n {\n int row = grid.size();\n int cols = grid[0].size();\n \n vector<int> onesRow(row , 0);\n vector<int> onesCol(cols , 0);\n vector<vector<int>> ans;\n \n ...
2,606
<p>You are given a <strong>0-indexed</strong> <code>m x n</code> binary matrix <code>grid</code>.</p> <p>A <strong>0-indexed</strong> <code>m x n</code> difference matrix <code>diff</code> is created with the following procedure:</p> <ul> <li>Let the number of ones in the <code>i<sup>th</sup></code> row be <code>one...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> onesMinusZeros(vector<vector<int>>& grid) {\n vector<int> rz(grid.size(),0);\n vector<int> cz(grid[0].size(),0);\n vector<int> r1(grid.size(),0);\n vector<int> c1(grid[0].size(),0);\n for(int i=0;i<grid.size();i++)\n ...
2,606
<p>You are given a <strong>0-indexed</strong> <code>m x n</code> binary matrix <code>grid</code>.</p> <p>A <strong>0-indexed</strong> <code>m x n</code> difference matrix <code>diff</code> is created with the following procedure:</p> <ul> <li>Let the number of ones in the <code>i<sup>th</sup></code> row be <code>one...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> onesMinusZeros(vector<vector<int>>& grid) {\n vector<vector<int>> diff;\n vector<int>diff1;\n vector<int> diff2;\n int onesRow,onesCol,zerosRow,zerosCol;\n onesRow=onesCol=zerosRow=zerosCol=0;\n for(int r=0;r<grid...
2,606
<p>You are given a <strong>0-indexed</strong> <code>m x n</code> binary matrix <code>grid</code>.</p> <p>A <strong>0-indexed</strong> <code>m x n</code> difference matrix <code>diff</code> is created with the following procedure:</p> <ul> <li>Let the number of ones in the <code>i<sup>th</sup></code> row be <code>one...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> onesMinusZeros(vector<vector<int>>& grid) {\n vector<vector<int>> arr;\n vector<int> row;\n vector<int> col;\n\n for(int i=0;i<grid.size();i++){\n int oner=0,zeror=0;\n // int zeror=0,zeroc=0;\n ...
2,606
<p>You are given a <strong>0-indexed</strong> <code>m x n</code> binary matrix <code>grid</code>.</p> <p>A <strong>0-indexed</strong> <code>m x n</code> difference matrix <code>diff</code> is created with the following procedure:</p> <ul> <li>Let the number of ones in the <code>i<sup>th</sup></code> row be <code>one...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> onesMinusZeros(vector<vector<int>>& grid) {\n vector<vector<int>> arr;\n vector<int> row;\n vector<int> col;\n\n for(int i=0;i<grid.size();i++){\n int oner=0,zeror=0;\n // int zeror=0,zeroc=0;\n ...
2,606
<p>You are given a <strong>0-indexed</strong> <code>m x n</code> binary matrix <code>grid</code>.</p> <p>A <strong>0-indexed</strong> <code>m x n</code> difference matrix <code>diff</code> is created with the following procedure:</p> <ul> <li>Let the number of ones in the <code>i<sup>th</sup></code> row be <code>one...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> onesMinusZeros(vector<vector<int>>& grid) \n {\n vector<vector<int>> ans;\n vector<int> r,c;\n for(int i=0;i<grid.size();i++)\n {\n int s=0;\n for(int j=0;j<grid[0].size();j++)\n {\n ...
2,606
<p>You are given a <strong>0-indexed</strong> <code>m x n</code> binary matrix <code>grid</code>.</p> <p>A <strong>0-indexed</strong> <code>m x n</code> difference matrix <code>diff</code> is created with the following procedure:</p> <ul> <li>Let the number of ones in the <code>i<sup>th</sup></code> row be <code>one...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> onesMinusZeros(vector<vector<int>>& grid) {\n\n int row_cnt = grid.size();\n int col_cnt = grid[0].size();\n\n // Initialize num_ones_zeros_at_row and num_ones_zeros_at_col with [0, 0]\n vector<vector<int>> num_ones_zeros_at_row(row_cnt, vector...
2,606
<p>You are given a <strong>0-indexed</strong> <code>m x n</code> binary matrix <code>grid</code>.</p> <p>A <strong>0-indexed</strong> <code>m x n</code> difference matrix <code>diff</code> is created with the following procedure:</p> <ul> <li>Let the number of ones in the <code>i<sup>th</sup></code> row be <code>one...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> onesMinusZeros(vector<vector<int>>& grid) {\n int n = grid.size();\n int m = grid[0].size();\n vector<vector<int>> row(n, vector<int>(2, 0));\n vector<vector<int>> col(m, vector<int>(2, 0));\n vector<vector<int>> res(n, v...
2,606
<p>You are given a <strong>0-indexed</strong> <code>m x n</code> binary matrix <code>grid</code>.</p> <p>A <strong>0-indexed</strong> <code>m x n</code> difference matrix <code>diff</code> is created with the following procedure:</p> <ul> <li>Let the number of ones in the <code>i<sup>th</sup></code> row be <code>one...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> onesMinusZeros(vector<vector<int>>& grid) {\n int n = grid.size();\n int m = grid[0].size();\n vector<vector<int>> row(n, vector<int>(2, 0));\n vector<vector<int>> col(m, vector<int>(2, 0));\n vector<vector<int>> res(n, v...
2,606
<p>You are given a <strong>0-indexed</strong> <code>m x n</code> binary matrix <code>grid</code>.</p> <p>A <strong>0-indexed</strong> <code>m x n</code> difference matrix <code>diff</code> is created with the following procedure:</p> <ul> <li>Let the number of ones in the <code>i<sup>th</sup></code> row be <code>one...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> onesMinusZeros(vector<vector<int>>& grid) {\n int n = grid.size();\n int m = grid[0].size();\n vector<vector<int>> row(n, vector<int>(2, 0));\n vector<vector<int>> col(m, vector<int>(2, 0));\n vector<vector<int>> res(n, v...
2,606
<p>You are given a <strong>0-indexed</strong> <code>m x n</code> binary matrix <code>grid</code>.</p> <p>A <strong>0-indexed</strong> <code>m x n</code> difference matrix <code>diff</code> is created with the following procedure:</p> <ul> <li>Let the number of ones in the <code>i<sup>th</sup></code> row be <code>one...
3
{ "code": "class Solution {\npublic:\n unordered_map<int,int> countOneRows;\n unordered_map<int,int> countOneCol; \nvoid countOnesRow(vector<vector<int>>& grid,int i,int m){\n int count=0;\n for(int j=0;j<m;j++){\n if(grid[i][j]==1){\n ...
2,606
<p>You are given a <strong>0-indexed</strong> <code>m x n</code> binary matrix <code>grid</code>.</p> <p>A <strong>0-indexed</strong> <code>m x n</code> difference matrix <code>diff</code> is created with the following procedure:</p> <ul> <li>Let the number of ones in the <code>i<sup>th</sup></code> row be <code>one...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> onesMinusZeros(vector<vector<int>>& grid) {\n int n = grid.size();\n int m = grid[0].size();\n unordered_map<int,int> row_ones;\n unordered_map<int,int> col_ones;\n for(int i=0;i<n;i++){\n int count = 0;\n ...
2,606
<p>You are given a <strong>0-indexed</strong> <code>m x n</code> binary matrix <code>grid</code>.</p> <p>A <strong>0-indexed</strong> <code>m x n</code> difference matrix <code>diff</code> is created with the following procedure:</p> <ul> <li>Let the number of ones in the <code>i<sup>th</sup></code> row be <code>one...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> onesMinusZeros(vector<vector<int>>& grid) {\n int n=grid.size(),m=grid[0].size();\n unordered_map<int,int>r;\n unordered_map<int,int>c;\n for(int i=0;i<n;i++){\n for(int j=0;j<m;j++){\n if(grid[i][j]==1...
2,606
<p>You are given a <strong>0-indexed</strong> <code>m x n</code> binary matrix <code>grid</code>.</p> <p>A <strong>0-indexed</strong> <code>m x n</code> difference matrix <code>diff</code> is created with the following procedure:</p> <ul> <li>Let the number of ones in the <code>i<sup>th</sup></code> row be <code>one...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> onesMinusZeros(vector<vector<int>>& grid) {\n vector<pair<int,int>>row;\n vector<pair<int,int>>col;\n int r=grid.size();\n int c=grid[0].size();\n for(int i=0;i<r;i++){\n int row_one=0;\n int row_zero=0;\...
2,606
<p>You are given a <strong>0-indexed</strong> <code>m x n</code> binary matrix <code>grid</code>.</p> <p>A <strong>0-indexed</strong> <code>m x n</code> difference matrix <code>diff</code> is created with the following procedure:</p> <ul> <li>Let the number of ones in the <code>i<sup>th</sup></code> row be <code>one...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> onesMinusZeros(vector<vector<int>>& grid) {\n int row = grid.size();\n int col = grid[0].size();\n unordered_map<int, int> ans;\n unordered_map<int,int>column;\n for (int i = 0; i < row; i++) {\n int sum = 0;\n...
2,576
<p>You are given the customer visit log of a shop represented by a <strong>0-indexed</strong> string <code>customers</code> consisting only of characters <code>&#39;N&#39;</code> and <code>&#39;Y&#39;</code>:</p> <ul> <li>if the <code>i<sup>th</sup></code> character is <code>&#39;Y&#39;</code>, it means that customer...
0
{ "code": "class Solution {\npublic:\n int bestClosingTime(string& str) {\n int co = 0;\n for(int i=0;i<str.size();++i)\n co+=str[i]=='Y';\n\n int mn = INT_MAX, cur = 0, ind = -1;\n for(int i=0;i<=str.size();++i)\n {\n if(mn>co+cur)\n mn = co+...
2,576
<p>You are given the customer visit log of a shop represented by a <strong>0-indexed</strong> string <code>customers</code> consisting only of characters <code>&#39;N&#39;</code> and <code>&#39;Y&#39;</code>:</p> <ul> <li>if the <code>i<sup>th</sup></code> character is <code>&#39;Y&#39;</code>, it means that customer...
0
{ "code": "class Solution {\npublic:\n int bestClosingTime(const string &c) {\n cin.tie(0); ios::sync_with_stdio(0);\n\n int penalty = 0;\n int minPenalty = penalty;\n int j = 0;\n\n for (int i=1; i<=c.size(); ++i)\n {\n if (c[i-1] == 'Y')\n penalty-...
2,576
<p>You are given the customer visit log of a shop represented by a <strong>0-indexed</strong> string <code>customers</code> consisting only of characters <code>&#39;N&#39;</code> and <code>&#39;Y&#39;</code>:</p> <ul> <li>if the <code>i<sup>th</sup></code> character is <code>&#39;Y&#39;</code>, it means that customer...
0
{ "code": "class Solution {\npublic:\n int bestClosingTime(const string &c) {\n cin.tie(0); ios::sync_with_stdio(0);\n\n int penalty = 0;\n int minPenalty = penalty;\n int j = 0;\n\n for (int i=1; i<=c.size(); ++i)\n {\n if (c[i-1] == 'Y')\n penalty-...
2,576
<p>You are given the customer visit log of a shop represented by a <strong>0-indexed</strong> string <code>customers</code> consisting only of characters <code>&#39;N&#39;</code> and <code>&#39;Y&#39;</code>:</p> <ul> <li>if the <code>i<sup>th</sup></code> character is <code>&#39;Y&#39;</code>, it means that customer...
0
{ "code": "class Solution {\npublic:\n int bestClosingTime(string customers) {\n int Ncount=0;\n int Ncount2=0;\n int Ycount=0;\n for(int i=0;i<customers.size();i++){\n if(customers[i]=='Y') Ycount++;\n }\n int minpen=Ycount;\n int index=0;\n for(i...
2,576
<p>You are given the customer visit log of a shop represented by a <strong>0-indexed</strong> string <code>customers</code> consisting only of characters <code>&#39;N&#39;</code> and <code>&#39;Y&#39;</code>:</p> <ul> <li>if the <code>i<sup>th</sup></code> character is <code>&#39;Y&#39;</code>, it means that customer...
0
{ "code": "class Solution {\npublic:\n int bestClosingTime(string customers) {\n\n // if close at j, penalty would be\n // 1) number of Y after j +\n // 2) number of N before j\n\n int numN = 0;\n /*\n for (int i=0;i<customers.size();i++) {\n if (customers[i] ==...
2,576
<p>You are given the customer visit log of a shop represented by a <strong>0-indexed</strong> string <code>customers</code> consisting only of characters <code>&#39;N&#39;</code> and <code>&#39;Y&#39;</code>:</p> <ul> <li>if the <code>i<sup>th</sup></code> character is <code>&#39;Y&#39;</code>, it means that customer...
0
{ "code": "class Solution {\npublic:\n int bestClosingTime(string customers) {\n int total = 0;\n // first calculate total penalty if shop closes at 0th hour\n for(int i=0; i<customers.length(); i++){\n if(customers[i] == 'Y')\n total++;\n }\n int res = ...
2,576
<p>You are given the customer visit log of a shop represented by a <strong>0-indexed</strong> string <code>customers</code> consisting only of characters <code>&#39;N&#39;</code> and <code>&#39;Y&#39;</code>:</p> <ul> <li>if the <code>i<sup>th</sup></code> character is <code>&#39;Y&#39;</code>, it means that customer...
0
{ "code": "class Solution {\npublic:\n int bestClosingTime(string customers) {\n auto penalty = 0;\n for(const auto &c : customers)\n if(c == 'Y')\n ++ penalty;\n \n auto minPenalty = penalty;\n auto earliest = 0;\n for(auto i = 0; i < customers.s...
2,576
<p>You are given the customer visit log of a shop represented by a <strong>0-indexed</strong> string <code>customers</code> consisting only of characters <code>&#39;N&#39;</code> and <code>&#39;Y&#39;</code>:</p> <ul> <li>if the <code>i<sup>th</sup></code> character is <code>&#39;Y&#39;</code>, it means that customer...
0
{ "code": "class Solution {\npublic:\n int bestClosingTime(string customers) {\n int n=customers.size();\n\n int penalty=count(begin(customers),end(customers),'Y');\n int minpenalty=penalty;\n int minhour=0;\n\n for(int i=0;i<n;i++){\n if(customers[i]=='Y'){\n penalty--;\...
2,576
<p>You are given the customer visit log of a shop represented by a <strong>0-indexed</strong> string <code>customers</code> consisting only of characters <code>&#39;N&#39;</code> and <code>&#39;Y&#39;</code>:</p> <ul> <li>if the <code>i<sup>th</sup></code> character is <code>&#39;Y&#39;</code>, it means that customer...
0
{ "code": "class Solution {\npublic:\n int bestClosingTime(string customers) {\n int n = customers.size();\n int ans = n, sum = 0, ma = 0;\n\n for(int i = n-1; i>=0; i--){\n sum += customers[i] == 'N'? 1:-1;\n if(sum>=ma){\n ma = sum;\n ans =...
2,576
<p>You are given the customer visit log of a shop represented by a <strong>0-indexed</strong> string <code>customers</code> consisting only of characters <code>&#39;N&#39;</code> and <code>&#39;Y&#39;</code>:</p> <ul> <li>if the <code>i<sup>th</sup></code> character is <code>&#39;Y&#39;</code>, it means that customer...
0
{ "code": "class Solution {\npublic:\n int bestClosingTime(string customers) {\n int n=customers.size();\n int pre[n+1];\n int c=0;\n for(int i=0;i<n;i++){\n if(customers[i]=='N') c++;\n }\n pre[n]=c;\n for(int i=n-1;i>=0;i--){\n if(customers[i...
2,576
<p>You are given the customer visit log of a shop represented by a <strong>0-indexed</strong> string <code>customers</code> consisting only of characters <code>&#39;N&#39;</code> and <code>&#39;Y&#39;</code>:</p> <ul> <li>if the <code>i<sup>th</sup></code> character is <code>&#39;Y&#39;</code>, it means that customer...
0
{ "code": "class Solution {\npublic:\n int bestClosingTime(string customers) {\n int n=customers.size();\n int pre[n+1];\n int c=0;\n for(int i=0;i<n;i++){\n if(customers[i]=='N') c++;\n }\n pre[n]=c;\n for(int i=n-1;i>=0;i--){\n if(customers[i...
2,576
<p>You are given the customer visit log of a shop represented by a <strong>0-indexed</strong> string <code>customers</code> consisting only of characters <code>&#39;N&#39;</code> and <code>&#39;Y&#39;</code>:</p> <ul> <li>if the <code>i<sup>th</sup></code> character is <code>&#39;Y&#39;</code>, it means that customer...
0
{ "code": "class Solution {\npublic:\n int bestClosingTime(string customers) {\n int count=0;\n int n=customers.size();\n for(int i=0;i<n;i++) if(customers[i]=='Y') count++;\n int arr[n+1];\n arr[0]=count;\n for(int i=0;i<n;i++){\n if(customers[i]=='Y'){\n ...
2,576
<p>You are given the customer visit log of a shop represented by a <strong>0-indexed</strong> string <code>customers</code> consisting only of characters <code>&#39;N&#39;</code> and <code>&#39;Y&#39;</code>:</p> <ul> <li>if the <code>i<sup>th</sup></code> character is <code>&#39;Y&#39;</code>, it means that customer...
0
{ "code": "class Solution {\npublic:\n int bestClosingTime(string customers) {\n int n=customers.size();\n int pre[n+1];\n int c=0;\n for(int i=0;i<n;i++){\n if(customers[i]=='N') c++;\n }\n pre[n]=c;\n for(int i=n-1;i>=0;i--){\n if(customers[i...
2,576
<p>You are given the customer visit log of a shop represented by a <strong>0-indexed</strong> string <code>customers</code> consisting only of characters <code>&#39;N&#39;</code> and <code>&#39;Y&#39;</code>:</p> <ul> <li>if the <code>i<sup>th</sup></code> character is <code>&#39;Y&#39;</code>, it means that customer...
0
{ "code": "class Solution {\npublic:\n int bestClosingTime(string customers) {\n int n = customers.size();\n int pre[n+1];\n int suf[n+1];\n pre[0]= 0;\n for(int i=0;i<n;i++){\n int count =0;\n // if(customers[i]=='N')count++;\n pre[i+1]= pre[i] + ((customers[i]=='N') ? 1:0);\n ...
2,576
<p>You are given the customer visit log of a shop represented by a <strong>0-indexed</strong> string <code>customers</code> consisting only of characters <code>&#39;N&#39;</code> and <code>&#39;Y&#39;</code>:</p> <ul> <li>if the <code>i<sup>th</sup></code> character is <code>&#39;Y&#39;</code>, it means that customer...
1
{ "code": "class Solution {\npublic:\n int bestClosingTime(string customers) {\n // at any j, no of ys from end(inclusive) + no of ns from start\n\n int n = customers.size();\n\n int suf[n+1], pre[n+1];\n\n for(int i = n-1; i>=0; i--){\n if(customers.at(i)=='Y') suf[i] = suf[...
2,576
<p>You are given the customer visit log of a shop represented by a <strong>0-indexed</strong> string <code>customers</code> consisting only of characters <code>&#39;N&#39;</code> and <code>&#39;Y&#39;</code>:</p> <ul> <li>if the <code>i<sup>th</sup></code> character is <code>&#39;Y&#39;</code>, it means that customer...
1
{ "code": "class Solution {\npublic:\n int bestClosingTime(string customers) {\n int n = customers.size();\n int pre[n+1]; // number of 'N' (no customers) before or at the kth hour\n int suf[n+1]; // number of 'Y' (customers) after and including the kth hour\n pre[0] = 0;\n \n ...
2,576
<p>You are given the customer visit log of a shop represented by a <strong>0-indexed</strong> string <code>customers</code> consisting only of characters <code>&#39;N&#39;</code> and <code>&#39;Y&#39;</code>:</p> <ul> <li>if the <code>i<sup>th</sup></code> character is <code>&#39;Y&#39;</code>, it means that customer...
1
{ "code": "class Solution {\npublic:\n int bestClosingTime(string customers) {\n int n = customers.size();\n int pre[n+1]; // no of N before kth hour\n int suf[n+1]; // no of Y after and including kth hour\n pre[0] = 0;\n for(int i=0; i<n; i++){\n pre[i+1] = pre[i] +...
2,576
<p>You are given the customer visit log of a shop represented by a <strong>0-indexed</strong> string <code>customers</code> consisting only of characters <code>&#39;N&#39;</code> and <code>&#39;Y&#39;</code>:</p> <ul> <li>if the <code>i<sup>th</sup></code> character is <code>&#39;Y&#39;</code>, it means that customer...
1
{ "code": "class Solution {\npublic:\n int bestClosingTime(string customers) {\n int n = customers.length();\n int pre[n+1];// no. of \"N\" before kth hour\n int suf[n+1];// no. of \"Y\" after and including kth hour\n pre[0]=0;\n for(int i=0;i<n;i++){\n pre[i+1]=pre[i]...
2,576
<p>You are given the customer visit log of a shop represented by a <strong>0-indexed</strong> string <code>customers</code> consisting only of characters <code>&#39;N&#39;</code> and <code>&#39;Y&#39;</code>:</p> <ul> <li>if the <code>i<sup>th</sup></code> character is <code>&#39;Y&#39;</code>, it means that customer...
1
{ "code": "class Solution {\npublic:\n int bestClosingTime(string customers) {\n int n = customers.size();\n //arrays ka ek size extra due to closing time\n int pre[n+1]; //no of N before kth hour\n int suf[n+1]; //no of Y after kth hour and including kth hour\n pre[0] = 0; //1st element ke s...
2,576
<p>You are given the customer visit log of a shop represented by a <strong>0-indexed</strong> string <code>customers</code> consisting only of characters <code>&#39;N&#39;</code> and <code>&#39;Y&#39;</code>:</p> <ul> <li>if the <code>i<sup>th</sup></code> character is <code>&#39;Y&#39;</code>, it means that customer...
1
{ "code": "class Solution {\npublic:\n int bestClosingTime(string customers) {\n int curRevenue= 0;\n int maxRevenue = curRevenue;\n int closingTime = 0;\n\n for(int i=0; i<customers.size(); i++){\n string customer = customers.substr(i, 1);\n curRevenue += customer...
2,576
<p>You are given the customer visit log of a shop represented by a <strong>0-indexed</strong> string <code>customers</code> consisting only of characters <code>&#39;N&#39;</code> and <code>&#39;Y&#39;</code>:</p> <ul> <li>if the <code>i<sup>th</sup></code> character is <code>&#39;Y&#39;</code>, it means that customer...
1
{ "code": "class Solution {\npublic:\n int bestClosingTime(string customers) {\n int curRevenue= 0;\n int maxRevenue = curRevenue;\n int closingTime = 0;\n\n for(int i=0; i<customers.size(); i++){\n string customer = customers.substr(i, 1);\n curRevenue += customer...
2,576
<p>You are given the customer visit log of a shop represented by a <strong>0-indexed</strong> string <code>customers</code> consisting only of characters <code>&#39;N&#39;</code> and <code>&#39;Y&#39;</code>:</p> <ul> <li>if the <code>i<sup>th</sup></code> character is <code>&#39;Y&#39;</code>, it means that customer...
1
{ "code": "class Solution {\npublic:\n int bestClosingTime(string customers) {\n int curPenalty = 0;\n for(int i=0; i<customers.size(); i++){\n string customer = customers.substr(i, 1);\n if(customer==\"Y\") curPenalty++;\n }\n\n int minPenalty = curPenalty;\n ...
2,576
<p>You are given the customer visit log of a shop represented by a <strong>0-indexed</strong> string <code>customers</code> consisting only of characters <code>&#39;N&#39;</code> and <code>&#39;Y&#39;</code>:</p> <ul> <li>if the <code>i<sup>th</sup></code> character is <code>&#39;Y&#39;</code>, it means that customer...
1
{ "code": "class Solution {\npublic:\n int bestClosingTime(string customers) {\n int curPenalty = 0;\n for(int i=0; i<customers.size(); i++){\n string customer = customers.substr(i, 1);\n if(customer==\"Y\") curPenalty++;\n }\n\n int minPenalty = curPenalty;\n ...
2,576
<p>You are given the customer visit log of a shop represented by a <strong>0-indexed</strong> string <code>customers</code> consisting only of characters <code>&#39;N&#39;</code> and <code>&#39;Y&#39;</code>:</p> <ul> <li>if the <code>i<sup>th</sup></code> character is <code>&#39;Y&#39;</code>, it means that customer...
1
{ "code": "class Solution {\npublic:\n int bestClosingTime(string customers) {\n int curRevenue= 0;\n int maxRevenue = curRevenue;\n int closingTime = 0;\n\n for(int i=0; i<customers.size(); i++){\n string customer = customers.substr(i, 1);\n curRevenue += customer...
2,576
<p>You are given the customer visit log of a shop represented by a <strong>0-indexed</strong> string <code>customers</code> consisting only of characters <code>&#39;N&#39;</code> and <code>&#39;Y&#39;</code>:</p> <ul> <li>if the <code>i<sup>th</sup></code> character is <code>&#39;Y&#39;</code>, it means that customer...
1
{ "code": "class Solution {\npublic:\n int bestClosingTime(string customers) {\n int curRevenue= 0;\n int maxRevenue = curRevenue;\n int closingTime = 0;\n\n for(int i=0; i<customers.size(); i++){\n string customer = customers.substr(i, 1);\n curRevenue += customer...
2,576
<p>You are given the customer visit log of a shop represented by a <strong>0-indexed</strong> string <code>customers</code> consisting only of characters <code>&#39;N&#39;</code> and <code>&#39;Y&#39;</code>:</p> <ul> <li>if the <code>i<sup>th</sup></code> character is <code>&#39;Y&#39;</code>, it means that customer...
1
{ "code": "class Solution {\npublic:\n int bestClosingTime(string customers) {\n int n = customers.length();\n int pre[n+1];\n pre[0] = 0;\n int suf[n+1];\n suf[n] = 0;\n int final[n+1];\n for(int i = 1;i<=n;i++){\n if(customers[i-1] == 'N'){\n ...
2,576
<p>You are given the customer visit log of a shop represented by a <strong>0-indexed</strong> string <code>customers</code> consisting only of characters <code>&#39;N&#39;</code> and <code>&#39;Y&#39;</code>:</p> <ul> <li>if the <code>i<sup>th</sup></code> character is <code>&#39;Y&#39;</code>, it means that customer...
1
{ "code": "class Solution {\npublic:\n int bestClosingTime(string customers) {\n int n = customers.length();\n int pre[n+1];\n int suf[n+1];\n int final[n+1];\n pre[0]=0;\n for(int i =0;i<n;i++){\n if(customers[i]=='N'){\n pre[i+1]=pre[i]+1;\n ...
2,576
<p>You are given the customer visit log of a shop represented by a <strong>0-indexed</strong> string <code>customers</code> consisting only of characters <code>&#39;N&#39;</code> and <code>&#39;Y&#39;</code>:</p> <ul> <li>if the <code>i<sup>th</sup></code> character is <code>&#39;Y&#39;</code>, it means that customer...
1
{ "code": "class Solution {\npublic:\n int bestClosingTime(string s) {\n int n=s.size();\n int cnty=0;\n int cntn=0;\n for(auto it:s){\n if(it=='Y') cnty++;\n else cntn++;\n }\n if(cnty==n) return n;\n if(cntn==n) return 0;\n int ln=0;\n...
2,576
<p>You are given the customer visit log of a shop represented by a <strong>0-indexed</strong> string <code>customers</code> consisting only of characters <code>&#39;N&#39;</code> and <code>&#39;Y&#39;</code>:</p> <ul> <li>if the <code>i<sup>th</sup></code> character is <code>&#39;Y&#39;</code>, it means that customer...
1
{ "code": "class Solution {\npublic:\n int bestClosingTime(string customers) {\n int n = customers.length();\n vector<int>pref(n+2,0);\n int tot = 0;\n for(int i=1;i<=n;i++){\n pref[i]=pref[i-1]+(customers[i-1]=='Y');\n tot += (customers[i-1]=='Y');\n }\n ...
2,576
<p>You are given the customer visit log of a shop represented by a <strong>0-indexed</strong> string <code>customers</code> consisting only of characters <code>&#39;N&#39;</code> and <code>&#39;Y&#39;</code>:</p> <ul> <li>if the <code>i<sup>th</sup></code> character is <code>&#39;Y&#39;</code>, it means that customer...
1
{ "code": "class Solution {\npublic:\n int bestClosingTime(string customers) {\n int n=customers.size();\n vector<int> noprefix(n+1,0);\n\n for(int i=1;i<n;i++){\n if(customers[i-1]=='N')\n noprefix[i]=noprefix[i-1]+1;\n else\n noprefix[i]=noprefix...
2,576
<p>You are given the customer visit log of a shop represented by a <strong>0-indexed</strong> string <code>customers</code> consisting only of characters <code>&#39;N&#39;</code> and <code>&#39;Y&#39;</code>:</p> <ul> <li>if the <code>i<sup>th</sup></code> character is <code>&#39;Y&#39;</code>, it means that customer...
1
{ "code": "class Solution {\npublic:\n int bestClosingTime(string customers) {\n int n = customers.size();\n vector<int> suff(n+1);\n suff[n] = 0;\n\n for(int i = n-1;i>=0;i--){\n suff[i] = customers[i] == 'Y' ? suff[i+1]+1 : suff[i+1];\n }\n\n int penalty = 0;\...
2,576
<p>You are given the customer visit log of a shop represented by a <strong>0-indexed</strong> string <code>customers</code> consisting only of characters <code>&#39;N&#39;</code> and <code>&#39;Y&#39;</code>:</p> <ul> <li>if the <code>i<sup>th</sup></code> character is <code>&#39;Y&#39;</code>, it means that customer...
1
{ "code": "class Solution {\npublic:\n int bestClosingTime(string c) {\n int n = c.size();\n vector<int> tmp(n+1);\n for(int i=n-1;i>=0;i--) tmp[i] = tmp[i+1] + (c[i] == 'Y');\n int res = -1, mn = INT_MAX, cur = 0;\n for(int i=0;i<=n;i++) {\n if(mn > cur + tmp[i]) {\n ...
2,576
<p>You are given the customer visit log of a shop represented by a <strong>0-indexed</strong> string <code>customers</code> consisting only of characters <code>&#39;N&#39;</code> and <code>&#39;Y&#39;</code>:</p> <ul> <li>if the <code>i<sup>th</sup></code> character is <code>&#39;Y&#39;</code>, it means that customer...
1
{ "code": "class Solution {\npublic:\n int bestClosingTime(string customers) {\n int n = customers.size();\n vector<int> pen(n+1,0);\n int pre = 0, post = 0;\n for(int i=0; i<=n; i++){\n pen[i] += pre;\n if(customers[i] == 'N')\n pre++;\n ...
2,576
<p>You are given the customer visit log of a shop represented by a <strong>0-indexed</strong> string <code>customers</code> consisting only of characters <code>&#39;N&#39;</code> and <code>&#39;Y&#39;</code>:</p> <ul> <li>if the <code>i<sup>th</sup></code> character is <code>&#39;Y&#39;</code>, it means that customer...
2
{ "code": "class Solution {\npublic:\n int bestClosingTime(string customers) {\n vector<int> penaltyBack(customers.length() + 1, 0);\n for (int i = customers.length() - 1; i >= 0; i--) {\n penaltyBack[i] = customers[i] == 'Y' ? 1 : 0;\n penaltyBack[i] += penaltyBack[i + 1];\n ...
2,576
<p>You are given the customer visit log of a shop represented by a <strong>0-indexed</strong> string <code>customers</code> consisting only of characters <code>&#39;N&#39;</code> and <code>&#39;Y&#39;</code>:</p> <ul> <li>if the <code>i<sup>th</sup></code> character is <code>&#39;Y&#39;</code>, it means that customer...
2
{ "code": "class Solution {\npublic:\n int bestClosingTime(string c) {\n int n=c.size();\n int preN[n+1];\n preN[0]=0;\n int suffY[n+1];\n for(int i=1;i<=n;i++){\n if(c[i-1]=='N')preN[i]=1;\n else preN[i]=0;\n preN[i]+=preN[i-1];\n }\n ...
2,576
<p>You are given the customer visit log of a shop represented by a <strong>0-indexed</strong> string <code>customers</code> consisting only of characters <code>&#39;N&#39;</code> and <code>&#39;Y&#39;</code>:</p> <ul> <li>if the <code>i<sup>th</sup></code> character is <code>&#39;Y&#39;</code>, it means that customer...
2
{ "code": "class Solution {\npublic:\n int bestClosingTime(string& c) {\n int n = c.size();\n vector<int>pre(n+1,0);\n for(int i=1;i<=n;i++){\n if(c[i-1] == 'Y') pre[i] = pre[i-1] ;\n else pre[i] = pre[i-1] + 1;\n }\n vector<int>suf(n+1,0);\n for(int ...
2,576
<p>You are given the customer visit log of a shop represented by a <strong>0-indexed</strong> string <code>customers</code> consisting only of characters <code>&#39;N&#39;</code> and <code>&#39;Y&#39;</code>:</p> <ul> <li>if the <code>i<sup>th</sup></code> character is <code>&#39;Y&#39;</code>, it means that customer...
2
{ "code": "class Solution {\npublic:\n int bestClosingTime(const string& c) \n {\n vector<pair<int, int>> cnt(c.size());\n for (int i = 0; i < c.size(); ++i)\n {\n cnt[i] = i? cnt[i - 1] : make_pair(0, 0);\n ++(c[i] == 'Y'? cnt[i].first : cnt[i].second);\n }\n\n...
2,576
<p>You are given the customer visit log of a shop represented by a <strong>0-indexed</strong> string <code>customers</code> consisting only of characters <code>&#39;N&#39;</code> and <code>&#39;Y&#39;</code>:</p> <ul> <li>if the <code>i<sup>th</sup></code> character is <code>&#39;Y&#39;</code>, it means that customer...
2
{ "code": "class Solution {\npublic:\n int bestClosingTime(string c) {\n int n = c.size();\n vector<int>v(n+1,0);\n int cnt = 0;\n for(int i=0;i<n;i++){\n if(c[i]=='Y'){\n v[i+1] = 1;\n cnt++;\n }\n else{\n v[...
2,576
<p>You are given the customer visit log of a shop represented by a <strong>0-indexed</strong> string <code>customers</code> consisting only of characters <code>&#39;N&#39;</code> and <code>&#39;Y&#39;</code>:</p> <ul> <li>if the <code>i<sup>th</sup></code> character is <code>&#39;Y&#39;</code>, it means that customer...
2
{ "code": "class Solution {\npublic:\n int bestClosingTime(string customers) {\n int n = customers.length();\n vector<int> pre(n+1);\n pre[0] = 0;\n int count = 0;\n for(int i=1; i<n+1; i++) {\n if(customers[i-1]=='N') {\n count++;\n }\n ...
2,576
<p>You are given the customer visit log of a shop represented by a <strong>0-indexed</strong> string <code>customers</code> consisting only of characters <code>&#39;N&#39;</code> and <code>&#39;Y&#39;</code>:</p> <ul> <li>if the <code>i<sup>th</sup></code> character is <code>&#39;Y&#39;</code>, it means that customer...
2
{ "code": "class Solution {\npublic:\n int bestClosingTime(string customers) {\n int n = customers.length();\n vector<int> pre(n+1);\n pre[0] = 0;\n int count = 0;\n for(int i=1; i<n+1; i++) {\n if(customers[i-1]=='N') {\n count++;\n }\n ...
2,576
<p>You are given the customer visit log of a shop represented by a <strong>0-indexed</strong> string <code>customers</code> consisting only of characters <code>&#39;N&#39;</code> and <code>&#39;Y&#39;</code>:</p> <ul> <li>if the <code>i<sup>th</sup></code> character is <code>&#39;Y&#39;</code>, it means that customer...
2
{ "code": "class Solution {\npublic:\n int bestClosingTime(string customers) {\n int n = customers.length();\n vector<int> pre(n+1);\n pre[0] = 0;\n int count = 0;\n for(int i=1; i<n+1; i++) {\n if(customers[i-1]=='N') count++;\n pre[i] = count;\n }\n...
2,576
<p>You are given the customer visit log of a shop represented by a <strong>0-indexed</strong> string <code>customers</code> consisting only of characters <code>&#39;N&#39;</code> and <code>&#39;Y&#39;</code>:</p> <ul> <li>if the <code>i<sup>th</sup></code> character is <code>&#39;Y&#39;</code>, it means that customer...
2
{ "code": "class Solution {\npublic:\n int bestClosingTime(string customers) {\n int n = customers.size();\n vector<int> right(n+1,0);\n vector<int> left(n+1,0);\n left[0] = 0;\n\n for(int i = 1;i<n+1;i++){\n if(customers[i-1] == 'N'){\n left[i] = left[i...
2,576
<p>You are given the customer visit log of a shop represented by a <strong>0-indexed</strong> string <code>customers</code> consisting only of characters <code>&#39;N&#39;</code> and <code>&#39;Y&#39;</code>:</p> <ul> <li>if the <code>i<sup>th</sup></code> character is <code>&#39;Y&#39;</code>, it means that customer...
2
{ "code": "class Solution {\npublic:\n int bestClosingTime(string customers) {\n int n = customers.size();\n\n vector<int> pre(n+1);\n vector<int> suf(n+1);\n\n pre[0] =0;\n for(int i = 0; i < n; i++){\n int count = 0;\n if(customers[i] == 'N') count++;\n ...
2,576
<p>You are given the customer visit log of a shop represented by a <strong>0-indexed</strong> string <code>customers</code> consisting only of characters <code>&#39;N&#39;</code> and <code>&#39;Y&#39;</code>:</p> <ul> <li>if the <code>i<sup>th</sup></code> character is <code>&#39;Y&#39;</code>, it means that customer...
2
{ "code": "class Solution {\npublic:\n int bestClosingTime(string customers) {\n int n=customers.size();\n vector<int>Ncount(n+1,0), Ycount(n+1,0);\n \n int count=0;\n for(int i=1;i<=n;i++) {\n if(customers[i-1]=='N') {\n count++;\n }\n ...
2,576
<p>You are given the customer visit log of a shop represented by a <strong>0-indexed</strong> string <code>customers</code> consisting only of characters <code>&#39;N&#39;</code> and <code>&#39;Y&#39;</code>:</p> <ul> <li>if the <code>i<sup>th</sup></code> character is <code>&#39;Y&#39;</code>, it means that customer...
3
{ "code": "class Solution {\npublic:\n int bestClosingTime(string customers) {\n customers.push_back('#');\n int n = customers.size();\n vector<int> postfixYes(n, 0);\n vector<int> prefixNo(n, 0);\n for (int i = 0; i < n; i++) {\n prefixNo[i] = customers[i] == 'N';\n ...
2,576
<p>You are given the customer visit log of a shop represented by a <strong>0-indexed</strong> string <code>customers</code> consisting only of characters <code>&#39;N&#39;</code> and <code>&#39;Y&#39;</code>:</p> <ul> <li>if the <code>i<sup>th</sup></code> character is <code>&#39;Y&#39;</code>, it means that customer...
3
{ "code": "class Solution {\npublic:\n int bestClosingTime(string c) {\n int l=0, m=0;vector<int>v;\n v.push_back(0);int idx=0;\n for(int i=0;i<c.size();i++){\n if(c[i]=='Y'){\n m+=1;\n v.push_back(m);}\n\n else{ \n m-=1;\n ...
2,576
<p>You are given the customer visit log of a shop represented by a <strong>0-indexed</strong> string <code>customers</code> consisting only of characters <code>&#39;N&#39;</code> and <code>&#39;Y&#39;</code>:</p> <ul> <li>if the <code>i<sup>th</sup></code> character is <code>&#39;Y&#39;</code>, it means that customer...
3
{ "code": "class Solution {\npublic:\n int bestClosingTime(string customers) {\n // compute the penalty if we close at i-th hour\n vector<int> penalty;\n int cur = 0;\n for (char c : customers) {\n penalty.push_back(cur);\n cur += (c=='N');\n }\n pena...
2,576
<p>You are given the customer visit log of a shop represented by a <strong>0-indexed</strong> string <code>customers</code> consisting only of characters <code>&#39;N&#39;</code> and <code>&#39;Y&#39;</code>:</p> <ul> <li>if the <code>i<sup>th</sup></code> character is <code>&#39;Y&#39;</code>, it means that customer...
3
{ "code": "class Solution {\npublic:\n int bestClosingTime(string s) {\n int n_before=0;\n int n_after=0;\n int y_after=0;\n int y_before=0;\n for(int i=0;i<s.length();i++){\n if(s[i] == 'Y'){\n y_after++;\n }\n else{\n ...
2,576
<p>You are given the customer visit log of a shop represented by a <strong>0-indexed</strong> string <code>customers</code> consisting only of characters <code>&#39;N&#39;</code> and <code>&#39;Y&#39;</code>:</p> <ul> <li>if the <code>i<sup>th</sup></code> character is <code>&#39;Y&#39;</code>, it means that customer...
3
{ "code": "class Solution {\npublic:\n int bestClosingTime(string customers) {\n // penalty\n vector <int> close_shop;\n vector <int> open_shop;\n for(int i =0; i<customers.size(); i++)\n {\n if(customers[i] == 'Y')\n close_shop.push_back(i); // y data\n\n else\n open_shop.push_bac...
2,576
<p>You are given the customer visit log of a shop represented by a <strong>0-indexed</strong> string <code>customers</code> consisting only of characters <code>&#39;N&#39;</code> and <code>&#39;Y&#39;</code>:</p> <ul> <li>if the <code>i<sup>th</sup></code> character is <code>&#39;Y&#39;</code>, it means that customer...
3
{ "code": "class Solution {\npublic:\nvector <int> close_shop;\nvector <int> open_shop;\n\n\n\n// int open_shop(string customers , int i)\n// {\n// if(i<0) return 0;\n// int count =0;\n// while(i>=0)\n// {\n// if( customers[i] == 'N')\n// count +=1;\n\n// i-=1;\n// }\n\n// return count;\n\n\n// }\n\n// ...
2,576
<p>You are given the customer visit log of a shop represented by a <strong>0-indexed</strong> string <code>customers</code> consisting only of characters <code>&#39;N&#39;</code> and <code>&#39;Y&#39;</code>:</p> <ul> <li>if the <code>i<sup>th</sup></code> character is <code>&#39;Y&#39;</code>, it means that customer...
3
{ "code": "class Solution {\npublic:\n \n// int close_shop(string customers , int i)\n// {\n// int n = customers.size();\n// if(i ==n) return 0; int count = 0; \n// while(i<n)\n// {\n// if(customers[i] == 'Y')\n// count +=1;\n \n// i++;\n// }\n\n\n// return co...
2,576
<p>You are given the customer visit log of a shop represented by a <strong>0-indexed</strong> string <code>customers</code> consisting only of characters <code>&#39;N&#39;</code> and <code>&#39;Y&#39;</code>:</p> <ul> <li>if the <code>i<sup>th</sup></code> character is <code>&#39;Y&#39;</code>, it means that customer...
3
{ "code": "class Solution {\n int n;\n vector<int> open, close;\n \n void prePopulate(string& customers) {\n \n for(int i=0;i<n;++i) {\n char ch = customers[i];\n if(i == 0) {\n open[i] = 0;\n } else if(ch == 'N') {\n open[i] = o...
2,576
<p>You are given the customer visit log of a shop represented by a <strong>0-indexed</strong> string <code>customers</code> consisting only of characters <code>&#39;N&#39;</code> and <code>&#39;Y&#39;</code>:</p> <ul> <li>if the <code>i<sup>th</sup></code> character is <code>&#39;Y&#39;</code>, it means that customer...
3
{ "code": "class Solution {\npublic:\n int bestClosingTime(string customers) {\n int n = customers.length();\n vector<int> pre(n+1);\n pre[0] = 0;\n int count = 0;\n for(int i=1; i<n+1; i++) {\n if(customers[i-1]=='N') {\n count++;\n }\n ...
2,576
<p>You are given the customer visit log of a shop represented by a <strong>0-indexed</strong> string <code>customers</code> consisting only of characters <code>&#39;N&#39;</code> and <code>&#39;Y&#39;</code>:</p> <ul> <li>if the <code>i<sup>th</sup></code> character is <code>&#39;Y&#39;</code>, it means that customer...
3
{ "code": "class Solution {\npublic:\n int bestClosingTime(string customers) {\n int n = customers.size();\n vector<int>left(n,0), right(n,0),res(n,1e9);\n int l = 0, r = 0;\n for(int i=0;i<n;i++){\n left[i] = l;\n right[n-1-i] = r;\n l += customers[i] =...
2,576
<p>You are given the customer visit log of a shop represented by a <strong>0-indexed</strong> string <code>customers</code> consisting only of characters <code>&#39;N&#39;</code> and <code>&#39;Y&#39;</code>:</p> <ul> <li>if the <code>i<sup>th</sup></code> character is <code>&#39;Y&#39;</code>, it means that customer...
3
{ "code": "class Solution {\npublic:\n int bestClosingTime(string s) {\n vector<int>open(s.length());\n vector<int>closed(s.length());\n int oi=0;\n for(int i=0;i<s.length();i++){\n if(s[i]=='Y'){\n open[i]=oi;\n }\n else{\n open[i]=oi;\n ...
2,576
<p>You are given the customer visit log of a shop represented by a <strong>0-indexed</strong> string <code>customers</code> consisting only of characters <code>&#39;N&#39;</code> and <code>&#39;Y&#39;</code>:</p> <ul> <li>if the <code>i<sup>th</sup></code> character is <code>&#39;Y&#39;</code>, it means that customer...
3
{ "code": "class Solution {\npublic:\n int bestClosingTime(string s) {\n vector<int>open(s.length());\n vector<int>closed(s.length());\n int oi=0;\n for(int i=0;i<s.length();i++){\n if(s[i]=='Y'){\n open[i]=oi;\n }\n else{\n open[i]=oi;\n ...
2,571
<p>Given a positive integer <code>n</code>, find the <strong>pivot integer</strong> <code>x</code> such that:</p> <ul> <li>The sum of all elements between <code>1</code> and <code>x</code> inclusively equals the sum of all elements between <code>x</code> and <code>n</code> inclusively.</li> </ul> <p>Return <em>the p...
1
{ "code": "class Solution {\npublic:\n int pivotInteger(int n) { \n\n int left = 0;\n int summ = 0;\n for (int i = 0; i <= n; ++i) {\n summ += i;\n }\n \n int right = summ - 1;\n \n for (int pivot = 1; pivot <= n; ++pivot) {\n if (left > right) return -1;\n if (left == ...
2,571
<p>Given a positive integer <code>n</code>, find the <strong>pivot integer</strong> <code>x</code> such that:</p> <ul> <li>The sum of all elements between <code>1</code> and <code>x</code> inclusively equals the sum of all elements between <code>x</code> and <code>n</code> inclusively.</li> </ul> <p>Return <em>the p...
1
{ "code": "class Solution {\npublic:\n int p(int n) { return n * (n + 1) / 2; }\n int pivotInteger(int n) {\n // 1 3 6 10 15 21 28 36\n int k = p(n);\n int l = 1, r = n;\n while (l <= r) {\n int m = (l + r) / 2;\n int f = p(m);\n int s = k + m - f;\n ...
2,571
<p>Given a positive integer <code>n</code>, find the <strong>pivot integer</strong> <code>x</code> such that:</p> <ul> <li>The sum of all elements between <code>1</code> and <code>x</code> inclusively equals the sum of all elements between <code>x</code> and <code>n</code> inclusively.</li> </ul> <p>Return <em>the p...
3
{ "code": "class Solution {\npublic:\n int getSum(int x) {\n return x * (x + 1) / 2;\n }\n\n int pivotInteger(int n) {\n int sum = getSum(n);\n\n int l = 1;\n int r = n;\n while (l <= r) {\n int m = (l + r) / 2;\n int firstPart = getSum(m);\n ...
2,571
<p>Given a positive integer <code>n</code>, find the <strong>pivot integer</strong> <code>x</code> such that:</p> <ul> <li>The sum of all elements between <code>1</code> and <code>x</code> inclusively equals the sum of all elements between <code>x</code> and <code>n</code> inclusively.</li> </ul> <p>Return <em>the p...
3
{ "code": "class Solution {\npublic:\n int pivotInteger(int n) {\n int left=(n*(n+1))/2;\n int right=n;\n if(n==1)return n;\n for(int i=n-1;i>0;i--){\n left-=i+1;\n right+=i;\n if(left==right)\n return i;\n }\n return -1;\n ...
2,571
<p>Given a positive integer <code>n</code>, find the <strong>pivot integer</strong> <code>x</code> such that:</p> <ul> <li>The sum of all elements between <code>1</code> and <code>x</code> inclusively equals the sum of all elements between <code>x</code> and <code>n</code> inclusively.</li> </ul> <p>Return <em>the p...
3
{ "code": "class Solution {\npublic:\n int pivotInteger(int n) {\n int mid = n/2;\n for (int i = mid; i <= n; i++) {\n cout << i << endl;\n double prefix = i/2.0 * (i + 1);\n double suffix = (n-i+1)/2.0 * ((2*i)+((n-i+1) - 1));\n cout << prefix << endl;\n ...
2,571
<p>Given a positive integer <code>n</code>, find the <strong>pivot integer</strong> <code>x</code> such that:</p> <ul> <li>The sum of all elements between <code>1</code> and <code>x</code> inclusively equals the sum of all elements between <code>x</code> and <code>n</code> inclusively.</li> </ul> <p>Return <em>the p...
3
{ "code": "class Solution {\npublic:\n int pivotInteger(int n) {\n auto it=pivots.find(n);\n if(it!=pivots.end())\n return it->second;\n else return -1;\n }\n Solution(){\n \n for(int i=1;i<1001;i++){\n float p=sqrt(float(i*i+i)/2.f);\n ...
2,571
<p>Given a positive integer <code>n</code>, find the <strong>pivot integer</strong> <code>x</code> such that:</p> <ul> <li>The sum of all elements between <code>1</code> and <code>x</code> inclusively equals the sum of all elements between <code>x</code> and <code>n</code> inclusively.</li> </ul> <p>Return <em>the p...
3
{ "code": "class Solution {\npublic:\n int pivotInteger(int n) {\n int arr[1001][2];\n int ans;\n int s = 0, e = 0;\n for (int i = 1; i <= n; i++) {\n s += i;\n e += n - i + 1;\n arr[i][0] = s;\n arr[n - i + 1][1] = e;\n }\n s = ...
2,571
<p>Given a positive integer <code>n</code>, find the <strong>pivot integer</strong> <code>x</code> such that:</p> <ul> <li>The sum of all elements between <code>1</code> and <code>x</code> inclusively equals the sum of all elements between <code>x</code> and <code>n</code> inclusively.</li> </ul> <p>Return <em>the p...
3
{ "code": "class Solution {\npublic:\n int pivotInteger(int n) {\n int arr[1001][2];\n int ans;\n int s = 0, e = 0;\n for (int i = 1; i <= n; i++) {\n s += i;\n e += n - i + 1;\n arr[i][0] = s;\n arr[n - i + 1][1] = e;\n }\n s = ...
2,571
<p>Given a positive integer <code>n</code>, find the <strong>pivot integer</strong> <code>x</code> such that:</p> <ul> <li>The sum of all elements between <code>1</code> and <code>x</code> inclusively equals the sum of all elements between <code>x</code> and <code>n</code> inclusively.</li> </ul> <p>Return <em>the p...
3
{ "code": "class Solution {\npublic:\n int pivotInteger(int n) {\n int arr[1001][2];\n int ans;\n int s = 0, e = 0;\n for (int i = 1; i <= n; i++) {\n s += i;\n e += n - i + 1;\n arr[i][0] = s;\n arr[n - i + 1][1] = e;\n }\n s = ...
2,571
<p>Given a positive integer <code>n</code>, find the <strong>pivot integer</strong> <code>x</code> such that:</p> <ul> <li>The sum of all elements between <code>1</code> and <code>x</code> inclusively equals the sum of all elements between <code>x</code> and <code>n</code> inclusively.</li> </ul> <p>Return <em>the p...
3
{ "code": "class Solution {\npublic:\n int pivotInteger(int n) {\n int arr[1001][2];\n int ans;\n int s = 0, e = 0;\n for (int i = 1; i <= n; i++) {\n s += i;\n e += n - i + 1;\n arr[i][0] = s;\n arr[n - i + 1][1] = e;\n }\n s = ...
2,571
<p>Given a positive integer <code>n</code>, find the <strong>pivot integer</strong> <code>x</code> such that:</p> <ul> <li>The sum of all elements between <code>1</code> and <code>x</code> inclusively equals the sum of all elements between <code>x</code> and <code>n</code> inclusively.</li> </ul> <p>Return <em>the p...
3
{ "code": "class Solution {\npublic:\n int pivotInteger(int n) {\n int a[1001];\n a[0]=0;\n for(int i=1;i<1001;i++){\n a[i]=i+a[i-1];\n }\n for(int i=1;i<=n;i++){\n if(a[i-1]==a[n]-a[i]){\n return i;\n }\n }\n return -...
2,571
<p>Given a positive integer <code>n</code>, find the <strong>pivot integer</strong> <code>x</code> such that:</p> <ul> <li>The sum of all elements between <code>1</code> and <code>x</code> inclusively equals the sum of all elements between <code>x</code> and <code>n</code> inclusively.</li> </ul> <p>Return <em>the p...
3
{ "code": "class Solution {\npublic:\n int pivotInteger(int n) {\n \n vector<int> prefixSum(n+1, 0);\n prefixSum[0] = 0;\n for (int i = 1; i <= n; ++i){\n prefixSum[i] = prefixSum[i - 1] + i;\n }\n for (int x = 1; x <= n; ++x) {\n int suffixSum = p...
2,571
<p>Given a positive integer <code>n</code>, find the <strong>pivot integer</strong> <code>x</code> such that:</p> <ul> <li>The sum of all elements between <code>1</code> and <code>x</code> inclusively equals the sum of all elements between <code>x</code> and <code>n</code> inclusively.</li> </ul> <p>Return <em>the p...
3
{ "code": "class Solution {\npublic:\n int pivotInteger(int n) {\n vector<int> dp(n);\n iota(dp.begin(),dp.end(),1);\n if(n==1) return 1;\n int sum = 0;\n long long s = accumulate(dp.begin(),dp.end(),0LL);\n for(auto x : dp) cout<<x<<' ';\n for(int i = 0;i<n;i++){\n...
2,571
<p>Given a positive integer <code>n</code>, find the <strong>pivot integer</strong> <code>x</code> such that:</p> <ul> <li>The sum of all elements between <code>1</code> and <code>x</code> inclusively equals the sum of all elements between <code>x</code> and <code>n</code> inclusively.</li> </ul> <p>Return <em>the p...
3
{ "code": "class Solution {\npublic:\nint pivotInteger(int n) {\n\tif (n == 1) {\n\t\treturn 1;\n\t}\n\tvector<int> ale(n);\n\tale[0] = 1;\n\tfor (int i = 1; i < ale.size(); i++) {\n\t\tale[i] = ale[i - 1] + i+1;\n\t}\n\tint maxsum = ale[ale.size() - 1];\n\tfor (int i = 0; i < ale.size(); i++) {\n\t\t//cout << ale[i]...
2,571
<p>Given a positive integer <code>n</code>, find the <strong>pivot integer</strong> <code>x</code> such that:</p> <ul> <li>The sum of all elements between <code>1</code> and <code>x</code> inclusively equals the sum of all elements between <code>x</code> and <code>n</code> inclusively.</li> </ul> <p>Return <em>the p...
3
{ "code": "class Solution {\npublic:\n int pivotInteger(int n) {\n vector<int> number(n);\n for(int i=1;i<=n;i++){\n number[i-1]=i;\n }\n int total_sum=0;\n int curr_sum=0;\n int right_sum=0;\n\n for(int i=0;i<number.size();i++){\n total_sum+=n...
2,571
<p>Given a positive integer <code>n</code>, find the <strong>pivot integer</strong> <code>x</code> such that:</p> <ul> <li>The sum of all elements between <code>1</code> and <code>x</code> inclusively equals the sum of all elements between <code>x</code> and <code>n</code> inclusively.</li> </ul> <p>Return <em>the p...
3
{ "code": "class Solution {\npublic:\n int pivotInteger(int n) {\n vector<int> left(n);\n vector<int> right(n);\n left[0] = 0;\n right[n-1] = 0;\n for(int i=1;i<n;i++){\n left[i] = left[i-1] + i;\n }\n for(int i=n-2;i>=0;i--){\n right[i] = righ...
2,571
<p>Given a positive integer <code>n</code>, find the <strong>pivot integer</strong> <code>x</code> such that:</p> <ul> <li>The sum of all elements between <code>1</code> and <code>x</code> inclusively equals the sum of all elements between <code>x</code> and <code>n</code> inclusively.</li> </ul> <p>Return <em>the p...
3
{ "code": "class Solution {\npublic:\n int pivotInteger(int n) {\n vector<int>leftsum(n);\n vector<int>rightsum(n);\n leftsum[0]=1;\n rightsum[n-1]=n;\n int i=1;\n while(i<n){\n leftsum[i]=leftsum[i-1]+i+1;\n rightsum[n-i-1]=rightsum[n-i]+n-i;\n ...
2,571
<p>Given a positive integer <code>n</code>, find the <strong>pivot integer</strong> <code>x</code> such that:</p> <ul> <li>The sum of all elements between <code>1</code> and <code>x</code> inclusively equals the sum of all elements between <code>x</code> and <code>n</code> inclusively.</li> </ul> <p>Return <em>the p...
3
{ "code": "class Solution {\npublic:\n int pivotInteger(int n) {\n vector<int> left(n, 0);\n vector<int> right(n, 0);\n left[0] = 1;\n right[n-1] = n;\n for(int i = 1; i < n; i++){\n left[i] = left[i-1] + i + 1;\n }\n for(int i = n-2; i >= 0; i--){\n ...
2,572
<p>You are given two strings <code>s</code> and <code>t</code> consisting of only lowercase English letters.</p> <p>Return <em>the minimum number of characters that need to be appended to the end of </em><code>s</code><em> so that </em><code>t</code><em> becomes a <strong>subsequence</strong> of </em><code>s</code>.</...
0
{ "code": "class Solution {\npublic:\n static int appendCharacters(string& s, string& t) {\n const int ns=s.size(), nt=t.size(); \n int i=0, j=0;\n for(i=0; i<ns && j<nt; i++){\n if(s[i]==t[j]) j++;\n }\n return nt-j;\n }\n};\n\nauto init = []() {\n ios::sync_wit...
2,572
<p>You are given two strings <code>s</code> and <code>t</code> consisting of only lowercase English letters.</p> <p>Return <em>the minimum number of characters that need to be appended to the end of </em><code>s</code><em> so that </em><code>t</code><em> becomes a <strong>subsequence</strong> of </em><code>s</code>.</...
0
{ "code": "class Solution {\npublic:\n static int appendCharacters(string& s, string& t) {\n const int ns=s.size(), nt=t.size(); \n int i=0, j=0;\n for(i=0; i<ns && j<nt; i++){\n if(s[i]==t[j]) j++;\n }\n return nt-j;\n }\n};\n\nauto init = []() {\n ios::sync_wit...
2,572
<p>You are given two strings <code>s</code> and <code>t</code> consisting of only lowercase English letters.</p> <p>Return <em>the minimum number of characters that need to be appended to the end of </em><code>s</code><em> so that </em><code>t</code><em> becomes a <strong>subsequence</strong> of </em><code>s</code>.</...
0
{ "code": "class Solution {\npublic:\n static int appendCharacters(string& s, string& t) {\n const int ns=s.size(), nt=t.size(); \n int i=0, j=0;\n for(i=0; i<ns && j<nt; i++){\n if(s[i]==t[j]) j++;\n }\n return nt-j;\n }\n};\n\nauto init = []() {\n ios::sync_wit...
2,572
<p>You are given two strings <code>s</code> and <code>t</code> consisting of only lowercase English letters.</p> <p>Return <em>the minimum number of characters that need to be appended to the end of </em><code>s</code><em> so that </em><code>t</code><em> becomes a <strong>subsequence</strong> of </em><code>s</code>.</...
0
{ "code": "class Solution {\npublic:\n static int appendCharacters(string& s, string& t) {\n const int ns=s.size(), nt=t.size(); \n int i=0, j=0;\n for(i=0; i<ns && j<nt; i++){\n if(s[i]==t[j]) j++;\n }\n return nt-j;\n }\n};\n\nauto init = []() {\n ios::sync_wit...
2,572
<p>You are given two strings <code>s</code> and <code>t</code> consisting of only lowercase English letters.</p> <p>Return <em>the minimum number of characters that need to be appended to the end of </em><code>s</code><em> so that </em><code>t</code><em> becomes a <strong>subsequence</strong> of </em><code>s</code>.</...
0
{ "code": "class Solution {\npublic:\n int appendCharacters(string s, string t) {\n int m = s.length();\n int n = t.length();\n int i =0;\n int j = 0;\n\n while( i<m && j< n){\n\n if(s[i] == t[j]){\n i++;\n j++;\n }\n ...
2,572
<p>You are given two strings <code>s</code> and <code>t</code> consisting of only lowercase English letters.</p> <p>Return <em>the minimum number of characters that need to be appended to the end of </em><code>s</code><em> so that </em><code>t</code><em> becomes a <strong>subsequence</strong> of </em><code>s</code>.</...
0
{ "code": "class Solution {\npublic:\n int appendCharacters(string s, string t) {\n int ssz = s.size();\n int tsz = t.size();\n int sidx = 0;\n int tidx = 0;\n while(sidx<ssz && tidx<tsz){\n if (s[sidx]==t[tidx]){\n tidx++;\n }\n si...
2,572
<p>You are given two strings <code>s</code> and <code>t</code> consisting of only lowercase English letters.</p> <p>Return <em>the minimum number of characters that need to be appended to the end of </em><code>s</code><em> so that </em><code>t</code><em> becomes a <strong>subsequence</strong> of </em><code>s</code>.</...
0
{ "code": "class Solution {\npublic:\n int appendCharacters(string s, string t) {\n int p1 = 0, p2 = 0;\n while(p1 != s.size()) {\n if(s[p1] == t[p2]) {\n p1++;\n p2++;\n }\n else {\n p1++;\n }\n }\n ...
2,572
<p>You are given two strings <code>s</code> and <code>t</code> consisting of only lowercase English letters.</p> <p>Return <em>the minimum number of characters that need to be appended to the end of </em><code>s</code><em> so that </em><code>t</code><em> becomes a <strong>subsequence</strong> of </em><code>s</code>.</...
0
{ "code": "class Solution {\npublic:\n int appendCharacters(string s, string t) {\n\n int i = 0;\n int j = 0;\n\n int m = s.length();\n int n = t.length();\n\n while(i<m && j<n){\n if(s[i] == t[j]){\n i++;\n j++;\n }\n ...
2,572
<p>You are given two strings <code>s</code> and <code>t</code> consisting of only lowercase English letters.</p> <p>Return <em>the minimum number of characters that need to be appended to the end of </em><code>s</code><em> so that </em><code>t</code><em> becomes a <strong>subsequence</strong> of </em><code>s</code>.</...
0
{ "code": "class Solution {\npublic:\n int appendCharacters(string s, string t) {\n int n=s.size(),m=t.size();\n int i=0,j=0,cnt=0;\n while(i<n&&j<m){\n if(s[i]==t[j]){\n cnt++;\n i++;\n j++;\n }\n else{\n ...