id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
1,808
<p>Alice and Bob take turns playing a game, with <strong>Alice starting first</strong>.</p> <p>There are <code>n</code> stones arranged in a row. On each player&#39;s turn, they can <strong>remove</strong> either the leftmost stone or the rightmost stone from the row and receive points equal to the <strong>sum</strong> of the remaining stones&#39; values in the row. The winner is the one with the higher score when there are no stones left to remove.</p> <p>Bob found that he will always lose this game (poor Bob, he always loses), so he decided to <strong>minimize the score&#39;s difference</strong>. Alice&#39;s goal is to <strong>maximize the difference</strong> in the score.</p> <p>Given an array of integers <code>stones</code> where <code>stones[i]</code> represents the value of the <code>i<sup>th</sup></code> stone <strong>from the left</strong>, return <em>the <strong>difference</strong> in Alice and Bob&#39;s score if they both play <strong>optimally</strong>.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [5,3,1,4,2] <strong>Output:</strong> 6 <strong>Explanation:</strong> - Alice removes 2 and gets 5 + 3 + 1 + 4 = 13 points. Alice = 13, Bob = 0, stones = [5,3,1,4]. - Bob removes 5 and gets 3 + 1 + 4 = 8 points. Alice = 13, Bob = 8, stones = [3,1,4]. - Alice removes 3 and gets 1 + 4 = 5 points. Alice = 18, Bob = 8, stones = [1,4]. - Bob removes 1 and gets 4 points. Alice = 18, Bob = 12, stones = [4]. - Alice removes 4 and gets 0 points. Alice = 18, Bob = 12, stones = []. The score difference is 18 - 12 = 6. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [7,90,5,1,100,10,10,2] <strong>Output:</strong> 122</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == stones.length</code></li> <li><code>2 &lt;= n &lt;= 1000</code></li> <li><code>1 &lt;= stones[i] &lt;= 1000</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n pair<int,int> aliceCount(int i,int j,int turn,vector<int>& pre,vector<vector<vector<pair<int,int>>>>& dp)\n {\n if(i >= j)\n return {0,0};\n \n if(dp[i][j][turn].first != -1 && dp[i][j][turn].second != -1) \n return dp[i][j][turn];\n\n int sum1 = pre[j] - pre[i]; \n int sum2 = pre[j-1] - ((i-1 >=0) ? pre[i-1] : 0); \n pair<int,int> ret;\n\n if(turn == 0) \n {\n auto ans1 = aliceCount(i+1,j,1,pre,dp);\n auto ans2 = aliceCount(i,j-1,1,pre,dp);\n if(sum1 + ans1.first - ans1.second > sum2 + ans2.first - ans2.second) {\n ret = {sum1 + ans1.first, ans1.second};\n } else {\n ret = {sum2 + ans2.first, ans2.second};\n }\n }\n else \n {\n auto ans1 = aliceCount(i+1,j,0,pre,dp);\n auto ans2 = aliceCount(i,j-1,0,pre,dp);\n if(ans1.first - ans1.second - sum1 < ans2.first - ans2.second - sum2) {\n ret = {ans1.first, ans1.second + sum1};\n } else {\n ret = {ans2.first, ans2.second + sum2};\n }\n }\n\n return dp[i][j][turn] = ret;\n }\n\n int stoneGameVII(vector<int>& stones) {\n int n = stones.size();\n vector<int> pre(n,0);\n pre[0] = stones[0];\n for(int i=1;i<n;i++) {\n pre[i] = pre[i-1] + stones[i];\n }\n vector<vector<vector<pair<int,int>>>> dp(n,vector<vector<pair<int,int>>>(n,vector<pair<int,int>>(2,{-1,-1})));\n auto alice = aliceCount(0,n-1,0,pre,dp);\n return alice.first - alice.second;\n }\n};\n", "memory": "264478" }
1,808
<p>Alice and Bob take turns playing a game, with <strong>Alice starting first</strong>.</p> <p>There are <code>n</code> stones arranged in a row. On each player&#39;s turn, they can <strong>remove</strong> either the leftmost stone or the rightmost stone from the row and receive points equal to the <strong>sum</strong> of the remaining stones&#39; values in the row. The winner is the one with the higher score when there are no stones left to remove.</p> <p>Bob found that he will always lose this game (poor Bob, he always loses), so he decided to <strong>minimize the score&#39;s difference</strong>. Alice&#39;s goal is to <strong>maximize the difference</strong> in the score.</p> <p>Given an array of integers <code>stones</code> where <code>stones[i]</code> represents the value of the <code>i<sup>th</sup></code> stone <strong>from the left</strong>, return <em>the <strong>difference</strong> in Alice and Bob&#39;s score if they both play <strong>optimally</strong>.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [5,3,1,4,2] <strong>Output:</strong> 6 <strong>Explanation:</strong> - Alice removes 2 and gets 5 + 3 + 1 + 4 = 13 points. Alice = 13, Bob = 0, stones = [5,3,1,4]. - Bob removes 5 and gets 3 + 1 + 4 = 8 points. Alice = 13, Bob = 8, stones = [3,1,4]. - Alice removes 3 and gets 1 + 4 = 5 points. Alice = 18, Bob = 8, stones = [1,4]. - Bob removes 1 and gets 4 points. Alice = 18, Bob = 12, stones = [4]. - Alice removes 4 and gets 0 points. Alice = 18, Bob = 12, stones = []. The score difference is 18 - 12 = 6. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [7,90,5,1,100,10,10,2] <strong>Output:</strong> 122</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == stones.length</code></li> <li><code>2 &lt;= n &lt;= 1000</code></li> <li><code>1 &lt;= stones[i] &lt;= 1000</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int stoneGameVII(vector<int>& stones) {\n int n=stones.size();\n vector<vector<vector<pair<int,int>>>>dp(n+1,vector<vector<pair<int,int>>>(n+1,vector<pair<int,int>>(2,{0,0})));\n int total=0;\n for(int i=n-1;i>=0;i--)\n {\n total=stones[i];\n for(int j=i+1;j<n;j++)\n {\n total+=stones[j];\n for(int k=0;k<2;k++)\n {\n if(i>=j)\n {\n dp[i][j][k]={0,0};\n continue;\n }\n pair<int,int>a=dp[i+1][j][k^1];\n pair<int,int>b=dp[i][j-1][k^1];\n if(k==0)\n {\n if(total-stones[i]+a.first-a.second>=total-stones[j]+b.first-b.second)\n {\n dp[i][j][k]=a;\n dp[i][j][k].first+=total-stones[i];\n }\n else\n {\n dp[i][j][k]=b;\n dp[i][j][k].first+=total-stones[j];\n }\n }\n else{\n if(a.first-(total-stones[i]+a.second)<=b.first-(total-stones[j]+b.second))\n {\n dp[i][j][k]=a;\n dp[i][j][k].second+=total-stones[i];\n }\n else\n {\n dp[i][j][k]=b;\n dp[i][j][k].second+=total-stones[j];\n }\n }\n }\n }\n }\n return (dp[0][n-1][0].first-dp[0][n-1][0].second);\n }\n};", "memory": "273651" }
1,808
<p>Alice and Bob take turns playing a game, with <strong>Alice starting first</strong>.</p> <p>There are <code>n</code> stones arranged in a row. On each player&#39;s turn, they can <strong>remove</strong> either the leftmost stone or the rightmost stone from the row and receive points equal to the <strong>sum</strong> of the remaining stones&#39; values in the row. The winner is the one with the higher score when there are no stones left to remove.</p> <p>Bob found that he will always lose this game (poor Bob, he always loses), so he decided to <strong>minimize the score&#39;s difference</strong>. Alice&#39;s goal is to <strong>maximize the difference</strong> in the score.</p> <p>Given an array of integers <code>stones</code> where <code>stones[i]</code> represents the value of the <code>i<sup>th</sup></code> stone <strong>from the left</strong>, return <em>the <strong>difference</strong> in Alice and Bob&#39;s score if they both play <strong>optimally</strong>.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [5,3,1,4,2] <strong>Output:</strong> 6 <strong>Explanation:</strong> - Alice removes 2 and gets 5 + 3 + 1 + 4 = 13 points. Alice = 13, Bob = 0, stones = [5,3,1,4]. - Bob removes 5 and gets 3 + 1 + 4 = 8 points. Alice = 13, Bob = 8, stones = [3,1,4]. - Alice removes 3 and gets 1 + 4 = 5 points. Alice = 18, Bob = 8, stones = [1,4]. - Bob removes 1 and gets 4 points. Alice = 18, Bob = 12, stones = [4]. - Alice removes 4 and gets 0 points. Alice = 18, Bob = 12, stones = []. The score difference is 18 - 12 = 6. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [7,90,5,1,100,10,10,2] <strong>Output:</strong> 122</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == stones.length</code></li> <li><code>2 &lt;= n &lt;= 1000</code></li> <li><code>1 &lt;= stones[i] &lt;= 1000</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n pair<int,int> dynamicthink(vector<vector<vector<pair<int,int>>>>&dp,int curr,int start,int end,int total,vector<int>&stones)\n {\n if(start>=end)\n {\n return {0,0};\n }\n if(dp[start][end][curr].first!=-1)\n {\n return dp[start][end][curr];\n }\n dp[start][end][curr]={0,0};\n pair<int,int>a=dynamicthink(dp,curr^1,start+1,end,total-stones[start],stones);\n pair<int,int>b=dynamicthink(dp,curr^1,start,end-1,total-stones[end],stones);\n if(curr==0)\n {\n if(total-stones[start]+a.first-a.second>=total-stones[end]+b.first-b.second)\n {\n dp[start][end][curr]=a;\n dp[start][end][curr].first+=total-stones[start];\n }\n else\n {\n dp[start][end][curr]=b;\n dp[start][end][curr].first+=total-stones[end];\n }\n }\n else\n {\n if(abs(total-stones[start]+a.second-a.first)>=abs(total-stones[end]+b.second-b.first))\n {\n dp[start][end][curr]=a;\n dp[start][end][curr].second+=total-stones[start];\n }\n else\n {\n dp[start][end][curr]=b;\n dp[start][end][curr].second+=total-stones[end];\n }\n }\n return dp[start][end][curr];\n }\n int stoneGameVII(vector<int>& stones) {\n int n=stones.size();\n vector<vector<vector<pair<int,int>>>>dp(n,vector<vector<pair<int,int>>>(n,vector<pair<int,int>>(3,{-1,-1})));\n pair<int,int>a=dynamicthink(dp,0,0,n-1,accumulate(stones.begin(),stones.end(),0),stones);\n return (a.first-a.second);\n }\n};", "memory": "301173" }
1,808
<p>Alice and Bob take turns playing a game, with <strong>Alice starting first</strong>.</p> <p>There are <code>n</code> stones arranged in a row. On each player&#39;s turn, they can <strong>remove</strong> either the leftmost stone or the rightmost stone from the row and receive points equal to the <strong>sum</strong> of the remaining stones&#39; values in the row. The winner is the one with the higher score when there are no stones left to remove.</p> <p>Bob found that he will always lose this game (poor Bob, he always loses), so he decided to <strong>minimize the score&#39;s difference</strong>. Alice&#39;s goal is to <strong>maximize the difference</strong> in the score.</p> <p>Given an array of integers <code>stones</code> where <code>stones[i]</code> represents the value of the <code>i<sup>th</sup></code> stone <strong>from the left</strong>, return <em>the <strong>difference</strong> in Alice and Bob&#39;s score if they both play <strong>optimally</strong>.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [5,3,1,4,2] <strong>Output:</strong> 6 <strong>Explanation:</strong> - Alice removes 2 and gets 5 + 3 + 1 + 4 = 13 points. Alice = 13, Bob = 0, stones = [5,3,1,4]. - Bob removes 5 and gets 3 + 1 + 4 = 8 points. Alice = 13, Bob = 8, stones = [3,1,4]. - Alice removes 3 and gets 1 + 4 = 5 points. Alice = 18, Bob = 8, stones = [1,4]. - Bob removes 1 and gets 4 points. Alice = 18, Bob = 12, stones = [4]. - Alice removes 4 and gets 0 points. Alice = 18, Bob = 12, stones = []. The score difference is 18 - 12 = 6. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [7,90,5,1,100,10,10,2] <strong>Output:</strong> 122</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == stones.length</code></li> <li><code>2 &lt;= n &lt;= 1000</code></li> <li><code>1 &lt;= stones[i] &lt;= 1000</code></li> </ul>
3
{ "code": "class Solution {\n int f(int i,int j,int k,int sum,vector<int>& stones,vector<vector<vector<int>>>&dp){\n if(i>j)return 0;\n if(dp[i][j][k]!=-1)return dp[i][j][k];\n int x=-1e9,y=1e9;\n if(k==0){\n x=max(x,sum-stones[i]+f(i+1,j,1,sum-stones[i],stones,dp));\n x=max(x,sum-stones[j]+f(i,j-1,1,sum-stones[j],stones,dp));\n }\n else{\n y=min(y,-sum+stones[i]+f(i+1,j,0,sum-stones[i],stones,dp));\n y=min(y,-sum+stones[j]+f(i,j-1,0,sum-stones[j],stones,dp));\n }\n if(k==0)return dp[i][j][k]=x;\n return dp[i][j][k]=y;\n }\npublic:\n int stoneGameVII(vector<int>& stones) {\n int n=stones.size();\n int sum=0;\n for(auto it:stones)sum+=it;\n vector<vector<vector<int>>>dp(n+1,vector<vector<int>>(n+1,vector<int>(3,-1)));\n return f(0,n-1,0,sum,stones,dp);\n return dp[0][n-1][0];\n }\n};", "memory": "310346" }
1,808
<p>Alice and Bob take turns playing a game, with <strong>Alice starting first</strong>.</p> <p>There are <code>n</code> stones arranged in a row. On each player&#39;s turn, they can <strong>remove</strong> either the leftmost stone or the rightmost stone from the row and receive points equal to the <strong>sum</strong> of the remaining stones&#39; values in the row. The winner is the one with the higher score when there are no stones left to remove.</p> <p>Bob found that he will always lose this game (poor Bob, he always loses), so he decided to <strong>minimize the score&#39;s difference</strong>. Alice&#39;s goal is to <strong>maximize the difference</strong> in the score.</p> <p>Given an array of integers <code>stones</code> where <code>stones[i]</code> represents the value of the <code>i<sup>th</sup></code> stone <strong>from the left</strong>, return <em>the <strong>difference</strong> in Alice and Bob&#39;s score if they both play <strong>optimally</strong>.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [5,3,1,4,2] <strong>Output:</strong> 6 <strong>Explanation:</strong> - Alice removes 2 and gets 5 + 3 + 1 + 4 = 13 points. Alice = 13, Bob = 0, stones = [5,3,1,4]. - Bob removes 5 and gets 3 + 1 + 4 = 8 points. Alice = 13, Bob = 8, stones = [3,1,4]. - Alice removes 3 and gets 1 + 4 = 5 points. Alice = 18, Bob = 8, stones = [1,4]. - Bob removes 1 and gets 4 points. Alice = 18, Bob = 12, stones = [4]. - Alice removes 4 and gets 0 points. Alice = 18, Bob = 12, stones = []. The score difference is 18 - 12 = 6. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [7,90,5,1,100,10,10,2] <strong>Output:</strong> 122</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == stones.length</code></li> <li><code>2 &lt;= n &lt;= 1000</code></li> <li><code>1 &lt;= stones[i] &lt;= 1000</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int solve(vector<int>& piles, int i, int j, int c,\n vector<vector<vector<int>>>& dp, int sum) {\n if (i == j)\n return 0;\n if (dp[i][j][c] != -1)\n return dp[i][j][c];\n int a = 0;\n if (c == 1) {\n a = INT_MAX;\n int temp1 = -(sum - piles[i]) +\n solve(piles, i + 1, j, 0, dp, sum - piles[i]);\n int temp2 = -(sum - piles[j]) +\n solve(piles, i, j - 1, 0, dp, sum - piles[j]);\n a = min({a, temp1, temp2});\n } else {\n int temp1 = (sum - piles[i]) +\n solve(piles, i + 1, j, 1, dp, sum - piles[i]);\n int temp2 = (sum - piles[j]) +\n solve(piles, i, j - 1, 1, dp, sum - piles[j]);\n a = max({a, temp1, temp2});\n }\n return dp[i][j][c] = a;\n }\n int stoneGameVII(vector<int>& piles) {\n vector<vector<vector<int>>> dp(\n piles.size() + 1,\n vector<vector<int>>(piles.size() + 1, vector<int>(3, -1)));\n int sum = accumulate(piles.begin(), piles.end(), 0);\n int a = solve(piles, 0, piles.size() - 1, 0, dp, sum);\n return a;\n }\n};", "memory": "319520" }
1,808
<p>Alice and Bob take turns playing a game, with <strong>Alice starting first</strong>.</p> <p>There are <code>n</code> stones arranged in a row. On each player&#39;s turn, they can <strong>remove</strong> either the leftmost stone or the rightmost stone from the row and receive points equal to the <strong>sum</strong> of the remaining stones&#39; values in the row. The winner is the one with the higher score when there are no stones left to remove.</p> <p>Bob found that he will always lose this game (poor Bob, he always loses), so he decided to <strong>minimize the score&#39;s difference</strong>. Alice&#39;s goal is to <strong>maximize the difference</strong> in the score.</p> <p>Given an array of integers <code>stones</code> where <code>stones[i]</code> represents the value of the <code>i<sup>th</sup></code> stone <strong>from the left</strong>, return <em>the <strong>difference</strong> in Alice and Bob&#39;s score if they both play <strong>optimally</strong>.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [5,3,1,4,2] <strong>Output:</strong> 6 <strong>Explanation:</strong> - Alice removes 2 and gets 5 + 3 + 1 + 4 = 13 points. Alice = 13, Bob = 0, stones = [5,3,1,4]. - Bob removes 5 and gets 3 + 1 + 4 = 8 points. Alice = 13, Bob = 8, stones = [3,1,4]. - Alice removes 3 and gets 1 + 4 = 5 points. Alice = 18, Bob = 8, stones = [1,4]. - Bob removes 1 and gets 4 points. Alice = 18, Bob = 12, stones = [4]. - Alice removes 4 and gets 0 points. Alice = 18, Bob = 12, stones = []. The score difference is 18 - 12 = 6. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [7,90,5,1,100,10,10,2] <strong>Output:</strong> 122</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == stones.length</code></li> <li><code>2 &lt;= n &lt;= 1000</code></li> <li><code>1 &lt;= stones[i] &lt;= 1000</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int solve(vector<int>& piles, int i, int j, int c,\n vector<vector<vector<int>>>& dp, int sum) {\n if (i == j)\n return 0;\n if (dp[i][j][c] != -1)\n return dp[i][j][c];\n int a = 0;\n if (c == 1) {\n a = INT_MAX;\n int temp1 = -(sum - piles[i]) +\n solve(piles, i + 1, j, 0, dp, sum - piles[i]);\n int temp2 = -(sum - piles[j]) +\n solve(piles, i, j - 1, 0, dp, sum - piles[j]);\n a = min({a, temp1, temp2});\n } else {\n int temp1 = (sum - piles[i]) +\n solve(piles, i + 1, j, 1, dp, sum - piles[i]);\n int temp2 = (sum - piles[j]) +\n solve(piles, i, j - 1, 1, dp, sum - piles[j]);\n a = max({a, temp1, temp2});\n }\n return dp[i][j][c] = a;\n }\n int stoneGameVII(vector<int>& piles) {\n vector<vector<vector<int>>> dp(\n piles.size() + 1,\n vector<vector<int>>(piles.size() + 1, vector<int>(3, -1)));\n int sum = accumulate(piles.begin(), piles.end(), 0);\n int a = solve(piles, 0, piles.size() - 1, 0, dp, sum);\n return a;\n }\n};", "memory": "319520" }
1,808
<p>Alice and Bob take turns playing a game, with <strong>Alice starting first</strong>.</p> <p>There are <code>n</code> stones arranged in a row. On each player&#39;s turn, they can <strong>remove</strong> either the leftmost stone or the rightmost stone from the row and receive points equal to the <strong>sum</strong> of the remaining stones&#39; values in the row. The winner is the one with the higher score when there are no stones left to remove.</p> <p>Bob found that he will always lose this game (poor Bob, he always loses), so he decided to <strong>minimize the score&#39;s difference</strong>. Alice&#39;s goal is to <strong>maximize the difference</strong> in the score.</p> <p>Given an array of integers <code>stones</code> where <code>stones[i]</code> represents the value of the <code>i<sup>th</sup></code> stone <strong>from the left</strong>, return <em>the <strong>difference</strong> in Alice and Bob&#39;s score if they both play <strong>optimally</strong>.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [5,3,1,4,2] <strong>Output:</strong> 6 <strong>Explanation:</strong> - Alice removes 2 and gets 5 + 3 + 1 + 4 = 13 points. Alice = 13, Bob = 0, stones = [5,3,1,4]. - Bob removes 5 and gets 3 + 1 + 4 = 8 points. Alice = 13, Bob = 8, stones = [3,1,4]. - Alice removes 3 and gets 1 + 4 = 5 points. Alice = 18, Bob = 8, stones = [1,4]. - Bob removes 1 and gets 4 points. Alice = 18, Bob = 12, stones = [4]. - Alice removes 4 and gets 0 points. Alice = 18, Bob = 12, stones = []. The score difference is 18 - 12 = 6. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [7,90,5,1,100,10,10,2] <strong>Output:</strong> 122</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == stones.length</code></li> <li><code>2 &lt;= n &lt;= 1000</code></li> <li><code>1 &lt;= stones[i] &lt;= 1000</code></li> </ul>
3
{ "code": "class Solution {\npublic:\nint solve(vector<int> &v,int i,int j,int turn,int sum,vector<vector<vector<int>>> &dp)\n {\n if(i>j || sum<0)\n {\n return 0;\n }\n\n if(dp[i][j][turn]!=-1)\n return dp[i][j][turn];\n\n int ans;\n if(turn==0)\n {\n ans=-1e9;\n ans=max(ans,sum-v[i]+solve(v,i+1,j,!turn,sum-v[i],dp));\n ans=max(ans,sum-v[j]+solve(v,i,j-1,!turn,sum-v[j],dp));\n }\n else\n {\n ans=1e9;\n ans=min(ans,-(sum-v[i])+solve(v,i+1,j,!turn,sum-v[i],dp));\n ans=min(ans,-(sum-v[j])+solve(v,i,j-1,!turn,sum-v[j],dp));\n }\n\n return dp[i][j][turn]=ans;\n }\n int stoneGameVII(vector<int>& v) {\n int i=0,j=v.size()-1;\n int turn=0;\n\n int sum=0;\n for(int k=0;k<v.size();k++)\n sum+=v[k];\n\n vector<vector<vector<int>>> dp(v.size()+1,vector<vector<int>>(v.size()+1,vector<int>(2,-1)));\n return solve(v,i,j,turn,sum,dp);\n }\n};", "memory": "328694" }
1,808
<p>Alice and Bob take turns playing a game, with <strong>Alice starting first</strong>.</p> <p>There are <code>n</code> stones arranged in a row. On each player&#39;s turn, they can <strong>remove</strong> either the leftmost stone or the rightmost stone from the row and receive points equal to the <strong>sum</strong> of the remaining stones&#39; values in the row. The winner is the one with the higher score when there are no stones left to remove.</p> <p>Bob found that he will always lose this game (poor Bob, he always loses), so he decided to <strong>minimize the score&#39;s difference</strong>. Alice&#39;s goal is to <strong>maximize the difference</strong> in the score.</p> <p>Given an array of integers <code>stones</code> where <code>stones[i]</code> represents the value of the <code>i<sup>th</sup></code> stone <strong>from the left</strong>, return <em>the <strong>difference</strong> in Alice and Bob&#39;s score if they both play <strong>optimally</strong>.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [5,3,1,4,2] <strong>Output:</strong> 6 <strong>Explanation:</strong> - Alice removes 2 and gets 5 + 3 + 1 + 4 = 13 points. Alice = 13, Bob = 0, stones = [5,3,1,4]. - Bob removes 5 and gets 3 + 1 + 4 = 8 points. Alice = 13, Bob = 8, stones = [3,1,4]. - Alice removes 3 and gets 1 + 4 = 5 points. Alice = 18, Bob = 8, stones = [1,4]. - Bob removes 1 and gets 4 points. Alice = 18, Bob = 12, stones = [4]. - Alice removes 4 and gets 0 points. Alice = 18, Bob = 12, stones = []. The score difference is 18 - 12 = 6. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [7,90,5,1,100,10,10,2] <strong>Output:</strong> 122</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == stones.length</code></li> <li><code>2 &lt;= n &lt;= 1000</code></li> <li><code>1 &lt;= stones[i] &lt;= 1000</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n\n int func(int i,int j,int t,int sum,vector<int>&a,vector<vector<vector<int>>>&dp){\n if(i>=j) return 0;\n if(dp[i][j][t]!=-1) return dp[i][j][t];\n int ans;\n if(t){\n ans=max(sum-a[i]+func(i+1,j,1-t,sum-a[i],a,dp),sum-a[j]+func(i,j-1,1-t,sum-a[j],a,dp));\n }\n else{\n ans=min(-(sum-a[i])+func(i+1,j,1-t,sum-a[i],a,dp),-(sum-a[j])+func(i,j-1,1-t,sum-a[j],a,dp));\n }\n return dp[i][j][t]=ans;\n }\n\n\n int stoneGameVII(vector<int>& a) {\n int n=a.size();\n vector<vector<vector<int>>> dp(n+1,vector<vector<int>>(n+1,vector<int>(2,-1)));\n return func(0,n-1,1,accumulate(a.begin(),a.end(),0),a,dp);\n }\n};", "memory": "337868" }
1,808
<p>Alice and Bob take turns playing a game, with <strong>Alice starting first</strong>.</p> <p>There are <code>n</code> stones arranged in a row. On each player&#39;s turn, they can <strong>remove</strong> either the leftmost stone or the rightmost stone from the row and receive points equal to the <strong>sum</strong> of the remaining stones&#39; values in the row. The winner is the one with the higher score when there are no stones left to remove.</p> <p>Bob found that he will always lose this game (poor Bob, he always loses), so he decided to <strong>minimize the score&#39;s difference</strong>. Alice&#39;s goal is to <strong>maximize the difference</strong> in the score.</p> <p>Given an array of integers <code>stones</code> where <code>stones[i]</code> represents the value of the <code>i<sup>th</sup></code> stone <strong>from the left</strong>, return <em>the <strong>difference</strong> in Alice and Bob&#39;s score if they both play <strong>optimally</strong>.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [5,3,1,4,2] <strong>Output:</strong> 6 <strong>Explanation:</strong> - Alice removes 2 and gets 5 + 3 + 1 + 4 = 13 points. Alice = 13, Bob = 0, stones = [5,3,1,4]. - Bob removes 5 and gets 3 + 1 + 4 = 8 points. Alice = 13, Bob = 8, stones = [3,1,4]. - Alice removes 3 and gets 1 + 4 = 5 points. Alice = 18, Bob = 8, stones = [1,4]. - Bob removes 1 and gets 4 points. Alice = 18, Bob = 12, stones = [4]. - Alice removes 4 and gets 0 points. Alice = 18, Bob = 12, stones = []. The score difference is 18 - 12 = 6. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [7,90,5,1,100,10,10,2] <strong>Output:</strong> 122</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == stones.length</code></li> <li><code>2 &lt;= n &lt;= 1000</code></li> <li><code>1 &lt;= stones[i] &lt;= 1000</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int stoneGameVII(vector<int>& stones) {\n int n = stones.size();\n vector<int> s(n + 1, 0);\n for (int i = n - 1; i >= 0; i--)\n s[i] = s[i + 1] + stones[i];\n int sum = s[0];\n vector<vector<vector<int>>> dp(\n n + 1, vector<vector<int>>(n + 1, vector<int>(2, 0)));\n\n for (int i = 2; i <= n; i++) {\n for (int j = 0; j <= n - i; j++) {\n int leftSum = s[j + 1] - s[i + j];\n int rightSum = s[j] - s[i + j - 1];\n if (leftSum + dp[j + 1][i + j - 1][1] -\n dp[j + 1][i + j - 1][0] >\n rightSum + dp[j][i + j - 2][1] - dp[j][i + j - 2][0]) {\n dp[j][j + i - 1][0] = leftSum + dp[j + 1][i + j - 1][1];\n dp[j][j + i - 1][1] = dp[j + 1][i + j - 1][0];\n } else {\n dp[j][j + i - 1][0] = rightSum + dp[j][i + j - 2][1];\n dp[j][j + i - 1][1] = dp[j][i + j - 2][0];\n }\n }\n }\n\n return dp[0][n - 1][0] - dp[0][n - 1][1];\n }\n};", "memory": "347041" }
1,808
<p>Alice and Bob take turns playing a game, with <strong>Alice starting first</strong>.</p> <p>There are <code>n</code> stones arranged in a row. On each player&#39;s turn, they can <strong>remove</strong> either the leftmost stone or the rightmost stone from the row and receive points equal to the <strong>sum</strong> of the remaining stones&#39; values in the row. The winner is the one with the higher score when there are no stones left to remove.</p> <p>Bob found that he will always lose this game (poor Bob, he always loses), so he decided to <strong>minimize the score&#39;s difference</strong>. Alice&#39;s goal is to <strong>maximize the difference</strong> in the score.</p> <p>Given an array of integers <code>stones</code> where <code>stones[i]</code> represents the value of the <code>i<sup>th</sup></code> stone <strong>from the left</strong>, return <em>the <strong>difference</strong> in Alice and Bob&#39;s score if they both play <strong>optimally</strong>.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [5,3,1,4,2] <strong>Output:</strong> 6 <strong>Explanation:</strong> - Alice removes 2 and gets 5 + 3 + 1 + 4 = 13 points. Alice = 13, Bob = 0, stones = [5,3,1,4]. - Bob removes 5 and gets 3 + 1 + 4 = 8 points. Alice = 13, Bob = 8, stones = [3,1,4]. - Alice removes 3 and gets 1 + 4 = 5 points. Alice = 18, Bob = 8, stones = [1,4]. - Bob removes 1 and gets 4 points. Alice = 18, Bob = 12, stones = [4]. - Alice removes 4 and gets 0 points. Alice = 18, Bob = 12, stones = []. The score difference is 18 - 12 = 6. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [7,90,5,1,100,10,10,2] <strong>Output:</strong> 122</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == stones.length</code></li> <li><code>2 &lt;= n &lt;= 1000</code></li> <li><code>1 &lt;= stones[i] &lt;= 1000</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int solve(int i, int j, int turn, vector<int>&pre,vector<vector<vector<int>>> &dp){\n if(j-i+1==0){\n return 0; // base case when only one element left\n } \n\n if(dp[i][j][turn]!=-1){\n return dp[i][j][turn];\n }\n int ans;\n if(turn==0){\n // i+1 and j\n int a = pre[j] - pre[i] + solve(i+1,j,(turn+1)%2,pre,dp);\n // i and j-1\n int b = pre[j-1] - pre[i-1] + solve(i,j-1,(turn+1)%2,pre,dp);\n\n ans = max(a,b);\n }else{\n int a = -(pre[j] - pre[i]) + solve(i+1,j,(turn+1)%2,pre,dp);\n // i and j-1\n int b = -(pre[j-1] - pre[i-1]) + solve(i,j-1,(turn+1)%2,pre,dp);\n\n ans = min(a,b);\n }\n\n return dp[i][j][turn] = ans;\n }\n\n int stoneGameVII(vector<int>& stones) {\n int n = stones.size();\n vector<int> pre(n+1,0);\n for(int i = 1;i<=n;i++){\n pre[i] = pre[i-1] + stones[i-1];\n }\n\n vector<vector<vector<int>>> dp(n+1,vector<vector<int>>(n+1,vector<int>(2,-1)));\n\n return solve(1,n,0,pre,dp);\n }\n};", "memory": "356215" }
1,808
<p>Alice and Bob take turns playing a game, with <strong>Alice starting first</strong>.</p> <p>There are <code>n</code> stones arranged in a row. On each player&#39;s turn, they can <strong>remove</strong> either the leftmost stone or the rightmost stone from the row and receive points equal to the <strong>sum</strong> of the remaining stones&#39; values in the row. The winner is the one with the higher score when there are no stones left to remove.</p> <p>Bob found that he will always lose this game (poor Bob, he always loses), so he decided to <strong>minimize the score&#39;s difference</strong>. Alice&#39;s goal is to <strong>maximize the difference</strong> in the score.</p> <p>Given an array of integers <code>stones</code> where <code>stones[i]</code> represents the value of the <code>i<sup>th</sup></code> stone <strong>from the left</strong>, return <em>the <strong>difference</strong> in Alice and Bob&#39;s score if they both play <strong>optimally</strong>.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [5,3,1,4,2] <strong>Output:</strong> 6 <strong>Explanation:</strong> - Alice removes 2 and gets 5 + 3 + 1 + 4 = 13 points. Alice = 13, Bob = 0, stones = [5,3,1,4]. - Bob removes 5 and gets 3 + 1 + 4 = 8 points. Alice = 13, Bob = 8, stones = [3,1,4]. - Alice removes 3 and gets 1 + 4 = 5 points. Alice = 18, Bob = 8, stones = [1,4]. - Bob removes 1 and gets 4 points. Alice = 18, Bob = 12, stones = [4]. - Alice removes 4 and gets 0 points. Alice = 18, Bob = 12, stones = []. The score difference is 18 - 12 = 6. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [7,90,5,1,100,10,10,2] <strong>Output:</strong> 122</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == stones.length</code></li> <li><code>2 &lt;= n &lt;= 1000</code></li> <li><code>1 &lt;= stones[i] &lt;= 1000</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n\n int rec(vector<int>& stones,int start,int end,int turn,vector<vector<vector<int>>>&dp,int sum){\n if(start>end){\n return 0;\n }\n if(dp[start][end][turn]!=-1){\n return dp[start][end][turn];\n }\n if(turn){\n int first=sum-stones[start] + rec(stones,start+1,end,0,dp,sum-stones[start]);\n int last=sum-stones[end]+ rec(stones,start,end-1,0,dp,sum-stones[end]);\n return dp[start][end][turn]=max(first,last);\n }\n else{\n int first=-(sum-stones[start]) + rec(stones,start+1,end,1,dp,sum-stones[start] );\n int last=-(sum-stones[end])+ rec(stones,start,end-1,1,dp,sum-stones[end]);\n return dp[start][end][turn]=min(first,last);\n }\n }\n\n int stoneGameVII(vector<int>& stones) {\n int n=stones.size();\n int sum=0;\n for(int x:stones){\n sum+=x;\n }\n vector<vector<vector<int>>>dp(n,vector<vector<int>>(n,vector<int>(2,-1)));\n return rec(stones,0,stones.size()-1,1,dp,sum);\n }\n};", "memory": "365389" }
2,182
<p>A <strong>critical point</strong> in a linked list is defined as <strong>either</strong> a <strong>local maxima</strong> or a <strong>local minima</strong>.</p> <p>A node is a <strong>local maxima</strong> if the current node has a value <strong>strictly greater</strong> than the previous node and the next node.</p> <p>A node is a <strong>local minima</strong> if the current node has a value <strong>strictly smaller</strong> than the previous node and the next node.</p> <p>Note that a node can only be a local maxima/minima if there exists <strong>both</strong> a previous node and a next node.</p> <p>Given a linked list <code>head</code>, return <em>an array of length 2 containing </em><code>[minDistance, maxDistance]</code><em> where </em><code>minDistance</code><em> is the <strong>minimum distance</strong> between <strong>any&nbsp;two distinct</strong> critical points and </em><code>maxDistance</code><em> is the <strong>maximum distance</strong> between <strong>any&nbsp;two distinct</strong> critical points. If there are <strong>fewer</strong> than two critical points, return </em><code>[-1, -1]</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/10/13/a1.png" style="width: 148px; height: 55px;" /> <pre> <strong>Input:</strong> head = [3,1] <strong>Output:</strong> [-1,-1] <strong>Explanation:</strong> There are no critical points in [3,1]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/10/13/a2.png" style="width: 624px; height: 46px;" /> <pre> <strong>Input:</strong> head = [5,3,1,2,5,1,2] <strong>Output:</strong> [1,3] <strong>Explanation:</strong> There are three critical points: - [5,3,<strong><u>1</u></strong>,2,5,1,2]: The third node is a local minima because 1 is less than 3 and 2. - [5,3,1,2,<u><strong>5</strong></u>,1,2]: The fifth node is a local maxima because 5 is greater than 2 and 1. - [5,3,1,2,5,<u><strong>1</strong></u>,2]: The sixth node is a local minima because 1 is less than 5 and 2. The minimum distance is between the fifth and the sixth node. minDistance = 6 - 5 = 1. The maximum distance is between the third and the sixth node. maxDistance = 6 - 3 = 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/10/14/a5.png" style="width: 624px; height: 39px;" /> <pre> <strong>Input:</strong> head = [1,3,2,2,3,2,2,2,7] <strong>Output:</strong> [3,3] <strong>Explanation:</strong> There are two critical points: - [1,<u><strong>3</strong></u>,2,2,3,2,2,2,7]: The second node is a local maxima because 3 is greater than 1 and 2. - [1,3,2,2,<u><strong>3</strong></u>,2,2,2,7]: The fifth node is a local maxima because 3 is greater than 2 and 2. Both the minimum and maximum distances are between the second and the fifth node. Thus, minDistance and maxDistance is 5 - 2 = 3. Note that the last node is not considered a local maxima because it does not have a next node. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the list is in the range <code>[2, 10<sup>5</sup>]</code>.</li> <li><code>1 &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "static const bool Booster = [](){\n std::ios_base::sync_with_stdio(false);\n std::cout.tie(nullptr);\n std::cin.tie(nullptr);\n return true;\n}();\n\ninline bool is_digit(const char c) {\n return (c >= '0') && (c <= '9');\n}\n\ninline bool is_critical_point(const int n1, const int n2, const int n3) {\n return (n1 > n2) && (n2 < n3)\n || (n1 < n2) && (n2 > n3);\n}\n\nstatic bool IllegalFastSolve = [](){\n std::ofstream out(\"user.out\");\n std::string s;\n while (std::getline(std::cin, s)) {\n int n1 = -1;\n int n2 = -1;\n int n3 = -1;\n int first_crit_idx = -1;\n int prev_crit_idx = -1;\n int number = 0;\n int idx = 0;\n int min_dist = std::numeric_limits<int>::max();\n\n for (int i = 1; i < s.size(); ++i) {\n if (is_digit(s[i])) {\n number = 10 * number + (s[i] - '0');\n } else {\n n1 = n2;\n n2 = n3;\n n3 = number;\n\n if (n1 != -1) {\n if (is_critical_point(n1, n2, n3)) {\n if (first_crit_idx == -1) {\n first_crit_idx = idx;\n } else {\n min_dist = std::min(min_dist, idx - prev_crit_idx);\n }\n prev_crit_idx = idx;\n }\n }\n number = 0;\n ++idx;\n }\n }\n if (min_dist == std::numeric_limits<int>::max()) {\n out << \"[-1,-1]\\n\";\n } else {\n const int max_dist = prev_crit_idx - first_crit_idx;\n out << \"[\" << min_dist << \",\" << max_dist << \"]\\n\";\n }\n }\n out.flush();\n exit(0);\n return true;\n}();\n/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n vector<int> nodesBetweenCriticalPoints(ListNode* head) {\n ListNode* prev = head;\n ListNode* curr = head-> next;\n int currPos = 1;\n int prevCP = 0;\n int firststCp = 0;\n int miniDis = INT_MAX;\n while(curr->next != NULL){\n if((curr->val > prev->val && curr->val > curr->next->val) ||\n (curr->val < prev->val && curr->val < curr->next->val)){\n if(prevCP == 0){\n prevCP = currPos;\n firststCp = currPos;\n }\n else{\n miniDis = min(miniDis,currPos - prevCP);\n prevCP = currPos;\n }\n\n }\n currPos++;\n prev = curr;\n curr = curr->next;\n }\n if(miniDis == INT_MAX){\n return {-1,-1};\n }\n return {miniDis,prevCP - firststCp};\n }\n};", "memory": "9200" }
2,182
<p>A <strong>critical point</strong> in a linked list is defined as <strong>either</strong> a <strong>local maxima</strong> or a <strong>local minima</strong>.</p> <p>A node is a <strong>local maxima</strong> if the current node has a value <strong>strictly greater</strong> than the previous node and the next node.</p> <p>A node is a <strong>local minima</strong> if the current node has a value <strong>strictly smaller</strong> than the previous node and the next node.</p> <p>Note that a node can only be a local maxima/minima if there exists <strong>both</strong> a previous node and a next node.</p> <p>Given a linked list <code>head</code>, return <em>an array of length 2 containing </em><code>[minDistance, maxDistance]</code><em> where </em><code>minDistance</code><em> is the <strong>minimum distance</strong> between <strong>any&nbsp;two distinct</strong> critical points and </em><code>maxDistance</code><em> is the <strong>maximum distance</strong> between <strong>any&nbsp;two distinct</strong> critical points. If there are <strong>fewer</strong> than two critical points, return </em><code>[-1, -1]</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/10/13/a1.png" style="width: 148px; height: 55px;" /> <pre> <strong>Input:</strong> head = [3,1] <strong>Output:</strong> [-1,-1] <strong>Explanation:</strong> There are no critical points in [3,1]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/10/13/a2.png" style="width: 624px; height: 46px;" /> <pre> <strong>Input:</strong> head = [5,3,1,2,5,1,2] <strong>Output:</strong> [1,3] <strong>Explanation:</strong> There are three critical points: - [5,3,<strong><u>1</u></strong>,2,5,1,2]: The third node is a local minima because 1 is less than 3 and 2. - [5,3,1,2,<u><strong>5</strong></u>,1,2]: The fifth node is a local maxima because 5 is greater than 2 and 1. - [5,3,1,2,5,<u><strong>1</strong></u>,2]: The sixth node is a local minima because 1 is less than 5 and 2. The minimum distance is between the fifth and the sixth node. minDistance = 6 - 5 = 1. The maximum distance is between the third and the sixth node. maxDistance = 6 - 3 = 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/10/14/a5.png" style="width: 624px; height: 39px;" /> <pre> <strong>Input:</strong> head = [1,3,2,2,3,2,2,2,7] <strong>Output:</strong> [3,3] <strong>Explanation:</strong> There are two critical points: - [1,<u><strong>3</strong></u>,2,2,3,2,2,2,7]: The second node is a local maxima because 3 is greater than 1 and 2. - [1,3,2,2,<u><strong>3</strong></u>,2,2,2,7]: The fifth node is a local maxima because 3 is greater than 2 and 2. Both the minimum and maximum distances are between the second and the fifth node. Thus, minDistance and maxDistance is 5 - 2 = 3. Note that the last node is not considered a local maxima because it does not have a next node. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the list is in the range <code>[2, 10<sup>5</sup>]</code>.</li> <li><code>1 &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "// NOT MY SOLUTION\n\nstatic const bool Booster = [](){\n std::ios_base::sync_with_stdio(false);\n std::cout.tie(nullptr);\n std::cin.tie(nullptr);\n return true;\n}();\n\ninline bool is_digit(const char c) {\n return (c >= '0') && (c <= '9');\n}\n\ninline bool is_critical_point(const int n1, const int n2, const int n3) {\n return (n1 > n2) && (n2 < n3)\n || (n1 < n2) && (n2 > n3);\n}\n\nstatic bool IllegalFastSolve = [](){\n std::ofstream out(\"user.out\");\n std::string s;\n while (std::getline(std::cin, s)) {\n int n1 = -1;\n int n2 = -1;\n int n3 = -1;\n int first_crit_idx = -1;\n int prev_crit_idx = -1;\n int number = 0;\n int idx = 0;\n int min_dist = std::numeric_limits<int>::max();\n\n for (int i = 1; i < s.size(); ++i) {\n if (is_digit(s[i])) {\n number = 10 * number + (s[i] - '0');\n } else {\n n1 = n2;\n n2 = n3;\n n3 = number;\n\n if (n1 != -1) {\n if (is_critical_point(n1, n2, n3)) {\n if (first_crit_idx == -1) {\n first_crit_idx = idx;\n } else {\n min_dist = std::min(min_dist, idx - prev_crit_idx);\n }\n prev_crit_idx = idx;\n }\n }\n number = 0;\n ++idx;\n }\n }\n if (min_dist == std::numeric_limits<int>::max()) {\n out << \"[-1,-1]\\n\";\n } else {\n const int max_dist = prev_crit_idx - first_crit_idx;\n out << \"[\" << min_dist << \",\" << max_dist << \"]\\n\";\n }\n }\n out.flush();\n exit(0);\n return true;\n}();\n\n/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\n inline bool is_critical_point(const ListNode* prev, const ListNode* curr, const ListNode* next) {\n return ((prev->val < curr->val) && (curr->val > next->val)) // local maximum\n || ((prev->val > curr->val) && (curr->val < next->val)); // local minimum\n }\npublic:\n std::vector<int> nodesBetweenCriticalPoints(ListNode* head) {\n int min_dist = std::numeric_limits<int>::max();\n int first_crit_idx = -1;\n int prev_crit_idx = -1;\n int idx = 0;\n ListNode* prev = head;\n ListNode* curr = prev->next;\n ListNode* next = curr->next;\n while (next != nullptr) {\n if (is_critical_point(prev, curr, next)) {\n if (first_crit_idx == -1) {\n first_crit_idx = idx;\n } else {\n min_dist = std::min(min_dist, idx - prev_crit_idx);\n }\n prev_crit_idx = idx;\n }\n idx++;\n prev = curr;\n curr = next;\n next = next->next;\n }\n if (min_dist == std::numeric_limits<int>::max()) {\n return {-1, -1};\n }\n const int max_dist = prev_crit_idx - first_crit_idx;\n return {min_dist, max_dist};\n }\n};", "memory": "9300" }
2,182
<p>A <strong>critical point</strong> in a linked list is defined as <strong>either</strong> a <strong>local maxima</strong> or a <strong>local minima</strong>.</p> <p>A node is a <strong>local maxima</strong> if the current node has a value <strong>strictly greater</strong> than the previous node and the next node.</p> <p>A node is a <strong>local minima</strong> if the current node has a value <strong>strictly smaller</strong> than the previous node and the next node.</p> <p>Note that a node can only be a local maxima/minima if there exists <strong>both</strong> a previous node and a next node.</p> <p>Given a linked list <code>head</code>, return <em>an array of length 2 containing </em><code>[minDistance, maxDistance]</code><em> where </em><code>minDistance</code><em> is the <strong>minimum distance</strong> between <strong>any&nbsp;two distinct</strong> critical points and </em><code>maxDistance</code><em> is the <strong>maximum distance</strong> between <strong>any&nbsp;two distinct</strong> critical points. If there are <strong>fewer</strong> than two critical points, return </em><code>[-1, -1]</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/10/13/a1.png" style="width: 148px; height: 55px;" /> <pre> <strong>Input:</strong> head = [3,1] <strong>Output:</strong> [-1,-1] <strong>Explanation:</strong> There are no critical points in [3,1]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/10/13/a2.png" style="width: 624px; height: 46px;" /> <pre> <strong>Input:</strong> head = [5,3,1,2,5,1,2] <strong>Output:</strong> [1,3] <strong>Explanation:</strong> There are three critical points: - [5,3,<strong><u>1</u></strong>,2,5,1,2]: The third node is a local minima because 1 is less than 3 and 2. - [5,3,1,2,<u><strong>5</strong></u>,1,2]: The fifth node is a local maxima because 5 is greater than 2 and 1. - [5,3,1,2,5,<u><strong>1</strong></u>,2]: The sixth node is a local minima because 1 is less than 5 and 2. The minimum distance is between the fifth and the sixth node. minDistance = 6 - 5 = 1. The maximum distance is between the third and the sixth node. maxDistance = 6 - 3 = 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/10/14/a5.png" style="width: 624px; height: 39px;" /> <pre> <strong>Input:</strong> head = [1,3,2,2,3,2,2,2,7] <strong>Output:</strong> [3,3] <strong>Explanation:</strong> There are two critical points: - [1,<u><strong>3</strong></u>,2,2,3,2,2,2,7]: The second node is a local maxima because 3 is greater than 1 and 2. - [1,3,2,2,<u><strong>3</strong></u>,2,2,2,7]: The fifth node is a local maxima because 3 is greater than 2 and 2. Both the minimum and maximum distances are between the second and the fifth node. Thus, minDistance and maxDistance is 5 - 2 = 3. Note that the last node is not considered a local maxima because it does not have a next node. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the list is in the range <code>[2, 10<sup>5</sup>]</code>.</li> <li><code>1 &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "auto init = []()\n{ \n ios::sync_with_stdio(0);\n cin.tie(0);\n cout.tie(0);\n return 'c';\n}();\n/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n vector<int> nodesBetweenCriticalPoints(ListNode* head) {\n vector<int> ans(2, -1);\n\n if (head == nullptr || head->next == nullptr) {\n return ans;\n }\n\n int prevprev = head->val;\n int prev = head->next->val;\n int c = 0;\n int first = -1;\n\n head = head->next->next;\n while (head != nullptr && first == -1) {\n if ((prev > prevprev && prev > head->val) ||\n (prev < prevprev && prev < head->val)) {\n first = c;\n }\n\n prevprev = prev;\n prev = head->val;\n c++;\n head = head->next;\n }\n\n int prevCrit = first;\n int last = -1;\n\n while (head != nullptr) {\n if ((prev > prevprev && prev > head->val) ||\n (prev < prevprev && prev < head->val)) {\n last = c;\n\n if (ans[0] == -1 || ans[0] > last-prevCrit) {\n ans[0] = last-prevCrit;\n }\n prevCrit = last;\n }\n\n prevprev = prev;\n prev = head->val;\n c++;\n head = head->next;\n }\n\n if (last != -1) {\n ans[1] = last-first;\n }\n\n return ans;\n }\n};", "memory": "115700" }
2,182
<p>A <strong>critical point</strong> in a linked list is defined as <strong>either</strong> a <strong>local maxima</strong> or a <strong>local minima</strong>.</p> <p>A node is a <strong>local maxima</strong> if the current node has a value <strong>strictly greater</strong> than the previous node and the next node.</p> <p>A node is a <strong>local minima</strong> if the current node has a value <strong>strictly smaller</strong> than the previous node and the next node.</p> <p>Note that a node can only be a local maxima/minima if there exists <strong>both</strong> a previous node and a next node.</p> <p>Given a linked list <code>head</code>, return <em>an array of length 2 containing </em><code>[minDistance, maxDistance]</code><em> where </em><code>minDistance</code><em> is the <strong>minimum distance</strong> between <strong>any&nbsp;two distinct</strong> critical points and </em><code>maxDistance</code><em> is the <strong>maximum distance</strong> between <strong>any&nbsp;two distinct</strong> critical points. If there are <strong>fewer</strong> than two critical points, return </em><code>[-1, -1]</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/10/13/a1.png" style="width: 148px; height: 55px;" /> <pre> <strong>Input:</strong> head = [3,1] <strong>Output:</strong> [-1,-1] <strong>Explanation:</strong> There are no critical points in [3,1]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/10/13/a2.png" style="width: 624px; height: 46px;" /> <pre> <strong>Input:</strong> head = [5,3,1,2,5,1,2] <strong>Output:</strong> [1,3] <strong>Explanation:</strong> There are three critical points: - [5,3,<strong><u>1</u></strong>,2,5,1,2]: The third node is a local minima because 1 is less than 3 and 2. - [5,3,1,2,<u><strong>5</strong></u>,1,2]: The fifth node is a local maxima because 5 is greater than 2 and 1. - [5,3,1,2,5,<u><strong>1</strong></u>,2]: The sixth node is a local minima because 1 is less than 5 and 2. The minimum distance is between the fifth and the sixth node. minDistance = 6 - 5 = 1. The maximum distance is between the third and the sixth node. maxDistance = 6 - 3 = 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/10/14/a5.png" style="width: 624px; height: 39px;" /> <pre> <strong>Input:</strong> head = [1,3,2,2,3,2,2,2,7] <strong>Output:</strong> [3,3] <strong>Explanation:</strong> There are two critical points: - [1,<u><strong>3</strong></u>,2,2,3,2,2,2,7]: The second node is a local maxima because 3 is greater than 1 and 2. - [1,3,2,2,<u><strong>3</strong></u>,2,2,2,7]: The fifth node is a local maxima because 3 is greater than 2 and 2. Both the minimum and maximum distances are between the second and the fifth node. Thus, minDistance and maxDistance is 5 - 2 = 3. Note that the last node is not considered a local maxima because it does not have a next node. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the list is in the range <code>[2, 10<sup>5</sup>]</code>.</li> <li><code>1 &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "\nclass Solution {\npublic:\n vector<int> nodesBetweenCriticalPoints(ListNode* head) {\n int idx=1;\n int fidx=-1;\n int sidx=-1;\n ListNode* a=head;\n ListNode* b=head->next; \n ListNode* c=head->next->next;\n if(c==NULL) return {-1,-1};\n int mind=INT_MAX;\n int f=-1;\n int s =-1;\n while(c){\n if(b->val > a->val && b->val > c->val || b->val < a->val && b->val < c->val){\n // for maximum distance\n if(fidx==-1) fidx=idx;\n else sidx=idx;\n f=s;\n s=idx;\n if(f!=-1){\n int d=s-f;\n mind=min(mind,d);\n }\n }\n a=a->next;\n b=b->next;\n c=c->next;\n idx++;\n }\n if(sidx==-1) return {-1,-1};\n int maxd=sidx-fidx;\n return {mind,maxd};\n }\n};", "memory": "115800" }
2,182
<p>A <strong>critical point</strong> in a linked list is defined as <strong>either</strong> a <strong>local maxima</strong> or a <strong>local minima</strong>.</p> <p>A node is a <strong>local maxima</strong> if the current node has a value <strong>strictly greater</strong> than the previous node and the next node.</p> <p>A node is a <strong>local minima</strong> if the current node has a value <strong>strictly smaller</strong> than the previous node and the next node.</p> <p>Note that a node can only be a local maxima/minima if there exists <strong>both</strong> a previous node and a next node.</p> <p>Given a linked list <code>head</code>, return <em>an array of length 2 containing </em><code>[minDistance, maxDistance]</code><em> where </em><code>minDistance</code><em> is the <strong>minimum distance</strong> between <strong>any&nbsp;two distinct</strong> critical points and </em><code>maxDistance</code><em> is the <strong>maximum distance</strong> between <strong>any&nbsp;two distinct</strong> critical points. If there are <strong>fewer</strong> than two critical points, return </em><code>[-1, -1]</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/10/13/a1.png" style="width: 148px; height: 55px;" /> <pre> <strong>Input:</strong> head = [3,1] <strong>Output:</strong> [-1,-1] <strong>Explanation:</strong> There are no critical points in [3,1]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/10/13/a2.png" style="width: 624px; height: 46px;" /> <pre> <strong>Input:</strong> head = [5,3,1,2,5,1,2] <strong>Output:</strong> [1,3] <strong>Explanation:</strong> There are three critical points: - [5,3,<strong><u>1</u></strong>,2,5,1,2]: The third node is a local minima because 1 is less than 3 and 2. - [5,3,1,2,<u><strong>5</strong></u>,1,2]: The fifth node is a local maxima because 5 is greater than 2 and 1. - [5,3,1,2,5,<u><strong>1</strong></u>,2]: The sixth node is a local minima because 1 is less than 5 and 2. The minimum distance is between the fifth and the sixth node. minDistance = 6 - 5 = 1. The maximum distance is between the third and the sixth node. maxDistance = 6 - 3 = 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/10/14/a5.png" style="width: 624px; height: 39px;" /> <pre> <strong>Input:</strong> head = [1,3,2,2,3,2,2,2,7] <strong>Output:</strong> [3,3] <strong>Explanation:</strong> There are two critical points: - [1,<u><strong>3</strong></u>,2,2,3,2,2,2,7]: The second node is a local maxima because 3 is greater than 1 and 2. - [1,3,2,2,<u><strong>3</strong></u>,2,2,2,7]: The fifth node is a local maxima because 3 is greater than 2 and 2. Both the minimum and maximum distances are between the second and the fifth node. Thus, minDistance and maxDistance is 5 - 2 = 3. Note that the last node is not considered a local maxima because it does not have a next node. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the list is in the range <code>[2, 10<sup>5</sup>]</code>.</li> <li><code>1 &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n vector<int> nodesBetweenCriticalPoints(ListNode* head) {\n ListNode* a = head;\n ListNode* b = head->next;\n ListNode* c = head->next->next;\n if(c == NULL) return {-1,-1};\n int idx = 1;\n int fidx = -1;\n int sidx = -1;\n while(c) {\n if((b->val > a->val && b->val > c->val) || (b->val < a->val && b->val < c->val)) {\n if(fidx == -1) fidx = idx;\n else sidx = idx;\n }\n a = a->next;\n b = b->next;\n c = c->next;\n idx++;\n }\n if(fidx == -1 || sidx == -1) return{-1,-1};\n int maxd = sidx - fidx;\n\n idx = 1;\n fidx = -1;\n sidx = -1;\n a = head;\n b = head->next;\n c = head->next->next;\n int mind = INT_MAX;\n while(c) {\n if((b->val > a->val && b->val > c->val) || (b->val < a->val && b->val < c->val)) {\n fidx = sidx;\n sidx = idx;\n if(fidx != -1) {\n int d = sidx-fidx;\n mind = min(mind, d);\n }\n }\n \n a = a->next;\n b = b->next;\n c = c->next;\n idx++;\n }\n return {mind,maxd};\n }\n};", "memory": "115900" }
2,182
<p>A <strong>critical point</strong> in a linked list is defined as <strong>either</strong> a <strong>local maxima</strong> or a <strong>local minima</strong>.</p> <p>A node is a <strong>local maxima</strong> if the current node has a value <strong>strictly greater</strong> than the previous node and the next node.</p> <p>A node is a <strong>local minima</strong> if the current node has a value <strong>strictly smaller</strong> than the previous node and the next node.</p> <p>Note that a node can only be a local maxima/minima if there exists <strong>both</strong> a previous node and a next node.</p> <p>Given a linked list <code>head</code>, return <em>an array of length 2 containing </em><code>[minDistance, maxDistance]</code><em> where </em><code>minDistance</code><em> is the <strong>minimum distance</strong> between <strong>any&nbsp;two distinct</strong> critical points and </em><code>maxDistance</code><em> is the <strong>maximum distance</strong> between <strong>any&nbsp;two distinct</strong> critical points. If there are <strong>fewer</strong> than two critical points, return </em><code>[-1, -1]</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/10/13/a1.png" style="width: 148px; height: 55px;" /> <pre> <strong>Input:</strong> head = [3,1] <strong>Output:</strong> [-1,-1] <strong>Explanation:</strong> There are no critical points in [3,1]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/10/13/a2.png" style="width: 624px; height: 46px;" /> <pre> <strong>Input:</strong> head = [5,3,1,2,5,1,2] <strong>Output:</strong> [1,3] <strong>Explanation:</strong> There are three critical points: - [5,3,<strong><u>1</u></strong>,2,5,1,2]: The third node is a local minima because 1 is less than 3 and 2. - [5,3,1,2,<u><strong>5</strong></u>,1,2]: The fifth node is a local maxima because 5 is greater than 2 and 1. - [5,3,1,2,5,<u><strong>1</strong></u>,2]: The sixth node is a local minima because 1 is less than 5 and 2. The minimum distance is between the fifth and the sixth node. minDistance = 6 - 5 = 1. The maximum distance is between the third and the sixth node. maxDistance = 6 - 3 = 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/10/14/a5.png" style="width: 624px; height: 39px;" /> <pre> <strong>Input:</strong> head = [1,3,2,2,3,2,2,2,7] <strong>Output:</strong> [3,3] <strong>Explanation:</strong> There are two critical points: - [1,<u><strong>3</strong></u>,2,2,3,2,2,2,7]: The second node is a local maxima because 3 is greater than 1 and 2. - [1,3,2,2,<u><strong>3</strong></u>,2,2,2,7]: The fifth node is a local maxima because 3 is greater than 2 and 2. Both the minimum and maximum distances are between the second and the fifth node. Thus, minDistance and maxDistance is 5 - 2 = 3. Note that the last node is not considered a local maxima because it does not have a next node. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the list is in the range <code>[2, 10<sup>5</sup>]</code>.</li> <li><code>1 &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n vector<int> nodesBetweenCriticalPoints(ListNode* head) {\n vector<int> ans;\n int max=-1;\n int min=INT_MAX;\n if(head==NULL || head->next==NULL || head->next->next==NULL)\n {\n min=-1;\n ans.push_back(min);\n ans.push_back(max);\n return ans; \n }\n int i=1;\n int prev_point=-1;\n int st_point=-1;\n\n ListNode* prev=head;\n ListNode* curr=head->next;\n ListNode* nxt=head->next->next;\n while(nxt!=nullptr)\n {\n if((curr->val < prev->val && curr->val < nxt->val) || ( curr->val > prev->val && curr->val > nxt->val ))\n {\n if(prev_point!=-1)\n {\n min=i-prev_point < min ? i-prev_point : min ;\n prev_point=i;\n }\n else\n {\n prev_point=i;\n st_point=i;\n }\n }\n prev=curr;\n curr=nxt;\n nxt=nxt->next;\n i++;\n }\n if(st_point != -1 && st_point != prev_point)\n {\n max=prev_point-st_point;\n }\n if(min==INT_MAX) min = -1;\n ans.push_back(min);\n ans.push_back(max);\n return ans;\n }\n};", "memory": "116000" }
2,182
<p>A <strong>critical point</strong> in a linked list is defined as <strong>either</strong> a <strong>local maxima</strong> or a <strong>local minima</strong>.</p> <p>A node is a <strong>local maxima</strong> if the current node has a value <strong>strictly greater</strong> than the previous node and the next node.</p> <p>A node is a <strong>local minima</strong> if the current node has a value <strong>strictly smaller</strong> than the previous node and the next node.</p> <p>Note that a node can only be a local maxima/minima if there exists <strong>both</strong> a previous node and a next node.</p> <p>Given a linked list <code>head</code>, return <em>an array of length 2 containing </em><code>[minDistance, maxDistance]</code><em> where </em><code>minDistance</code><em> is the <strong>minimum distance</strong> between <strong>any&nbsp;two distinct</strong> critical points and </em><code>maxDistance</code><em> is the <strong>maximum distance</strong> between <strong>any&nbsp;two distinct</strong> critical points. If there are <strong>fewer</strong> than two critical points, return </em><code>[-1, -1]</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/10/13/a1.png" style="width: 148px; height: 55px;" /> <pre> <strong>Input:</strong> head = [3,1] <strong>Output:</strong> [-1,-1] <strong>Explanation:</strong> There are no critical points in [3,1]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/10/13/a2.png" style="width: 624px; height: 46px;" /> <pre> <strong>Input:</strong> head = [5,3,1,2,5,1,2] <strong>Output:</strong> [1,3] <strong>Explanation:</strong> There are three critical points: - [5,3,<strong><u>1</u></strong>,2,5,1,2]: The third node is a local minima because 1 is less than 3 and 2. - [5,3,1,2,<u><strong>5</strong></u>,1,2]: The fifth node is a local maxima because 5 is greater than 2 and 1. - [5,3,1,2,5,<u><strong>1</strong></u>,2]: The sixth node is a local minima because 1 is less than 5 and 2. The minimum distance is between the fifth and the sixth node. minDistance = 6 - 5 = 1. The maximum distance is between the third and the sixth node. maxDistance = 6 - 3 = 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/10/14/a5.png" style="width: 624px; height: 39px;" /> <pre> <strong>Input:</strong> head = [1,3,2,2,3,2,2,2,7] <strong>Output:</strong> [3,3] <strong>Explanation:</strong> There are two critical points: - [1,<u><strong>3</strong></u>,2,2,3,2,2,2,7]: The second node is a local maxima because 3 is greater than 1 and 2. - [1,3,2,2,<u><strong>3</strong></u>,2,2,2,7]: The fifth node is a local maxima because 3 is greater than 2 and 2. Both the minimum and maximum distances are between the second and the fifth node. Thus, minDistance and maxDistance is 5 - 2 = 3. Note that the last node is not considered a local maxima because it does not have a next node. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the list is in the range <code>[2, 10<sup>5</sup>]</code>.</li> <li><code>1 &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n vector<int> nodesBetweenCriticalPoints(ListNode* head) {\n int first = INT_MAX, last = 0, prev_val = head->val, min_d = INT_MAX;\n\n for(int i = 0; head->next != nullptr; ++i) {\n if ((max(prev_val, head->next->val) < head->val) || (min(prev_val, head->next->val) > head->val)) {\n if (last != 0) min_d = min(min_d, i - last);\n first = min(first, i);\n last = i;\n }\n prev_val = head->val;\n head = head->next;\n }\n\n return (min_d == INT_MAX) ? vector<int>{-1, -1} : vector<int>{min_d, last - first};\n }\n};", "memory": "116100" }
2,182
<p>A <strong>critical point</strong> in a linked list is defined as <strong>either</strong> a <strong>local maxima</strong> or a <strong>local minima</strong>.</p> <p>A node is a <strong>local maxima</strong> if the current node has a value <strong>strictly greater</strong> than the previous node and the next node.</p> <p>A node is a <strong>local minima</strong> if the current node has a value <strong>strictly smaller</strong> than the previous node and the next node.</p> <p>Note that a node can only be a local maxima/minima if there exists <strong>both</strong> a previous node and a next node.</p> <p>Given a linked list <code>head</code>, return <em>an array of length 2 containing </em><code>[minDistance, maxDistance]</code><em> where </em><code>minDistance</code><em> is the <strong>minimum distance</strong> between <strong>any&nbsp;two distinct</strong> critical points and </em><code>maxDistance</code><em> is the <strong>maximum distance</strong> between <strong>any&nbsp;two distinct</strong> critical points. If there are <strong>fewer</strong> than two critical points, return </em><code>[-1, -1]</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/10/13/a1.png" style="width: 148px; height: 55px;" /> <pre> <strong>Input:</strong> head = [3,1] <strong>Output:</strong> [-1,-1] <strong>Explanation:</strong> There are no critical points in [3,1]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/10/13/a2.png" style="width: 624px; height: 46px;" /> <pre> <strong>Input:</strong> head = [5,3,1,2,5,1,2] <strong>Output:</strong> [1,3] <strong>Explanation:</strong> There are three critical points: - [5,3,<strong><u>1</u></strong>,2,5,1,2]: The third node is a local minima because 1 is less than 3 and 2. - [5,3,1,2,<u><strong>5</strong></u>,1,2]: The fifth node is a local maxima because 5 is greater than 2 and 1. - [5,3,1,2,5,<u><strong>1</strong></u>,2]: The sixth node is a local minima because 1 is less than 5 and 2. The minimum distance is between the fifth and the sixth node. minDistance = 6 - 5 = 1. The maximum distance is between the third and the sixth node. maxDistance = 6 - 3 = 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/10/14/a5.png" style="width: 624px; height: 39px;" /> <pre> <strong>Input:</strong> head = [1,3,2,2,3,2,2,2,7] <strong>Output:</strong> [3,3] <strong>Explanation:</strong> There are two critical points: - [1,<u><strong>3</strong></u>,2,2,3,2,2,2,7]: The second node is a local maxima because 3 is greater than 1 and 2. - [1,3,2,2,<u><strong>3</strong></u>,2,2,2,7]: The fifth node is a local maxima because 3 is greater than 2 and 2. Both the minimum and maximum distances are between the second and the fifth node. Thus, minDistance and maxDistance is 5 - 2 = 3. Note that the last node is not considered a local maxima because it does not have a next node. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the list is in the range <code>[2, 10<sup>5</sup>]</code>.</li> <li><code>1 &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n vector<int> nodesBetweenCriticalPoints(ListNode* head) {\n ListNode* a = head;\n ListNode* b = head->next;\n ListNode* c = head->next->next;\n if(c == NULL) return {-1,-1};\n int idx = 1;\n int fidx = -1;\n int sidx = -1;\n int f = -1;\n int s = -1;\n int mind = INT_MAX;\n\n while(c) {\n if((b->val > a->val && b->val > c->val) || (b->val < a->val && b->val < c->val)) {\n //maximum distance\n if(fidx == -1) fidx = idx;\n else sidx = idx;\n\n //minimum distance\n f = s;\n s = idx;\n if(f != -1) {\n int d = s-f;\n mind = min(mind, d);\n }\n }\n a = a->next;\n b = b->next;\n c = c->next;\n idx++;\n }\n if(fidx == -1 || sidx == -1) return{-1,-1};\n int maxd = sidx - fidx;\n return {mind,maxd};\n }\n};", "memory": "116100" }
2,182
<p>A <strong>critical point</strong> in a linked list is defined as <strong>either</strong> a <strong>local maxima</strong> or a <strong>local minima</strong>.</p> <p>A node is a <strong>local maxima</strong> if the current node has a value <strong>strictly greater</strong> than the previous node and the next node.</p> <p>A node is a <strong>local minima</strong> if the current node has a value <strong>strictly smaller</strong> than the previous node and the next node.</p> <p>Note that a node can only be a local maxima/minima if there exists <strong>both</strong> a previous node and a next node.</p> <p>Given a linked list <code>head</code>, return <em>an array of length 2 containing </em><code>[minDistance, maxDistance]</code><em> where </em><code>minDistance</code><em> is the <strong>minimum distance</strong> between <strong>any&nbsp;two distinct</strong> critical points and </em><code>maxDistance</code><em> is the <strong>maximum distance</strong> between <strong>any&nbsp;two distinct</strong> critical points. If there are <strong>fewer</strong> than two critical points, return </em><code>[-1, -1]</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/10/13/a1.png" style="width: 148px; height: 55px;" /> <pre> <strong>Input:</strong> head = [3,1] <strong>Output:</strong> [-1,-1] <strong>Explanation:</strong> There are no critical points in [3,1]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/10/13/a2.png" style="width: 624px; height: 46px;" /> <pre> <strong>Input:</strong> head = [5,3,1,2,5,1,2] <strong>Output:</strong> [1,3] <strong>Explanation:</strong> There are three critical points: - [5,3,<strong><u>1</u></strong>,2,5,1,2]: The third node is a local minima because 1 is less than 3 and 2. - [5,3,1,2,<u><strong>5</strong></u>,1,2]: The fifth node is a local maxima because 5 is greater than 2 and 1. - [5,3,1,2,5,<u><strong>1</strong></u>,2]: The sixth node is a local minima because 1 is less than 5 and 2. The minimum distance is between the fifth and the sixth node. minDistance = 6 - 5 = 1. The maximum distance is between the third and the sixth node. maxDistance = 6 - 3 = 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/10/14/a5.png" style="width: 624px; height: 39px;" /> <pre> <strong>Input:</strong> head = [1,3,2,2,3,2,2,2,7] <strong>Output:</strong> [3,3] <strong>Explanation:</strong> There are two critical points: - [1,<u><strong>3</strong></u>,2,2,3,2,2,2,7]: The second node is a local maxima because 3 is greater than 1 and 2. - [1,3,2,2,<u><strong>3</strong></u>,2,2,2,7]: The fifth node is a local maxima because 3 is greater than 2 and 2. Both the minimum and maximum distances are between the second and the fifth node. Thus, minDistance and maxDistance is 5 - 2 = 3. Note that the last node is not considered a local maxima because it does not have a next node. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the list is in the range <code>[2, 10<sup>5</sup>]</code>.</li> <li><code>1 &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul>
1
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n vector<int> nodesBetweenCriticalPoints(ListNode* head) {\n vector<int> ans;\n int max=-1;\n int min=INT_MAX;\n if(head==NULL || head->next==NULL || head->next->next==NULL)\n {\n min=-1;\n ans.push_back(min);\n ans.push_back(max);\n return ans; \n }\n int i=1;\n int prev_point=-1;\n int st_point=-1;\n\n ListNode* prev=head;\n ListNode* curr=head->next;\n ListNode* nxt=head->next->next;\n while(nxt!=nullptr)\n {\n if((curr->val < prev->val && curr->val < nxt->val) || ( curr->val > prev->val && curr->val > nxt->val ))\n {\n if(prev_point!=-1)\n {\n min=i-prev_point < min ? i-prev_point : min ;\n prev_point=i;\n }\n else\n {\n prev_point=i;\n st_point=i;\n }\n }\n /*\n else if(curr->val > prev->val && curr->val >nxt->val)\n {\n if(prev_point!=-1)\n {\n min=i-prev_point < min ? i-prev_point : min ;\n }\n else\n {\n ;\n }\n }\n */\n prev=curr;\n curr=nxt;\n nxt=nxt->next;\n i++;\n }\n if(st_point != -1 && st_point != prev_point)\n {\n max=prev_point-st_point;\n }\n if(min==INT_MAX) min = -1;\n ans.push_back(min);\n ans.push_back(max);\n return ans;\n }\n};", "memory": "116200" }
2,182
<p>A <strong>critical point</strong> in a linked list is defined as <strong>either</strong> a <strong>local maxima</strong> or a <strong>local minima</strong>.</p> <p>A node is a <strong>local maxima</strong> if the current node has a value <strong>strictly greater</strong> than the previous node and the next node.</p> <p>A node is a <strong>local minima</strong> if the current node has a value <strong>strictly smaller</strong> than the previous node and the next node.</p> <p>Note that a node can only be a local maxima/minima if there exists <strong>both</strong> a previous node and a next node.</p> <p>Given a linked list <code>head</code>, return <em>an array of length 2 containing </em><code>[minDistance, maxDistance]</code><em> where </em><code>minDistance</code><em> is the <strong>minimum distance</strong> between <strong>any&nbsp;two distinct</strong> critical points and </em><code>maxDistance</code><em> is the <strong>maximum distance</strong> between <strong>any&nbsp;two distinct</strong> critical points. If there are <strong>fewer</strong> than two critical points, return </em><code>[-1, -1]</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/10/13/a1.png" style="width: 148px; height: 55px;" /> <pre> <strong>Input:</strong> head = [3,1] <strong>Output:</strong> [-1,-1] <strong>Explanation:</strong> There are no critical points in [3,1]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/10/13/a2.png" style="width: 624px; height: 46px;" /> <pre> <strong>Input:</strong> head = [5,3,1,2,5,1,2] <strong>Output:</strong> [1,3] <strong>Explanation:</strong> There are three critical points: - [5,3,<strong><u>1</u></strong>,2,5,1,2]: The third node is a local minima because 1 is less than 3 and 2. - [5,3,1,2,<u><strong>5</strong></u>,1,2]: The fifth node is a local maxima because 5 is greater than 2 and 1. - [5,3,1,2,5,<u><strong>1</strong></u>,2]: The sixth node is a local minima because 1 is less than 5 and 2. The minimum distance is between the fifth and the sixth node. minDistance = 6 - 5 = 1. The maximum distance is between the third and the sixth node. maxDistance = 6 - 3 = 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/10/14/a5.png" style="width: 624px; height: 39px;" /> <pre> <strong>Input:</strong> head = [1,3,2,2,3,2,2,2,7] <strong>Output:</strong> [3,3] <strong>Explanation:</strong> There are two critical points: - [1,<u><strong>3</strong></u>,2,2,3,2,2,2,7]: The second node is a local maxima because 3 is greater than 1 and 2. - [1,3,2,2,<u><strong>3</strong></u>,2,2,2,7]: The fifth node is a local maxima because 3 is greater than 2 and 2. Both the minimum and maximum distances are between the second and the fifth node. Thus, minDistance and maxDistance is 5 - 2 = 3. Note that the last node is not considered a local maxima because it does not have a next node. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the list is in the range <code>[2, 10<sup>5</sup>]</code>.</li> <li><code>1 &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n vector<int> nodesBetweenCriticalPoints(ListNode* head) {\n ListNode* a = head;\n ListNode* b = head->next;\n ListNode* c = head->next->next;\n if(c == NULL) return {-1,-1};\n int idx = 1;\n int firstIdx = -1;\n int lastIdx = -1;\n int f = -1;\n int s = -1;\n int mind = INT_MAX;\n\n while(c) {\n if((b->val > a->val && b->val > c->val) || (b->val < a->val && b->val < c->val)) {\n //maximum distance\n if(firstIdx == -1) firstIdx = idx;\n else lastIdx = idx;\n\n //minimum distance\n f = s;\n s = idx;\n if(f != -1) {\n int d = s-f;\n mind = min(mind, d);\n }\n }\n a = a->next;\n b = b->next;\n c = c->next;\n idx++;\n }\n if(firstIdx == -1 || lastIdx == -1) return{-1,-1};\n int maxd = lastIdx - firstIdx;\n return {mind,maxd};\n }\n};", "memory": "116200" }
2,182
<p>A <strong>critical point</strong> in a linked list is defined as <strong>either</strong> a <strong>local maxima</strong> or a <strong>local minima</strong>.</p> <p>A node is a <strong>local maxima</strong> if the current node has a value <strong>strictly greater</strong> than the previous node and the next node.</p> <p>A node is a <strong>local minima</strong> if the current node has a value <strong>strictly smaller</strong> than the previous node and the next node.</p> <p>Note that a node can only be a local maxima/minima if there exists <strong>both</strong> a previous node and a next node.</p> <p>Given a linked list <code>head</code>, return <em>an array of length 2 containing </em><code>[minDistance, maxDistance]</code><em> where </em><code>minDistance</code><em> is the <strong>minimum distance</strong> between <strong>any&nbsp;two distinct</strong> critical points and </em><code>maxDistance</code><em> is the <strong>maximum distance</strong> between <strong>any&nbsp;two distinct</strong> critical points. If there are <strong>fewer</strong> than two critical points, return </em><code>[-1, -1]</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/10/13/a1.png" style="width: 148px; height: 55px;" /> <pre> <strong>Input:</strong> head = [3,1] <strong>Output:</strong> [-1,-1] <strong>Explanation:</strong> There are no critical points in [3,1]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/10/13/a2.png" style="width: 624px; height: 46px;" /> <pre> <strong>Input:</strong> head = [5,3,1,2,5,1,2] <strong>Output:</strong> [1,3] <strong>Explanation:</strong> There are three critical points: - [5,3,<strong><u>1</u></strong>,2,5,1,2]: The third node is a local minima because 1 is less than 3 and 2. - [5,3,1,2,<u><strong>5</strong></u>,1,2]: The fifth node is a local maxima because 5 is greater than 2 and 1. - [5,3,1,2,5,<u><strong>1</strong></u>,2]: The sixth node is a local minima because 1 is less than 5 and 2. The minimum distance is between the fifth and the sixth node. minDistance = 6 - 5 = 1. The maximum distance is between the third and the sixth node. maxDistance = 6 - 3 = 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/10/14/a5.png" style="width: 624px; height: 39px;" /> <pre> <strong>Input:</strong> head = [1,3,2,2,3,2,2,2,7] <strong>Output:</strong> [3,3] <strong>Explanation:</strong> There are two critical points: - [1,<u><strong>3</strong></u>,2,2,3,2,2,2,7]: The second node is a local maxima because 3 is greater than 1 and 2. - [1,3,2,2,<u><strong>3</strong></u>,2,2,2,7]: The fifth node is a local maxima because 3 is greater than 2 and 2. Both the minimum and maximum distances are between the second and the fifth node. Thus, minDistance and maxDistance is 5 - 2 = 3. Note that the last node is not considered a local maxima because it does not have a next node. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the list is in the range <code>[2, 10<sup>5</sup>]</code>.</li> <li><code>1 &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n vector<int> nodesBetweenCriticalPoints(ListNode* head) {\n vector<bool> v;\n v.push_back(false);\n int prev = head->val;\n head = head->next;\n int curr=-1;\n while(head->next!=NULL){\n curr = head->val;\n int suc = head->next->val;\n if(prev<curr && curr>suc)v.push_back(true);\n else if(curr<prev && curr<suc)v.push_back(true);\n else v.push_back(false);\n prev = curr;\n head= head->next;\n }\n v.push_back(false);\n int maxi = -1; int mini = 1e5+1;\n int pos=-1;\n int mxpos = -1;\n for(int i=0;i<v.size();i++){\n if(v[i]==1){\n if(pos==-1){\n pos = i;\n mxpos = i;\n }\n else{\n mini = min(mini, i - pos);\n pos = i;\n }\n }\n }\n if(mini == 1e5+1)mini=-1;\n for(int j=v.size()-1;j>=0;j--){\n if(v[j]==1 && j!=mxpos){\n maxi = j - mxpos;\n break;\n }\n }\n return {mini,maxi};\n }\n};", "memory": "116600" }
2,182
<p>A <strong>critical point</strong> in a linked list is defined as <strong>either</strong> a <strong>local maxima</strong> or a <strong>local minima</strong>.</p> <p>A node is a <strong>local maxima</strong> if the current node has a value <strong>strictly greater</strong> than the previous node and the next node.</p> <p>A node is a <strong>local minima</strong> if the current node has a value <strong>strictly smaller</strong> than the previous node and the next node.</p> <p>Note that a node can only be a local maxima/minima if there exists <strong>both</strong> a previous node and a next node.</p> <p>Given a linked list <code>head</code>, return <em>an array of length 2 containing </em><code>[minDistance, maxDistance]</code><em> where </em><code>minDistance</code><em> is the <strong>minimum distance</strong> between <strong>any&nbsp;two distinct</strong> critical points and </em><code>maxDistance</code><em> is the <strong>maximum distance</strong> between <strong>any&nbsp;two distinct</strong> critical points. If there are <strong>fewer</strong> than two critical points, return </em><code>[-1, -1]</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/10/13/a1.png" style="width: 148px; height: 55px;" /> <pre> <strong>Input:</strong> head = [3,1] <strong>Output:</strong> [-1,-1] <strong>Explanation:</strong> There are no critical points in [3,1]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/10/13/a2.png" style="width: 624px; height: 46px;" /> <pre> <strong>Input:</strong> head = [5,3,1,2,5,1,2] <strong>Output:</strong> [1,3] <strong>Explanation:</strong> There are three critical points: - [5,3,<strong><u>1</u></strong>,2,5,1,2]: The third node is a local minima because 1 is less than 3 and 2. - [5,3,1,2,<u><strong>5</strong></u>,1,2]: The fifth node is a local maxima because 5 is greater than 2 and 1. - [5,3,1,2,5,<u><strong>1</strong></u>,2]: The sixth node is a local minima because 1 is less than 5 and 2. The minimum distance is between the fifth and the sixth node. minDistance = 6 - 5 = 1. The maximum distance is between the third and the sixth node. maxDistance = 6 - 3 = 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/10/14/a5.png" style="width: 624px; height: 39px;" /> <pre> <strong>Input:</strong> head = [1,3,2,2,3,2,2,2,7] <strong>Output:</strong> [3,3] <strong>Explanation:</strong> There are two critical points: - [1,<u><strong>3</strong></u>,2,2,3,2,2,2,7]: The second node is a local maxima because 3 is greater than 1 and 2. - [1,3,2,2,<u><strong>3</strong></u>,2,2,2,7]: The fifth node is a local maxima because 3 is greater than 2 and 2. Both the minimum and maximum distances are between the second and the fifth node. Thus, minDistance and maxDistance is 5 - 2 = 3. Note that the last node is not considered a local maxima because it does not have a next node. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the list is in the range <code>[2, 10<sup>5</sup>]</code>.</li> <li><code>1 &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "\nclass Solution {\npublic:\n vector<int> nodesBetweenCriticalPoints(ListNode* head) {\n ios_base::sync_with_stdio(false);\n if(head == NULL || head->next == NULL || head->next->next == NULL) {\n return {-1, -1};\n }\n ListNode* temp2 = head->next;\n ListNode* temp3 = head->next->next;\n vector<int> v;\n int i = 2;\n while(temp3) {\n if((head->val < temp2->val && temp3->val < temp2->val) || \n (head->val > temp2->val && temp3->val > temp2->val)) {\n v.push_back(i);\n }\n i++;\n head = head->next;\n temp2 = temp2->next;\n temp3 = temp3->next;\n }\n if(v.size() < 2) {\n return {-1, -1};\n }\n int mini = INT_MAX;\n for(int i = 1; i < v.size(); i++) {\n mini = min(mini, v[i] - v[i-1]);\n }\n return {mini, v[v.size()-1] - v[0]};\n }\n};\n", "memory": "119700" }
2,182
<p>A <strong>critical point</strong> in a linked list is defined as <strong>either</strong> a <strong>local maxima</strong> or a <strong>local minima</strong>.</p> <p>A node is a <strong>local maxima</strong> if the current node has a value <strong>strictly greater</strong> than the previous node and the next node.</p> <p>A node is a <strong>local minima</strong> if the current node has a value <strong>strictly smaller</strong> than the previous node and the next node.</p> <p>Note that a node can only be a local maxima/minima if there exists <strong>both</strong> a previous node and a next node.</p> <p>Given a linked list <code>head</code>, return <em>an array of length 2 containing </em><code>[minDistance, maxDistance]</code><em> where </em><code>minDistance</code><em> is the <strong>minimum distance</strong> between <strong>any&nbsp;two distinct</strong> critical points and </em><code>maxDistance</code><em> is the <strong>maximum distance</strong> between <strong>any&nbsp;two distinct</strong> critical points. If there are <strong>fewer</strong> than two critical points, return </em><code>[-1, -1]</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/10/13/a1.png" style="width: 148px; height: 55px;" /> <pre> <strong>Input:</strong> head = [3,1] <strong>Output:</strong> [-1,-1] <strong>Explanation:</strong> There are no critical points in [3,1]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/10/13/a2.png" style="width: 624px; height: 46px;" /> <pre> <strong>Input:</strong> head = [5,3,1,2,5,1,2] <strong>Output:</strong> [1,3] <strong>Explanation:</strong> There are three critical points: - [5,3,<strong><u>1</u></strong>,2,5,1,2]: The third node is a local minima because 1 is less than 3 and 2. - [5,3,1,2,<u><strong>5</strong></u>,1,2]: The fifth node is a local maxima because 5 is greater than 2 and 1. - [5,3,1,2,5,<u><strong>1</strong></u>,2]: The sixth node is a local minima because 1 is less than 5 and 2. The minimum distance is between the fifth and the sixth node. minDistance = 6 - 5 = 1. The maximum distance is between the third and the sixth node. maxDistance = 6 - 3 = 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/10/14/a5.png" style="width: 624px; height: 39px;" /> <pre> <strong>Input:</strong> head = [1,3,2,2,3,2,2,2,7] <strong>Output:</strong> [3,3] <strong>Explanation:</strong> There are two critical points: - [1,<u><strong>3</strong></u>,2,2,3,2,2,2,7]: The second node is a local maxima because 3 is greater than 1 and 2. - [1,3,2,2,<u><strong>3</strong></u>,2,2,2,7]: The fifth node is a local maxima because 3 is greater than 2 and 2. Both the minimum and maximum distances are between the second and the fifth node. Thus, minDistance and maxDistance is 5 - 2 = 3. Note that the last node is not considered a local maxima because it does not have a next node. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the list is in the range <code>[2, 10<sup>5</sup>]</code>.</li> <li><code>1 &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "\nclass Solution {\npublic:\n vector<int> nodesBetweenCriticalPoints(ListNode* head) {\n ios_base::sync_with_stdio(false);\n if(head == NULL || head->next == NULL || head->next->next == NULL) {\n return {-1, -1};\n }\n ListNode* temp2 = head->next;\n ListNode* temp3 = head->next->next;\n vector<int> v;\n int i = 2;\n while(temp3) {\n if((head->val < temp2->val && temp3->val < temp2->val) || \n (head->val > temp2->val && temp3->val > temp2->val)) {\n v.push_back(i);\n }\n i++;\n head = temp2;\n temp2 = temp2->next;\n temp3 = temp3->next;\n }\n if(v.size() < 2) {\n return {-1, -1};\n }\n int mini = INT_MAX;\n for(int i = 1; i < v.size(); i++) {\n mini = min(mini, v[i] - v[i-1]);\n }\n return {mini, v[v.size()-1] - v[0]};\n }\n};\n", "memory": "119800" }
2,182
<p>A <strong>critical point</strong> in a linked list is defined as <strong>either</strong> a <strong>local maxima</strong> or a <strong>local minima</strong>.</p> <p>A node is a <strong>local maxima</strong> if the current node has a value <strong>strictly greater</strong> than the previous node and the next node.</p> <p>A node is a <strong>local minima</strong> if the current node has a value <strong>strictly smaller</strong> than the previous node and the next node.</p> <p>Note that a node can only be a local maxima/minima if there exists <strong>both</strong> a previous node and a next node.</p> <p>Given a linked list <code>head</code>, return <em>an array of length 2 containing </em><code>[minDistance, maxDistance]</code><em> where </em><code>minDistance</code><em> is the <strong>minimum distance</strong> between <strong>any&nbsp;two distinct</strong> critical points and </em><code>maxDistance</code><em> is the <strong>maximum distance</strong> between <strong>any&nbsp;two distinct</strong> critical points. If there are <strong>fewer</strong> than two critical points, return </em><code>[-1, -1]</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/10/13/a1.png" style="width: 148px; height: 55px;" /> <pre> <strong>Input:</strong> head = [3,1] <strong>Output:</strong> [-1,-1] <strong>Explanation:</strong> There are no critical points in [3,1]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/10/13/a2.png" style="width: 624px; height: 46px;" /> <pre> <strong>Input:</strong> head = [5,3,1,2,5,1,2] <strong>Output:</strong> [1,3] <strong>Explanation:</strong> There are three critical points: - [5,3,<strong><u>1</u></strong>,2,5,1,2]: The third node is a local minima because 1 is less than 3 and 2. - [5,3,1,2,<u><strong>5</strong></u>,1,2]: The fifth node is a local maxima because 5 is greater than 2 and 1. - [5,3,1,2,5,<u><strong>1</strong></u>,2]: The sixth node is a local minima because 1 is less than 5 and 2. The minimum distance is between the fifth and the sixth node. minDistance = 6 - 5 = 1. The maximum distance is between the third and the sixth node. maxDistance = 6 - 3 = 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/10/14/a5.png" style="width: 624px; height: 39px;" /> <pre> <strong>Input:</strong> head = [1,3,2,2,3,2,2,2,7] <strong>Output:</strong> [3,3] <strong>Explanation:</strong> There are two critical points: - [1,<u><strong>3</strong></u>,2,2,3,2,2,2,7]: The second node is a local maxima because 3 is greater than 1 and 2. - [1,3,2,2,<u><strong>3</strong></u>,2,2,2,7]: The fifth node is a local maxima because 3 is greater than 2 and 2. Both the minimum and maximum distances are between the second and the fifth node. Thus, minDistance and maxDistance is 5 - 2 = 3. Note that the last node is not considered a local maxima because it does not have a next node. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the list is in the range <code>[2, 10<sup>5</sup>]</code>.</li> <li><code>1 &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n vector<int> nodesBetweenCriticalPoints(ListNode* head) {\n ListNode *prev=head;\n ListNode *curr=head->next;\n ListNode *nex=head->next->next;\n int count=1;\n vector<int>a;\n while(nex!=nullptr)\n {\n if(prev->val>curr->val&&nex->val>curr->val||prev->val<curr->val&&nex->val<curr->val)\n {\n a.push_back(count);\n }\n count++;\n prev=curr;\n curr=nex;\n nex=nex->next;\n }\n if(a.size()<2)\n {\n return {-1,-1};\n }\n int mini=INT_MAX;\n int maxi=a[a.size()-1]-a[0];\n for(int i=1;i<a.size();i++)\n {\n if(a[i]-a[i-1]<mini)\n {\n mini=a[i]-a[i-1];\n }\n }\n return {mini,maxi};\n }\n};", "memory": "119900" }
2,182
<p>A <strong>critical point</strong> in a linked list is defined as <strong>either</strong> a <strong>local maxima</strong> or a <strong>local minima</strong>.</p> <p>A node is a <strong>local maxima</strong> if the current node has a value <strong>strictly greater</strong> than the previous node and the next node.</p> <p>A node is a <strong>local minima</strong> if the current node has a value <strong>strictly smaller</strong> than the previous node and the next node.</p> <p>Note that a node can only be a local maxima/minima if there exists <strong>both</strong> a previous node and a next node.</p> <p>Given a linked list <code>head</code>, return <em>an array of length 2 containing </em><code>[minDistance, maxDistance]</code><em> where </em><code>minDistance</code><em> is the <strong>minimum distance</strong> between <strong>any&nbsp;two distinct</strong> critical points and </em><code>maxDistance</code><em> is the <strong>maximum distance</strong> between <strong>any&nbsp;two distinct</strong> critical points. If there are <strong>fewer</strong> than two critical points, return </em><code>[-1, -1]</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/10/13/a1.png" style="width: 148px; height: 55px;" /> <pre> <strong>Input:</strong> head = [3,1] <strong>Output:</strong> [-1,-1] <strong>Explanation:</strong> There are no critical points in [3,1]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/10/13/a2.png" style="width: 624px; height: 46px;" /> <pre> <strong>Input:</strong> head = [5,3,1,2,5,1,2] <strong>Output:</strong> [1,3] <strong>Explanation:</strong> There are three critical points: - [5,3,<strong><u>1</u></strong>,2,5,1,2]: The third node is a local minima because 1 is less than 3 and 2. - [5,3,1,2,<u><strong>5</strong></u>,1,2]: The fifth node is a local maxima because 5 is greater than 2 and 1. - [5,3,1,2,5,<u><strong>1</strong></u>,2]: The sixth node is a local minima because 1 is less than 5 and 2. The minimum distance is between the fifth and the sixth node. minDistance = 6 - 5 = 1. The maximum distance is between the third and the sixth node. maxDistance = 6 - 3 = 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/10/14/a5.png" style="width: 624px; height: 39px;" /> <pre> <strong>Input:</strong> head = [1,3,2,2,3,2,2,2,7] <strong>Output:</strong> [3,3] <strong>Explanation:</strong> There are two critical points: - [1,<u><strong>3</strong></u>,2,2,3,2,2,2,7]: The second node is a local maxima because 3 is greater than 1 and 2. - [1,3,2,2,<u><strong>3</strong></u>,2,2,2,7]: The fifth node is a local maxima because 3 is greater than 2 and 2. Both the minimum and maximum distances are between the second and the fifth node. Thus, minDistance and maxDistance is 5 - 2 = 3. Note that the last node is not considered a local maxima because it does not have a next node. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the list is in the range <code>[2, 10<sup>5</sup>]</code>.</li> <li><code>1 &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "\nclass Solution {\npublic:\n vector<int> nodesBetweenCriticalPoints(ListNode* head) {\n ios_base::sync_with_stdio(false);\n if(head == NULL || head->next == NULL || head->next->next == NULL) {\n return {-1, -1};\n }\n ListNode* temp2 = head->next;\n ListNode* temp3 = head->next->next;\n vector<int> v;\n int i = 2;\n while(temp3) {\n if((head->val < temp2->val && temp2->val > temp3->val) || \n (head->val > temp2->val && temp2->val < temp3->val)) {\n v.push_back(i);\n }\n i++;\n head = temp2;\n temp2 = temp3;\n temp3 = temp3->next;\n }\n if(v.size() < 2) {\n return {-1, -1};\n }\n int mini = INT_MAX;\n for(int i = 1; i < v.size(); i++) {\n mini = min(mini, v[i] - v[i-1]);\n }\n return {mini, v.back() - v[0]};\n }\n};\n", "memory": "119900" }
2,182
<p>A <strong>critical point</strong> in a linked list is defined as <strong>either</strong> a <strong>local maxima</strong> or a <strong>local minima</strong>.</p> <p>A node is a <strong>local maxima</strong> if the current node has a value <strong>strictly greater</strong> than the previous node and the next node.</p> <p>A node is a <strong>local minima</strong> if the current node has a value <strong>strictly smaller</strong> than the previous node and the next node.</p> <p>Note that a node can only be a local maxima/minima if there exists <strong>both</strong> a previous node and a next node.</p> <p>Given a linked list <code>head</code>, return <em>an array of length 2 containing </em><code>[minDistance, maxDistance]</code><em> where </em><code>minDistance</code><em> is the <strong>minimum distance</strong> between <strong>any&nbsp;two distinct</strong> critical points and </em><code>maxDistance</code><em> is the <strong>maximum distance</strong> between <strong>any&nbsp;two distinct</strong> critical points. If there are <strong>fewer</strong> than two critical points, return </em><code>[-1, -1]</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/10/13/a1.png" style="width: 148px; height: 55px;" /> <pre> <strong>Input:</strong> head = [3,1] <strong>Output:</strong> [-1,-1] <strong>Explanation:</strong> There are no critical points in [3,1]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/10/13/a2.png" style="width: 624px; height: 46px;" /> <pre> <strong>Input:</strong> head = [5,3,1,2,5,1,2] <strong>Output:</strong> [1,3] <strong>Explanation:</strong> There are three critical points: - [5,3,<strong><u>1</u></strong>,2,5,1,2]: The third node is a local minima because 1 is less than 3 and 2. - [5,3,1,2,<u><strong>5</strong></u>,1,2]: The fifth node is a local maxima because 5 is greater than 2 and 1. - [5,3,1,2,5,<u><strong>1</strong></u>,2]: The sixth node is a local minima because 1 is less than 5 and 2. The minimum distance is between the fifth and the sixth node. minDistance = 6 - 5 = 1. The maximum distance is between the third and the sixth node. maxDistance = 6 - 3 = 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/10/14/a5.png" style="width: 624px; height: 39px;" /> <pre> <strong>Input:</strong> head = [1,3,2,2,3,2,2,2,7] <strong>Output:</strong> [3,3] <strong>Explanation:</strong> There are two critical points: - [1,<u><strong>3</strong></u>,2,2,3,2,2,2,7]: The second node is a local maxima because 3 is greater than 1 and 2. - [1,3,2,2,<u><strong>3</strong></u>,2,2,2,7]: The fifth node is a local maxima because 3 is greater than 2 and 2. Both the minimum and maximum distances are between the second and the fifth node. Thus, minDistance and maxDistance is 5 - 2 = 3. Note that the last node is not considered a local maxima because it does not have a next node. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the list is in the range <code>[2, 10<sup>5</sup>]</code>.</li> <li><code>1 &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n vector<int> nodesBetweenCriticalPoints(ListNode* head) {\n int count=0;\n vector<int> v;\n ListNode* temp=head;\n if(!head || !head->next || !head->next->next)\n return {-1,-1};\n while(temp->next->next){\n if(temp->next->val>temp->val && temp->next->val>temp->next->next->val)\n v.push_back(count+1);\n else if(temp->next->val<temp->val && temp->next->val<temp->next->next->val)\n v.push_back(count+1);\n temp=temp->next;\n count++;\n }\n int n=v.size();\n if(n<2)\n return {-1,-1};\n int mini=INT_MAX;\n for(int i=0;i<n-1;i++){\n mini=min(v[i+1]-v[i],mini);\n }\n return {mini,v[n-1]-v[0]};\n }\n};", "memory": "120000" }
2,182
<p>A <strong>critical point</strong> in a linked list is defined as <strong>either</strong> a <strong>local maxima</strong> or a <strong>local minima</strong>.</p> <p>A node is a <strong>local maxima</strong> if the current node has a value <strong>strictly greater</strong> than the previous node and the next node.</p> <p>A node is a <strong>local minima</strong> if the current node has a value <strong>strictly smaller</strong> than the previous node and the next node.</p> <p>Note that a node can only be a local maxima/minima if there exists <strong>both</strong> a previous node and a next node.</p> <p>Given a linked list <code>head</code>, return <em>an array of length 2 containing </em><code>[minDistance, maxDistance]</code><em> where </em><code>minDistance</code><em> is the <strong>minimum distance</strong> between <strong>any&nbsp;two distinct</strong> critical points and </em><code>maxDistance</code><em> is the <strong>maximum distance</strong> between <strong>any&nbsp;two distinct</strong> critical points. If there are <strong>fewer</strong> than two critical points, return </em><code>[-1, -1]</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/10/13/a1.png" style="width: 148px; height: 55px;" /> <pre> <strong>Input:</strong> head = [3,1] <strong>Output:</strong> [-1,-1] <strong>Explanation:</strong> There are no critical points in [3,1]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/10/13/a2.png" style="width: 624px; height: 46px;" /> <pre> <strong>Input:</strong> head = [5,3,1,2,5,1,2] <strong>Output:</strong> [1,3] <strong>Explanation:</strong> There are three critical points: - [5,3,<strong><u>1</u></strong>,2,5,1,2]: The third node is a local minima because 1 is less than 3 and 2. - [5,3,1,2,<u><strong>5</strong></u>,1,2]: The fifth node is a local maxima because 5 is greater than 2 and 1. - [5,3,1,2,5,<u><strong>1</strong></u>,2]: The sixth node is a local minima because 1 is less than 5 and 2. The minimum distance is between the fifth and the sixth node. minDistance = 6 - 5 = 1. The maximum distance is between the third and the sixth node. maxDistance = 6 - 3 = 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/10/14/a5.png" style="width: 624px; height: 39px;" /> <pre> <strong>Input:</strong> head = [1,3,2,2,3,2,2,2,7] <strong>Output:</strong> [3,3] <strong>Explanation:</strong> There are two critical points: - [1,<u><strong>3</strong></u>,2,2,3,2,2,2,7]: The second node is a local maxima because 3 is greater than 1 and 2. - [1,3,2,2,<u><strong>3</strong></u>,2,2,2,7]: The fifth node is a local maxima because 3 is greater than 2 and 2. Both the minimum and maximum distances are between the second and the fifth node. Thus, minDistance and maxDistance is 5 - 2 = 3. Note that the last node is not considered a local maxima because it does not have a next node. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the list is in the range <code>[2, 10<sup>5</sup>]</code>.</li> <li><code>1 &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "\nclass Solution {\npublic:\n vector<int> nodesBetweenCriticalPoints(ListNode* head) {\n ios_base::sync_with_stdio(false);\n if(head == NULL || head->next == NULL || head->next->next == NULL) {\n return {-1, -1};\n }\n ListNode* temp2 = head->next;\n ListNode* temp3 = head->next->next;\n vector<int> v;\n int i = 2;\n while(temp3) {\n if((head->val < temp2->val && temp3->val < temp2->val) || \n (head->val > temp2->val && temp3->val > temp2->val)) {\n v.push_back(i);\n }\n i++;\n head = temp2;\n temp2 = temp3;\n temp3 = temp3->next;\n }\n if(v.size() < 2) {\n return {-1, -1};\n }\n int mini = INT_MAX;\n for(int i = 1; i < v.size(); i++) {\n mini = min(mini, v[i] - v[i-1]);\n }\n return {mini, v.back() - v.front()};\n }\n};\n", "memory": "120000" }
2,182
<p>A <strong>critical point</strong> in a linked list is defined as <strong>either</strong> a <strong>local maxima</strong> or a <strong>local minima</strong>.</p> <p>A node is a <strong>local maxima</strong> if the current node has a value <strong>strictly greater</strong> than the previous node and the next node.</p> <p>A node is a <strong>local minima</strong> if the current node has a value <strong>strictly smaller</strong> than the previous node and the next node.</p> <p>Note that a node can only be a local maxima/minima if there exists <strong>both</strong> a previous node and a next node.</p> <p>Given a linked list <code>head</code>, return <em>an array of length 2 containing </em><code>[minDistance, maxDistance]</code><em> where </em><code>minDistance</code><em> is the <strong>minimum distance</strong> between <strong>any&nbsp;two distinct</strong> critical points and </em><code>maxDistance</code><em> is the <strong>maximum distance</strong> between <strong>any&nbsp;two distinct</strong> critical points. If there are <strong>fewer</strong> than two critical points, return </em><code>[-1, -1]</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/10/13/a1.png" style="width: 148px; height: 55px;" /> <pre> <strong>Input:</strong> head = [3,1] <strong>Output:</strong> [-1,-1] <strong>Explanation:</strong> There are no critical points in [3,1]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/10/13/a2.png" style="width: 624px; height: 46px;" /> <pre> <strong>Input:</strong> head = [5,3,1,2,5,1,2] <strong>Output:</strong> [1,3] <strong>Explanation:</strong> There are three critical points: - [5,3,<strong><u>1</u></strong>,2,5,1,2]: The third node is a local minima because 1 is less than 3 and 2. - [5,3,1,2,<u><strong>5</strong></u>,1,2]: The fifth node is a local maxima because 5 is greater than 2 and 1. - [5,3,1,2,5,<u><strong>1</strong></u>,2]: The sixth node is a local minima because 1 is less than 5 and 2. The minimum distance is between the fifth and the sixth node. minDistance = 6 - 5 = 1. The maximum distance is between the third and the sixth node. maxDistance = 6 - 3 = 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/10/14/a5.png" style="width: 624px; height: 39px;" /> <pre> <strong>Input:</strong> head = [1,3,2,2,3,2,2,2,7] <strong>Output:</strong> [3,3] <strong>Explanation:</strong> There are two critical points: - [1,<u><strong>3</strong></u>,2,2,3,2,2,2,7]: The second node is a local maxima because 3 is greater than 1 and 2. - [1,3,2,2,<u><strong>3</strong></u>,2,2,2,7]: The fifth node is a local maxima because 3 is greater than 2 and 2. Both the minimum and maximum distances are between the second and the fifth node. Thus, minDistance and maxDistance is 5 - 2 = 3. Note that the last node is not considered a local maxima because it does not have a next node. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the list is in the range <code>[2, 10<sup>5</sup>]</code>.</li> <li><code>1 &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n vector<int> nodesBetweenCriticalPoints(ListNode* head) {\n if(head->next->next==NULL)\n return {-1,-1};\n\n vector<int>v;\n ListNode* prev=head;\n ListNode* curr=head->next;\n ListNode* temp=head->next->next;\n\n int i=1;\n\n while(temp)\n {\n if((prev->val>curr->val && curr->val<temp->val)||(prev->val<curr->val && curr->val>temp->val))\n {\n v.push_back(i);\n }\n prev=prev->next;\n curr=curr->next;\n temp=temp->next;\n i++;\n }\n int n=v.size();\n if(n==0||n==1)\n return {-1,-1};\n\n int max=v[n-1]-v[0];\n\n int min_=v[1]-v[0];\n\n for(int i=1;i<n-1;i++)\n {\n min_=min(v[i+1]-v[i],min_);\n }\n return {min_,max};\n }\n};", "memory": "120100" }
2,182
<p>A <strong>critical point</strong> in a linked list is defined as <strong>either</strong> a <strong>local maxima</strong> or a <strong>local minima</strong>.</p> <p>A node is a <strong>local maxima</strong> if the current node has a value <strong>strictly greater</strong> than the previous node and the next node.</p> <p>A node is a <strong>local minima</strong> if the current node has a value <strong>strictly smaller</strong> than the previous node and the next node.</p> <p>Note that a node can only be a local maxima/minima if there exists <strong>both</strong> a previous node and a next node.</p> <p>Given a linked list <code>head</code>, return <em>an array of length 2 containing </em><code>[minDistance, maxDistance]</code><em> where </em><code>minDistance</code><em> is the <strong>minimum distance</strong> between <strong>any&nbsp;two distinct</strong> critical points and </em><code>maxDistance</code><em> is the <strong>maximum distance</strong> between <strong>any&nbsp;two distinct</strong> critical points. If there are <strong>fewer</strong> than two critical points, return </em><code>[-1, -1]</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/10/13/a1.png" style="width: 148px; height: 55px;" /> <pre> <strong>Input:</strong> head = [3,1] <strong>Output:</strong> [-1,-1] <strong>Explanation:</strong> There are no critical points in [3,1]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/10/13/a2.png" style="width: 624px; height: 46px;" /> <pre> <strong>Input:</strong> head = [5,3,1,2,5,1,2] <strong>Output:</strong> [1,3] <strong>Explanation:</strong> There are three critical points: - [5,3,<strong><u>1</u></strong>,2,5,1,2]: The third node is a local minima because 1 is less than 3 and 2. - [5,3,1,2,<u><strong>5</strong></u>,1,2]: The fifth node is a local maxima because 5 is greater than 2 and 1. - [5,3,1,2,5,<u><strong>1</strong></u>,2]: The sixth node is a local minima because 1 is less than 5 and 2. The minimum distance is between the fifth and the sixth node. minDistance = 6 - 5 = 1. The maximum distance is between the third and the sixth node. maxDistance = 6 - 3 = 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/10/14/a5.png" style="width: 624px; height: 39px;" /> <pre> <strong>Input:</strong> head = [1,3,2,2,3,2,2,2,7] <strong>Output:</strong> [3,3] <strong>Explanation:</strong> There are two critical points: - [1,<u><strong>3</strong></u>,2,2,3,2,2,2,7]: The second node is a local maxima because 3 is greater than 1 and 2. - [1,3,2,2,<u><strong>3</strong></u>,2,2,2,7]: The fifth node is a local maxima because 3 is greater than 2 and 2. Both the minimum and maximum distances are between the second and the fifth node. Thus, minDistance and maxDistance is 5 - 2 = 3. Note that the last node is not considered a local maxima because it does not have a next node. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the list is in the range <code>[2, 10<sup>5</sup>]</code>.</li> <li><code>1 &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n vector<int> nodesBetweenCriticalPoints(ListNode* head) {\n int count=0;\n vector<int> v;\n ListNode* temp=head;\n if(!head || !head->next || !head->next->next)\n return {-1,-1};\n while(temp->next->next){\n if(temp->next->val>temp->val && temp->next->val>temp->next->next->val)\n v.push_back(count+1);\n else if(temp->next->val<temp->val && temp->next->val<temp->next->next->val)\n v.push_back(count+1);\n temp=temp->next;\n count++;\n }\n int n=v.size();\n if(n<2)\n return {-1,-1};\n int mini=INT_MAX;\n for(int i=0;i<n-1;i++){\n mini=min(v[i+1]-v[i],mini);\n }\n return {mini,v[n-1]-v[0]};\n }\n};", "memory": "120100" }
2,182
<p>A <strong>critical point</strong> in a linked list is defined as <strong>either</strong> a <strong>local maxima</strong> or a <strong>local minima</strong>.</p> <p>A node is a <strong>local maxima</strong> if the current node has a value <strong>strictly greater</strong> than the previous node and the next node.</p> <p>A node is a <strong>local minima</strong> if the current node has a value <strong>strictly smaller</strong> than the previous node and the next node.</p> <p>Note that a node can only be a local maxima/minima if there exists <strong>both</strong> a previous node and a next node.</p> <p>Given a linked list <code>head</code>, return <em>an array of length 2 containing </em><code>[minDistance, maxDistance]</code><em> where </em><code>minDistance</code><em> is the <strong>minimum distance</strong> between <strong>any&nbsp;two distinct</strong> critical points and </em><code>maxDistance</code><em> is the <strong>maximum distance</strong> between <strong>any&nbsp;two distinct</strong> critical points. If there are <strong>fewer</strong> than two critical points, return </em><code>[-1, -1]</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/10/13/a1.png" style="width: 148px; height: 55px;" /> <pre> <strong>Input:</strong> head = [3,1] <strong>Output:</strong> [-1,-1] <strong>Explanation:</strong> There are no critical points in [3,1]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/10/13/a2.png" style="width: 624px; height: 46px;" /> <pre> <strong>Input:</strong> head = [5,3,1,2,5,1,2] <strong>Output:</strong> [1,3] <strong>Explanation:</strong> There are three critical points: - [5,3,<strong><u>1</u></strong>,2,5,1,2]: The third node is a local minima because 1 is less than 3 and 2. - [5,3,1,2,<u><strong>5</strong></u>,1,2]: The fifth node is a local maxima because 5 is greater than 2 and 1. - [5,3,1,2,5,<u><strong>1</strong></u>,2]: The sixth node is a local minima because 1 is less than 5 and 2. The minimum distance is between the fifth and the sixth node. minDistance = 6 - 5 = 1. The maximum distance is between the third and the sixth node. maxDistance = 6 - 3 = 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/10/14/a5.png" style="width: 624px; height: 39px;" /> <pre> <strong>Input:</strong> head = [1,3,2,2,3,2,2,2,7] <strong>Output:</strong> [3,3] <strong>Explanation:</strong> There are two critical points: - [1,<u><strong>3</strong></u>,2,2,3,2,2,2,7]: The second node is a local maxima because 3 is greater than 1 and 2. - [1,3,2,2,<u><strong>3</strong></u>,2,2,2,7]: The fifth node is a local maxima because 3 is greater than 2 and 2. Both the minimum and maximum distances are between the second and the fifth node. Thus, minDistance and maxDistance is 5 - 2 = 3. Note that the last node is not considered a local maxima because it does not have a next node. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the list is in the range <code>[2, 10<sup>5</sup>]</code>.</li> <li><code>1 &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n vector<int> nodesBetweenCriticalPoints(ListNode* head) {\n if (head == NULL || head->next == NULL || head->next->next == NULL)\n return {-1, -1};\n vector<int> store; // vector (mảng) dùng để lưu index các điểm cực trị địa phương\n ListNode* prev = head;\n ListNode* cur = head->next;\n ListNode* fut = head->next->next;\n int pos = 2; // cập nhật index của cur\n int n = 0;\n int min_distant = INT_MAX; // Biến này lưu khoảng cách ngắn nhất giữa hai điểm quan trọng. Ban đầu được đặt là INT_MAX (một số lớn nhất) để có thể cập nhật dễ dàng.\n // khoảng cách lớn nhất thì cứ lấy số cuối trừ số đầu nên không cần xét\n while (fut != NULL){\n if ((cur->val < prev->val && cur->val < fut->val) || (cur->val > prev->val && cur->val > fut->val))\n store.push_back(pos);\n pos++;\n prev = prev->next;\n cur = cur->next;\n fut = fut->next;\n n = store.size();\n if (n > 1)\n min_distant = min(min_distant, store[n - 1] - store[n - 2]);\n }\n if (n <= 1)\n return {-1, -1};\n return {min_distant, store.back() - store[0]};\n }\n};", "memory": "120200" }
2,182
<p>A <strong>critical point</strong> in a linked list is defined as <strong>either</strong> a <strong>local maxima</strong> or a <strong>local minima</strong>.</p> <p>A node is a <strong>local maxima</strong> if the current node has a value <strong>strictly greater</strong> than the previous node and the next node.</p> <p>A node is a <strong>local minima</strong> if the current node has a value <strong>strictly smaller</strong> than the previous node and the next node.</p> <p>Note that a node can only be a local maxima/minima if there exists <strong>both</strong> a previous node and a next node.</p> <p>Given a linked list <code>head</code>, return <em>an array of length 2 containing </em><code>[minDistance, maxDistance]</code><em> where </em><code>minDistance</code><em> is the <strong>minimum distance</strong> between <strong>any&nbsp;two distinct</strong> critical points and </em><code>maxDistance</code><em> is the <strong>maximum distance</strong> between <strong>any&nbsp;two distinct</strong> critical points. If there are <strong>fewer</strong> than two critical points, return </em><code>[-1, -1]</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/10/13/a1.png" style="width: 148px; height: 55px;" /> <pre> <strong>Input:</strong> head = [3,1] <strong>Output:</strong> [-1,-1] <strong>Explanation:</strong> There are no critical points in [3,1]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/10/13/a2.png" style="width: 624px; height: 46px;" /> <pre> <strong>Input:</strong> head = [5,3,1,2,5,1,2] <strong>Output:</strong> [1,3] <strong>Explanation:</strong> There are three critical points: - [5,3,<strong><u>1</u></strong>,2,5,1,2]: The third node is a local minima because 1 is less than 3 and 2. - [5,3,1,2,<u><strong>5</strong></u>,1,2]: The fifth node is a local maxima because 5 is greater than 2 and 1. - [5,3,1,2,5,<u><strong>1</strong></u>,2]: The sixth node is a local minima because 1 is less than 5 and 2. The minimum distance is between the fifth and the sixth node. minDistance = 6 - 5 = 1. The maximum distance is between the third and the sixth node. maxDistance = 6 - 3 = 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/10/14/a5.png" style="width: 624px; height: 39px;" /> <pre> <strong>Input:</strong> head = [1,3,2,2,3,2,2,2,7] <strong>Output:</strong> [3,3] <strong>Explanation:</strong> There are two critical points: - [1,<u><strong>3</strong></u>,2,2,3,2,2,2,7]: The second node is a local maxima because 3 is greater than 1 and 2. - [1,3,2,2,<u><strong>3</strong></u>,2,2,2,7]: The fifth node is a local maxima because 3 is greater than 2 and 2. Both the minimum and maximum distances are between the second and the fifth node. Thus, minDistance and maxDistance is 5 - 2 = 3. Note that the last node is not considered a local maxima because it does not have a next node. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the list is in the range <code>[2, 10<sup>5</sup>]</code>.</li> <li><code>1 &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n vector<int> nodesBetweenCriticalPoints(ListNode* head) {\n if(head==NULL || head->next==NULL){\n return {-1, -1};\n }\n ListNode* prevNode=head;\n ListNode* currNode=prevNode->next;\n ListNode* nextNode=currNode->next;\n int CriticalPoints=0;\n int minDistance=0;\n int maxDistance=0;\n vector<int>Distances;\n vector<int>DistanceBetweenCriticalPoints;\n int position=2;\n while(nextNode!=NULL){\n if((currNode->val > prevNode->val && currNode->val > nextNode->val) || (currNode->val < prevNode->val && currNode->val < nextNode->val)){\n CriticalPoints++;\n Distances.push_back(position);\n prevNode=currNode;\n currNode=nextNode;\n nextNode=nextNode->next;\n position++;\n }\n else{\n prevNode=currNode;\n currNode=nextNode;\n nextNode=nextNode->next;\n position++;\n }\n }\n if(CriticalPoints<2){\n return {-1,-1};\n }\n else if(CriticalPoints==2){\n minDistance=maxDistance=Distances[Distances.size()-1] - Distances[0];\n DistanceBetweenCriticalPoints.push_back(minDistance);\n DistanceBetweenCriticalPoints.push_back(maxDistance);\n }\n else{\n maxDistance=Distances[Distances.size()-1] - Distances[0];\n minDistance=Distances[1]-Distances[0];\n for(int i=0; i<Distances.size()-1; i++){\n if(minDistance > (Distances[i+1] - Distances[i])){\n minDistance=Distances[i+1] - Distances[i];\n }\n }\n DistanceBetweenCriticalPoints.push_back(minDistance);\n DistanceBetweenCriticalPoints.push_back(maxDistance);\n }\n return DistanceBetweenCriticalPoints;\n }\n};\n// class Solution {\n// public:\n// vector<int> nodesBetweenCriticalPoints(ListNode* head) {\n// if (head == nullptr || head->next == nullptr) {\n// return {-1, -1};\n// }\n\n// ListNode* prevNode = head;\n// ListNode* currNode = head->next;\n// int position = 2; // Start from the second node\n// int firstCritical = -1;\n// int lastCritical = -1;\n// int minDistance = INT_MAX;\n\n// while (currNode->next != nullptr) {\n// if ((currNode->val > prevNode->val && currNode->val > currNode->next->val) ||\n// (currNode->val < prevNode->val && currNode->val < currNode->next->val)) {\n \n// if (firstCritical == -1) {\n// firstCritical = position;\n// } else {\n// minDistance = min(minDistance, position - lastCritical);\n// }\n// lastCritical = position;\n// }\n// prevNode = currNode;\n// currNode = currNode->next;\n// position++;\n// }\n\n// if (lastCritical == -1 || firstCritical == lastCritical) {\n// return {-1, -1};\n// }\n\n// int maxDistance = lastCritical - firstCritical;\n// return {minDistance, maxDistance};\n// }\n// };\n", "memory": "120200" }
2,182
<p>A <strong>critical point</strong> in a linked list is defined as <strong>either</strong> a <strong>local maxima</strong> or a <strong>local minima</strong>.</p> <p>A node is a <strong>local maxima</strong> if the current node has a value <strong>strictly greater</strong> than the previous node and the next node.</p> <p>A node is a <strong>local minima</strong> if the current node has a value <strong>strictly smaller</strong> than the previous node and the next node.</p> <p>Note that a node can only be a local maxima/minima if there exists <strong>both</strong> a previous node and a next node.</p> <p>Given a linked list <code>head</code>, return <em>an array of length 2 containing </em><code>[minDistance, maxDistance]</code><em> where </em><code>minDistance</code><em> is the <strong>minimum distance</strong> between <strong>any&nbsp;two distinct</strong> critical points and </em><code>maxDistance</code><em> is the <strong>maximum distance</strong> between <strong>any&nbsp;two distinct</strong> critical points. If there are <strong>fewer</strong> than two critical points, return </em><code>[-1, -1]</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/10/13/a1.png" style="width: 148px; height: 55px;" /> <pre> <strong>Input:</strong> head = [3,1] <strong>Output:</strong> [-1,-1] <strong>Explanation:</strong> There are no critical points in [3,1]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/10/13/a2.png" style="width: 624px; height: 46px;" /> <pre> <strong>Input:</strong> head = [5,3,1,2,5,1,2] <strong>Output:</strong> [1,3] <strong>Explanation:</strong> There are three critical points: - [5,3,<strong><u>1</u></strong>,2,5,1,2]: The third node is a local minima because 1 is less than 3 and 2. - [5,3,1,2,<u><strong>5</strong></u>,1,2]: The fifth node is a local maxima because 5 is greater than 2 and 1. - [5,3,1,2,5,<u><strong>1</strong></u>,2]: The sixth node is a local minima because 1 is less than 5 and 2. The minimum distance is between the fifth and the sixth node. minDistance = 6 - 5 = 1. The maximum distance is between the third and the sixth node. maxDistance = 6 - 3 = 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/10/14/a5.png" style="width: 624px; height: 39px;" /> <pre> <strong>Input:</strong> head = [1,3,2,2,3,2,2,2,7] <strong>Output:</strong> [3,3] <strong>Explanation:</strong> There are two critical points: - [1,<u><strong>3</strong></u>,2,2,3,2,2,2,7]: The second node is a local maxima because 3 is greater than 1 and 2. - [1,3,2,2,<u><strong>3</strong></u>,2,2,2,7]: The fifth node is a local maxima because 3 is greater than 2 and 2. Both the minimum and maximum distances are between the second and the fifth node. Thus, minDistance and maxDistance is 5 - 2 = 3. Note that the last node is not considered a local maxima because it does not have a next node. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the list is in the range <code>[2, 10<sup>5</sup>]</code>.</li> <li><code>1 &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n int getLength(ListNode* head){\n int len = 0;\n ListNode* temp = head;\n while(temp != NULL){\n len++;\n temp = temp -> next;\n }\n return len;\n }\n vector<int> nodesBetweenCriticalPoints(ListNode* head) {\n int length = getLength(head);\n vector<int> v(length,0);\n ListNode* pre = head;\n ListNode* cur = pre -> next;\n ListNode* nxt = cur -> next;\n int i = 1;\n if(length == 2){\n return {-1,-1};\n }\n while(nxt != NULL){\n if(cur -> val > pre -> val && cur -> val > nxt -> val){\n v[i] = 1;\n }\n else if(cur -> val < pre -> val && cur -> val < nxt -> val){\n v[i] = 1;\n }\n pre = cur;\n cur = nxt;\n // if(nxt -> next != NULL)\n nxt = nxt -> next;\n i++;\n }\n int maxi = INT_MIN;\n int mini = INT_MAX;\n // for(auto it : v) cout<<it<<\" \";\n \n // finding the mini\n int l = 0;\n int currentPeak = -1;\n int lastPeak = -1;\n for(int l = 0; l<length; l++){\n if(v[l] == 1){\n currentPeak = l;\n if(lastPeak != -1){\n mini = min(mini, currentPeak - lastPeak);\n }\n lastPeak = l;\n }\n }\n // finding the maxi\n int s = 0;\n int e = length -1;\n while(s < e){\n if(v[s] == 1 && v[e] == 1){\n maxi = max(maxi, e-s);\n s++;\n e--;\n }\n else if(v[s] != 1){\n s++;\n }\n else{\n e--;\n }\n }\n if(mini == INT_MAX && maxi == INT_MIN) return {-1,-1};\n return {mini, maxi};\n\n }\n\n};", "memory": "120300" }
2,182
<p>A <strong>critical point</strong> in a linked list is defined as <strong>either</strong> a <strong>local maxima</strong> or a <strong>local minima</strong>.</p> <p>A node is a <strong>local maxima</strong> if the current node has a value <strong>strictly greater</strong> than the previous node and the next node.</p> <p>A node is a <strong>local minima</strong> if the current node has a value <strong>strictly smaller</strong> than the previous node and the next node.</p> <p>Note that a node can only be a local maxima/minima if there exists <strong>both</strong> a previous node and a next node.</p> <p>Given a linked list <code>head</code>, return <em>an array of length 2 containing </em><code>[minDistance, maxDistance]</code><em> where </em><code>minDistance</code><em> is the <strong>minimum distance</strong> between <strong>any&nbsp;two distinct</strong> critical points and </em><code>maxDistance</code><em> is the <strong>maximum distance</strong> between <strong>any&nbsp;two distinct</strong> critical points. If there are <strong>fewer</strong> than two critical points, return </em><code>[-1, -1]</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/10/13/a1.png" style="width: 148px; height: 55px;" /> <pre> <strong>Input:</strong> head = [3,1] <strong>Output:</strong> [-1,-1] <strong>Explanation:</strong> There are no critical points in [3,1]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/10/13/a2.png" style="width: 624px; height: 46px;" /> <pre> <strong>Input:</strong> head = [5,3,1,2,5,1,2] <strong>Output:</strong> [1,3] <strong>Explanation:</strong> There are three critical points: - [5,3,<strong><u>1</u></strong>,2,5,1,2]: The third node is a local minima because 1 is less than 3 and 2. - [5,3,1,2,<u><strong>5</strong></u>,1,2]: The fifth node is a local maxima because 5 is greater than 2 and 1. - [5,3,1,2,5,<u><strong>1</strong></u>,2]: The sixth node is a local minima because 1 is less than 5 and 2. The minimum distance is between the fifth and the sixth node. minDistance = 6 - 5 = 1. The maximum distance is between the third and the sixth node. maxDistance = 6 - 3 = 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/10/14/a5.png" style="width: 624px; height: 39px;" /> <pre> <strong>Input:</strong> head = [1,3,2,2,3,2,2,2,7] <strong>Output:</strong> [3,3] <strong>Explanation:</strong> There are two critical points: - [1,<u><strong>3</strong></u>,2,2,3,2,2,2,7]: The second node is a local maxima because 3 is greater than 1 and 2. - [1,3,2,2,<u><strong>3</strong></u>,2,2,2,7]: The fifth node is a local maxima because 3 is greater than 2 and 2. Both the minimum and maximum distances are between the second and the fifth node. Thus, minDistance and maxDistance is 5 - 2 = 3. Note that the last node is not considered a local maxima because it does not have a next node. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the list is in the range <code>[2, 10<sup>5</sup>]</code>.</li> <li><code>1 &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "\nclass Solution {\npublic:\n vector<int> nodesBetweenCriticalPoints(ListNode* head) {\n int count=2;\n vector<int>v;\n ListNode * temp=head->next;\n ListNode * prev=head;\n while(temp->next)\n {\n if((temp->val > prev->val && temp->val > temp->next->val) ||\n (temp->val < prev->val && temp->val < temp->next->val))\n {\n v.push_back(count);\n }\n count++;\n prev=temp;\n temp=temp->next;\n }\n if(v.size()<2)\n {\n return {-1,-1};\n }\n sort(v.begin(), v.end()); // Sort the vector\n\n int maxDifference = v.back() - v.front(); \n int minDifference = INT_MAX;\n\n for (int i = 1; i < v.size(); ++i) {\n int diff = v[i] - v[i - 1];\n minDifference = min(minDifference, diff);\n }\n return {minDifference,maxDifference};\n }\n};\n", "memory": "121000" }
2,182
<p>A <strong>critical point</strong> in a linked list is defined as <strong>either</strong> a <strong>local maxima</strong> or a <strong>local minima</strong>.</p> <p>A node is a <strong>local maxima</strong> if the current node has a value <strong>strictly greater</strong> than the previous node and the next node.</p> <p>A node is a <strong>local minima</strong> if the current node has a value <strong>strictly smaller</strong> than the previous node and the next node.</p> <p>Note that a node can only be a local maxima/minima if there exists <strong>both</strong> a previous node and a next node.</p> <p>Given a linked list <code>head</code>, return <em>an array of length 2 containing </em><code>[minDistance, maxDistance]</code><em> where </em><code>minDistance</code><em> is the <strong>minimum distance</strong> between <strong>any&nbsp;two distinct</strong> critical points and </em><code>maxDistance</code><em> is the <strong>maximum distance</strong> between <strong>any&nbsp;two distinct</strong> critical points. If there are <strong>fewer</strong> than two critical points, return </em><code>[-1, -1]</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/10/13/a1.png" style="width: 148px; height: 55px;" /> <pre> <strong>Input:</strong> head = [3,1] <strong>Output:</strong> [-1,-1] <strong>Explanation:</strong> There are no critical points in [3,1]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/10/13/a2.png" style="width: 624px; height: 46px;" /> <pre> <strong>Input:</strong> head = [5,3,1,2,5,1,2] <strong>Output:</strong> [1,3] <strong>Explanation:</strong> There are three critical points: - [5,3,<strong><u>1</u></strong>,2,5,1,2]: The third node is a local minima because 1 is less than 3 and 2. - [5,3,1,2,<u><strong>5</strong></u>,1,2]: The fifth node is a local maxima because 5 is greater than 2 and 1. - [5,3,1,2,5,<u><strong>1</strong></u>,2]: The sixth node is a local minima because 1 is less than 5 and 2. The minimum distance is between the fifth and the sixth node. minDistance = 6 - 5 = 1. The maximum distance is between the third and the sixth node. maxDistance = 6 - 3 = 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/10/14/a5.png" style="width: 624px; height: 39px;" /> <pre> <strong>Input:</strong> head = [1,3,2,2,3,2,2,2,7] <strong>Output:</strong> [3,3] <strong>Explanation:</strong> There are two critical points: - [1,<u><strong>3</strong></u>,2,2,3,2,2,2,7]: The second node is a local maxima because 3 is greater than 1 and 2. - [1,3,2,2,<u><strong>3</strong></u>,2,2,2,7]: The fifth node is a local maxima because 3 is greater than 2 and 2. Both the minimum and maximum distances are between the second and the fifth node. Thus, minDistance and maxDistance is 5 - 2 = 3. Note that the last node is not considered a local maxima because it does not have a next node. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the list is in the range <code>[2, 10<sup>5</sup>]</code>.</li> <li><code>1 &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> nodesBetweenCriticalPoints(ListNode* head) {\n if(head==NULL || head->next==NULL) return {-1,-1};\n int n=1;\n int c=0;\n ListNode* pre=head;\n ListNode* curr=head->next;\n ListNode* agla=head->next->next;\n vector<int>v;\n while(agla!=NULL)\n {\n int x=pre->val;\n int y=curr->val;\n int z=agla->val;\n \n if(y>x && y>z)\n {\n v.push_back(n+1);\n c++;\n } \n else if(y<x && y<z)\n {\n v.push_back(n+1);\n c++;\n } \n pre=pre->next;\n curr=curr->next;\n agla=agla->next;\n n++;\n }\n int x=0;\n int min=INT_MAX;\n if(c>1)\n {\n sort(v.begin(),v.end());\n for(int i=0;i<v.size()-1;i++)\n {\n int p=v[i+1]-v[i];\n if(p==1)\n {\n x=1;\n break;\n }\n if(p<min)\n {\n min=p;\n x=min;\n }\n }\n int y=v[v.size()-1] - v[0];\n return {x,y};\n }\n\n\n\nreturn {-1,-1};\n \n }\n};", "memory": "121100" }
2,182
<p>A <strong>critical point</strong> in a linked list is defined as <strong>either</strong> a <strong>local maxima</strong> or a <strong>local minima</strong>.</p> <p>A node is a <strong>local maxima</strong> if the current node has a value <strong>strictly greater</strong> than the previous node and the next node.</p> <p>A node is a <strong>local minima</strong> if the current node has a value <strong>strictly smaller</strong> than the previous node and the next node.</p> <p>Note that a node can only be a local maxima/minima if there exists <strong>both</strong> a previous node and a next node.</p> <p>Given a linked list <code>head</code>, return <em>an array of length 2 containing </em><code>[minDistance, maxDistance]</code><em> where </em><code>minDistance</code><em> is the <strong>minimum distance</strong> between <strong>any&nbsp;two distinct</strong> critical points and </em><code>maxDistance</code><em> is the <strong>maximum distance</strong> between <strong>any&nbsp;two distinct</strong> critical points. If there are <strong>fewer</strong> than two critical points, return </em><code>[-1, -1]</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/10/13/a1.png" style="width: 148px; height: 55px;" /> <pre> <strong>Input:</strong> head = [3,1] <strong>Output:</strong> [-1,-1] <strong>Explanation:</strong> There are no critical points in [3,1]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/10/13/a2.png" style="width: 624px; height: 46px;" /> <pre> <strong>Input:</strong> head = [5,3,1,2,5,1,2] <strong>Output:</strong> [1,3] <strong>Explanation:</strong> There are three critical points: - [5,3,<strong><u>1</u></strong>,2,5,1,2]: The third node is a local minima because 1 is less than 3 and 2. - [5,3,1,2,<u><strong>5</strong></u>,1,2]: The fifth node is a local maxima because 5 is greater than 2 and 1. - [5,3,1,2,5,<u><strong>1</strong></u>,2]: The sixth node is a local minima because 1 is less than 5 and 2. The minimum distance is between the fifth and the sixth node. minDistance = 6 - 5 = 1. The maximum distance is between the third and the sixth node. maxDistance = 6 - 3 = 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/10/14/a5.png" style="width: 624px; height: 39px;" /> <pre> <strong>Input:</strong> head = [1,3,2,2,3,2,2,2,7] <strong>Output:</strong> [3,3] <strong>Explanation:</strong> There are two critical points: - [1,<u><strong>3</strong></u>,2,2,3,2,2,2,7]: The second node is a local maxima because 3 is greater than 1 and 2. - [1,3,2,2,<u><strong>3</strong></u>,2,2,2,7]: The fifth node is a local maxima because 3 is greater than 2 and 2. Both the minimum and maximum distances are between the second and the fifth node. Thus, minDistance and maxDistance is 5 - 2 = 3. Note that the last node is not considered a local maxima because it does not have a next node. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the list is in the range <code>[2, 10<sup>5</sup>]</code>.</li> <li><code>1 &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n vector<int> nodesBetweenCriticalPoints(ListNode* head) {\n vector<int> v(2, -1);\n if (head == nullptr)\n return v;\n int count = 2;\n vector<int> criticalPoints;\n ListNode* prev = head;\n ListNode* curr = head->next;\n if (curr->next == nullptr)\n return v;\n while (curr != nullptr && curr->next != nullptr) {\n if ((prev != nullptr) && ((curr->val) > (prev->val)) &&\n ((curr->val) > (curr->next->val))) {\n criticalPoints.push_back(count);\n } else if (prev != nullptr && (curr->val) < (prev->val) &&\n (curr->val) < (curr->next->val)) {\n criticalPoints.push_back(count);\n }\n count++;\n prev = curr;\n curr = curr->next;\n }\n sort(criticalPoints.begin(), criticalPoints.end());\n if (criticalPoints.size() < 2)\n return v;\n int minim = INT_MAX;\n for(int i = 1;i<criticalPoints.size();i++){\n minim = min(minim, criticalPoints[i]-criticalPoints[i-1]);\n }\n v[0] = minim;\n v[1] = criticalPoints[criticalPoints.size()-1] - criticalPoints[0];\n return v;\n }\n};", "memory": "121200" }
2,177
<p>Two strings <code>word1</code> and <code>word2</code> are considered <strong>almost equivalent</strong> if the differences between the frequencies of each letter from <code>&#39;a&#39;</code> to <code>&#39;z&#39;</code> between <code>word1</code> and <code>word2</code> is <strong>at most</strong> <code>3</code>.</p> <p>Given two strings <code>word1</code> and <code>word2</code>, each of length <code>n</code>, return <code>true</code> <em>if </em><code>word1</code> <em>and</em> <code>word2</code> <em>are <strong>almost equivalent</strong>, or</em> <code>false</code> <em>otherwise</em>.</p> <p>The <strong>frequency</strong> of a letter <code>x</code> is the number of times it occurs in the string.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;aaaa&quot;, word2 = &quot;bccb&quot; <strong>Output:</strong> false <strong>Explanation:</strong> There are 4 &#39;a&#39;s in &quot;aaaa&quot; but 0 &#39;a&#39;s in &quot;bccb&quot;. The difference is 4, which is more than the allowed 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;abcdeef&quot;, word2 = &quot;abaaacc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> The differences between the frequencies of each letter in word1 and word2 are at most 3: - &#39;a&#39; appears 1 time in word1 and 4 times in word2. The difference is 3. - &#39;b&#39; appears 1 time in word1 and 1 time in word2. The difference is 0. - &#39;c&#39; appears 1 time in word1 and 2 times in word2. The difference is 1. - &#39;d&#39; appears 1 time in word1 and 0 times in word2. The difference is 1. - &#39;e&#39; appears 2 times in word1 and 0 times in word2. The difference is 2. - &#39;f&#39; appears 1 time in word1 and 0 times in word2. The difference is 1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;cccddabba&quot;, word2 = &quot;babababab&quot; <strong>Output:</strong> true <strong>Explanation:</strong> The differences between the frequencies of each letter in word1 and word2 are at most 3: - &#39;a&#39; appears 2 times in word1 and 4 times in word2. The difference is 2. - &#39;b&#39; appears 2 times in word1 and 5 times in word2. The difference is 3. - &#39;c&#39; appears 3 times in word1 and 0 times in word2. The difference is 3. - &#39;d&#39; appears 2 times in word1 and 0 times in word2. The difference is 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == word1.length == word2.length</code></li> <li><code>1 &lt;= n &lt;= 100</code></li> <li><code>word1</code> and <code>word2</code> consist only of lowercase English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n bool checkAlmostEquivalent(string_view word1, string_view word2) {\n array<int, 'z' - 'a' + 1> wf{};\n for (auto c : word1)\n ++wf[c - 'a'];\n for (auto c : word2)\n --wf[c - 'a'];\n return ranges::all_of(wf, [](int f){ return abs(f) < 4; });\n }\n};", "memory": "7600" }
2,177
<p>Two strings <code>word1</code> and <code>word2</code> are considered <strong>almost equivalent</strong> if the differences between the frequencies of each letter from <code>&#39;a&#39;</code> to <code>&#39;z&#39;</code> between <code>word1</code> and <code>word2</code> is <strong>at most</strong> <code>3</code>.</p> <p>Given two strings <code>word1</code> and <code>word2</code>, each of length <code>n</code>, return <code>true</code> <em>if </em><code>word1</code> <em>and</em> <code>word2</code> <em>are <strong>almost equivalent</strong>, or</em> <code>false</code> <em>otherwise</em>.</p> <p>The <strong>frequency</strong> of a letter <code>x</code> is the number of times it occurs in the string.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;aaaa&quot;, word2 = &quot;bccb&quot; <strong>Output:</strong> false <strong>Explanation:</strong> There are 4 &#39;a&#39;s in &quot;aaaa&quot; but 0 &#39;a&#39;s in &quot;bccb&quot;. The difference is 4, which is more than the allowed 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;abcdeef&quot;, word2 = &quot;abaaacc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> The differences between the frequencies of each letter in word1 and word2 are at most 3: - &#39;a&#39; appears 1 time in word1 and 4 times in word2. The difference is 3. - &#39;b&#39; appears 1 time in word1 and 1 time in word2. The difference is 0. - &#39;c&#39; appears 1 time in word1 and 2 times in word2. The difference is 1. - &#39;d&#39; appears 1 time in word1 and 0 times in word2. The difference is 1. - &#39;e&#39; appears 2 times in word1 and 0 times in word2. The difference is 2. - &#39;f&#39; appears 1 time in word1 and 0 times in word2. The difference is 1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;cccddabba&quot;, word2 = &quot;babababab&quot; <strong>Output:</strong> true <strong>Explanation:</strong> The differences between the frequencies of each letter in word1 and word2 are at most 3: - &#39;a&#39; appears 2 times in word1 and 4 times in word2. The difference is 2. - &#39;b&#39; appears 2 times in word1 and 5 times in word2. The difference is 3. - &#39;c&#39; appears 3 times in word1 and 0 times in word2. The difference is 3. - &#39;d&#39; appears 2 times in word1 and 0 times in word2. The difference is 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == word1.length == word2.length</code></li> <li><code>1 &lt;= n &lt;= 100</code></li> <li><code>word1</code> and <code>word2</code> consist only of lowercase English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n bool checkAlmostEquivalent(string word1, string word2) {\n vector<int> alpha1(26,0);\n int nSize = word1.length();\n for (int i = 0; i < nSize; i++) {\n alpha1[word1[i]-'a']++;\n }\n nSize = word2.length();\n for (int i = 0; i < nSize; i++){ \n alpha1[word2[i]-'a']--;\n }\n for (int i = 0; i < 26; i++){\n if (abs(alpha1[i]) > 3) return false;\n }\n return true;\n }\n};", "memory": "7800" }
2,177
<p>Two strings <code>word1</code> and <code>word2</code> are considered <strong>almost equivalent</strong> if the differences between the frequencies of each letter from <code>&#39;a&#39;</code> to <code>&#39;z&#39;</code> between <code>word1</code> and <code>word2</code> is <strong>at most</strong> <code>3</code>.</p> <p>Given two strings <code>word1</code> and <code>word2</code>, each of length <code>n</code>, return <code>true</code> <em>if </em><code>word1</code> <em>and</em> <code>word2</code> <em>are <strong>almost equivalent</strong>, or</em> <code>false</code> <em>otherwise</em>.</p> <p>The <strong>frequency</strong> of a letter <code>x</code> is the number of times it occurs in the string.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;aaaa&quot;, word2 = &quot;bccb&quot; <strong>Output:</strong> false <strong>Explanation:</strong> There are 4 &#39;a&#39;s in &quot;aaaa&quot; but 0 &#39;a&#39;s in &quot;bccb&quot;. The difference is 4, which is more than the allowed 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;abcdeef&quot;, word2 = &quot;abaaacc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> The differences between the frequencies of each letter in word1 and word2 are at most 3: - &#39;a&#39; appears 1 time in word1 and 4 times in word2. The difference is 3. - &#39;b&#39; appears 1 time in word1 and 1 time in word2. The difference is 0. - &#39;c&#39; appears 1 time in word1 and 2 times in word2. The difference is 1. - &#39;d&#39; appears 1 time in word1 and 0 times in word2. The difference is 1. - &#39;e&#39; appears 2 times in word1 and 0 times in word2. The difference is 2. - &#39;f&#39; appears 1 time in word1 and 0 times in word2. The difference is 1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;cccddabba&quot;, word2 = &quot;babababab&quot; <strong>Output:</strong> true <strong>Explanation:</strong> The differences between the frequencies of each letter in word1 and word2 are at most 3: - &#39;a&#39; appears 2 times in word1 and 4 times in word2. The difference is 2. - &#39;b&#39; appears 2 times in word1 and 5 times in word2. The difference is 3. - &#39;c&#39; appears 3 times in word1 and 0 times in word2. The difference is 3. - &#39;d&#39; appears 2 times in word1 and 0 times in word2. The difference is 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == word1.length == word2.length</code></li> <li><code>1 &lt;= n &lt;= 100</code></li> <li><code>word1</code> and <code>word2</code> consist only of lowercase English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n bool checkAlmostEquivalent(string word1, string word2) {\n vector<int> freq(26,0);\n for(auto c: word1) freq[c - 'a']++;\n for(auto c: word2) freq[c - 'a']--;\n for(auto i: freq)\n if(i > 3 || i < -3) \n return false;\n return true;\n }\n};", "memory": "7800" }
2,177
<p>Two strings <code>word1</code> and <code>word2</code> are considered <strong>almost equivalent</strong> if the differences between the frequencies of each letter from <code>&#39;a&#39;</code> to <code>&#39;z&#39;</code> between <code>word1</code> and <code>word2</code> is <strong>at most</strong> <code>3</code>.</p> <p>Given two strings <code>word1</code> and <code>word2</code>, each of length <code>n</code>, return <code>true</code> <em>if </em><code>word1</code> <em>and</em> <code>word2</code> <em>are <strong>almost equivalent</strong>, or</em> <code>false</code> <em>otherwise</em>.</p> <p>The <strong>frequency</strong> of a letter <code>x</code> is the number of times it occurs in the string.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;aaaa&quot;, word2 = &quot;bccb&quot; <strong>Output:</strong> false <strong>Explanation:</strong> There are 4 &#39;a&#39;s in &quot;aaaa&quot; but 0 &#39;a&#39;s in &quot;bccb&quot;. The difference is 4, which is more than the allowed 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;abcdeef&quot;, word2 = &quot;abaaacc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> The differences between the frequencies of each letter in word1 and word2 are at most 3: - &#39;a&#39; appears 1 time in word1 and 4 times in word2. The difference is 3. - &#39;b&#39; appears 1 time in word1 and 1 time in word2. The difference is 0. - &#39;c&#39; appears 1 time in word1 and 2 times in word2. The difference is 1. - &#39;d&#39; appears 1 time in word1 and 0 times in word2. The difference is 1. - &#39;e&#39; appears 2 times in word1 and 0 times in word2. The difference is 2. - &#39;f&#39; appears 1 time in word1 and 0 times in word2. The difference is 1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;cccddabba&quot;, word2 = &quot;babababab&quot; <strong>Output:</strong> true <strong>Explanation:</strong> The differences between the frequencies of each letter in word1 and word2 are at most 3: - &#39;a&#39; appears 2 times in word1 and 4 times in word2. The difference is 2. - &#39;b&#39; appears 2 times in word1 and 5 times in word2. The difference is 3. - &#39;c&#39; appears 3 times in word1 and 0 times in word2. The difference is 3. - &#39;d&#39; appears 2 times in word1 and 0 times in word2. The difference is 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == word1.length == word2.length</code></li> <li><code>1 &lt;= n &lt;= 100</code></li> <li><code>word1</code> and <code>word2</code> consist only of lowercase English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n bool checkAlmostEquivalent(string word1, string word2) {\n int cnt[26]{};\n for (char& c : word1) {\n ++cnt[c - 'a'];\n }\n for (char& c : word2) {\n --cnt[c - 'a'];\n }\n for (int i = 0; i < 26; ++i) {\n if (abs(cnt[i]) > 3) {\n return false;\n }\n }\n return true;\n }\n};", "memory": "7900" }
2,177
<p>Two strings <code>word1</code> and <code>word2</code> are considered <strong>almost equivalent</strong> if the differences between the frequencies of each letter from <code>&#39;a&#39;</code> to <code>&#39;z&#39;</code> between <code>word1</code> and <code>word2</code> is <strong>at most</strong> <code>3</code>.</p> <p>Given two strings <code>word1</code> and <code>word2</code>, each of length <code>n</code>, return <code>true</code> <em>if </em><code>word1</code> <em>and</em> <code>word2</code> <em>are <strong>almost equivalent</strong>, or</em> <code>false</code> <em>otherwise</em>.</p> <p>The <strong>frequency</strong> of a letter <code>x</code> is the number of times it occurs in the string.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;aaaa&quot;, word2 = &quot;bccb&quot; <strong>Output:</strong> false <strong>Explanation:</strong> There are 4 &#39;a&#39;s in &quot;aaaa&quot; but 0 &#39;a&#39;s in &quot;bccb&quot;. The difference is 4, which is more than the allowed 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;abcdeef&quot;, word2 = &quot;abaaacc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> The differences between the frequencies of each letter in word1 and word2 are at most 3: - &#39;a&#39; appears 1 time in word1 and 4 times in word2. The difference is 3. - &#39;b&#39; appears 1 time in word1 and 1 time in word2. The difference is 0. - &#39;c&#39; appears 1 time in word1 and 2 times in word2. The difference is 1. - &#39;d&#39; appears 1 time in word1 and 0 times in word2. The difference is 1. - &#39;e&#39; appears 2 times in word1 and 0 times in word2. The difference is 2. - &#39;f&#39; appears 1 time in word1 and 0 times in word2. The difference is 1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;cccddabba&quot;, word2 = &quot;babababab&quot; <strong>Output:</strong> true <strong>Explanation:</strong> The differences between the frequencies of each letter in word1 and word2 are at most 3: - &#39;a&#39; appears 2 times in word1 and 4 times in word2. The difference is 2. - &#39;b&#39; appears 2 times in word1 and 5 times in word2. The difference is 3. - &#39;c&#39; appears 3 times in word1 and 0 times in word2. The difference is 3. - &#39;d&#39; appears 2 times in word1 and 0 times in word2. The difference is 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == word1.length == word2.length</code></li> <li><code>1 &lt;= n &lt;= 100</code></li> <li><code>word1</code> and <code>word2</code> consist only of lowercase English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n bool checkAlmostEquivalent(string word1, string word2) {\n vector<int> check(26, 0);\n for (int i = 0; i < word1.size(); i++)\n {\n check[word1[i] - 'a']++;\n }\n\n for (int i = 0; i < word2.size(); i++)\n {\n check[word2[i] - 'a']--;\n }\n\n for (int i = 0; i < check.size(); i++)\n {\n if (check[i] > 3 || check[i] < -3)\n {\n return false;\n }\n }\n return true;\n }\n};", "memory": "7900" }
2,177
<p>Two strings <code>word1</code> and <code>word2</code> are considered <strong>almost equivalent</strong> if the differences between the frequencies of each letter from <code>&#39;a&#39;</code> to <code>&#39;z&#39;</code> between <code>word1</code> and <code>word2</code> is <strong>at most</strong> <code>3</code>.</p> <p>Given two strings <code>word1</code> and <code>word2</code>, each of length <code>n</code>, return <code>true</code> <em>if </em><code>word1</code> <em>and</em> <code>word2</code> <em>are <strong>almost equivalent</strong>, or</em> <code>false</code> <em>otherwise</em>.</p> <p>The <strong>frequency</strong> of a letter <code>x</code> is the number of times it occurs in the string.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;aaaa&quot;, word2 = &quot;bccb&quot; <strong>Output:</strong> false <strong>Explanation:</strong> There are 4 &#39;a&#39;s in &quot;aaaa&quot; but 0 &#39;a&#39;s in &quot;bccb&quot;. The difference is 4, which is more than the allowed 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;abcdeef&quot;, word2 = &quot;abaaacc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> The differences between the frequencies of each letter in word1 and word2 are at most 3: - &#39;a&#39; appears 1 time in word1 and 4 times in word2. The difference is 3. - &#39;b&#39; appears 1 time in word1 and 1 time in word2. The difference is 0. - &#39;c&#39; appears 1 time in word1 and 2 times in word2. The difference is 1. - &#39;d&#39; appears 1 time in word1 and 0 times in word2. The difference is 1. - &#39;e&#39; appears 2 times in word1 and 0 times in word2. The difference is 2. - &#39;f&#39; appears 1 time in word1 and 0 times in word2. The difference is 1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;cccddabba&quot;, word2 = &quot;babababab&quot; <strong>Output:</strong> true <strong>Explanation:</strong> The differences between the frequencies of each letter in word1 and word2 are at most 3: - &#39;a&#39; appears 2 times in word1 and 4 times in word2. The difference is 2. - &#39;b&#39; appears 2 times in word1 and 5 times in word2. The difference is 3. - &#39;c&#39; appears 3 times in word1 and 0 times in word2. The difference is 3. - &#39;d&#39; appears 2 times in word1 and 0 times in word2. The difference is 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == word1.length == word2.length</code></li> <li><code>1 &lt;= n &lt;= 100</code></li> <li><code>word1</code> and <code>word2</code> consist only of lowercase English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n bool checkAlmostEquivalent(string s, string t) {\n int cnt[26] = {};\n for (char c : s) cnt[c - 'a']++;\n for (char c : t) cnt[c - 'a']--;\n for (int i = 0; i < 26; ++i) {\n if (abs(cnt[i]) > 3) return false;\n }\n return true;\n }\n};", "memory": "8000" }
2,177
<p>Two strings <code>word1</code> and <code>word2</code> are considered <strong>almost equivalent</strong> if the differences between the frequencies of each letter from <code>&#39;a&#39;</code> to <code>&#39;z&#39;</code> between <code>word1</code> and <code>word2</code> is <strong>at most</strong> <code>3</code>.</p> <p>Given two strings <code>word1</code> and <code>word2</code>, each of length <code>n</code>, return <code>true</code> <em>if </em><code>word1</code> <em>and</em> <code>word2</code> <em>are <strong>almost equivalent</strong>, or</em> <code>false</code> <em>otherwise</em>.</p> <p>The <strong>frequency</strong> of a letter <code>x</code> is the number of times it occurs in the string.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;aaaa&quot;, word2 = &quot;bccb&quot; <strong>Output:</strong> false <strong>Explanation:</strong> There are 4 &#39;a&#39;s in &quot;aaaa&quot; but 0 &#39;a&#39;s in &quot;bccb&quot;. The difference is 4, which is more than the allowed 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;abcdeef&quot;, word2 = &quot;abaaacc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> The differences between the frequencies of each letter in word1 and word2 are at most 3: - &#39;a&#39; appears 1 time in word1 and 4 times in word2. The difference is 3. - &#39;b&#39; appears 1 time in word1 and 1 time in word2. The difference is 0. - &#39;c&#39; appears 1 time in word1 and 2 times in word2. The difference is 1. - &#39;d&#39; appears 1 time in word1 and 0 times in word2. The difference is 1. - &#39;e&#39; appears 2 times in word1 and 0 times in word2. The difference is 2. - &#39;f&#39; appears 1 time in word1 and 0 times in word2. The difference is 1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;cccddabba&quot;, word2 = &quot;babababab&quot; <strong>Output:</strong> true <strong>Explanation:</strong> The differences between the frequencies of each letter in word1 and word2 are at most 3: - &#39;a&#39; appears 2 times in word1 and 4 times in word2. The difference is 2. - &#39;b&#39; appears 2 times in word1 and 5 times in word2. The difference is 3. - &#39;c&#39; appears 3 times in word1 and 0 times in word2. The difference is 3. - &#39;d&#39; appears 2 times in word1 and 0 times in word2. The difference is 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == word1.length == word2.length</code></li> <li><code>1 &lt;= n &lt;= 100</code></li> <li><code>word1</code> and <code>word2</code> consist only of lowercase English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n bool checkAlmostEquivalent(string word1, string word2) {\n vector<int> check(26,0);\n for(int i=0;i<word1.size();i++){\n check[word1[i]-'a']++;\n }\n\n for (int i=0;i<word2.size();i++){\n check[word2[i]-'a']--;\n }\n\n for(int i=0;i<check.size();i++){\n cout<<check[i]<<\" \";\n if(check[i]>3 || check[i]<-3){\n return false;\n }\n }\n return true;\n }\n};", "memory": "8000" }
2,177
<p>Two strings <code>word1</code> and <code>word2</code> are considered <strong>almost equivalent</strong> if the differences between the frequencies of each letter from <code>&#39;a&#39;</code> to <code>&#39;z&#39;</code> between <code>word1</code> and <code>word2</code> is <strong>at most</strong> <code>3</code>.</p> <p>Given two strings <code>word1</code> and <code>word2</code>, each of length <code>n</code>, return <code>true</code> <em>if </em><code>word1</code> <em>and</em> <code>word2</code> <em>are <strong>almost equivalent</strong>, or</em> <code>false</code> <em>otherwise</em>.</p> <p>The <strong>frequency</strong> of a letter <code>x</code> is the number of times it occurs in the string.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;aaaa&quot;, word2 = &quot;bccb&quot; <strong>Output:</strong> false <strong>Explanation:</strong> There are 4 &#39;a&#39;s in &quot;aaaa&quot; but 0 &#39;a&#39;s in &quot;bccb&quot;. The difference is 4, which is more than the allowed 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;abcdeef&quot;, word2 = &quot;abaaacc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> The differences between the frequencies of each letter in word1 and word2 are at most 3: - &#39;a&#39; appears 1 time in word1 and 4 times in word2. The difference is 3. - &#39;b&#39; appears 1 time in word1 and 1 time in word2. The difference is 0. - &#39;c&#39; appears 1 time in word1 and 2 times in word2. The difference is 1. - &#39;d&#39; appears 1 time in word1 and 0 times in word2. The difference is 1. - &#39;e&#39; appears 2 times in word1 and 0 times in word2. The difference is 2. - &#39;f&#39; appears 1 time in word1 and 0 times in word2. The difference is 1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;cccddabba&quot;, word2 = &quot;babababab&quot; <strong>Output:</strong> true <strong>Explanation:</strong> The differences between the frequencies of each letter in word1 and word2 are at most 3: - &#39;a&#39; appears 2 times in word1 and 4 times in word2. The difference is 2. - &#39;b&#39; appears 2 times in word1 and 5 times in word2. The difference is 3. - &#39;c&#39; appears 3 times in word1 and 0 times in word2. The difference is 3. - &#39;d&#39; appears 2 times in word1 and 0 times in word2. The difference is 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == word1.length == word2.length</code></li> <li><code>1 &lt;= n &lt;= 100</code></li> <li><code>word1</code> and <code>word2</code> consist only of lowercase English letters.</li> </ul>
1
{ "code": "class Solution {\npublic:\n bool checkAlmostEquivalent(string word1, string word2) {\n vector<int> freq1(26,0);\n vector<int> freq2(26,0);\n for(int i=0;i<word1.size();i++){\n freq1[word1[i]-'a']++;\n freq2[word2[i]-'a']++;\n }\n for(int i=0;i<26;i++){\n if(abs(freq1[i]-freq2[i])>3){\n return false;\n }\n }\n return true;\n }\n};", "memory": "8100" }
2,177
<p>Two strings <code>word1</code> and <code>word2</code> are considered <strong>almost equivalent</strong> if the differences between the frequencies of each letter from <code>&#39;a&#39;</code> to <code>&#39;z&#39;</code> between <code>word1</code> and <code>word2</code> is <strong>at most</strong> <code>3</code>.</p> <p>Given two strings <code>word1</code> and <code>word2</code>, each of length <code>n</code>, return <code>true</code> <em>if </em><code>word1</code> <em>and</em> <code>word2</code> <em>are <strong>almost equivalent</strong>, or</em> <code>false</code> <em>otherwise</em>.</p> <p>The <strong>frequency</strong> of a letter <code>x</code> is the number of times it occurs in the string.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;aaaa&quot;, word2 = &quot;bccb&quot; <strong>Output:</strong> false <strong>Explanation:</strong> There are 4 &#39;a&#39;s in &quot;aaaa&quot; but 0 &#39;a&#39;s in &quot;bccb&quot;. The difference is 4, which is more than the allowed 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;abcdeef&quot;, word2 = &quot;abaaacc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> The differences between the frequencies of each letter in word1 and word2 are at most 3: - &#39;a&#39; appears 1 time in word1 and 4 times in word2. The difference is 3. - &#39;b&#39; appears 1 time in word1 and 1 time in word2. The difference is 0. - &#39;c&#39; appears 1 time in word1 and 2 times in word2. The difference is 1. - &#39;d&#39; appears 1 time in word1 and 0 times in word2. The difference is 1. - &#39;e&#39; appears 2 times in word1 and 0 times in word2. The difference is 2. - &#39;f&#39; appears 1 time in word1 and 0 times in word2. The difference is 1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;cccddabba&quot;, word2 = &quot;babababab&quot; <strong>Output:</strong> true <strong>Explanation:</strong> The differences between the frequencies of each letter in word1 and word2 are at most 3: - &#39;a&#39; appears 2 times in word1 and 4 times in word2. The difference is 2. - &#39;b&#39; appears 2 times in word1 and 5 times in word2. The difference is 3. - &#39;c&#39; appears 3 times in word1 and 0 times in word2. The difference is 3. - &#39;d&#39; appears 2 times in word1 and 0 times in word2. The difference is 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == word1.length == word2.length</code></li> <li><code>1 &lt;= n &lt;= 100</code></li> <li><code>word1</code> and <code>word2</code> consist only of lowercase English letters.</li> </ul>
2
{ "code": "class Solution {\nprivate:\n vector<int> get_chars(string w) {\n vector<int> char_count(26, 0);\n for (char c : w) {\n char_count[c - 'a'] += 1;\n }\n return char_count;\n }\npublic:\n bool checkAlmostEquivalent(string word1, string word2) {\n vector<int> word1_count = get_chars(word1);\n vector<int> word2_count = get_chars(word2);\n\n for (int i = 0; i < word1_count.size(); i++) {\n int diff = abs(word1_count[i] - word2_count[i]);\n if (diff > 3) {\n return false;\n }\n }\n return true;\n }\n};\n\n// L: length of word\n// time: O(L)\n// space: O(1)", "memory": "8200" }
2,177
<p>Two strings <code>word1</code> and <code>word2</code> are considered <strong>almost equivalent</strong> if the differences between the frequencies of each letter from <code>&#39;a&#39;</code> to <code>&#39;z&#39;</code> between <code>word1</code> and <code>word2</code> is <strong>at most</strong> <code>3</code>.</p> <p>Given two strings <code>word1</code> and <code>word2</code>, each of length <code>n</code>, return <code>true</code> <em>if </em><code>word1</code> <em>and</em> <code>word2</code> <em>are <strong>almost equivalent</strong>, or</em> <code>false</code> <em>otherwise</em>.</p> <p>The <strong>frequency</strong> of a letter <code>x</code> is the number of times it occurs in the string.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;aaaa&quot;, word2 = &quot;bccb&quot; <strong>Output:</strong> false <strong>Explanation:</strong> There are 4 &#39;a&#39;s in &quot;aaaa&quot; but 0 &#39;a&#39;s in &quot;bccb&quot;. The difference is 4, which is more than the allowed 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;abcdeef&quot;, word2 = &quot;abaaacc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> The differences between the frequencies of each letter in word1 and word2 are at most 3: - &#39;a&#39; appears 1 time in word1 and 4 times in word2. The difference is 3. - &#39;b&#39; appears 1 time in word1 and 1 time in word2. The difference is 0. - &#39;c&#39; appears 1 time in word1 and 2 times in word2. The difference is 1. - &#39;d&#39; appears 1 time in word1 and 0 times in word2. The difference is 1. - &#39;e&#39; appears 2 times in word1 and 0 times in word2. The difference is 2. - &#39;f&#39; appears 1 time in word1 and 0 times in word2. The difference is 1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;cccddabba&quot;, word2 = &quot;babababab&quot; <strong>Output:</strong> true <strong>Explanation:</strong> The differences between the frequencies of each letter in word1 and word2 are at most 3: - &#39;a&#39; appears 2 times in word1 and 4 times in word2. The difference is 2. - &#39;b&#39; appears 2 times in word1 and 5 times in word2. The difference is 3. - &#39;c&#39; appears 3 times in word1 and 0 times in word2. The difference is 3. - &#39;d&#39; appears 2 times in word1 and 0 times in word2. The difference is 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == word1.length == word2.length</code></li> <li><code>1 &lt;= n &lt;= 100</code></li> <li><code>word1</code> and <code>word2</code> consist only of lowercase English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n bool checkAlmostEquivalent(string str_1, string str_2) {\n int arr_1[130]={0};\n int arr_2[130]={0};\n for(int i=0;i<str_1.length();i++){\n int val = (int)str_1[i];\n arr_1[val]++;\n }\n for(int i=0;i<str_2.length();i++){\n int val = (int)str_2[i];\n arr_2[val]++;\n }\n\n for(int i=97;i<130;i++){\n int difference = abs(arr_1[i]-arr_2[i]);\n if(difference>=4){\n return false;\n }\n }\n\n return true;\n }\n};", "memory": "8300" }
2,177
<p>Two strings <code>word1</code> and <code>word2</code> are considered <strong>almost equivalent</strong> if the differences between the frequencies of each letter from <code>&#39;a&#39;</code> to <code>&#39;z&#39;</code> between <code>word1</code> and <code>word2</code> is <strong>at most</strong> <code>3</code>.</p> <p>Given two strings <code>word1</code> and <code>word2</code>, each of length <code>n</code>, return <code>true</code> <em>if </em><code>word1</code> <em>and</em> <code>word2</code> <em>are <strong>almost equivalent</strong>, or</em> <code>false</code> <em>otherwise</em>.</p> <p>The <strong>frequency</strong> of a letter <code>x</code> is the number of times it occurs in the string.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;aaaa&quot;, word2 = &quot;bccb&quot; <strong>Output:</strong> false <strong>Explanation:</strong> There are 4 &#39;a&#39;s in &quot;aaaa&quot; but 0 &#39;a&#39;s in &quot;bccb&quot;. The difference is 4, which is more than the allowed 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;abcdeef&quot;, word2 = &quot;abaaacc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> The differences between the frequencies of each letter in word1 and word2 are at most 3: - &#39;a&#39; appears 1 time in word1 and 4 times in word2. The difference is 3. - &#39;b&#39; appears 1 time in word1 and 1 time in word2. The difference is 0. - &#39;c&#39; appears 1 time in word1 and 2 times in word2. The difference is 1. - &#39;d&#39; appears 1 time in word1 and 0 times in word2. The difference is 1. - &#39;e&#39; appears 2 times in word1 and 0 times in word2. The difference is 2. - &#39;f&#39; appears 1 time in word1 and 0 times in word2. The difference is 1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;cccddabba&quot;, word2 = &quot;babababab&quot; <strong>Output:</strong> true <strong>Explanation:</strong> The differences between the frequencies of each letter in word1 and word2 are at most 3: - &#39;a&#39; appears 2 times in word1 and 4 times in word2. The difference is 2. - &#39;b&#39; appears 2 times in word1 and 5 times in word2. The difference is 3. - &#39;c&#39; appears 3 times in word1 and 0 times in word2. The difference is 3. - &#39;d&#39; appears 2 times in word1 and 0 times in word2. The difference is 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == word1.length == word2.length</code></li> <li><code>1 &lt;= n &lt;= 100</code></li> <li><code>word1</code> and <code>word2</code> consist only of lowercase English letters.</li> </ul>
2
{ "code": "#include <iostream>\n#include <set>\n#include <string>\n#include <cmath>\nclass Solution {\npublic:\n bool checkAlmostEquivalent(string w1, string w2) {\n set<char>a(w1.begin(),w1.end());\n set<char>b(w2.begin(),w2.end());\n set<char>al(a.begin(),a.end());\n al.insert(b.begin(),b.end());\n for(char i:al){\n int c=count(w1.begin(),w1.end(),i);\n int d=count(w2.begin(),w2.end(),i);\n if(abs(c-d)>3){\n return false;\n break;\n }\n\n }\n return true;\n }\n};", "memory": "8600" }
2,177
<p>Two strings <code>word1</code> and <code>word2</code> are considered <strong>almost equivalent</strong> if the differences between the frequencies of each letter from <code>&#39;a&#39;</code> to <code>&#39;z&#39;</code> between <code>word1</code> and <code>word2</code> is <strong>at most</strong> <code>3</code>.</p> <p>Given two strings <code>word1</code> and <code>word2</code>, each of length <code>n</code>, return <code>true</code> <em>if </em><code>word1</code> <em>and</em> <code>word2</code> <em>are <strong>almost equivalent</strong>, or</em> <code>false</code> <em>otherwise</em>.</p> <p>The <strong>frequency</strong> of a letter <code>x</code> is the number of times it occurs in the string.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;aaaa&quot;, word2 = &quot;bccb&quot; <strong>Output:</strong> false <strong>Explanation:</strong> There are 4 &#39;a&#39;s in &quot;aaaa&quot; but 0 &#39;a&#39;s in &quot;bccb&quot;. The difference is 4, which is more than the allowed 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;abcdeef&quot;, word2 = &quot;abaaacc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> The differences between the frequencies of each letter in word1 and word2 are at most 3: - &#39;a&#39; appears 1 time in word1 and 4 times in word2. The difference is 3. - &#39;b&#39; appears 1 time in word1 and 1 time in word2. The difference is 0. - &#39;c&#39; appears 1 time in word1 and 2 times in word2. The difference is 1. - &#39;d&#39; appears 1 time in word1 and 0 times in word2. The difference is 1. - &#39;e&#39; appears 2 times in word1 and 0 times in word2. The difference is 2. - &#39;f&#39; appears 1 time in word1 and 0 times in word2. The difference is 1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;cccddabba&quot;, word2 = &quot;babababab&quot; <strong>Output:</strong> true <strong>Explanation:</strong> The differences between the frequencies of each letter in word1 and word2 are at most 3: - &#39;a&#39; appears 2 times in word1 and 4 times in word2. The difference is 2. - &#39;b&#39; appears 2 times in word1 and 5 times in word2. The difference is 3. - &#39;c&#39; appears 3 times in word1 and 0 times in word2. The difference is 3. - &#39;d&#39; appears 2 times in word1 and 0 times in word2. The difference is 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == word1.length == word2.length</code></li> <li><code>1 &lt;= n &lt;= 100</code></li> <li><code>word1</code> and <code>word2</code> consist only of lowercase English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n bool checkAlmostEquivalent(string word1, string word2) {\n unordered_map<char,int>mp1;\n for(int i=0;i<word1.size();i++){\n mp1[word1[i]]++;\n }\n for(int i=0;i<word2.size();i++){\n mp1[word2[i]]--;\n }\n for(auto i:mp1){\n if(abs(i.second)>3){\n return false;\n }\n }\n return true;\n }\n};", "memory": "9000" }
2,177
<p>Two strings <code>word1</code> and <code>word2</code> are considered <strong>almost equivalent</strong> if the differences between the frequencies of each letter from <code>&#39;a&#39;</code> to <code>&#39;z&#39;</code> between <code>word1</code> and <code>word2</code> is <strong>at most</strong> <code>3</code>.</p> <p>Given two strings <code>word1</code> and <code>word2</code>, each of length <code>n</code>, return <code>true</code> <em>if </em><code>word1</code> <em>and</em> <code>word2</code> <em>are <strong>almost equivalent</strong>, or</em> <code>false</code> <em>otherwise</em>.</p> <p>The <strong>frequency</strong> of a letter <code>x</code> is the number of times it occurs in the string.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;aaaa&quot;, word2 = &quot;bccb&quot; <strong>Output:</strong> false <strong>Explanation:</strong> There are 4 &#39;a&#39;s in &quot;aaaa&quot; but 0 &#39;a&#39;s in &quot;bccb&quot;. The difference is 4, which is more than the allowed 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;abcdeef&quot;, word2 = &quot;abaaacc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> The differences between the frequencies of each letter in word1 and word2 are at most 3: - &#39;a&#39; appears 1 time in word1 and 4 times in word2. The difference is 3. - &#39;b&#39; appears 1 time in word1 and 1 time in word2. The difference is 0. - &#39;c&#39; appears 1 time in word1 and 2 times in word2. The difference is 1. - &#39;d&#39; appears 1 time in word1 and 0 times in word2. The difference is 1. - &#39;e&#39; appears 2 times in word1 and 0 times in word2. The difference is 2. - &#39;f&#39; appears 1 time in word1 and 0 times in word2. The difference is 1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;cccddabba&quot;, word2 = &quot;babababab&quot; <strong>Output:</strong> true <strong>Explanation:</strong> The differences between the frequencies of each letter in word1 and word2 are at most 3: - &#39;a&#39; appears 2 times in word1 and 4 times in word2. The difference is 2. - &#39;b&#39; appears 2 times in word1 and 5 times in word2. The difference is 3. - &#39;c&#39; appears 3 times in word1 and 0 times in word2. The difference is 3. - &#39;d&#39; appears 2 times in word1 and 0 times in word2. The difference is 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == word1.length == word2.length</code></li> <li><code>1 &lt;= n &lt;= 100</code></li> <li><code>word1</code> and <code>word2</code> consist only of lowercase English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n bool checkAlmostEquivalent(string word1, string word2) {\n unordered_map<char,int>mp1;\n for(char ch:word1){\n mp1[ch]++;\n }\n for(char ch:word2){\n mp1[ch]--;\n }\n for(auto ele:mp1){\n if(abs(ele.second)>3) return false;\n }\n return true;\n\n\n\n }\n};", "memory": "9100" }
2,177
<p>Two strings <code>word1</code> and <code>word2</code> are considered <strong>almost equivalent</strong> if the differences between the frequencies of each letter from <code>&#39;a&#39;</code> to <code>&#39;z&#39;</code> between <code>word1</code> and <code>word2</code> is <strong>at most</strong> <code>3</code>.</p> <p>Given two strings <code>word1</code> and <code>word2</code>, each of length <code>n</code>, return <code>true</code> <em>if </em><code>word1</code> <em>and</em> <code>word2</code> <em>are <strong>almost equivalent</strong>, or</em> <code>false</code> <em>otherwise</em>.</p> <p>The <strong>frequency</strong> of a letter <code>x</code> is the number of times it occurs in the string.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;aaaa&quot;, word2 = &quot;bccb&quot; <strong>Output:</strong> false <strong>Explanation:</strong> There are 4 &#39;a&#39;s in &quot;aaaa&quot; but 0 &#39;a&#39;s in &quot;bccb&quot;. The difference is 4, which is more than the allowed 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;abcdeef&quot;, word2 = &quot;abaaacc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> The differences between the frequencies of each letter in word1 and word2 are at most 3: - &#39;a&#39; appears 1 time in word1 and 4 times in word2. The difference is 3. - &#39;b&#39; appears 1 time in word1 and 1 time in word2. The difference is 0. - &#39;c&#39; appears 1 time in word1 and 2 times in word2. The difference is 1. - &#39;d&#39; appears 1 time in word1 and 0 times in word2. The difference is 1. - &#39;e&#39; appears 2 times in word1 and 0 times in word2. The difference is 2. - &#39;f&#39; appears 1 time in word1 and 0 times in word2. The difference is 1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;cccddabba&quot;, word2 = &quot;babababab&quot; <strong>Output:</strong> true <strong>Explanation:</strong> The differences between the frequencies of each letter in word1 and word2 are at most 3: - &#39;a&#39; appears 2 times in word1 and 4 times in word2. The difference is 2. - &#39;b&#39; appears 2 times in word1 and 5 times in word2. The difference is 3. - &#39;c&#39; appears 3 times in word1 and 0 times in word2. The difference is 3. - &#39;d&#39; appears 2 times in word1 and 0 times in word2. The difference is 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == word1.length == word2.length</code></li> <li><code>1 &lt;= n &lt;= 100</code></li> <li><code>word1</code> and <code>word2</code> consist only of lowercase English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n bool checkAlmostEquivalent(string word1, string word2) {\n unordered_map<char, int> count;\n\n for (char c : word1) count[c]++;\n for (char c : word2) count[c]--;\n\n for (auto it : count) {\n if (abs(it.second) > 3) return false;\n }\n\n return true;\n }\n};", "memory": "9200" }
2,177
<p>Two strings <code>word1</code> and <code>word2</code> are considered <strong>almost equivalent</strong> if the differences between the frequencies of each letter from <code>&#39;a&#39;</code> to <code>&#39;z&#39;</code> between <code>word1</code> and <code>word2</code> is <strong>at most</strong> <code>3</code>.</p> <p>Given two strings <code>word1</code> and <code>word2</code>, each of length <code>n</code>, return <code>true</code> <em>if </em><code>word1</code> <em>and</em> <code>word2</code> <em>are <strong>almost equivalent</strong>, or</em> <code>false</code> <em>otherwise</em>.</p> <p>The <strong>frequency</strong> of a letter <code>x</code> is the number of times it occurs in the string.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;aaaa&quot;, word2 = &quot;bccb&quot; <strong>Output:</strong> false <strong>Explanation:</strong> There are 4 &#39;a&#39;s in &quot;aaaa&quot; but 0 &#39;a&#39;s in &quot;bccb&quot;. The difference is 4, which is more than the allowed 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;abcdeef&quot;, word2 = &quot;abaaacc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> The differences between the frequencies of each letter in word1 and word2 are at most 3: - &#39;a&#39; appears 1 time in word1 and 4 times in word2. The difference is 3. - &#39;b&#39; appears 1 time in word1 and 1 time in word2. The difference is 0. - &#39;c&#39; appears 1 time in word1 and 2 times in word2. The difference is 1. - &#39;d&#39; appears 1 time in word1 and 0 times in word2. The difference is 1. - &#39;e&#39; appears 2 times in word1 and 0 times in word2. The difference is 2. - &#39;f&#39; appears 1 time in word1 and 0 times in word2. The difference is 1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;cccddabba&quot;, word2 = &quot;babababab&quot; <strong>Output:</strong> true <strong>Explanation:</strong> The differences between the frequencies of each letter in word1 and word2 are at most 3: - &#39;a&#39; appears 2 times in word1 and 4 times in word2. The difference is 2. - &#39;b&#39; appears 2 times in word1 and 5 times in word2. The difference is 3. - &#39;c&#39; appears 3 times in word1 and 0 times in word2. The difference is 3. - &#39;d&#39; appears 2 times in word1 and 0 times in word2. The difference is 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == word1.length == word2.length</code></li> <li><code>1 &lt;= n &lt;= 100</code></li> <li><code>word1</code> and <code>word2</code> consist only of lowercase English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n bool checkAlmostEquivalent(string word1, string word2) {\n unordered_map<char,int>mp1,mp2;\n for(int i=0;i<word1.length();i++){\n mp1[word1[i]]++;\n }\n for(int i=0;i<word2.length();i++){\n mp2[word2[i]]++;\n }\n bool ans=true;\n for(auto it:mp1){\n if(mp2.find(it.first)!=mp2.end()){\n if(abs(mp1[it.first]-mp2[it.first])>3){\n ans=false;\n break;\n }\n }\n else{\n if(mp1[it.first]>3)ans=false;\n }\n }\n for(auto it:mp2){\n if(mp1.find(it.first)!=mp1.end()){\n if(abs(mp1[it.first]-mp2[it.first])>3){\n ans=false;\n break;\n }\n }\n else{\n if(mp2[it.first]>3)ans=false;\n }\n }\n return ans;\n \n \n }\n};", "memory": "9300" }
2,177
<p>Two strings <code>word1</code> and <code>word2</code> are considered <strong>almost equivalent</strong> if the differences between the frequencies of each letter from <code>&#39;a&#39;</code> to <code>&#39;z&#39;</code> between <code>word1</code> and <code>word2</code> is <strong>at most</strong> <code>3</code>.</p> <p>Given two strings <code>word1</code> and <code>word2</code>, each of length <code>n</code>, return <code>true</code> <em>if </em><code>word1</code> <em>and</em> <code>word2</code> <em>are <strong>almost equivalent</strong>, or</em> <code>false</code> <em>otherwise</em>.</p> <p>The <strong>frequency</strong> of a letter <code>x</code> is the number of times it occurs in the string.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;aaaa&quot;, word2 = &quot;bccb&quot; <strong>Output:</strong> false <strong>Explanation:</strong> There are 4 &#39;a&#39;s in &quot;aaaa&quot; but 0 &#39;a&#39;s in &quot;bccb&quot;. The difference is 4, which is more than the allowed 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;abcdeef&quot;, word2 = &quot;abaaacc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> The differences between the frequencies of each letter in word1 and word2 are at most 3: - &#39;a&#39; appears 1 time in word1 and 4 times in word2. The difference is 3. - &#39;b&#39; appears 1 time in word1 and 1 time in word2. The difference is 0. - &#39;c&#39; appears 1 time in word1 and 2 times in word2. The difference is 1. - &#39;d&#39; appears 1 time in word1 and 0 times in word2. The difference is 1. - &#39;e&#39; appears 2 times in word1 and 0 times in word2. The difference is 2. - &#39;f&#39; appears 1 time in word1 and 0 times in word2. The difference is 1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;cccddabba&quot;, word2 = &quot;babababab&quot; <strong>Output:</strong> true <strong>Explanation:</strong> The differences between the frequencies of each letter in word1 and word2 are at most 3: - &#39;a&#39; appears 2 times in word1 and 4 times in word2. The difference is 2. - &#39;b&#39; appears 2 times in word1 and 5 times in word2. The difference is 3. - &#39;c&#39; appears 3 times in word1 and 0 times in word2. The difference is 3. - &#39;d&#39; appears 2 times in word1 and 0 times in word2. The difference is 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == word1.length == word2.length</code></li> <li><code>1 &lt;= n &lt;= 100</code></li> <li><code>word1</code> and <code>word2</code> consist only of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n bool checkAlmostEquivalent(string word1, string word2) {\n unordered_map<char,int>mp1,mp2;\n for(int i=0;i<word1.length();i++){\n mp1[word1[i]]++;\n }\n for(int i=0;i<word2.length();i++){\n mp2[word2[i]]++;\n }\n bool ans=true;\n for(auto it:mp1){\n if(mp2.find(it.first)!=mp2.end()){\n if(abs(mp1[it.first]-mp2[it.first])>3){\n ans=false;\n break;\n }\n }\n else{\n if(mp1[it.first]>3)ans=false;\n }\n }\n for(auto it:mp2){\n if(mp1.find(it.first)!=mp1.end()){\n if(abs(mp1[it.first]-mp2[it.first])>3){\n ans=false;\n break;\n }\n }\n else{\n if(mp2[it.first]>3)ans=false;\n }\n }\n return ans;\n \n \n }\n};", "memory": "9400" }
1,906
<p>You are given <code>nums</code>, an array of positive integers of size <code>2 * n</code>. You must perform <code>n</code> operations on this array.</p> <p>In the <code>i<sup>th</sup></code> operation <strong>(1-indexed)</strong>, you will:</p> <ul> <li>Choose two elements, <code>x</code> and <code>y</code>.</li> <li>Receive a score of <code>i * gcd(x, y)</code>.</li> <li>Remove <code>x</code> and <code>y</code> from <code>nums</code>.</li> </ul> <p>Return <em>the maximum score you can receive after performing </em><code>n</code><em> operations.</em></p> <p>The function <code>gcd(x, y)</code> is the greatest common divisor of <code>x</code> and <code>y</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2] <strong>Output:</strong> 1 <strong>Explanation:</strong>&nbsp;The optimal choice of operations is: (1 * gcd(1, 2)) = 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,6,8] <strong>Output:</strong> 11 <strong>Explanation:</strong>&nbsp;The optimal choice of operations is: (1 * gcd(3, 6)) + (2 * gcd(4, 8)) = 3 + 8 = 11 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5,6] <strong>Output:</strong> 14 <strong>Explanation:</strong>&nbsp;The optimal choice of operations is: (1 * gcd(1, 5)) + (2 * gcd(2, 4)) + (3 * gcd(3, 6)) = 1 + 4 + 9 = 14 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 7</code></li> <li><code>nums.length == 2 * n</code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> </ul>
2
{ "code": "class Solution {\n unordered_map<string,int>dp;\n int solve(vector<int>&nums, int ind, int taken){\n int n = nums.size();\n int score = 0;\n string key = to_string(ind) + '%' + to_string(taken);\n if(dp.count(key))return dp[key];\n for(int i =0;i<n;i++){\n for(int j =i+1;j<n;j++){\n if(!(taken & (1<<i)) && !(taken & (1<<j))){\n int temp = (taken | (1<<j) | (1<<i));\n score = max(score,ind*__gcd(nums[i],nums[j]) + solve(nums,ind+1,temp));\n }\n }\n }\n return dp[key] = score;\n }\npublic:\n int maxScore(vector<int>& nums) {\n return solve(nums,1,0);\n }\n};", "memory": "31461" }
1,906
<p>You are given <code>nums</code>, an array of positive integers of size <code>2 * n</code>. You must perform <code>n</code> operations on this array.</p> <p>In the <code>i<sup>th</sup></code> operation <strong>(1-indexed)</strong>, you will:</p> <ul> <li>Choose two elements, <code>x</code> and <code>y</code>.</li> <li>Receive a score of <code>i * gcd(x, y)</code>.</li> <li>Remove <code>x</code> and <code>y</code> from <code>nums</code>.</li> </ul> <p>Return <em>the maximum score you can receive after performing </em><code>n</code><em> operations.</em></p> <p>The function <code>gcd(x, y)</code> is the greatest common divisor of <code>x</code> and <code>y</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2] <strong>Output:</strong> 1 <strong>Explanation:</strong>&nbsp;The optimal choice of operations is: (1 * gcd(1, 2)) = 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,6,8] <strong>Output:</strong> 11 <strong>Explanation:</strong>&nbsp;The optimal choice of operations is: (1 * gcd(3, 6)) + (2 * gcd(4, 8)) = 3 + 8 = 11 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5,6] <strong>Output:</strong> 14 <strong>Explanation:</strong>&nbsp;The optimal choice of operations is: (1 * gcd(1, 5)) + (2 * gcd(2, 4)) + (3 * gcd(3, 6)) = 1 + 4 + 9 = 14 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 7</code></li> <li><code>nums.length == 2 * n</code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> </ul>
2
{ "code": "class Solution {\n unordered_map<string,int>dp;\n int solve(vector<int>&nums, int ind, int taken){\n int n = nums.size();\n int score = 0;\n // no need to memoize ind : because to reach a particular taken\n // the number of steps/ind remains same\n // ex: {abcd} -> {cd} requires removal of 2 elements. irrespective of \n // whichever removed first.\n string key = to_string(taken);\n if(dp.count(key))return dp[key];\n for(int i =0;i<n;i++){\n for(int j =i+1;j<n;j++){\n if(!(taken & (1<<i)) && !(taken & (1<<j))){\n int temp = (taken | (1<<j) | (1<<i));\n score = max(score,ind*__gcd(nums[i],nums[j]) + solve(nums,ind+1,temp));\n }\n }\n }\n return dp[key] = score;\n }\npublic:\n int maxScore(vector<int>& nums) {\n return solve(nums,1,0);\n }\n};", "memory": "31461" }
1,906
<p>You are given <code>nums</code>, an array of positive integers of size <code>2 * n</code>. You must perform <code>n</code> operations on this array.</p> <p>In the <code>i<sup>th</sup></code> operation <strong>(1-indexed)</strong>, you will:</p> <ul> <li>Choose two elements, <code>x</code> and <code>y</code>.</li> <li>Receive a score of <code>i * gcd(x, y)</code>.</li> <li>Remove <code>x</code> and <code>y</code> from <code>nums</code>.</li> </ul> <p>Return <em>the maximum score you can receive after performing </em><code>n</code><em> operations.</em></p> <p>The function <code>gcd(x, y)</code> is the greatest common divisor of <code>x</code> and <code>y</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2] <strong>Output:</strong> 1 <strong>Explanation:</strong>&nbsp;The optimal choice of operations is: (1 * gcd(1, 2)) = 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,6,8] <strong>Output:</strong> 11 <strong>Explanation:</strong>&nbsp;The optimal choice of operations is: (1 * gcd(3, 6)) + (2 * gcd(4, 8)) = 3 + 8 = 11 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5,6] <strong>Output:</strong> 14 <strong>Explanation:</strong>&nbsp;The optimal choice of operations is: (1 * gcd(1, 5)) + (2 * gcd(2, 4)) + (3 * gcd(3, 6)) = 1 + 4 + 9 = 14 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 7</code></li> <li><code>nums.length == 2 * n</code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> </ul>
2
{ "code": "class Solution \n{\n public:\n int dp[1<<14];\n int gcd(int num1,int num2)\n {\n if(num2==0) return num1;\n return gcd(num2,num1%num2);\n }\n int dfs(vector<int>& nums,int state,int current)\n {\n if(current==0) return 0;\n if(dp[state]) return dp[state];\n int result=0,n=nums.size();\n \n vector<int> total;\n\t\t\n\t\t//remaining element\n for(int i=0;i<n;i++)\n if(state&(1<<i))\n total.push_back(i);\n\t\t\t\t\n \n for(int i=0;i<total.size();i++)\n for(int j=i+1;j<total.size();j++)\n result=max(result,gcd(nums[total[i]],nums[total[j]])*current+dfs(nums,(state&(~((1<<total[i])|(1<<total[j])))),current-1));\n \n dp[state]=result;\n return result;\n }\n int maxScore(vector<int>& nums) \n {\n memset(dp,0,sizeof(dp));\n return dfs(nums,(1<<14)-1,nums.size()/2);\n }\n};", "memory": "35508" }
1,906
<p>You are given <code>nums</code>, an array of positive integers of size <code>2 * n</code>. You must perform <code>n</code> operations on this array.</p> <p>In the <code>i<sup>th</sup></code> operation <strong>(1-indexed)</strong>, you will:</p> <ul> <li>Choose two elements, <code>x</code> and <code>y</code>.</li> <li>Receive a score of <code>i * gcd(x, y)</code>.</li> <li>Remove <code>x</code> and <code>y</code> from <code>nums</code>.</li> </ul> <p>Return <em>the maximum score you can receive after performing </em><code>n</code><em> operations.</em></p> <p>The function <code>gcd(x, y)</code> is the greatest common divisor of <code>x</code> and <code>y</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2] <strong>Output:</strong> 1 <strong>Explanation:</strong>&nbsp;The optimal choice of operations is: (1 * gcd(1, 2)) = 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,6,8] <strong>Output:</strong> 11 <strong>Explanation:</strong>&nbsp;The optimal choice of operations is: (1 * gcd(3, 6)) + (2 * gcd(4, 8)) = 3 + 8 = 11 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5,6] <strong>Output:</strong> 14 <strong>Explanation:</strong>&nbsp;The optimal choice of operations is: (1 * gcd(1, 5)) + (2 * gcd(2, 4)) + (3 * gcd(3, 6)) = 1 + 4 + 9 = 14 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 7</code></li> <li><code>nums.length == 2 * n</code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> </ul>
2
{ "code": "class Solution \n{\n public:\n int dp[1<<14];\n int gcd(int num1,int num2)\n {\n if(num2==0) return num1;\n return gcd(num2,num1%num2);\n }\n int dfs(vector<int>& nums,int state,int current)\n {\n if(current==0) return 0;\n if(dp[state]) return dp[state];\n int result=0,n=nums.size();\n \n vector<int> total;\n\t\t\n\t\t//remaining element\n for(int i=0;i<n;i++)\n if(state&(1<<i))\n total.push_back(i);\n\t\t\t\t\n \n for(int i=0;i<total.size();i++)\n for(int j=i+1;j<total.size();j++)\n result=max(result,gcd(nums[total[i]],nums[total[j]])*current+dfs(nums,(state&(~((1<<total[i])|(1<<total[j])))),current-1));\n \n dp[state]=result;\n return result;\n }\n int maxScore(vector<int>& nums) \n {\n memset(dp,0,sizeof(dp));\n return dfs(nums,(1<<14)-1,nums.size()/2);\n }\n};", "memory": "35508" }
1,906
<p>You are given <code>nums</code>, an array of positive integers of size <code>2 * n</code>. You must perform <code>n</code> operations on this array.</p> <p>In the <code>i<sup>th</sup></code> operation <strong>(1-indexed)</strong>, you will:</p> <ul> <li>Choose two elements, <code>x</code> and <code>y</code>.</li> <li>Receive a score of <code>i * gcd(x, y)</code>.</li> <li>Remove <code>x</code> and <code>y</code> from <code>nums</code>.</li> </ul> <p>Return <em>the maximum score you can receive after performing </em><code>n</code><em> operations.</em></p> <p>The function <code>gcd(x, y)</code> is the greatest common divisor of <code>x</code> and <code>y</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2] <strong>Output:</strong> 1 <strong>Explanation:</strong>&nbsp;The optimal choice of operations is: (1 * gcd(1, 2)) = 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,6,8] <strong>Output:</strong> 11 <strong>Explanation:</strong>&nbsp;The optimal choice of operations is: (1 * gcd(3, 6)) + (2 * gcd(4, 8)) = 3 + 8 = 11 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5,6] <strong>Output:</strong> 14 <strong>Explanation:</strong>&nbsp;The optimal choice of operations is: (1 * gcd(1, 5)) + (2 * gcd(2, 4)) + (3 * gcd(3, 6)) = 1 + 4 + 9 = 14 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 7</code></li> <li><code>nums.length == 2 * n</code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic: \n int n;\n map<vector<int>,int>mp;\n\n int solve(vector<int>&nums,vector<int>&visited,int operations)\n {\n int ans=0;\n int sum=0;\n\n if(mp.find(visited)!=mp.end())\n {\n return mp[visited];\n }\n\n for(int i=0;i<n-1;i++)\n {\n if(visited[i]==1)\n {\n continue;\n }\n for(int j=i+1;j<n;j++)\n {\n if(visited[j]==1)\n {\n continue;\n }\n\n visited[i]=1;\n visited[j]=1;\n\n int cost=operations*__gcd(nums[i],nums[j]);\n sum=cost+solve(nums,visited,operations+1);\n ans=max(ans,sum);\n\n visited[i]=0;\n visited[j]=0;\n }\n }\n mp[visited]=ans;\n return ans;\n }\n\n int maxScore(vector<int>& nums) \n {\n n=nums.size();\n vector<int>visited(n,0);\n return solve(nums,visited,1);\n \n }\n};", "memory": "39556" }
1,906
<p>You are given <code>nums</code>, an array of positive integers of size <code>2 * n</code>. You must perform <code>n</code> operations on this array.</p> <p>In the <code>i<sup>th</sup></code> operation <strong>(1-indexed)</strong>, you will:</p> <ul> <li>Choose two elements, <code>x</code> and <code>y</code>.</li> <li>Receive a score of <code>i * gcd(x, y)</code>.</li> <li>Remove <code>x</code> and <code>y</code> from <code>nums</code>.</li> </ul> <p>Return <em>the maximum score you can receive after performing </em><code>n</code><em> operations.</em></p> <p>The function <code>gcd(x, y)</code> is the greatest common divisor of <code>x</code> and <code>y</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2] <strong>Output:</strong> 1 <strong>Explanation:</strong>&nbsp;The optimal choice of operations is: (1 * gcd(1, 2)) = 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,6,8] <strong>Output:</strong> 11 <strong>Explanation:</strong>&nbsp;The optimal choice of operations is: (1 * gcd(3, 6)) + (2 * gcd(4, 8)) = 3 + 8 = 11 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5,6] <strong>Output:</strong> 14 <strong>Explanation:</strong>&nbsp;The optimal choice of operations is: (1 * gcd(1, 5)) + (2 * gcd(2, 4)) + (3 * gcd(3, 6)) = 1 + 4 + 9 = 14 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 7</code></li> <li><code>nums.length == 2 * n</code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int solve(vector<int>& nums, vector<int>& vis, int op, map<vector<int>, int>& dp) {\n if (dp.count(vis)) return dp[vis];\n int maxScore = 0;\n int n = nums.size();\n for (int i = 0; i < n - 1; i++) {\n if (vis[i]) continue;\n for (int j = i + 1; j < n; j++) {\n if (vis[j]) continue;\n vis[i] = 1;\n vis[j] = 1;\n int score = op * __gcd(nums[i], nums[j]);\n int rem_score = solve(nums, vis, op + 1, dp);\n vis[i] = 0;\n vis[j] = 0;\n maxScore = max(maxScore, score + rem_score);\n }\n }\n return dp[vis] = maxScore;\n }\n \n int maxScore(vector<int>& nums) {\n int n = nums.size();\n vector<int> vis(n, 0);\n map<vector<int>, int> dp;\n return solve(nums, vis, 1, dp);\n }\n};\n", "memory": "39556" }
1,906
<p>You are given <code>nums</code>, an array of positive integers of size <code>2 * n</code>. You must perform <code>n</code> operations on this array.</p> <p>In the <code>i<sup>th</sup></code> operation <strong>(1-indexed)</strong>, you will:</p> <ul> <li>Choose two elements, <code>x</code> and <code>y</code>.</li> <li>Receive a score of <code>i * gcd(x, y)</code>.</li> <li>Remove <code>x</code> and <code>y</code> from <code>nums</code>.</li> </ul> <p>Return <em>the maximum score you can receive after performing </em><code>n</code><em> operations.</em></p> <p>The function <code>gcd(x, y)</code> is the greatest common divisor of <code>x</code> and <code>y</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2] <strong>Output:</strong> 1 <strong>Explanation:</strong>&nbsp;The optimal choice of operations is: (1 * gcd(1, 2)) = 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,6,8] <strong>Output:</strong> 11 <strong>Explanation:</strong>&nbsp;The optimal choice of operations is: (1 * gcd(3, 6)) + (2 * gcd(4, 8)) = 3 + 8 = 11 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5,6] <strong>Output:</strong> 14 <strong>Explanation:</strong>&nbsp;The optimal choice of operations is: (1 * gcd(1, 5)) + (2 * gcd(2, 4)) + (3 * gcd(3, 6)) = 1 + 4 + 9 = 14 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 7</code></li> <li><code>nums.length == 2 * n</code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int n;\n \n int fun(int mask,int k, vector<int>& nums,vector<vector<int>> &dp) {\n if (mask == (1 << n) - 1) {\n return 0;\n }\n if(dp[mask][k]!=-1)\n {\n return dp[mask][k];\n }\n int ans = 0;\n for (int i = 0; i < n; i++) {\n if (mask & (1 << (n - i - 1))) {\n continue;\n }\n for (int j = i + 1; j < n; j++) {\n if (!(mask & (1 << (n - j - 1)))) {\n ans = max(ans, k*__gcd(nums[i], nums[j]) + fun(mask | (1 << (n - j - 1)) | (1 << (n - i - 1)),k+1, nums,dp));\n }\n }\n }\n \n return dp[mask][k]=ans;\n }\n \n int maxScore(vector<int>& nums) {\n n = nums.size();\n vector<vector<int>> dp((1<<n),vector<int>(nums.size()+1,-1));\n return fun(0,1,nums,dp);\n }\n};\n", "memory": "43603" }
1,906
<p>You are given <code>nums</code>, an array of positive integers of size <code>2 * n</code>. You must perform <code>n</code> operations on this array.</p> <p>In the <code>i<sup>th</sup></code> operation <strong>(1-indexed)</strong>, you will:</p> <ul> <li>Choose two elements, <code>x</code> and <code>y</code>.</li> <li>Receive a score of <code>i * gcd(x, y)</code>.</li> <li>Remove <code>x</code> and <code>y</code> from <code>nums</code>.</li> </ul> <p>Return <em>the maximum score you can receive after performing </em><code>n</code><em> operations.</em></p> <p>The function <code>gcd(x, y)</code> is the greatest common divisor of <code>x</code> and <code>y</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2] <strong>Output:</strong> 1 <strong>Explanation:</strong>&nbsp;The optimal choice of operations is: (1 * gcd(1, 2)) = 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,6,8] <strong>Output:</strong> 11 <strong>Explanation:</strong>&nbsp;The optimal choice of operations is: (1 * gcd(3, 6)) + (2 * gcd(4, 8)) = 3 + 8 = 11 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5,6] <strong>Output:</strong> 14 <strong>Explanation:</strong>&nbsp;The optimal choice of operations is: (1 * gcd(1, 5)) + (2 * gcd(2, 4)) + (3 * gcd(3, 6)) = 1 + 4 + 9 = 14 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 7</code></li> <li><code>nums.length == 2 * n</code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> </ul>
3
{ "code": "class Solution {\nprivate:\n int gcd(int a, int b){\n if(b==0)return a;\n return gcd(b, a%b);\n }\n\n int solve(vector<int>&nums, int op, int mask, vector<vector<int>>&dp){\n int n=nums.size();\n int req=n/2;\n if(op>req)return 0;\n\n if(dp[op][mask]!=-1)return dp[op][mask];\n\n int maxi=0;\n\n for(int i=0;i<n;i++){\n if(mask&(1<<i))continue;\n for(int j=i+1;j<n;j++){\n if(mask&(1<<j))continue;\n int newMask=mask|(1<<i)|(1<<j);\n maxi=max(maxi, gcd(nums[i], nums[j])*op+solve(nums, op+1, newMask, dp));\n }\n }\n\n return dp[op][mask] = maxi;\n }\n\npublic:\n int maxScore(vector<int>& nums) {\n int n=nums.size();\n vector<vector<int>>dp(n/2+1, vector<int>(1<<14, -1));;\n return solve(nums, 1, 0, dp);\n }\n};", "memory": "43603" }
1,906
<p>You are given <code>nums</code>, an array of positive integers of size <code>2 * n</code>. You must perform <code>n</code> operations on this array.</p> <p>In the <code>i<sup>th</sup></code> operation <strong>(1-indexed)</strong>, you will:</p> <ul> <li>Choose two elements, <code>x</code> and <code>y</code>.</li> <li>Receive a score of <code>i * gcd(x, y)</code>.</li> <li>Remove <code>x</code> and <code>y</code> from <code>nums</code>.</li> </ul> <p>Return <em>the maximum score you can receive after performing </em><code>n</code><em> operations.</em></p> <p>The function <code>gcd(x, y)</code> is the greatest common divisor of <code>x</code> and <code>y</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2] <strong>Output:</strong> 1 <strong>Explanation:</strong>&nbsp;The optimal choice of operations is: (1 * gcd(1, 2)) = 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,6,8] <strong>Output:</strong> 11 <strong>Explanation:</strong>&nbsp;The optimal choice of operations is: (1 * gcd(3, 6)) + (2 * gcd(4, 8)) = 3 + 8 = 11 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5,6] <strong>Output:</strong> 14 <strong>Explanation:</strong>&nbsp;The optimal choice of operations is: (1 * gcd(1, 5)) + (2 * gcd(2, 4)) + (3 * gcd(3, 6)) = 1 + 4 + 9 = 14 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 7</code></li> <li><code>nums.length == 2 * n</code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\nint gcd(int a, int b) {\n if (b == 0) return a;\n return gcd(b, a % b);\n}\n\n\nint fun(vector<int>& nums, int turn, int n, vector<vector<int>>& dp, int bit){\n\n if(turn == n/2 + 1){ // \n return 0;\n }\n if(dp[turn][bit] != -1){\n return dp[turn][bit];\n }\n int maxi = INT_MIN;\n \n\n /* for(int j=0;j<n;j++){\n for(int k=j+1;k<n;k++){\n if(!vis[j] && !vis[k]){\n vis[j] = 1;\n vis[k] = 1;\n maxi = max(maxi, gcd(nums[j], nums[k]) * turn + fun(nums, turn+1, n, vis));\n vis[k]=0;\n vis[j]=0;\n }\n }\n } */\n\n for(int j=0;j<n;j++){\n for(int k=j+1;k<n;k++){\n int temp = bit;\n\n if( !(bit&(1<<j)) && !(bit&(1<<k)) ){\n bit = (bit|(1<<j));\n bit = (bit|(1<<k));\n maxi = max(maxi, gcd(nums[j], nums[k]) * turn + fun(nums, turn+1, n, dp, bit));\n bit = temp;\n }\n }\n }\n\n return dp[turn][bit] = maxi;\n \n}\n\nint maxScore(vector<int>& nums) {\n int n=nums.size();\n vector<int> vis(n+1, 0);\n int bit = 0;\n vector<vector<int>> dp(n/2 + 2, vector<int>( pow(2, 14) + 1, -1));\n\n return fun(nums, 1, n, dp, bit);\n\n }\n};", "memory": "47651" }
1,906
<p>You are given <code>nums</code>, an array of positive integers of size <code>2 * n</code>. You must perform <code>n</code> operations on this array.</p> <p>In the <code>i<sup>th</sup></code> operation <strong>(1-indexed)</strong>, you will:</p> <ul> <li>Choose two elements, <code>x</code> and <code>y</code>.</li> <li>Receive a score of <code>i * gcd(x, y)</code>.</li> <li>Remove <code>x</code> and <code>y</code> from <code>nums</code>.</li> </ul> <p>Return <em>the maximum score you can receive after performing </em><code>n</code><em> operations.</em></p> <p>The function <code>gcd(x, y)</code> is the greatest common divisor of <code>x</code> and <code>y</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2] <strong>Output:</strong> 1 <strong>Explanation:</strong>&nbsp;The optimal choice of operations is: (1 * gcd(1, 2)) = 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,6,8] <strong>Output:</strong> 11 <strong>Explanation:</strong>&nbsp;The optimal choice of operations is: (1 * gcd(3, 6)) + (2 * gcd(4, 8)) = 3 + 8 = 11 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5,6] <strong>Output:</strong> 14 <strong>Explanation:</strong>&nbsp;The optimal choice of operations is: (1 * gcd(1, 5)) + (2 * gcd(2, 4)) + (3 * gcd(3, 6)) = 1 + 4 + 9 = 14 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 7</code></li> <li><code>nums.length == 2 * n</code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int n;\n vector<int> base;\n vector<vector<int>> dp;\n int maxScore(vector<int>& nums) {\n base = nums;\n n = base.size();\n dp = vector<vector<int>>(n, vector<int>(pow(2,n)+1,0));\n for (int i = 0; i < n; i++){\n for (int j = i+1; j < n; j++){\n dp[0][0 | (1<<i) | (1<<j)] = __gcd(base[i],base[j]);\n }\n }\n for (int i = 0; i < n-2; i += 2){\n for (int j = 0; j < pow(2,n); j++){\n if (dp[i][j] == 0) continue;\n vector<int> v;\n int num = j;\n for (int i = 0; i < n; i++){\n if (num%2==0) v.push_back(i);\n num >>= 1;\n }\n for (int a = 0; a < v.size(); a++){\n for (int b = a+1; b < v.size(); b++){\n // cout << dp[i][j] << \" \" << __gcd(base[v[a]],base[v[b]]) << \"\\n\";\n dp[i+2][j | (1<<v[a]) | (1<<v[b])] = max(dp[i+2][j | (1<<v[a]) | (1<<v[b])], dp[i][j] + (i+4)/2 * __gcd(base[v[a]],base[v[b]]));\n }\n }\n }\n // break;\n }\n return dp[n-2][pow(2,n)-1];\n }\n};", "memory": "51698" }
1,906
<p>You are given <code>nums</code>, an array of positive integers of size <code>2 * n</code>. You must perform <code>n</code> operations on this array.</p> <p>In the <code>i<sup>th</sup></code> operation <strong>(1-indexed)</strong>, you will:</p> <ul> <li>Choose two elements, <code>x</code> and <code>y</code>.</li> <li>Receive a score of <code>i * gcd(x, y)</code>.</li> <li>Remove <code>x</code> and <code>y</code> from <code>nums</code>.</li> </ul> <p>Return <em>the maximum score you can receive after performing </em><code>n</code><em> operations.</em></p> <p>The function <code>gcd(x, y)</code> is the greatest common divisor of <code>x</code> and <code>y</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2] <strong>Output:</strong> 1 <strong>Explanation:</strong>&nbsp;The optimal choice of operations is: (1 * gcd(1, 2)) = 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,6,8] <strong>Output:</strong> 11 <strong>Explanation:</strong>&nbsp;The optimal choice of operations is: (1 * gcd(3, 6)) + (2 * gcd(4, 8)) = 3 + 8 = 11 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5,6] <strong>Output:</strong> 14 <strong>Explanation:</strong>&nbsp;The optimal choice of operations is: (1 * gcd(1, 5)) + (2 * gcd(2, 4)) + (3 * gcd(3, 6)) = 1 + 4 + 9 = 14 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 7</code></li> <li><code>nums.length == 2 * n</code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> dp;\n int solve(int bm,int k,int n,vector<int>& nums){\n if(k==(n+1)) return 0;\n int m=2*n;\n int ans=0;\n if(dp[bm][k]!=-1) return dp[bm][k];\n for(int i=0;i<m;i++){\n int mask=(1<<i);\n if((mask & bm)==0){\n for(int j=i+1;j<m;j++){\n int maskj=(1<<j);\n if((maskj & bm)==0){\n ans=max(ans,(k*__gcd(nums[i],nums[j]))+solve((bm|mask)|maskj,k+1,n,nums));\n\n\n }\n }\n }\n }\n\n return dp[bm][k]=ans;\n }\n\n int maxScore(vector<int>& nums) {\n int n=nums.size()/2;\n dp.resize(pow(2,(n*2)+1),vector<int>(n+1,-1));\n return solve(0,1,n,nums);\n }\n};", "memory": "51698" }
1,906
<p>You are given <code>nums</code>, an array of positive integers of size <code>2 * n</code>. You must perform <code>n</code> operations on this array.</p> <p>In the <code>i<sup>th</sup></code> operation <strong>(1-indexed)</strong>, you will:</p> <ul> <li>Choose two elements, <code>x</code> and <code>y</code>.</li> <li>Receive a score of <code>i * gcd(x, y)</code>.</li> <li>Remove <code>x</code> and <code>y</code> from <code>nums</code>.</li> </ul> <p>Return <em>the maximum score you can receive after performing </em><code>n</code><em> operations.</em></p> <p>The function <code>gcd(x, y)</code> is the greatest common divisor of <code>x</code> and <code>y</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2] <strong>Output:</strong> 1 <strong>Explanation:</strong>&nbsp;The optimal choice of operations is: (1 * gcd(1, 2)) = 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,6,8] <strong>Output:</strong> 11 <strong>Explanation:</strong>&nbsp;The optimal choice of operations is: (1 * gcd(3, 6)) + (2 * gcd(4, 8)) = 3 + 8 = 11 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5,6] <strong>Output:</strong> 14 <strong>Explanation:</strong>&nbsp;The optimal choice of operations is: (1 * gcd(1, 5)) + (2 * gcd(2, 4)) + (3 * gcd(3, 6)) = 1 + 4 + 9 = 14 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 7</code></li> <li><code>nums.length == 2 * n</code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n \n int get(int optNo,int mask,vector<int> &nums, vector<vector<int>> &dp){\n if(optNo>nums.size()) return 0;\n \n if(dp[optNo][mask]!=-1) return dp[optNo][mask];\n \n int ans=0;\n for(int i=0;i<nums.size();i++){\n if(mask&(1<<i)){\n for(int j=0;j<nums.size();j++){\n if(j!=i && mask&(1<<j)){\n ans=max(ans,optNo*__gcd(nums[i],nums[j])+get(optNo+1,(mask^(1<<i)^(1<<j)),nums,dp));\n }\n }\n }\n }\n \n dp[optNo][mask]=ans;\n return ans;\n }\n \n int maxScore(vector<int>& nums) {\n int n=nums.size();\n vector<vector<int>> dp(n+1,vector<int> ((1<<14)+5,-1));\n \n return get(1,(1<<n)-1,nums,dp);\n }\n};", "memory": "55746" }
1,906
<p>You are given <code>nums</code>, an array of positive integers of size <code>2 * n</code>. You must perform <code>n</code> operations on this array.</p> <p>In the <code>i<sup>th</sup></code> operation <strong>(1-indexed)</strong>, you will:</p> <ul> <li>Choose two elements, <code>x</code> and <code>y</code>.</li> <li>Receive a score of <code>i * gcd(x, y)</code>.</li> <li>Remove <code>x</code> and <code>y</code> from <code>nums</code>.</li> </ul> <p>Return <em>the maximum score you can receive after performing </em><code>n</code><em> operations.</em></p> <p>The function <code>gcd(x, y)</code> is the greatest common divisor of <code>x</code> and <code>y</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2] <strong>Output:</strong> 1 <strong>Explanation:</strong>&nbsp;The optimal choice of operations is: (1 * gcd(1, 2)) = 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,6,8] <strong>Output:</strong> 11 <strong>Explanation:</strong>&nbsp;The optimal choice of operations is: (1 * gcd(3, 6)) + (2 * gcd(4, 8)) = 3 + 8 = 11 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5,6] <strong>Output:</strong> 14 <strong>Explanation:</strong>&nbsp;The optimal choice of operations is: (1 * gcd(1, 5)) + (2 * gcd(2, 4)) + (3 * gcd(3, 6)) = 1 + 4 + 9 = 14 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 7</code></li> <li><code>nums.length == 2 * n</code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> </ul>
3
{ "code": "typedef vector<int> vi;\ntypedef vector<vi> vvi;\n\nclass Solution {\n int gcd(int a, int b) {\n return b ? gcd(b, a % b) : a;\n }\n\n vvi gcdab;\n map<int, map<int, int> > dp; // operation, mask\n int all;\n\n int rec(int op, int mask) {\n // cout << op << \" \" << bitset<8>(mask) << endl;\n if (mask == all) return 0;\n if (dp[op].contains(mask)) return dp[op][mask];\n\n vi pos;\n vi idx;\n int bit = 1;\n int ix = 0;\n while (bit <= all) {\n if ((bit&mask)==0) {\n idx.push_back(ix);\n pos.push_back(bit);\n }\n bit <<=1;\n ix++;\n }\n\n int maxi = 0;\n for (int i = 0; i < pos.size(); i++) {\n for (int j = i + 1; j < pos.size(); j++) {\n maxi = max(maxi, rec(op+1, mask|pos[i]|pos[j]) + gcdab[idx[i]][idx[j]]*op);\n }\n }\n\n dp[op][mask] = maxi;\n return maxi;\n\n }\n\npublic:\n int maxScore(vector<int>& nums) {\n int speed = []() {ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);return 0;}();\n int n = nums.size();\n all = 1 << n;\n all--;\n gcdab = vvi(n, vi(n)); \n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n gcdab[i][j] = gcd(nums[i], nums[j]);\n }\n }\n\n return rec(1, 0);\n }\n};", "memory": "59793" }
1,906
<p>You are given <code>nums</code>, an array of positive integers of size <code>2 * n</code>. You must perform <code>n</code> operations on this array.</p> <p>In the <code>i<sup>th</sup></code> operation <strong>(1-indexed)</strong>, you will:</p> <ul> <li>Choose two elements, <code>x</code> and <code>y</code>.</li> <li>Receive a score of <code>i * gcd(x, y)</code>.</li> <li>Remove <code>x</code> and <code>y</code> from <code>nums</code>.</li> </ul> <p>Return <em>the maximum score you can receive after performing </em><code>n</code><em> operations.</em></p> <p>The function <code>gcd(x, y)</code> is the greatest common divisor of <code>x</code> and <code>y</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2] <strong>Output:</strong> 1 <strong>Explanation:</strong>&nbsp;The optimal choice of operations is: (1 * gcd(1, 2)) = 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,6,8] <strong>Output:</strong> 11 <strong>Explanation:</strong>&nbsp;The optimal choice of operations is: (1 * gcd(3, 6)) + (2 * gcd(4, 8)) = 3 + 8 = 11 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5,6] <strong>Output:</strong> 14 <strong>Explanation:</strong>&nbsp;The optimal choice of operations is: (1 * gcd(1, 5)) + (2 * gcd(2, 4)) + (3 * gcd(3, 6)) = 1 + 4 + 9 = 14 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 7</code></li> <li><code>nums.length == 2 * n</code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int sol(vector<int> &nums,int n,int round,int vis[],int &mask,vector<vector<int>> &dp)\n {\n if(round>n)\n {\n return 0;\n }\n if(dp[round][mask]!=-1)\n {\n return dp[round][mask];\n }\n int ans=0;\n for(int i=0;i<n;i++)\n {\n for(int j=i+1;j<n;j++)\n {\n if(!(mask>>i&1) && !(mask>>j&1))\n {\n mask+=1<<i;\n mask+=1<<j;\n ans=max(ans,round*(__gcd(nums[i],nums[j]))+sol(nums,n,round+1,vis,mask,dp));\n mask-=1<<i;\n mask-=1<<j;\n }\n }\n }\n return dp[round][mask]=ans;\n }\n int maxScore(vector<int>& nums) {\n int n=nums.size();\n int vis[n+1];\n int mask=0;\n vector<vector<int>> dp(n+1,vector<int> ((1<<(n+2))+1,-1));\n for(int i=0;i<n;i++) vis[i]=0; \n return sol(nums,n,1,vis,mask,dp);\n }\n};", "memory": "63841" }
1,906
<p>You are given <code>nums</code>, an array of positive integers of size <code>2 * n</code>. You must perform <code>n</code> operations on this array.</p> <p>In the <code>i<sup>th</sup></code> operation <strong>(1-indexed)</strong>, you will:</p> <ul> <li>Choose two elements, <code>x</code> and <code>y</code>.</li> <li>Receive a score of <code>i * gcd(x, y)</code>.</li> <li>Remove <code>x</code> and <code>y</code> from <code>nums</code>.</li> </ul> <p>Return <em>the maximum score you can receive after performing </em><code>n</code><em> operations.</em></p> <p>The function <code>gcd(x, y)</code> is the greatest common divisor of <code>x</code> and <code>y</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2] <strong>Output:</strong> 1 <strong>Explanation:</strong>&nbsp;The optimal choice of operations is: (1 * gcd(1, 2)) = 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,6,8] <strong>Output:</strong> 11 <strong>Explanation:</strong>&nbsp;The optimal choice of operations is: (1 * gcd(3, 6)) + (2 * gcd(4, 8)) = 3 + 8 = 11 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5,6] <strong>Output:</strong> 14 <strong>Explanation:</strong>&nbsp;The optimal choice of operations is: (1 * gcd(1, 5)) + (2 * gcd(2, 4)) + (3 * gcd(3, 6)) = 1 + 4 + 9 = 14 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 7</code></li> <li><code>nums.length == 2 * n</code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int maxscore(vector<int>& nums, int bit, int left,\n vector<vector<int>>& dp) {\n int n = nums.size();\n int result = 0;\n if (left == n / 2)\n return 0;\n\n if (dp[bit][left] != -1)\n return dp[bit][left];\n for (int i = 0; i < n; i++) {\n if (!(bit & (1 << i))) {\n int c = (bit | (1 << i));\n for (int j = 0; j < n; j++) {\n if (i != j && (!(bit & (1 << j)))) {\n\n result =\n max(result,\n ((left + 1) * (__gcd(nums[i], nums[j])) +\n maxscore(nums, (c | (1 << j)), left + 1, dp)));\n }\n }\n }\n }\n return dp[bit][left] = result;\n }\n int maxScore(vector<int>& nums) {\n int n = nums.size();\n int left = n / 2;\n vector<vector<int>> dp((1 << 14), vector<int>(left, -1));\n return maxscore(nums, 0, 0, dp);\n }\n};", "memory": "67888" }
1,906
<p>You are given <code>nums</code>, an array of positive integers of size <code>2 * n</code>. You must perform <code>n</code> operations on this array.</p> <p>In the <code>i<sup>th</sup></code> operation <strong>(1-indexed)</strong>, you will:</p> <ul> <li>Choose two elements, <code>x</code> and <code>y</code>.</li> <li>Receive a score of <code>i * gcd(x, y)</code>.</li> <li>Remove <code>x</code> and <code>y</code> from <code>nums</code>.</li> </ul> <p>Return <em>the maximum score you can receive after performing </em><code>n</code><em> operations.</em></p> <p>The function <code>gcd(x, y)</code> is the greatest common divisor of <code>x</code> and <code>y</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2] <strong>Output:</strong> 1 <strong>Explanation:</strong>&nbsp;The optimal choice of operations is: (1 * gcd(1, 2)) = 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,6,8] <strong>Output:</strong> 11 <strong>Explanation:</strong>&nbsp;The optimal choice of operations is: (1 * gcd(3, 6)) + (2 * gcd(4, 8)) = 3 + 8 = 11 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5,6] <strong>Output:</strong> 14 <strong>Explanation:</strong>&nbsp;The optimal choice of operations is: (1 * gcd(1, 5)) + (2 * gcd(2, 4)) + (3 * gcd(3, 6)) = 1 + 4 + 9 = 14 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 7</code></li> <li><code>nums.length == 2 * n</code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> </ul>
3
{ "code": "class Solution {\n int bt(vector<int>& nums, vector<vector<int>>& memo, int i, int mask)\n {\n if(i > nums.size()/2)\n return 0;\n\n if(memo[i][mask] != -1)\n return memo[i][mask];\n\n for(int x{}; x < nums.size()-1; ++x)\n {\n for(int y{x+1}; y < nums.size(); ++y)\n {\n if((mask & (1 << x)) || (mask & (1 << y)))\n continue;\n\n memo[i][mask] = max(memo[i][mask], i * gcd(nums[x],nums[y]) + \n bt(nums,memo,i+1,mask | (1 << x) | (1 << y)));\n }\n }\n\n return memo[i][mask];\n }\npublic:\n int maxScore(vector<int>& nums) {\n vector<vector<int>> memo(8, vector<int>((1<<15),-1));\n return bt(nums,memo,1,0);\n }\n};", "memory": "71936" }
1,906
<p>You are given <code>nums</code>, an array of positive integers of size <code>2 * n</code>. You must perform <code>n</code> operations on this array.</p> <p>In the <code>i<sup>th</sup></code> operation <strong>(1-indexed)</strong>, you will:</p> <ul> <li>Choose two elements, <code>x</code> and <code>y</code>.</li> <li>Receive a score of <code>i * gcd(x, y)</code>.</li> <li>Remove <code>x</code> and <code>y</code> from <code>nums</code>.</li> </ul> <p>Return <em>the maximum score you can receive after performing </em><code>n</code><em> operations.</em></p> <p>The function <code>gcd(x, y)</code> is the greatest common divisor of <code>x</code> and <code>y</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2] <strong>Output:</strong> 1 <strong>Explanation:</strong>&nbsp;The optimal choice of operations is: (1 * gcd(1, 2)) = 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,6,8] <strong>Output:</strong> 11 <strong>Explanation:</strong>&nbsp;The optimal choice of operations is: (1 * gcd(3, 6)) + (2 * gcd(4, 8)) = 3 + 8 = 11 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5,6] <strong>Output:</strong> 14 <strong>Explanation:</strong>&nbsp;The optimal choice of operations is: (1 * gcd(1, 5)) + (2 * gcd(2, 4)) + (3 * gcd(3, 6)) = 1 + 4 + 9 = 14 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 7</code></li> <li><code>nums.length == 2 * n</code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int maxScore(vector<int>& nums) {\n function<int(int&,int&)> gcd = [&](int& a, int& b)->int {\n if(a > b) return gcd(b, a);\n if(a == 0) return b;\n int c = b % a;\n return gcd(c, a);\n };\n int ans = 0;\n int n = nums.size();\n vector<int> g;\n int visited;\n function<void()> bt = [&]() {\n if(g.size() == n/2){\n vector<int> gc(g);\n sort(gc.begin(), gc.end());\n int cur = 0;\n for(int i = 0; i < n/2; i++) {\n cur += (i+1)*gc[i];\n }\n ans = max(ans, cur);\n return;\n } \n\n int id = 0;\n while((visited & (1<<id)) != 0) id++;\n \n for(int i = id+1; i < n; i++) {\n if((visited & (1<<i)) == 0) {\n g.push_back(gcd(nums[id], nums[i]));\n visited = visited ^ (1<<id) ^ (1<<i);\n bt();\n visited = visited ^ (1<<id) ^ (1<<i);\n g.pop_back();\n }\n }\n };\n bt();\n return ans;\n }\n};", "memory": "75983" }
1,906
<p>You are given <code>nums</code>, an array of positive integers of size <code>2 * n</code>. You must perform <code>n</code> operations on this array.</p> <p>In the <code>i<sup>th</sup></code> operation <strong>(1-indexed)</strong>, you will:</p> <ul> <li>Choose two elements, <code>x</code> and <code>y</code>.</li> <li>Receive a score of <code>i * gcd(x, y)</code>.</li> <li>Remove <code>x</code> and <code>y</code> from <code>nums</code>.</li> </ul> <p>Return <em>the maximum score you can receive after performing </em><code>n</code><em> operations.</em></p> <p>The function <code>gcd(x, y)</code> is the greatest common divisor of <code>x</code> and <code>y</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2] <strong>Output:</strong> 1 <strong>Explanation:</strong>&nbsp;The optimal choice of operations is: (1 * gcd(1, 2)) = 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,6,8] <strong>Output:</strong> 11 <strong>Explanation:</strong>&nbsp;The optimal choice of operations is: (1 * gcd(3, 6)) + (2 * gcd(4, 8)) = 3 + 8 = 11 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5,6] <strong>Output:</strong> 14 <strong>Explanation:</strong>&nbsp;The optimal choice of operations is: (1 * gcd(1, 5)) + (2 * gcd(2, 4)) + (3 * gcd(3, 6)) = 1 + 4 + 9 = 14 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 7</code></li> <li><code>nums.length == 2 * n</code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int maxScore(vector<int>& nums) {\n function<int(int&,int&)> gcd_ = [&](int& a, int& b)->int {\n if(a > b) return gcd_(b, a);\n if(a == 0) return b;\n int c = b % a;\n return gcd_(c, a);\n };\n int n = nums.size();\n\n vector<vector<int>> gcd(n, vector<int>(n));\n for(int i = 0; i < n; i++) {\n for(int j = 0; j < n; j++) {\n gcd[i][j] = gcd_(nums[i], nums[j]);\n }\n }\n\n int ans = 0;\n vector<int> g;\n int visited;\n function<void()> bt = [&]() {\n if(g.size() == n/2){\n vector<int> gc(g);\n sort(gc.begin(), gc.end());\n int cur = 0;\n for(int i = 0; i < n/2; i++) {\n cur += (i+1)*gc[i];\n }\n ans = max(ans, cur);\n return;\n } \n\n int id = 0;\n while((visited & (1<<id)) != 0) id++;\n \n for(int i = id+1; i < n; i++) {\n if((visited & (1<<i)) == 0) {\n g.push_back(gcd[id][i]);\n visited = visited ^ (1<<id) ^ (1<<i);\n bt();\n visited = visited ^ (1<<id) ^ (1<<i);\n g.pop_back();\n }\n }\n };\n bt();\n return ans;\n }\n};", "memory": "80031" }
1,906
<p>You are given <code>nums</code>, an array of positive integers of size <code>2 * n</code>. You must perform <code>n</code> operations on this array.</p> <p>In the <code>i<sup>th</sup></code> operation <strong>(1-indexed)</strong>, you will:</p> <ul> <li>Choose two elements, <code>x</code> and <code>y</code>.</li> <li>Receive a score of <code>i * gcd(x, y)</code>.</li> <li>Remove <code>x</code> and <code>y</code> from <code>nums</code>.</li> </ul> <p>Return <em>the maximum score you can receive after performing </em><code>n</code><em> operations.</em></p> <p>The function <code>gcd(x, y)</code> is the greatest common divisor of <code>x</code> and <code>y</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2] <strong>Output:</strong> 1 <strong>Explanation:</strong>&nbsp;The optimal choice of operations is: (1 * gcd(1, 2)) = 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,6,8] <strong>Output:</strong> 11 <strong>Explanation:</strong>&nbsp;The optimal choice of operations is: (1 * gcd(3, 6)) + (2 * gcd(4, 8)) = 3 + 8 = 11 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5,6] <strong>Output:</strong> 14 <strong>Explanation:</strong>&nbsp;The optimal choice of operations is: (1 * gcd(1, 5)) + (2 * gcd(2, 4)) + (3 * gcd(3, 6)) = 1 + 4 + 9 = 14 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 7</code></li> <li><code>nums.length == 2 * n</code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int maxScore(vector<int>& nums) {\n function<int(int&,int&)> gcd_ = [&](int& a, int& b)->int {\n if(a > b) return gcd_(b, a);\n if(a == 0) return b;\n int c = b % a;\n return gcd_(c, a);\n };\n int n = nums.size();\n\n vector<vector<int>> gcd(n, vector<int>(n));\n for(int i = 0; i < n; i++) {\n for(int j = 0; j < n; j++) {\n gcd[i][j] = gcd_(nums[i], nums[j]);\n }\n }\n\n int ans = 0;\n vector<int> g(n/2);\n int sz = 0;\n int visited;\n function<void()> bt = [&]() {\n if(sz == n/2){\n vector<int> gc(g);\n sort(gc.begin(), gc.end());\n int cur = 0;\n for(int i = 0; i < n/2; i++) {\n cur += (i+1)*gc[i];\n }\n ans = max(ans, cur);\n return;\n } \n\n int id = 0;\n while((visited & (1<<id)) != 0) id++;\n \n for(int i = id+1; i < n; i++) {\n if((visited & (1<<i)) == 0) {\n g[sz++] = gcd[id][i];\n visited = visited ^ (1<<id) ^ (1<<i);\n bt();\n visited = visited ^ (1<<id) ^ (1<<i);\n sz--;\n }\n }\n };\n bt();\n return ans;\n }\n};", "memory": "80031" }
1,906
<p>You are given <code>nums</code>, an array of positive integers of size <code>2 * n</code>. You must perform <code>n</code> operations on this array.</p> <p>In the <code>i<sup>th</sup></code> operation <strong>(1-indexed)</strong>, you will:</p> <ul> <li>Choose two elements, <code>x</code> and <code>y</code>.</li> <li>Receive a score of <code>i * gcd(x, y)</code>.</li> <li>Remove <code>x</code> and <code>y</code> from <code>nums</code>.</li> </ul> <p>Return <em>the maximum score you can receive after performing </em><code>n</code><em> operations.</em></p> <p>The function <code>gcd(x, y)</code> is the greatest common divisor of <code>x</code> and <code>y</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2] <strong>Output:</strong> 1 <strong>Explanation:</strong>&nbsp;The optimal choice of operations is: (1 * gcd(1, 2)) = 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,6,8] <strong>Output:</strong> 11 <strong>Explanation:</strong>&nbsp;The optimal choice of operations is: (1 * gcd(3, 6)) + (2 * gcd(4, 8)) = 3 + 8 = 11 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5,6] <strong>Output:</strong> 14 <strong>Explanation:</strong>&nbsp;The optimal choice of operations is: (1 * gcd(1, 5)) + (2 * gcd(2, 4)) + (3 * gcd(3, 6)) = 1 + 4 + 9 = 14 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 7</code></li> <li><code>nums.length == 2 * n</code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int ans = 0;\n vector<int> memo;\n void DFS(vector<int>& is_visited, vector<vector<int>>& gcd_table){\n int now = -1;\n for(int i=0; i<is_visited.size(); i++){\n if(!is_visited[i]){\n now = i;\n break;\n }\n }\n if(now == -1){\n int M = 0;\n vector<int> tmp = memo;\n sort(tmp.begin(), tmp.end());\n for(int i=0; i<tmp.size(); i++){\n M += tmp[i] * (i+1);\n }\n ans = max(ans, M);\n return;\n }\n is_visited[now] = 1;\n for(int i=0; i<is_visited.size(); i++){\n if(!is_visited[i]){\n is_visited[i] = 1;\n memo.push_back(gcd_table[now][i]);\n DFS(is_visited, gcd_table);\n memo.pop_back();\n is_visited[i] = 0;\n }\n }\n is_visited[now] = 0;\n }\n int maxScore(vector<int>& nums) {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n cout.tie(NULL);\n vector<vector<int>> gcd_table(nums.size(), vector<int>(nums.size()));\n vector<int> is_visited(nums.size(), 0);\n for(int i=0; i<nums.size(); i++){\n for(int j=i+1; j<nums.size(); j++){\n gcd_table[i][j] = gcd(nums[i], nums[j]);\n gcd_table[j][i] = gcd(nums[i], nums[j]);\n }\n }\n DFS(is_visited, gcd_table);\n return ans;\n }\n};", "memory": "84078" }
1,906
<p>You are given <code>nums</code>, an array of positive integers of size <code>2 * n</code>. You must perform <code>n</code> operations on this array.</p> <p>In the <code>i<sup>th</sup></code> operation <strong>(1-indexed)</strong>, you will:</p> <ul> <li>Choose two elements, <code>x</code> and <code>y</code>.</li> <li>Receive a score of <code>i * gcd(x, y)</code>.</li> <li>Remove <code>x</code> and <code>y</code> from <code>nums</code>.</li> </ul> <p>Return <em>the maximum score you can receive after performing </em><code>n</code><em> operations.</em></p> <p>The function <code>gcd(x, y)</code> is the greatest common divisor of <code>x</code> and <code>y</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2] <strong>Output:</strong> 1 <strong>Explanation:</strong>&nbsp;The optimal choice of operations is: (1 * gcd(1, 2)) = 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,6,8] <strong>Output:</strong> 11 <strong>Explanation:</strong>&nbsp;The optimal choice of operations is: (1 * gcd(3, 6)) + (2 * gcd(4, 8)) = 3 + 8 = 11 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5,6] <strong>Output:</strong> 14 <strong>Explanation:</strong>&nbsp;The optimal choice of operations is: (1 * gcd(1, 5)) + (2 * gcd(2, 4)) + (3 * gcd(3, 6)) = 1 + 4 + 9 = 14 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 7</code></li> <li><code>nums.length == 2 * n</code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int check(vector<int> pair)\n {\n int sum = 0;\n int n = pair.size();\n sort(pair.rbegin(), pair.rend());\n\n for(auto p: pair)\n {\n sum = sum + p*n;\n n--;\n }\n return sum;\n }\n void sol(int i,vector<int>& nums, int& m,vector<int>& pair, int& res,vector<bool>& vs)\n {\n if(pair.size() == (m/2))\n {\n res = max(res,check(pair));\n return;\n }\n if(vs[i])\n sol(i+1,nums,m,pair,res,vs);\n else{ \n vs[i] = true;\n for(int j =i+1;j<m;j++)\n {\n if(!vs[j])\n {\n vs[j] = true;\n pair.push_back(__gcd(nums[i],nums[j]));\n sol(i+1,nums,m,pair,res,vs);\n vs[j] = false;\n pair.pop_back();\n }\n \n }\n vs[i] = false;\n }\n }\n int maxScore(vector<int>& nums) {\n int m = nums.size();\n int n = m/2;\n vector<int> pair;\n int res = INT_MIN;\n vector<bool> vs(m, false);\n sol(0,nums,m,pair,res,vs);\n return res;\n }\n};", "memory": "88126" }
1,906
<p>You are given <code>nums</code>, an array of positive integers of size <code>2 * n</code>. You must perform <code>n</code> operations on this array.</p> <p>In the <code>i<sup>th</sup></code> operation <strong>(1-indexed)</strong>, you will:</p> <ul> <li>Choose two elements, <code>x</code> and <code>y</code>.</li> <li>Receive a score of <code>i * gcd(x, y)</code>.</li> <li>Remove <code>x</code> and <code>y</code> from <code>nums</code>.</li> </ul> <p>Return <em>the maximum score you can receive after performing </em><code>n</code><em> operations.</em></p> <p>The function <code>gcd(x, y)</code> is the greatest common divisor of <code>x</code> and <code>y</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2] <strong>Output:</strong> 1 <strong>Explanation:</strong>&nbsp;The optimal choice of operations is: (1 * gcd(1, 2)) = 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,6,8] <strong>Output:</strong> 11 <strong>Explanation:</strong>&nbsp;The optimal choice of operations is: (1 * gcd(3, 6)) + (2 * gcd(4, 8)) = 3 + 8 = 11 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5,6] <strong>Output:</strong> 14 <strong>Explanation:</strong>&nbsp;The optimal choice of operations is: (1 * gcd(1, 5)) + (2 * gcd(2, 4)) + (3 * gcd(3, 6)) = 1 + 4 + 9 = 14 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 7</code></li> <li><code>nums.length == 2 * n</code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int dfs(vector<int> &nums,int mask,vector<vector<int>> &dp,int op){\n\n if(op > nums.size()){\n return 0; \n }\n \n \n if(dp[mask][op] != -1){\n return dp[mask][op]; \n }\n\n int maxi = 0; \n\n for(int i = 0;i<nums.size();i++){\n for(int j = i+1;j<nums.size();j++){ \n \n\n if(((int)1 << i)&mask){\n continue;\n }\n if(((int)1 << j)&mask){\n continue;\n }\n\n int temp = mask; \n\n mask |= ((int)1 << i); \n mask |= ((int)1 << j); \n\n maxi = max(op*__gcd(nums[i],nums[j])+dfs(nums,mask,dp,op+1),maxi); \n\n mask = temp; \n\n }\n }\n\n return dp[mask][op] = maxi; \n \n }\n int maxScore(vector<int>& nums) {\n vector<vector<int>> dp(16387,vector<int>(nums.size()+1,-1)); \n int mask = 0; \n return dfs(nums,mask,dp,1); \n }\n};", "memory": "92173" }
1,906
<p>You are given <code>nums</code>, an array of positive integers of size <code>2 * n</code>. You must perform <code>n</code> operations on this array.</p> <p>In the <code>i<sup>th</sup></code> operation <strong>(1-indexed)</strong>, you will:</p> <ul> <li>Choose two elements, <code>x</code> and <code>y</code>.</li> <li>Receive a score of <code>i * gcd(x, y)</code>.</li> <li>Remove <code>x</code> and <code>y</code> from <code>nums</code>.</li> </ul> <p>Return <em>the maximum score you can receive after performing </em><code>n</code><em> operations.</em></p> <p>The function <code>gcd(x, y)</code> is the greatest common divisor of <code>x</code> and <code>y</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2] <strong>Output:</strong> 1 <strong>Explanation:</strong>&nbsp;The optimal choice of operations is: (1 * gcd(1, 2)) = 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,6,8] <strong>Output:</strong> 11 <strong>Explanation:</strong>&nbsp;The optimal choice of operations is: (1 * gcd(3, 6)) + (2 * gcd(4, 8)) = 3 + 8 = 11 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5,6] <strong>Output:</strong> 14 <strong>Explanation:</strong>&nbsp;The optimal choice of operations is: (1 * gcd(1, 5)) + (2 * gcd(2, 4)) + (3 * gcd(3, 6)) = 1 + 4 + 9 = 14 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 7</code></li> <li><code>nums.length == 2 * n</code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\nint fun(int mask,int cnt,vector<int>&nums,vector<vector<int>>&dp){\nif(mask==(1<<nums.size())-1)return 0;\nif(dp[mask][cnt]!=-1)return dp[mask][cnt];\nint ans=0;\nfor(int i=0;i<nums.size();i++){\n for(int j=i+1;j<nums.size();j++){\n if((mask & (1<<i))==0 && (mask & (1<<j))==0){\n ans=max(ans,fun(mask|(1<<i) |(1<<j) ,cnt+1,nums,dp)+cnt*gcd(nums[i],nums[j]));\n }\n }\n}\ndp[mask][cnt]=ans;\nreturn dp[mask][cnt];\n\n}\n int maxScore(vector<int>& nums) {\n vector<vector<int>>dp(pow(2,14),vector<int>(10,-1));\n return fun(0,1,nums,dp); \n }\n};", "memory": "96221" }
1,906
<p>You are given <code>nums</code>, an array of positive integers of size <code>2 * n</code>. You must perform <code>n</code> operations on this array.</p> <p>In the <code>i<sup>th</sup></code> operation <strong>(1-indexed)</strong>, you will:</p> <ul> <li>Choose two elements, <code>x</code> and <code>y</code>.</li> <li>Receive a score of <code>i * gcd(x, y)</code>.</li> <li>Remove <code>x</code> and <code>y</code> from <code>nums</code>.</li> </ul> <p>Return <em>the maximum score you can receive after performing </em><code>n</code><em> operations.</em></p> <p>The function <code>gcd(x, y)</code> is the greatest common divisor of <code>x</code> and <code>y</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2] <strong>Output:</strong> 1 <strong>Explanation:</strong>&nbsp;The optimal choice of operations is: (1 * gcd(1, 2)) = 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,6,8] <strong>Output:</strong> 11 <strong>Explanation:</strong>&nbsp;The optimal choice of operations is: (1 * gcd(3, 6)) + (2 * gcd(4, 8)) = 3 + 8 = 11 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5,6] <strong>Output:</strong> 14 <strong>Explanation:</strong>&nbsp;The optimal choice of operations is: (1 * gcd(1, 5)) + (2 * gcd(2, 4)) + (3 * gcd(3, 6)) = 1 + 4 + 9 = 14 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 7</code></li> <li><code>nums.length == 2 * n</code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int n;\n int f(vector<int>&nums,vector<bool>visited,int op,unordered_map<vector<bool>, int>& mp)\n { if(mp.find(visited)!=mp.end())\n return mp[visited];\n int maxscore=0;\n for(int i=0;i<n-1;i++)\n {\n if(visited[i])\n continue;\n for(int j=i+1;j<n;j++)\n {\n if(visited[j])\n continue;\n visited[i]=true;\n visited[j]=true;\n int curr= op* __gcd(nums[i], nums[j]);\n int rem= f(nums,visited,op+1,mp);\n maxscore= max(maxscore,curr+rem);\n visited[i]=false;\n visited[j]=false;\n }\n }\n return mp[visited]= maxscore;\n }\n int maxScore(vector<int>& nums) {\n n=nums.size();\n vector<bool>visited(n,false);\n unordered_map<vector<bool>,int>mp;\n int ans= f(nums,visited,1,mp);\n return ans; \n }\n};", "memory": "100268" }
1,906
<p>You are given <code>nums</code>, an array of positive integers of size <code>2 * n</code>. You must perform <code>n</code> operations on this array.</p> <p>In the <code>i<sup>th</sup></code> operation <strong>(1-indexed)</strong>, you will:</p> <ul> <li>Choose two elements, <code>x</code> and <code>y</code>.</li> <li>Receive a score of <code>i * gcd(x, y)</code>.</li> <li>Remove <code>x</code> and <code>y</code> from <code>nums</code>.</li> </ul> <p>Return <em>the maximum score you can receive after performing </em><code>n</code><em> operations.</em></p> <p>The function <code>gcd(x, y)</code> is the greatest common divisor of <code>x</code> and <code>y</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2] <strong>Output:</strong> 1 <strong>Explanation:</strong>&nbsp;The optimal choice of operations is: (1 * gcd(1, 2)) = 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,6,8] <strong>Output:</strong> 11 <strong>Explanation:</strong>&nbsp;The optimal choice of operations is: (1 * gcd(3, 6)) + (2 * gcd(4, 8)) = 3 + 8 = 11 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5,6] <strong>Output:</strong> 14 <strong>Explanation:</strong>&nbsp;The optimal choice of operations is: (1 * gcd(1, 5)) + (2 * gcd(2, 4)) + (3 * gcd(3, 6)) = 1 + 4 + 9 = 14 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 7</code></li> <li><code>nums.length == 2 * n</code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int n;\n unordered_map<vector<bool>,int>mp;\n int solve(vector<int>&nums,vector<bool>vis,int op){\n \n int maxScore = 0;\n \n if(mp.find(vis)!=mp.end()){\n return mp[vis];\n }\n for(int i=0;i<n-1;i++){\n if(vis[i]) continue;\n for(int j=i+1;j<n;j++){\n if(vis[j]) continue;\n\n vis[i]=true;\n vis[j]=true;\n\n int curr =op * __gcd(nums[i],nums[j]);\n int remScore = solve(nums,vis,op+1);\n maxScore=max(maxScore,curr+remScore);\n\n vis[i]=false;\n vis[j]=false;\n }\n }\n\n return mp[vis]= maxScore;\n }\n\n int maxScore(vector<int>& nums) {\n n=nums.size();\n \n vector<bool> vis(n,false);\n\n return solve(nums,vis,1);\n\n }\n};", "memory": "104316" }
1,906
<p>You are given <code>nums</code>, an array of positive integers of size <code>2 * n</code>. You must perform <code>n</code> operations on this array.</p> <p>In the <code>i<sup>th</sup></code> operation <strong>(1-indexed)</strong>, you will:</p> <ul> <li>Choose two elements, <code>x</code> and <code>y</code>.</li> <li>Receive a score of <code>i * gcd(x, y)</code>.</li> <li>Remove <code>x</code> and <code>y</code> from <code>nums</code>.</li> </ul> <p>Return <em>the maximum score you can receive after performing </em><code>n</code><em> operations.</em></p> <p>The function <code>gcd(x, y)</code> is the greatest common divisor of <code>x</code> and <code>y</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2] <strong>Output:</strong> 1 <strong>Explanation:</strong>&nbsp;The optimal choice of operations is: (1 * gcd(1, 2)) = 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,6,8] <strong>Output:</strong> 11 <strong>Explanation:</strong>&nbsp;The optimal choice of operations is: (1 * gcd(3, 6)) + (2 * gcd(4, 8)) = 3 + 8 = 11 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5,6] <strong>Output:</strong> 14 <strong>Explanation:</strong>&nbsp;The optimal choice of operations is: (1 * gcd(1, 5)) + (2 * gcd(2, 4)) + (3 * gcd(3, 6)) = 1 + 4 + 9 = 14 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 7</code></li> <li><code>nums.length == 2 * n</code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> memo;\n\n int helper(int a, int b) {\n if(b == 0) return a;\n\n return helper(b, a % b);\n }\n int dfs(vector<int>& nums, int k, int mask, int n) {\n if(k == n + 1) {\n return 0;\n }\n if(memo[k][mask] != -1) return memo[k][mask];\n int res =0;\n\n for(int i = 0; i < nums.size(); i++){\n if(((mask >> i) & 1) == 1) continue;\n mask = mask | (1 << i);\n for(int j =0; j < nums.size(); j++) {\n if(((mask >> j) & 1) == 1) continue;\n mask = mask | (1 << j);\n // cout << i <<\n res = max(res, k * helper(nums[i],nums[j]) + dfs(nums, k+1, mask, n));\n mask = mask ^ (1 << j);\n }\n mask = mask ^ (1 << i);\n }\n return memo[k][mask] = res;\n }\n\n int maxScore(vector<int>& nums) {\n int n = nums.size() / 2;\n memo.resize(n+1, vector<int>(1e5, -1));\n int mask = 0;\n return dfs(nums, 1, mask, nums.size() / 2);\n // return res;\n }\n}; ", "memory": "108363" }
1,906
<p>You are given <code>nums</code>, an array of positive integers of size <code>2 * n</code>. You must perform <code>n</code> operations on this array.</p> <p>In the <code>i<sup>th</sup></code> operation <strong>(1-indexed)</strong>, you will:</p> <ul> <li>Choose two elements, <code>x</code> and <code>y</code>.</li> <li>Receive a score of <code>i * gcd(x, y)</code>.</li> <li>Remove <code>x</code> and <code>y</code> from <code>nums</code>.</li> </ul> <p>Return <em>the maximum score you can receive after performing </em><code>n</code><em> operations.</em></p> <p>The function <code>gcd(x, y)</code> is the greatest common divisor of <code>x</code> and <code>y</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2] <strong>Output:</strong> 1 <strong>Explanation:</strong>&nbsp;The optimal choice of operations is: (1 * gcd(1, 2)) = 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,6,8] <strong>Output:</strong> 11 <strong>Explanation:</strong>&nbsp;The optimal choice of operations is: (1 * gcd(3, 6)) + (2 * gcd(4, 8)) = 3 + 8 = 11 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5,6] <strong>Output:</strong> 14 <strong>Explanation:</strong>&nbsp;The optimal choice of operations is: (1 * gcd(1, 5)) + (2 * gcd(2, 4)) + (3 * gcd(3, 6)) = 1 + 4 + 9 = 14 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 7</code></li> <li><code>nums.length == 2 * n</code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int fun(vector<int>&nums,long mask,int n,int ope,vector<vector<long>>&dp){\n if(dp[mask][ope]!=-1){\n return dp[mask][ope];\n }\n int maxi = 0;\n for(int i=0;i<n;i++){\n for(int j=i+1;j<n;j++){\n if( (mask & 1<<i) == 0 && (mask & 1<< j) == 0 ){\n mask = mask | (1<<i);\n mask = mask | (1<<j);\n maxi = max(maxi,fun(nums,mask,n,ope+1,dp)+(ope*__gcd(nums[i],nums[j])));\n mask = mask ^ (1<<i);\n mask = mask ^ (1<<j);\n }\n }\n }\n return dp[mask][ope] = maxi;\n }\n int maxScore(vector<int>& nums) {\n int n = nums.size();\n vector<int>vis(n+1,0);\n long mask = 1<<(n+1); \n vector<vector<long>>dp(1<<(n+2),vector<long>(n+1,-1));\n return fun(nums,mask,n,1,dp);\n }\n};", "memory": "112411" }
1,906
<p>You are given <code>nums</code>, an array of positive integers of size <code>2 * n</code>. You must perform <code>n</code> operations on this array.</p> <p>In the <code>i<sup>th</sup></code> operation <strong>(1-indexed)</strong>, you will:</p> <ul> <li>Choose two elements, <code>x</code> and <code>y</code>.</li> <li>Receive a score of <code>i * gcd(x, y)</code>.</li> <li>Remove <code>x</code> and <code>y</code> from <code>nums</code>.</li> </ul> <p>Return <em>the maximum score you can receive after performing </em><code>n</code><em> operations.</em></p> <p>The function <code>gcd(x, y)</code> is the greatest common divisor of <code>x</code> and <code>y</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2] <strong>Output:</strong> 1 <strong>Explanation:</strong>&nbsp;The optimal choice of operations is: (1 * gcd(1, 2)) = 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,6,8] <strong>Output:</strong> 11 <strong>Explanation:</strong>&nbsp;The optimal choice of operations is: (1 * gcd(3, 6)) + (2 * gcd(4, 8)) = 3 + 8 = 11 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5,6] <strong>Output:</strong> 14 <strong>Explanation:</strong>&nbsp;The optimal choice of operations is: (1 * gcd(1, 5)) + (2 * gcd(2, 4)) + (3 * gcd(3, 6)) = 1 + 4 + 9 = 14 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 7</code></li> <li><code>nums.length == 2 * n</code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\nusing ll = long long;\nmap<vector<ll>, ll> dp;\n int getidx(int mask) {\n int idx = 0;\n\n while(mask != 1) {\n mask = (mask >> 1);\n idx++;\n }\n\n return idx;\n }\n\n ll solve(int mask, ll level, vector<int>& nums) {\n if (dp.find({mask, level}) != dp.end()) return dp[{mask, level}];\n\n int a = (1 << 13);\n ll ans = 0;\n\n for (; a != 1; a = (a >> 1)) {\n if ((mask & a) == 0) continue;\n\n for (int b = (a >> 1); b != 0; b = (b >> 1)) {\n if ((mask & b) == 0) continue;\n\n int n1 = nums[getidx(a)];\n int n2 = nums[getidx(b)];\n\n ll tmp = level*__gcd(n1, n2);\n tmp += solve(mask ^ a ^ b, level + 1, nums);\n ans = max(ans, tmp);\n }\n }\n\n dp[{mask, level}] = ans;\n return ans;\n }\n\n int maxScore(vector<int>& nums) {\n int n = nums.size();\n ll mask = (1 << n);\n mask--;\n\n return solve(mask, 1, nums);\n }\n};", "memory": "116458" }
1,906
<p>You are given <code>nums</code>, an array of positive integers of size <code>2 * n</code>. You must perform <code>n</code> operations on this array.</p> <p>In the <code>i<sup>th</sup></code> operation <strong>(1-indexed)</strong>, you will:</p> <ul> <li>Choose two elements, <code>x</code> and <code>y</code>.</li> <li>Receive a score of <code>i * gcd(x, y)</code>.</li> <li>Remove <code>x</code> and <code>y</code> from <code>nums</code>.</li> </ul> <p>Return <em>the maximum score you can receive after performing </em><code>n</code><em> operations.</em></p> <p>The function <code>gcd(x, y)</code> is the greatest common divisor of <code>x</code> and <code>y</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2] <strong>Output:</strong> 1 <strong>Explanation:</strong>&nbsp;The optimal choice of operations is: (1 * gcd(1, 2)) = 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,6,8] <strong>Output:</strong> 11 <strong>Explanation:</strong>&nbsp;The optimal choice of operations is: (1 * gcd(3, 6)) + (2 * gcd(4, 8)) = 3 + 8 = 11 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5,6] <strong>Output:</strong> 14 <strong>Explanation:</strong>&nbsp;The optimal choice of operations is: (1 * gcd(1, 5)) + (2 * gcd(2, 4)) + (3 * gcd(3, 6)) = 1 + 4 + 9 = 14 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 7</code></li> <li><code>nums.length == 2 * n</code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n\n int max_score(vector<int>& nums,int i,int mask,int type,vector<vector<vector<int>>>& t){\n if(t[i][mask][type] != -1){\n return t[i][mask][type];\n }\n if(mask == 0){\n return t[i][mask][type] = 0;\n }\n int ans = INT_MIN;\n int off = 0;\n for(int j=0;j<nums.size();j++){\n if((1<<j) & mask){\n off++;\n }\n }\n off = nums.size()-off;\n if(type == 0){\n for(int j=0;j<nums.size();j++){\n if((1<<j) & mask){\n int temp_ans = max_score(nums,j,mask^(1<<j),1,t);\n ans = max(ans,temp_ans);\n }\n }\n }else{\n int idx = 1+off/2;\n for(int j=0;j<nums.size();j++){\n if((1<<j) & mask){\n int temp_ans = idx*(__gcd(nums[j],nums[i]))+max_score(nums,i,mask^(1<<j),0,t);\n ans = max(ans,temp_ans);\n }\n }\n }\n return t[i][mask][type] = ans;\n }\n\n int maxScore(vector<int>& nums) {\n int n = nums.size();\n vector<vector<vector<int>>> t(n,vector<vector<int>>(1<<n,vector<int>(2,-1)));\n return max_score(nums,0,(1<<n)-1,0,t);\n }\n};", "memory": "120506" }
1,906
<p>You are given <code>nums</code>, an array of positive integers of size <code>2 * n</code>. You must perform <code>n</code> operations on this array.</p> <p>In the <code>i<sup>th</sup></code> operation <strong>(1-indexed)</strong>, you will:</p> <ul> <li>Choose two elements, <code>x</code> and <code>y</code>.</li> <li>Receive a score of <code>i * gcd(x, y)</code>.</li> <li>Remove <code>x</code> and <code>y</code> from <code>nums</code>.</li> </ul> <p>Return <em>the maximum score you can receive after performing </em><code>n</code><em> operations.</em></p> <p>The function <code>gcd(x, y)</code> is the greatest common divisor of <code>x</code> and <code>y</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2] <strong>Output:</strong> 1 <strong>Explanation:</strong>&nbsp;The optimal choice of operations is: (1 * gcd(1, 2)) = 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,6,8] <strong>Output:</strong> 11 <strong>Explanation:</strong>&nbsp;The optimal choice of operations is: (1 * gcd(3, 6)) + (2 * gcd(4, 8)) = 3 + 8 = 11 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5,6] <strong>Output:</strong> 14 <strong>Explanation:</strong>&nbsp;The optimal choice of operations is: (1 * gcd(1, 5)) + (2 * gcd(2, 4)) + (3 * gcd(3, 6)) = 1 + 4 + 9 = 14 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 7</code></li> <li><code>nums.length == 2 * n</code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int maxScore(vector<int>& nums) {\n auto f=[](auto& v){\n auto t=v|(v-1);\n v=(t+1)|(((~t&-~t)-1)>>__builtin_ctz(v)+1);\n };\n int n=nums.size(),res=0;\n vector<vector<int>> dp(1<<n);\n // dp[0]={0};\n for(int i=0;i<n;i+=2){\n // printf(\"%d,\",i);\n for(int j=0;j<n;j++){\n for(int k=0;k<j;k++){\n int x=(1<<j)+(1<<k);\n int z=gcd(nums[j],nums[k]);\n if(i==0){\n dp[x]={z,z};\n continue;\n }\n for(int y=(1<<i)-1;y<1<<n;f(y)){\n if(x&y) continue;\n // printf(\"%d %d %d %d %d;\",i,j,k,x,y);\n // printf(\"%d \",dp[y].size());\n // for(auto& x:dp[y]) printf(\"%d,\",x);\n auto it=ranges::lower_bound(dp[y],z);\n vector<int> v(begin(dp[y]),it);\n v.push_back(z);\n v.insert(end(v),it,end(dp[y]));\n v.back()=0;\n for(int j=0;j<=i/2;j++){\n v.back()+=v[j]*(j+1);\n }\n // printf(\"%d %d,\",i,v.size());\n if(dp[x|y].empty()||dp[x|y].back()<v.back()){\n dp[x|y]=v;\n }\n }\n }\n }\n }\n // for(auto& x:dp.back()) printf(\"%d \",x);\n return dp.back().back();\n }\n};", "memory": "124553" }
1,906
<p>You are given <code>nums</code>, an array of positive integers of size <code>2 * n</code>. You must perform <code>n</code> operations on this array.</p> <p>In the <code>i<sup>th</sup></code> operation <strong>(1-indexed)</strong>, you will:</p> <ul> <li>Choose two elements, <code>x</code> and <code>y</code>.</li> <li>Receive a score of <code>i * gcd(x, y)</code>.</li> <li>Remove <code>x</code> and <code>y</code> from <code>nums</code>.</li> </ul> <p>Return <em>the maximum score you can receive after performing </em><code>n</code><em> operations.</em></p> <p>The function <code>gcd(x, y)</code> is the greatest common divisor of <code>x</code> and <code>y</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2] <strong>Output:</strong> 1 <strong>Explanation:</strong>&nbsp;The optimal choice of operations is: (1 * gcd(1, 2)) = 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,6,8] <strong>Output:</strong> 11 <strong>Explanation:</strong>&nbsp;The optimal choice of operations is: (1 * gcd(3, 6)) + (2 * gcd(4, 8)) = 3 + 8 = 11 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5,6] <strong>Output:</strong> 14 <strong>Explanation:</strong>&nbsp;The optimal choice of operations is: (1 * gcd(1, 5)) + (2 * gcd(2, 4)) + (3 * gcd(3, 6)) = 1 + 4 + 9 = 14 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 7</code></li> <li><code>nums.length == 2 * n</code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int n ;\n int recurssion(vector<int>nums,vector<bool>&visited,int op,unordered_map<vector<bool>,int>&m){\n int maxi=0;\n if(m.find(visited)!=m.end()) return m[visited];\n for(int i=0;i<n-1;i++){\n if(visited[i]) continue;\n for(int j=i+1;j<n;j++){\n if(visited[j]) continue;\n visited[i]=true;\n visited[j]=true;\n int score=op*(__gcd(nums[i],nums[j]));\n maxi=max(maxi,score+recurssion(nums,visited,op+1,m));\n visited[i]=false;\n visited[j]=false;\n\n }\n }\n return m[visited]=maxi;\n }\n int maxScore(vector<int>& nums) {\n n=nums.size();\n vector<bool>visited(n);\n unordered_map<vector<bool>,int>m;\n return recurssion(nums,visited,1,m);\n \n }\n};", "memory": "128601" }
1,906
<p>You are given <code>nums</code>, an array of positive integers of size <code>2 * n</code>. You must perform <code>n</code> operations on this array.</p> <p>In the <code>i<sup>th</sup></code> operation <strong>(1-indexed)</strong>, you will:</p> <ul> <li>Choose two elements, <code>x</code> and <code>y</code>.</li> <li>Receive a score of <code>i * gcd(x, y)</code>.</li> <li>Remove <code>x</code> and <code>y</code> from <code>nums</code>.</li> </ul> <p>Return <em>the maximum score you can receive after performing </em><code>n</code><em> operations.</em></p> <p>The function <code>gcd(x, y)</code> is the greatest common divisor of <code>x</code> and <code>y</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2] <strong>Output:</strong> 1 <strong>Explanation:</strong>&nbsp;The optimal choice of operations is: (1 * gcd(1, 2)) = 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,6,8] <strong>Output:</strong> 11 <strong>Explanation:</strong>&nbsp;The optimal choice of operations is: (1 * gcd(3, 6)) + (2 * gcd(4, 8)) = 3 + 8 = 11 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5,6] <strong>Output:</strong> 14 <strong>Explanation:</strong>&nbsp;The optimal choice of operations is: (1 * gcd(1, 5)) + (2 * gcd(2, 4)) + (3 * gcd(3, 6)) = 1 + 4 + 9 = 14 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 7</code></li> <li><code>nums.length == 2 * n</code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\nint f(int op, vector<int>&nums,vector<int>vis,map<vector<int>,int>&dp){\n if(op>nums.size()/2){\n return 0;\n }\n if(dp.find(vis)!=dp.end()){\n return dp[vis];\n }\n \n int maxi = INT_MIN;\n for(int i = 0;i<nums.size()-1;i++){\n if(vis[i]){\n continue;\n }\n\n for(int j = i+1;j<nums.size();j++){\n if(vis[j]){\n continue;\n }\n\n vis[i]= 1;\n vis[j] = 1;\n int temp = op*__gcd(nums[i],nums[j]) + f(op+1,nums,vis,dp);\n maxi = max(maxi,temp);\n vis[i] = 0;\n vis[j] = 0;\n }\n }\n return dp[vis]=maxi;\n}\n int maxScore(vector<int>& nums) {\n vector<int> vis(nums.size(),0);\n map<vector<int> , int>dp;\n\n return f(1,nums,vis,dp);\n \n }\n};", "memory": "132648" }
2,553
<p>You are given a <strong>0-indexed</strong> integer array <code>costs</code> where <code>costs[i]</code> is the cost of hiring the <code>i<sup>th</sup></code> worker.</p> <p>You are also given two integers <code>k</code> and <code>candidates</code>. We want to hire exactly <code>k</code> workers according to the following rules:</p> <ul> <li>You will run <code>k</code> sessions and hire exactly one worker in each session.</li> <li>In each hiring session, choose the worker with the lowest cost from either the first <code>candidates</code> workers or the last <code>candidates</code> workers. Break the tie by the smallest index. <ul> <li>For example, if <code>costs = [3,2,7,7,1,2]</code> and <code>candidates = 2</code>, then in the first hiring session, we will choose the <code>4<sup>th</sup></code> worker because they have the lowest cost <code>[<u>3,2</u>,7,7,<u><strong>1</strong>,2</u>]</code>.</li> <li>In the second hiring session, we will choose <code>1<sup>st</sup></code> worker because they have the same lowest cost as <code>4<sup>th</sup></code> worker but they have the smallest index <code>[<u>3,<strong>2</strong></u>,7,<u>7,2</u>]</code>. Please note that the indexing may be changed in the process.</li> </ul> </li> <li>If there are fewer than candidates workers remaining, choose the worker with the lowest cost among them. Break the tie by the smallest index.</li> <li>A worker can only be chosen once.</li> </ul> <p>Return <em>the total cost to hire exactly </em><code>k</code><em> workers.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> costs = [17,12,10,2,7,2,11,20,8], k = 3, candidates = 4 <strong>Output:</strong> 11 <strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0. - In the first hiring round we choose the worker from [<u>17,12,10,2</u>,7,<u>2,11,20,8</u>]. The lowest cost is 2, and we break the tie by the smallest index, which is 3. The total cost = 0 + 2 = 2. - In the second hiring round we choose the worker from [<u>17,12,10,7</u>,<u>2,11,20,8</u>]. The lowest cost is 2 (index 4). The total cost = 2 + 2 = 4. - In the third hiring round we choose the worker from [<u>17,12,10,7,11,20,8</u>]. The lowest cost is 7 (index 3). The total cost = 4 + 7 = 11. Notice that the worker with index 3 was common in the first and last four workers. The total hiring cost is 11. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> costs = [1,2,4,1], k = 3, candidates = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0. - In the first hiring round we choose the worker from [<u>1,2,4,1</u>]. The lowest cost is 1, and we break the tie by the smallest index, which is 0. The total cost = 0 + 1 = 1. Notice that workers with index 1 and 2 are common in the first and last 3 workers. - In the second hiring round we choose the worker from [<u>2,4,1</u>]. The lowest cost is 1 (index 2). The total cost = 1 + 1 = 2. - In the third hiring round there are less than three candidates. We choose the worker from the remaining workers [<u>2,4</u>]. The lowest cost is 2 (index 0). The total cost = 2 + 2 = 4. The total hiring cost is 4. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= costs.length &lt;= 10<sup>5 </sup></code></li> <li><code>1 &lt;= costs[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k, candidates &lt;= costs.length</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n long long totalCost(vector<int>& costs, int k, int candidates) {\n // priority_queue<int, vector<int>, greater<int>> head, tail;\n long long total_cost = 0;\n cout << costs.size() << endl;\n if(costs.size() <= k){\n for(auto& c : costs){\n total_cost += c;\n }\n return total_cost;\n }\n\n auto l_begin = costs.begin();\n auto l_end = costs.begin() + candidates;\n auto r_begin = costs.end() - candidates;\n auto r_end = costs.end();\n\n auto l_min_idx = min_element(l_begin, l_end);\n auto r_min_idx = min_element(r_begin, r_end);\n\n bool single = false;\n\n while(k > 0){\n k --;\n // cout << total_cost << endl;\n // cout << \"--------\" << endl;\n // cout << \"l : \" << *l_min_idx << endl;\n // cout << \"r : \" << *r_min_idx << endl;\n if(*l_min_idx <= *r_min_idx){\n total_cost += *l_min_idx;\n *l_min_idx = *l_begin;\n ++ l_begin;\n\n if(++l_end >= r_begin){\n l_end = r_end;\n break;\n }else{\n l_min_idx = min_element(l_begin, l_end);\n }\n }else{\n total_cost += *r_min_idx;\n *r_min_idx = *(r_end - 1);\n -- r_end;\n\n if(--r_begin <= l_end){\n l_end = r_end;\n break;\n }else{\n r_min_idx = min_element(r_begin, r_end);\n }\n\n }\n }\n\n sort(l_begin, l_end);\n\n while(k > 0){\n total_cost += *l_begin;\n ++ l_begin;\n k--;\n }\n \n return total_cost;\n }\n};\n", "memory": "73555" }
2,553
<p>You are given a <strong>0-indexed</strong> integer array <code>costs</code> where <code>costs[i]</code> is the cost of hiring the <code>i<sup>th</sup></code> worker.</p> <p>You are also given two integers <code>k</code> and <code>candidates</code>. We want to hire exactly <code>k</code> workers according to the following rules:</p> <ul> <li>You will run <code>k</code> sessions and hire exactly one worker in each session.</li> <li>In each hiring session, choose the worker with the lowest cost from either the first <code>candidates</code> workers or the last <code>candidates</code> workers. Break the tie by the smallest index. <ul> <li>For example, if <code>costs = [3,2,7,7,1,2]</code> and <code>candidates = 2</code>, then in the first hiring session, we will choose the <code>4<sup>th</sup></code> worker because they have the lowest cost <code>[<u>3,2</u>,7,7,<u><strong>1</strong>,2</u>]</code>.</li> <li>In the second hiring session, we will choose <code>1<sup>st</sup></code> worker because they have the same lowest cost as <code>4<sup>th</sup></code> worker but they have the smallest index <code>[<u>3,<strong>2</strong></u>,7,<u>7,2</u>]</code>. Please note that the indexing may be changed in the process.</li> </ul> </li> <li>If there are fewer than candidates workers remaining, choose the worker with the lowest cost among them. Break the tie by the smallest index.</li> <li>A worker can only be chosen once.</li> </ul> <p>Return <em>the total cost to hire exactly </em><code>k</code><em> workers.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> costs = [17,12,10,2,7,2,11,20,8], k = 3, candidates = 4 <strong>Output:</strong> 11 <strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0. - In the first hiring round we choose the worker from [<u>17,12,10,2</u>,7,<u>2,11,20,8</u>]. The lowest cost is 2, and we break the tie by the smallest index, which is 3. The total cost = 0 + 2 = 2. - In the second hiring round we choose the worker from [<u>17,12,10,7</u>,<u>2,11,20,8</u>]. The lowest cost is 2 (index 4). The total cost = 2 + 2 = 4. - In the third hiring round we choose the worker from [<u>17,12,10,7,11,20,8</u>]. The lowest cost is 7 (index 3). The total cost = 4 + 7 = 11. Notice that the worker with index 3 was common in the first and last four workers. The total hiring cost is 11. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> costs = [1,2,4,1], k = 3, candidates = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0. - In the first hiring round we choose the worker from [<u>1,2,4,1</u>]. The lowest cost is 1, and we break the tie by the smallest index, which is 0. The total cost = 0 + 1 = 1. Notice that workers with index 1 and 2 are common in the first and last 3 workers. - In the second hiring round we choose the worker from [<u>2,4,1</u>]. The lowest cost is 1 (index 2). The total cost = 1 + 1 = 2. - In the third hiring round there are less than three candidates. We choose the worker from the remaining workers [<u>2,4</u>]. The lowest cost is 2 (index 0). The total cost = 2 + 2 = 4. The total hiring cost is 4. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= costs.length &lt;= 10<sup>5 </sup></code></li> <li><code>1 &lt;= costs[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k, candidates &lt;= costs.length</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n long long totalCost(vector<int>& costs, int k, int candidates) {\n // priority_queue<int, vector<int>, greater<int>> head, tail;\n long long total_cost = 0;\n if(costs.size() <= k){\n for(auto& c : costs){\n total_cost += c;\n }\n return total_cost;\n }\n\n auto l_begin = costs.begin();\n auto l_end = costs.begin() + candidates;\n auto r_begin = costs.end() - candidates;\n auto r_end = costs.end();\n\n auto l_min_idx = min_element(l_begin, l_end);\n auto r_min_idx = min_element(r_begin, r_end);\n\n bool single = false;\n\n while(k > 0){\n k --;\n // cout << total_cost << endl;\n // cout << \"--------\" << endl;\n // cout << \"l : \" << *l_min_idx << endl;\n // cout << \"r : \" << *r_min_idx << endl;\n if(*l_min_idx <= *r_min_idx){\n total_cost += *l_min_idx;\n *l_min_idx = *l_begin;\n ++ l_begin;\n\n if(++l_end >= r_begin){\n l_end = r_end;\n break;\n }else{\n l_min_idx = min_element(l_begin, l_end);\n }\n }else{\n total_cost += *r_min_idx;\n *r_min_idx = *(r_end - 1);\n -- r_end;\n\n if(--r_begin <= l_end){\n l_end = r_end;\n break;\n }else{\n r_min_idx = min_element(r_begin, r_end);\n }\n\n }\n }\n\n sort(l_begin, l_end);\n\n while(k > 0){\n total_cost += *l_begin;\n ++ l_begin;\n k--;\n }\n \n return total_cost;\n }\n};\n", "memory": "73555" }
2,553
<p>You are given a <strong>0-indexed</strong> integer array <code>costs</code> where <code>costs[i]</code> is the cost of hiring the <code>i<sup>th</sup></code> worker.</p> <p>You are also given two integers <code>k</code> and <code>candidates</code>. We want to hire exactly <code>k</code> workers according to the following rules:</p> <ul> <li>You will run <code>k</code> sessions and hire exactly one worker in each session.</li> <li>In each hiring session, choose the worker with the lowest cost from either the first <code>candidates</code> workers or the last <code>candidates</code> workers. Break the tie by the smallest index. <ul> <li>For example, if <code>costs = [3,2,7,7,1,2]</code> and <code>candidates = 2</code>, then in the first hiring session, we will choose the <code>4<sup>th</sup></code> worker because they have the lowest cost <code>[<u>3,2</u>,7,7,<u><strong>1</strong>,2</u>]</code>.</li> <li>In the second hiring session, we will choose <code>1<sup>st</sup></code> worker because they have the same lowest cost as <code>4<sup>th</sup></code> worker but they have the smallest index <code>[<u>3,<strong>2</strong></u>,7,<u>7,2</u>]</code>. Please note that the indexing may be changed in the process.</li> </ul> </li> <li>If there are fewer than candidates workers remaining, choose the worker with the lowest cost among them. Break the tie by the smallest index.</li> <li>A worker can only be chosen once.</li> </ul> <p>Return <em>the total cost to hire exactly </em><code>k</code><em> workers.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> costs = [17,12,10,2,7,2,11,20,8], k = 3, candidates = 4 <strong>Output:</strong> 11 <strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0. - In the first hiring round we choose the worker from [<u>17,12,10,2</u>,7,<u>2,11,20,8</u>]. The lowest cost is 2, and we break the tie by the smallest index, which is 3. The total cost = 0 + 2 = 2. - In the second hiring round we choose the worker from [<u>17,12,10,7</u>,<u>2,11,20,8</u>]. The lowest cost is 2 (index 4). The total cost = 2 + 2 = 4. - In the third hiring round we choose the worker from [<u>17,12,10,7,11,20,8</u>]. The lowest cost is 7 (index 3). The total cost = 4 + 7 = 11. Notice that the worker with index 3 was common in the first and last four workers. The total hiring cost is 11. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> costs = [1,2,4,1], k = 3, candidates = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0. - In the first hiring round we choose the worker from [<u>1,2,4,1</u>]. The lowest cost is 1, and we break the tie by the smallest index, which is 0. The total cost = 0 + 1 = 1. Notice that workers with index 1 and 2 are common in the first and last 3 workers. - In the second hiring round we choose the worker from [<u>2,4,1</u>]. The lowest cost is 1 (index 2). The total cost = 1 + 1 = 2. - In the third hiring round there are less than three candidates. We choose the worker from the remaining workers [<u>2,4</u>]. The lowest cost is 2 (index 0). The total cost = 2 + 2 = 4. The total hiring cost is 4. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= costs.length &lt;= 10<sup>5 </sup></code></li> <li><code>1 &lt;= costs[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k, candidates &lt;= costs.length</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n long long totalCost(vector<int>& costs, int k, int candidates) {\n long long ans = 0;\n int n = costs.size();\n if(candidates >= (n+1)/2){\n nth_element(costs.begin(), costs.begin()+k, costs.end());\n ans=0;\n while(k--) ans+=costs[k];\n return ans;\n }\n\n priority_queue<int,vector<int>,std::greater<int>> ql(costs.begin(), costs.begin()+candidates);\n priority_queue<int,vector<int>,std::greater<int>> qr(costs.begin()+n-candidates, costs.end());\n\n int l = candidates;\n int r = n-candidates-1;\n\n while(k>0){\n if(l > r) break;\n int minL = ql.top();\n int minR = qr.top();\n \n if(minL <= minR){\n ans+=minL;\n ql.pop();\n if(l <= r) ql.push(costs[l++]);\n }\n else{\n ans+=minR;\n qr.pop();\n if(l <= r) qr.push(costs[r--]);\n }\n k--;\n }\n\n while(k>0 && !ql.empty() && !qr.empty()){\n if(ql.top() <= qr.top()){\n ans+=ql.top();\n ql.pop();\n }\n else{\n ans+=qr.top();\n qr.pop();\n }\n k--;\n }\n\n while(k>0&&!ql.empty()){\n ans+=ql.top();\n ql.pop();\n k--;\n }\n while(k>0&&!qr.empty()){\n ans+=qr.top();\n qr.pop();\n k--;\n }\n return ans;\n }\n};", "memory": "74065" }
2,553
<p>You are given a <strong>0-indexed</strong> integer array <code>costs</code> where <code>costs[i]</code> is the cost of hiring the <code>i<sup>th</sup></code> worker.</p> <p>You are also given two integers <code>k</code> and <code>candidates</code>. We want to hire exactly <code>k</code> workers according to the following rules:</p> <ul> <li>You will run <code>k</code> sessions and hire exactly one worker in each session.</li> <li>In each hiring session, choose the worker with the lowest cost from either the first <code>candidates</code> workers or the last <code>candidates</code> workers. Break the tie by the smallest index. <ul> <li>For example, if <code>costs = [3,2,7,7,1,2]</code> and <code>candidates = 2</code>, then in the first hiring session, we will choose the <code>4<sup>th</sup></code> worker because they have the lowest cost <code>[<u>3,2</u>,7,7,<u><strong>1</strong>,2</u>]</code>.</li> <li>In the second hiring session, we will choose <code>1<sup>st</sup></code> worker because they have the same lowest cost as <code>4<sup>th</sup></code> worker but they have the smallest index <code>[<u>3,<strong>2</strong></u>,7,<u>7,2</u>]</code>. Please note that the indexing may be changed in the process.</li> </ul> </li> <li>If there are fewer than candidates workers remaining, choose the worker with the lowest cost among them. Break the tie by the smallest index.</li> <li>A worker can only be chosen once.</li> </ul> <p>Return <em>the total cost to hire exactly </em><code>k</code><em> workers.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> costs = [17,12,10,2,7,2,11,20,8], k = 3, candidates = 4 <strong>Output:</strong> 11 <strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0. - In the first hiring round we choose the worker from [<u>17,12,10,2</u>,7,<u>2,11,20,8</u>]. The lowest cost is 2, and we break the tie by the smallest index, which is 3. The total cost = 0 + 2 = 2. - In the second hiring round we choose the worker from [<u>17,12,10,7</u>,<u>2,11,20,8</u>]. The lowest cost is 2 (index 4). The total cost = 2 + 2 = 4. - In the third hiring round we choose the worker from [<u>17,12,10,7,11,20,8</u>]. The lowest cost is 7 (index 3). The total cost = 4 + 7 = 11. Notice that the worker with index 3 was common in the first and last four workers. The total hiring cost is 11. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> costs = [1,2,4,1], k = 3, candidates = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0. - In the first hiring round we choose the worker from [<u>1,2,4,1</u>]. The lowest cost is 1, and we break the tie by the smallest index, which is 0. The total cost = 0 + 1 = 1. Notice that workers with index 1 and 2 are common in the first and last 3 workers. - In the second hiring round we choose the worker from [<u>2,4,1</u>]. The lowest cost is 1 (index 2). The total cost = 1 + 1 = 2. - In the third hiring round there are less than three candidates. We choose the worker from the remaining workers [<u>2,4</u>]. The lowest cost is 2 (index 0). The total cost = 2 + 2 = 4. The total hiring cost is 4. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= costs.length &lt;= 10<sup>5 </sup></code></li> <li><code>1 &lt;= costs[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k, candidates &lt;= costs.length</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n long long totalCost(vector<int>& costs, int k, int candidates) {\n if(costs.size() < candidates * 2) {\n std::sort(costs.begin(),costs.end());\n long long ret = 0;\n for(int i = 0; i <k; ++i) {\n ret += costs[i];\n }\n return ret;\n }\n\n priority_queue<int,vector<int>,greater<int>> min_heap1;\n priority_queue<int,vector<int>,greater<int>> min_heap2;\n for(int i = 0; i < candidates; ++i) {\n min_heap1.push(costs[i]);\n }\n\n int sz = costs.size();\n for(int i = sz-1; i > sz-candidates-1; --i) {\n min_heap2.push(costs[i]);\n }\n\n int ind1 = candidates;\n int ind2 = sz-candidates-1;\n long long ret = 0;\n \n for(int i = 0; i < k; ++i) {\n int top1 = min_heap1.top();\n int top2 = min_heap2.top();\n if(min_heap1.size() == 0) {\n top1 = INT_MAX;\n }\n\n if(min_heap2.size() == 0) {\n top2 = INT_MAX;\n }\n\n if(top2 < top1) {\n ret += top2;\n min_heap2.pop();\n\n if(ind2 >= ind1) {\n min_heap2.push(costs[ind2]);\n ind2--;\n }\n } \n\n else {\n ret += top1;\n min_heap1.pop();\n\n if(ind1 <= ind2) {\n min_heap1.push(costs[ind1]);\n ++ind1;\n }\n }\n\n }\n\n return ret;\n }\n};", "memory": "74065" }
2,553
<p>You are given a <strong>0-indexed</strong> integer array <code>costs</code> where <code>costs[i]</code> is the cost of hiring the <code>i<sup>th</sup></code> worker.</p> <p>You are also given two integers <code>k</code> and <code>candidates</code>. We want to hire exactly <code>k</code> workers according to the following rules:</p> <ul> <li>You will run <code>k</code> sessions and hire exactly one worker in each session.</li> <li>In each hiring session, choose the worker with the lowest cost from either the first <code>candidates</code> workers or the last <code>candidates</code> workers. Break the tie by the smallest index. <ul> <li>For example, if <code>costs = [3,2,7,7,1,2]</code> and <code>candidates = 2</code>, then in the first hiring session, we will choose the <code>4<sup>th</sup></code> worker because they have the lowest cost <code>[<u>3,2</u>,7,7,<u><strong>1</strong>,2</u>]</code>.</li> <li>In the second hiring session, we will choose <code>1<sup>st</sup></code> worker because they have the same lowest cost as <code>4<sup>th</sup></code> worker but they have the smallest index <code>[<u>3,<strong>2</strong></u>,7,<u>7,2</u>]</code>. Please note that the indexing may be changed in the process.</li> </ul> </li> <li>If there are fewer than candidates workers remaining, choose the worker with the lowest cost among them. Break the tie by the smallest index.</li> <li>A worker can only be chosen once.</li> </ul> <p>Return <em>the total cost to hire exactly </em><code>k</code><em> workers.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> costs = [17,12,10,2,7,2,11,20,8], k = 3, candidates = 4 <strong>Output:</strong> 11 <strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0. - In the first hiring round we choose the worker from [<u>17,12,10,2</u>,7,<u>2,11,20,8</u>]. The lowest cost is 2, and we break the tie by the smallest index, which is 3. The total cost = 0 + 2 = 2. - In the second hiring round we choose the worker from [<u>17,12,10,7</u>,<u>2,11,20,8</u>]. The lowest cost is 2 (index 4). The total cost = 2 + 2 = 4. - In the third hiring round we choose the worker from [<u>17,12,10,7,11,20,8</u>]. The lowest cost is 7 (index 3). The total cost = 4 + 7 = 11. Notice that the worker with index 3 was common in the first and last four workers. The total hiring cost is 11. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> costs = [1,2,4,1], k = 3, candidates = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0. - In the first hiring round we choose the worker from [<u>1,2,4,1</u>]. The lowest cost is 1, and we break the tie by the smallest index, which is 0. The total cost = 0 + 1 = 1. Notice that workers with index 1 and 2 are common in the first and last 3 workers. - In the second hiring round we choose the worker from [<u>2,4,1</u>]. The lowest cost is 1 (index 2). The total cost = 1 + 1 = 2. - In the third hiring round there are less than three candidates. We choose the worker from the remaining workers [<u>2,4</u>]. The lowest cost is 2 (index 0). The total cost = 2 + 2 = 4. The total hiring cost is 4. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= costs.length &lt;= 10<sup>5 </sup></code></li> <li><code>1 &lt;= costs[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k, candidates &lt;= costs.length</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n long long totalCost(vector<int>& costs, int k, int candidates) {\n long long totalCost = 0;\n if(candidates >= (costs.size()+1)/2){\n sort(costs.begin(),costs.end());\n for(int i=0; i<k;i++){\n totalCost += costs[i];\n }\n return totalCost;\n }\n priority_queue<int,vector<int>, greater<int>> q1,q2;\n int cnt = 1, i=0, j=costs.size()-1;\n for(; cnt <= candidates; cnt++){\n q1.push(costs[i++]);\n q2.push(costs[j--]);\n }\n for(cnt = 1; cnt <= k; cnt++){\n if(q1.empty() || q2.empty()){\n if(!q1.empty()){\n totalCost += q1.top();\n q1.pop();\n }else if(!q2.empty()){\n totalCost += q2.top();\n q2.pop();\n }\n }else if(q1.top() <= q2.top()){\n cout<<\"q1 i \"<<i<<\" \"<<q1.top()<<endl;\n totalCost += q1.top();\n q1.pop();\n if(i <= j)\n q1.push(costs[i++]);\n }else{\n cout<<\"q2 j \"<<j<<\" \"<<q2.top()<<endl;\n totalCost += q2.top();\n q2.pop();\n if(i <= j ){\n q2.push(costs[j--]);\n }\n }\n }\n return totalCost;\n \n }\n};", "memory": "74575" }
2,553
<p>You are given a <strong>0-indexed</strong> integer array <code>costs</code> where <code>costs[i]</code> is the cost of hiring the <code>i<sup>th</sup></code> worker.</p> <p>You are also given two integers <code>k</code> and <code>candidates</code>. We want to hire exactly <code>k</code> workers according to the following rules:</p> <ul> <li>You will run <code>k</code> sessions and hire exactly one worker in each session.</li> <li>In each hiring session, choose the worker with the lowest cost from either the first <code>candidates</code> workers or the last <code>candidates</code> workers. Break the tie by the smallest index. <ul> <li>For example, if <code>costs = [3,2,7,7,1,2]</code> and <code>candidates = 2</code>, then in the first hiring session, we will choose the <code>4<sup>th</sup></code> worker because they have the lowest cost <code>[<u>3,2</u>,7,7,<u><strong>1</strong>,2</u>]</code>.</li> <li>In the second hiring session, we will choose <code>1<sup>st</sup></code> worker because they have the same lowest cost as <code>4<sup>th</sup></code> worker but they have the smallest index <code>[<u>3,<strong>2</strong></u>,7,<u>7,2</u>]</code>. Please note that the indexing may be changed in the process.</li> </ul> </li> <li>If there are fewer than candidates workers remaining, choose the worker with the lowest cost among them. Break the tie by the smallest index.</li> <li>A worker can only be chosen once.</li> </ul> <p>Return <em>the total cost to hire exactly </em><code>k</code><em> workers.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> costs = [17,12,10,2,7,2,11,20,8], k = 3, candidates = 4 <strong>Output:</strong> 11 <strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0. - In the first hiring round we choose the worker from [<u>17,12,10,2</u>,7,<u>2,11,20,8</u>]. The lowest cost is 2, and we break the tie by the smallest index, which is 3. The total cost = 0 + 2 = 2. - In the second hiring round we choose the worker from [<u>17,12,10,7</u>,<u>2,11,20,8</u>]. The lowest cost is 2 (index 4). The total cost = 2 + 2 = 4. - In the third hiring round we choose the worker from [<u>17,12,10,7,11,20,8</u>]. The lowest cost is 7 (index 3). The total cost = 4 + 7 = 11. Notice that the worker with index 3 was common in the first and last four workers. The total hiring cost is 11. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> costs = [1,2,4,1], k = 3, candidates = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0. - In the first hiring round we choose the worker from [<u>1,2,4,1</u>]. The lowest cost is 1, and we break the tie by the smallest index, which is 0. The total cost = 0 + 1 = 1. Notice that workers with index 1 and 2 are common in the first and last 3 workers. - In the second hiring round we choose the worker from [<u>2,4,1</u>]. The lowest cost is 1 (index 2). The total cost = 1 + 1 = 2. - In the third hiring round there are less than three candidates. We choose the worker from the remaining workers [<u>2,4</u>]. The lowest cost is 2 (index 0). The total cost = 2 + 2 = 4. The total hiring cost is 4. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= costs.length &lt;= 10<sup>5 </sup></code></li> <li><code>1 &lt;= costs[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k, candidates &lt;= costs.length</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n long long totalCost(vector<int>& costs, int k, int candidates) {\n if(2*candidates>=costs.size() || k==costs.size()){\n long long sum=0;\n sort(costs.begin(),costs.end());\n for(int i=0;i<k;i++) sum+=costs[i];\n return sum;\n }\n priority_queue<int,vector<int>, greater<int>> pq1;\n priority_queue<int,vector<int>, greater<int>> pq2;\n for(int i=0;i<candidates;i++) pq1.push(costs[i]);\n for(int i=costs.size()-1;i>=costs.size()-candidates;i--) pq2.push(costs[i]);\n int i=candidates,j=costs.size()-candidates-1;\n long long sum=0;\n while(i<=j && k>0){\n if(pq1.top()<=pq2.top()){\n sum+=pq1.top();\n //cout<<pq1.top()<<endl;\n pq1.pop();\n pq1.push(costs[i]);\n i++;\n }\n else{\n sum+=pq2.top();\n //cout<<pq2.top()<<endl;\n pq2.pop();\n pq2.push(costs[j]);\n j--;\n }\n k--;\n //cout<<sum<<\" \"<<i<<\" \"<<j<<endl;\n }\n cout<<k<<\"*\"<<endl;\n while(k>0 && !pq1.empty() && !pq2.empty()){\n if(pq1.top()<=pq2.top()){\n sum+=pq1.top();\n pq1.pop();\n }\n else{\n sum+=pq2.top();\n pq2.pop();\n }\n k--;\n //cout<<sum<<endl; \n }\n while(k>0 && !pq1.empty()){\n sum+=pq1.top();\n pq1.pop();k--;\n }\n while(k>0 && !pq2.empty()){\n sum+=pq2.top();\n pq2.pop();k--;\n }\n return sum;\n }\n};", "memory": "74575" }
2,553
<p>You are given a <strong>0-indexed</strong> integer array <code>costs</code> where <code>costs[i]</code> is the cost of hiring the <code>i<sup>th</sup></code> worker.</p> <p>You are also given two integers <code>k</code> and <code>candidates</code>. We want to hire exactly <code>k</code> workers according to the following rules:</p> <ul> <li>You will run <code>k</code> sessions and hire exactly one worker in each session.</li> <li>In each hiring session, choose the worker with the lowest cost from either the first <code>candidates</code> workers or the last <code>candidates</code> workers. Break the tie by the smallest index. <ul> <li>For example, if <code>costs = [3,2,7,7,1,2]</code> and <code>candidates = 2</code>, then in the first hiring session, we will choose the <code>4<sup>th</sup></code> worker because they have the lowest cost <code>[<u>3,2</u>,7,7,<u><strong>1</strong>,2</u>]</code>.</li> <li>In the second hiring session, we will choose <code>1<sup>st</sup></code> worker because they have the same lowest cost as <code>4<sup>th</sup></code> worker but they have the smallest index <code>[<u>3,<strong>2</strong></u>,7,<u>7,2</u>]</code>. Please note that the indexing may be changed in the process.</li> </ul> </li> <li>If there are fewer than candidates workers remaining, choose the worker with the lowest cost among them. Break the tie by the smallest index.</li> <li>A worker can only be chosen once.</li> </ul> <p>Return <em>the total cost to hire exactly </em><code>k</code><em> workers.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> costs = [17,12,10,2,7,2,11,20,8], k = 3, candidates = 4 <strong>Output:</strong> 11 <strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0. - In the first hiring round we choose the worker from [<u>17,12,10,2</u>,7,<u>2,11,20,8</u>]. The lowest cost is 2, and we break the tie by the smallest index, which is 3. The total cost = 0 + 2 = 2. - In the second hiring round we choose the worker from [<u>17,12,10,7</u>,<u>2,11,20,8</u>]. The lowest cost is 2 (index 4). The total cost = 2 + 2 = 4. - In the third hiring round we choose the worker from [<u>17,12,10,7,11,20,8</u>]. The lowest cost is 7 (index 3). The total cost = 4 + 7 = 11. Notice that the worker with index 3 was common in the first and last four workers. The total hiring cost is 11. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> costs = [1,2,4,1], k = 3, candidates = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0. - In the first hiring round we choose the worker from [<u>1,2,4,1</u>]. The lowest cost is 1, and we break the tie by the smallest index, which is 0. The total cost = 0 + 1 = 1. Notice that workers with index 1 and 2 are common in the first and last 3 workers. - In the second hiring round we choose the worker from [<u>2,4,1</u>]. The lowest cost is 1 (index 2). The total cost = 1 + 1 = 2. - In the third hiring round there are less than three candidates. We choose the worker from the remaining workers [<u>2,4</u>]. The lowest cost is 2 (index 0). The total cost = 2 + 2 = 4. The total hiring cost is 4. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= costs.length &lt;= 10<sup>5 </sup></code></li> <li><code>1 &lt;= costs[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k, candidates &lt;= costs.length</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n long long totalCost(vector<int>& costs, int k, int candidates) {\n long long hiringCost = 0;\n\n auto lastLow = std::min(candidates - 1, (int)(costs.size() / 2));\n auto lastHigh = std::max((int)costs.size() - candidates, lastLow + 1);\n\n // std::cout << \"lastLow=\" << lastLow << \", lastHigh=\" << lastHigh << std::endl;\n\n auto low = std::priority_queue<int, std::vector<int>, std::greater<int>>(costs.begin(), costs.begin() + lastLow + 1);\n auto high = std::priority_queue<int, std::vector<int>, std::greater<int>>(costs.begin() + lastHigh, costs.end());\n\n auto printPq = [&](auto pq)\n {\n auto ss = std::ostringstream{};\n auto seperator = \"\";\n for(auto i = std::size_t{0}; i < candidates && !pq.empty(); ++i)\n {\n ss << seperator << pq.top();\n pq.pop();\n seperator = \",\";\n }\n\n return ss.str();\n };\n\n while(k-- != 0)\n {\n auto popHigh = [&]\n {\n // std::cout << \" - \" << high.top() << std::endl;\n hiringCost += high.top();\n high.pop();\n if(lastHigh - lastLow > 1) high.emplace(costs[--lastHigh]);\n };\n\n auto popLow = [&]\n {\n // std::cout << \" - \" << low.top() << std::endl;\n hiringCost += low.top();\n low.pop();\n if(lastHigh - lastLow > 1) low.emplace(costs[++lastLow]);\n };\n\n // std::cout << \"low=[\" << printPq(low) << \"], high=[\" << printPq(high) << \"]\";\n\n if(high.empty()) popLow();\n else if(low.empty()) popHigh();\n else if(high.top() < low.top()) popHigh();\n else popLow();\n }\n\n return hiringCost;\n }\n};", "memory": "75085" }
2,553
<p>You are given a <strong>0-indexed</strong> integer array <code>costs</code> where <code>costs[i]</code> is the cost of hiring the <code>i<sup>th</sup></code> worker.</p> <p>You are also given two integers <code>k</code> and <code>candidates</code>. We want to hire exactly <code>k</code> workers according to the following rules:</p> <ul> <li>You will run <code>k</code> sessions and hire exactly one worker in each session.</li> <li>In each hiring session, choose the worker with the lowest cost from either the first <code>candidates</code> workers or the last <code>candidates</code> workers. Break the tie by the smallest index. <ul> <li>For example, if <code>costs = [3,2,7,7,1,2]</code> and <code>candidates = 2</code>, then in the first hiring session, we will choose the <code>4<sup>th</sup></code> worker because they have the lowest cost <code>[<u>3,2</u>,7,7,<u><strong>1</strong>,2</u>]</code>.</li> <li>In the second hiring session, we will choose <code>1<sup>st</sup></code> worker because they have the same lowest cost as <code>4<sup>th</sup></code> worker but they have the smallest index <code>[<u>3,<strong>2</strong></u>,7,<u>7,2</u>]</code>. Please note that the indexing may be changed in the process.</li> </ul> </li> <li>If there are fewer than candidates workers remaining, choose the worker with the lowest cost among them. Break the tie by the smallest index.</li> <li>A worker can only be chosen once.</li> </ul> <p>Return <em>the total cost to hire exactly </em><code>k</code><em> workers.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> costs = [17,12,10,2,7,2,11,20,8], k = 3, candidates = 4 <strong>Output:</strong> 11 <strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0. - In the first hiring round we choose the worker from [<u>17,12,10,2</u>,7,<u>2,11,20,8</u>]. The lowest cost is 2, and we break the tie by the smallest index, which is 3. The total cost = 0 + 2 = 2. - In the second hiring round we choose the worker from [<u>17,12,10,7</u>,<u>2,11,20,8</u>]. The lowest cost is 2 (index 4). The total cost = 2 + 2 = 4. - In the third hiring round we choose the worker from [<u>17,12,10,7,11,20,8</u>]. The lowest cost is 7 (index 3). The total cost = 4 + 7 = 11. Notice that the worker with index 3 was common in the first and last four workers. The total hiring cost is 11. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> costs = [1,2,4,1], k = 3, candidates = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0. - In the first hiring round we choose the worker from [<u>1,2,4,1</u>]. The lowest cost is 1, and we break the tie by the smallest index, which is 0. The total cost = 0 + 1 = 1. Notice that workers with index 1 and 2 are common in the first and last 3 workers. - In the second hiring round we choose the worker from [<u>2,4,1</u>]. The lowest cost is 1 (index 2). The total cost = 1 + 1 = 2. - In the third hiring round there are less than three candidates. We choose the worker from the remaining workers [<u>2,4</u>]. The lowest cost is 2 (index 0). The total cost = 2 + 2 = 4. The total hiring cost is 4. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= costs.length &lt;= 10<sup>5 </sup></code></li> <li><code>1 &lt;= costs[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k, candidates &lt;= costs.length</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n long long totalCost(vector<int>& costs, int k, int candidates) {\n priority_queue<int,vector<int>,greater<int>>pq1(costs.begin(),costs.begin()+candidates);\n int x = candidates;\n if(costs.size() < candidates * 2 ) x = costs.size() - candidates;\n priority_queue<int,vector<int>,greater<int>>pq2(costs.end()-x,costs.end());\n\n int i = candidates;\n int j = costs.size()-1-x;\n long long ans = 0;\n while(k--){\n if(pq1.empty()){\n ans += pq2.top();\n cout<<pq2.top();\n pq2.pop();\n if(j >= i)pq2.push(costs[j--]);\n }\n else if(pq2.empty()){\n ans += pq1.top();\n cout<<pq1.top();\n pq1.pop();\n if(j >= i)pq1.push(costs[i++]);\n }\n else{\n if(pq1.top() > pq2.top()){\n ans += pq2.top();\n cout<<pq2.top();\n pq2.pop();\n if(j >= i)pq2.push(costs[j--]);\n }\n else{\n ans += pq1.top();\n cout<<pq1.top();\n pq1.pop();\n if(j >= i)pq1.push(costs[i++]);\n }\n }\n cout<<\"a\"<<ans<<endl;\n }\n return ans;\n\n }\n};", "memory": "75085" }
2,553
<p>You are given a <strong>0-indexed</strong> integer array <code>costs</code> where <code>costs[i]</code> is the cost of hiring the <code>i<sup>th</sup></code> worker.</p> <p>You are also given two integers <code>k</code> and <code>candidates</code>. We want to hire exactly <code>k</code> workers according to the following rules:</p> <ul> <li>You will run <code>k</code> sessions and hire exactly one worker in each session.</li> <li>In each hiring session, choose the worker with the lowest cost from either the first <code>candidates</code> workers or the last <code>candidates</code> workers. Break the tie by the smallest index. <ul> <li>For example, if <code>costs = [3,2,7,7,1,2]</code> and <code>candidates = 2</code>, then in the first hiring session, we will choose the <code>4<sup>th</sup></code> worker because they have the lowest cost <code>[<u>3,2</u>,7,7,<u><strong>1</strong>,2</u>]</code>.</li> <li>In the second hiring session, we will choose <code>1<sup>st</sup></code> worker because they have the same lowest cost as <code>4<sup>th</sup></code> worker but they have the smallest index <code>[<u>3,<strong>2</strong></u>,7,<u>7,2</u>]</code>. Please note that the indexing may be changed in the process.</li> </ul> </li> <li>If there are fewer than candidates workers remaining, choose the worker with the lowest cost among them. Break the tie by the smallest index.</li> <li>A worker can only be chosen once.</li> </ul> <p>Return <em>the total cost to hire exactly </em><code>k</code><em> workers.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> costs = [17,12,10,2,7,2,11,20,8], k = 3, candidates = 4 <strong>Output:</strong> 11 <strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0. - In the first hiring round we choose the worker from [<u>17,12,10,2</u>,7,<u>2,11,20,8</u>]. The lowest cost is 2, and we break the tie by the smallest index, which is 3. The total cost = 0 + 2 = 2. - In the second hiring round we choose the worker from [<u>17,12,10,7</u>,<u>2,11,20,8</u>]. The lowest cost is 2 (index 4). The total cost = 2 + 2 = 4. - In the third hiring round we choose the worker from [<u>17,12,10,7,11,20,8</u>]. The lowest cost is 7 (index 3). The total cost = 4 + 7 = 11. Notice that the worker with index 3 was common in the first and last four workers. The total hiring cost is 11. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> costs = [1,2,4,1], k = 3, candidates = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0. - In the first hiring round we choose the worker from [<u>1,2,4,1</u>]. The lowest cost is 1, and we break the tie by the smallest index, which is 0. The total cost = 0 + 1 = 1. Notice that workers with index 1 and 2 are common in the first and last 3 workers. - In the second hiring round we choose the worker from [<u>2,4,1</u>]. The lowest cost is 1 (index 2). The total cost = 1 + 1 = 2. - In the third hiring round there are less than three candidates. We choose the worker from the remaining workers [<u>2,4</u>]. The lowest cost is 2 (index 0). The total cost = 2 + 2 = 4. The total hiring cost is 4. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= costs.length &lt;= 10<sup>5 </sup></code></li> <li><code>1 &lt;= costs[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k, candidates &lt;= costs.length</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n long long totalCost(vector<int>& costs, int k, int candidates) {\n int n = costs.size();\n if (candidates * 2 > n) {\n sort(costs.begin(), costs.end());\n return accumulate(costs.begin(), costs.begin() + k, 0LL);\n }\n using pii = pair<int, int>;\n priority_queue<pii, vector<pii>, greater<pii>> pq;\n for (int i = 0; i < candidates; ++i) {\n pq.emplace(costs[i], i);\n pq.emplace(costs[n - i - 1], n - i - 1);\n }\n long long ans = 0;\n int l = candidates, r = n - candidates - 1;\n while (k--) {\n auto [cost, i] = pq.top();\n pq.pop();\n ans += cost;\n if (l > r) {\n continue;\n }\n if (i < l) {\n pq.emplace(costs[l], l++);\n } else {\n pq.emplace(costs[r], r--);\n }\n }\n return ans;\n }\n};", "memory": "75595" }
2,553
<p>You are given a <strong>0-indexed</strong> integer array <code>costs</code> where <code>costs[i]</code> is the cost of hiring the <code>i<sup>th</sup></code> worker.</p> <p>You are also given two integers <code>k</code> and <code>candidates</code>. We want to hire exactly <code>k</code> workers according to the following rules:</p> <ul> <li>You will run <code>k</code> sessions and hire exactly one worker in each session.</li> <li>In each hiring session, choose the worker with the lowest cost from either the first <code>candidates</code> workers or the last <code>candidates</code> workers. Break the tie by the smallest index. <ul> <li>For example, if <code>costs = [3,2,7,7,1,2]</code> and <code>candidates = 2</code>, then in the first hiring session, we will choose the <code>4<sup>th</sup></code> worker because they have the lowest cost <code>[<u>3,2</u>,7,7,<u><strong>1</strong>,2</u>]</code>.</li> <li>In the second hiring session, we will choose <code>1<sup>st</sup></code> worker because they have the same lowest cost as <code>4<sup>th</sup></code> worker but they have the smallest index <code>[<u>3,<strong>2</strong></u>,7,<u>7,2</u>]</code>. Please note that the indexing may be changed in the process.</li> </ul> </li> <li>If there are fewer than candidates workers remaining, choose the worker with the lowest cost among them. Break the tie by the smallest index.</li> <li>A worker can only be chosen once.</li> </ul> <p>Return <em>the total cost to hire exactly </em><code>k</code><em> workers.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> costs = [17,12,10,2,7,2,11,20,8], k = 3, candidates = 4 <strong>Output:</strong> 11 <strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0. - In the first hiring round we choose the worker from [<u>17,12,10,2</u>,7,<u>2,11,20,8</u>]. The lowest cost is 2, and we break the tie by the smallest index, which is 3. The total cost = 0 + 2 = 2. - In the second hiring round we choose the worker from [<u>17,12,10,7</u>,<u>2,11,20,8</u>]. The lowest cost is 2 (index 4). The total cost = 2 + 2 = 4. - In the third hiring round we choose the worker from [<u>17,12,10,7,11,20,8</u>]. The lowest cost is 7 (index 3). The total cost = 4 + 7 = 11. Notice that the worker with index 3 was common in the first and last four workers. The total hiring cost is 11. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> costs = [1,2,4,1], k = 3, candidates = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0. - In the first hiring round we choose the worker from [<u>1,2,4,1</u>]. The lowest cost is 1, and we break the tie by the smallest index, which is 0. The total cost = 0 + 1 = 1. Notice that workers with index 1 and 2 are common in the first and last 3 workers. - In the second hiring round we choose the worker from [<u>2,4,1</u>]. The lowest cost is 1 (index 2). The total cost = 1 + 1 = 2. - In the third hiring round there are less than three candidates. We choose the worker from the remaining workers [<u>2,4</u>]. The lowest cost is 2 (index 0). The total cost = 2 + 2 = 4. The total hiring cost is 4. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= costs.length &lt;= 10<sup>5 </sup></code></li> <li><code>1 &lt;= costs[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k, candidates &lt;= costs.length</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n long long totalCost(vector<int>& costs, int k, int candidates) {\n int siz = costs.size();\n if(candidates*2>siz){\n sort(costs.begin(),costs.end());\n return accumulate(costs.begin(),costs.begin()+k,0LL);\n }\n priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> pq;\n for(int i=0;i<candidates;i++){\n pq.emplace(costs[i],i);\n pq.emplace(costs[siz-i-1],siz-i-1);\n }\n long long ans=0;\n int l=candidates,r=siz-candidates-1;\n while(k--){\n auto [cost,i]=pq.top();\n pq.pop();\n ans+=cost;\n if(l>r){\n continue;\n }\n if(i<l){\n pq.emplace(costs[l],l++);\n }\n else{\n pq.emplace(costs[r],r--);\n }\n }\n return ans;\n }\n};", "memory": "75595" }
2,553
<p>You are given a <strong>0-indexed</strong> integer array <code>costs</code> where <code>costs[i]</code> is the cost of hiring the <code>i<sup>th</sup></code> worker.</p> <p>You are also given two integers <code>k</code> and <code>candidates</code>. We want to hire exactly <code>k</code> workers according to the following rules:</p> <ul> <li>You will run <code>k</code> sessions and hire exactly one worker in each session.</li> <li>In each hiring session, choose the worker with the lowest cost from either the first <code>candidates</code> workers or the last <code>candidates</code> workers. Break the tie by the smallest index. <ul> <li>For example, if <code>costs = [3,2,7,7,1,2]</code> and <code>candidates = 2</code>, then in the first hiring session, we will choose the <code>4<sup>th</sup></code> worker because they have the lowest cost <code>[<u>3,2</u>,7,7,<u><strong>1</strong>,2</u>]</code>.</li> <li>In the second hiring session, we will choose <code>1<sup>st</sup></code> worker because they have the same lowest cost as <code>4<sup>th</sup></code> worker but they have the smallest index <code>[<u>3,<strong>2</strong></u>,7,<u>7,2</u>]</code>. Please note that the indexing may be changed in the process.</li> </ul> </li> <li>If there are fewer than candidates workers remaining, choose the worker with the lowest cost among them. Break the tie by the smallest index.</li> <li>A worker can only be chosen once.</li> </ul> <p>Return <em>the total cost to hire exactly </em><code>k</code><em> workers.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> costs = [17,12,10,2,7,2,11,20,8], k = 3, candidates = 4 <strong>Output:</strong> 11 <strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0. - In the first hiring round we choose the worker from [<u>17,12,10,2</u>,7,<u>2,11,20,8</u>]. The lowest cost is 2, and we break the tie by the smallest index, which is 3. The total cost = 0 + 2 = 2. - In the second hiring round we choose the worker from [<u>17,12,10,7</u>,<u>2,11,20,8</u>]. The lowest cost is 2 (index 4). The total cost = 2 + 2 = 4. - In the third hiring round we choose the worker from [<u>17,12,10,7,11,20,8</u>]. The lowest cost is 7 (index 3). The total cost = 4 + 7 = 11. Notice that the worker with index 3 was common in the first and last four workers. The total hiring cost is 11. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> costs = [1,2,4,1], k = 3, candidates = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0. - In the first hiring round we choose the worker from [<u>1,2,4,1</u>]. The lowest cost is 1, and we break the tie by the smallest index, which is 0. The total cost = 0 + 1 = 1. Notice that workers with index 1 and 2 are common in the first and last 3 workers. - In the second hiring round we choose the worker from [<u>2,4,1</u>]. The lowest cost is 1 (index 2). The total cost = 1 + 1 = 2. - In the third hiring round there are less than three candidates. We choose the worker from the remaining workers [<u>2,4</u>]. The lowest cost is 2 (index 0). The total cost = 2 + 2 = 4. The total hiring cost is 4. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= costs.length &lt;= 10<sup>5 </sup></code></li> <li><code>1 &lt;= costs[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k, candidates &lt;= costs.length</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n long long totalCost(vector<int>& costs, int k, int candidates) {\n priority_queue<int, vector<int>, greater<int>> pq1;\n priority_queue<int, vector<int>, greater<int>> pq2;\n int n = costs.size();\n\n if(n<=(long long)candidates*2){\n priority_queue<int> pq;\n for(int i = 0; i<k; i++){\n pq.push(costs[i]);\n }\n for(int i = k; i<costs.size(); i++){\n if(costs[i]<pq.top()){\n pq.pop();\n pq.push(costs[i]);\n }\n }\n long long cost = 0;\n while(!pq.empty()){\n cost +=pq.top();\n pq.pop();\n }\n return cost;\n }\n else{\n for(int i = 0; i<candidates; i++){\n pq1.push(costs[i]);\n pq2.push(costs[n-i-1]);\n }\n int i = candidates-1, j = n-candidates;\n long long cost = 0;\n for(int cnt = 0; cnt<k; cnt++){\n if(!pq1.empty() && (pq1.top()<=pq2.top() || pq2.empty())){\n cost+=pq1.top();\n pq1.pop();\n i++;\n if(i<j){\n pq1.push(costs[i]);\n }\n }\n else{\n cost+=pq2.top();\n pq2.pop();\n j--;\n if(i<j){\n pq2.push(costs[j]);\n }\n }\n }\n \n return cost;\n }\n }\n};", "memory": "76105" }
2,553
<p>You are given a <strong>0-indexed</strong> integer array <code>costs</code> where <code>costs[i]</code> is the cost of hiring the <code>i<sup>th</sup></code> worker.</p> <p>You are also given two integers <code>k</code> and <code>candidates</code>. We want to hire exactly <code>k</code> workers according to the following rules:</p> <ul> <li>You will run <code>k</code> sessions and hire exactly one worker in each session.</li> <li>In each hiring session, choose the worker with the lowest cost from either the first <code>candidates</code> workers or the last <code>candidates</code> workers. Break the tie by the smallest index. <ul> <li>For example, if <code>costs = [3,2,7,7,1,2]</code> and <code>candidates = 2</code>, then in the first hiring session, we will choose the <code>4<sup>th</sup></code> worker because they have the lowest cost <code>[<u>3,2</u>,7,7,<u><strong>1</strong>,2</u>]</code>.</li> <li>In the second hiring session, we will choose <code>1<sup>st</sup></code> worker because they have the same lowest cost as <code>4<sup>th</sup></code> worker but they have the smallest index <code>[<u>3,<strong>2</strong></u>,7,<u>7,2</u>]</code>. Please note that the indexing may be changed in the process.</li> </ul> </li> <li>If there are fewer than candidates workers remaining, choose the worker with the lowest cost among them. Break the tie by the smallest index.</li> <li>A worker can only be chosen once.</li> </ul> <p>Return <em>the total cost to hire exactly </em><code>k</code><em> workers.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> costs = [17,12,10,2,7,2,11,20,8], k = 3, candidates = 4 <strong>Output:</strong> 11 <strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0. - In the first hiring round we choose the worker from [<u>17,12,10,2</u>,7,<u>2,11,20,8</u>]. The lowest cost is 2, and we break the tie by the smallest index, which is 3. The total cost = 0 + 2 = 2. - In the second hiring round we choose the worker from [<u>17,12,10,7</u>,<u>2,11,20,8</u>]. The lowest cost is 2 (index 4). The total cost = 2 + 2 = 4. - In the third hiring round we choose the worker from [<u>17,12,10,7,11,20,8</u>]. The lowest cost is 7 (index 3). The total cost = 4 + 7 = 11. Notice that the worker with index 3 was common in the first and last four workers. The total hiring cost is 11. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> costs = [1,2,4,1], k = 3, candidates = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0. - In the first hiring round we choose the worker from [<u>1,2,4,1</u>]. The lowest cost is 1, and we break the tie by the smallest index, which is 0. The total cost = 0 + 1 = 1. Notice that workers with index 1 and 2 are common in the first and last 3 workers. - In the second hiring round we choose the worker from [<u>2,4,1</u>]. The lowest cost is 1 (index 2). The total cost = 1 + 1 = 2. - In the third hiring round there are less than three candidates. We choose the worker from the remaining workers [<u>2,4</u>]. The lowest cost is 2 (index 0). The total cost = 2 + 2 = 4. The total hiring cost is 4. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= costs.length &lt;= 10<sup>5 </sup></code></li> <li><code>1 &lt;= costs[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k, candidates &lt;= costs.length</code></li> </ul>
0
{ "code": "#pragma GCC optimize(\"O3,unroll-loops\")\n#pragma GCC target(\"avx2,bmi,bmi2,lzcnt,popcnt\")\n\nstatic const bool Booster = [](){\n std::ios_base::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::cout.tie(nullptr);\n return true;\n}();\n\nclass Solution {\npublic:\n long long totalCost(vector<int>& costs, int k, int candidates) {\n long long totCost = 0;\n\n if(costs.size() == 1)\n {\n return costs[0];\n }\n priority_queue<int, vector<int>, greater<int>> pq1;\n priority_queue<int, vector<int>, greater<int>> pq2;\n pq1.push(INT_MAX);\n pq2.push(INT_MAX);\n \n int pt1 = 0;\n int pt2 = costs.size() - 1;\n while(pt1<pt2 && pt1<candidates)\n {\n pq1.push(costs[pt1]);\n pq2.push(costs[pt2]);\n\n pt1++;\n pt2--;\n }\n\n while(k>0)\n {\n if(pt1>pt2)\n {\n break;\n }\n if(pq1.top() <= pq2.top())\n {\n cout<<pq1.top()<<\" \";\n totCost += pq1.top();\n pq1.pop();\n\n pq1.push(costs[pt1]);\n pt1++;\n }\n else\n {\n cout<<pq2.top()<<\" \";\n totCost += pq2.top();\n pq2.pop();\n\n pq2.push(costs[pt2]);\n pt2--;\n }\n k--;\n }\n\n while(k>0)\n {\n if(pq1.top() <= pq2.top())\n {\n cout<<pq1.top()<<\" \";\n totCost += pq1.top();\n pq1.pop();\n }\n else\n {\n cout<<pq2.top()<<\" \";\n totCost += pq2.top();\n pq2.pop();\n }\n k--;\n }\n return totCost;\n }\n};", "memory": "76105" }
2,553
<p>You are given a <strong>0-indexed</strong> integer array <code>costs</code> where <code>costs[i]</code> is the cost of hiring the <code>i<sup>th</sup></code> worker.</p> <p>You are also given two integers <code>k</code> and <code>candidates</code>. We want to hire exactly <code>k</code> workers according to the following rules:</p> <ul> <li>You will run <code>k</code> sessions and hire exactly one worker in each session.</li> <li>In each hiring session, choose the worker with the lowest cost from either the first <code>candidates</code> workers or the last <code>candidates</code> workers. Break the tie by the smallest index. <ul> <li>For example, if <code>costs = [3,2,7,7,1,2]</code> and <code>candidates = 2</code>, then in the first hiring session, we will choose the <code>4<sup>th</sup></code> worker because they have the lowest cost <code>[<u>3,2</u>,7,7,<u><strong>1</strong>,2</u>]</code>.</li> <li>In the second hiring session, we will choose <code>1<sup>st</sup></code> worker because they have the same lowest cost as <code>4<sup>th</sup></code> worker but they have the smallest index <code>[<u>3,<strong>2</strong></u>,7,<u>7,2</u>]</code>. Please note that the indexing may be changed in the process.</li> </ul> </li> <li>If there are fewer than candidates workers remaining, choose the worker with the lowest cost among them. Break the tie by the smallest index.</li> <li>A worker can only be chosen once.</li> </ul> <p>Return <em>the total cost to hire exactly </em><code>k</code><em> workers.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> costs = [17,12,10,2,7,2,11,20,8], k = 3, candidates = 4 <strong>Output:</strong> 11 <strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0. - In the first hiring round we choose the worker from [<u>17,12,10,2</u>,7,<u>2,11,20,8</u>]. The lowest cost is 2, and we break the tie by the smallest index, which is 3. The total cost = 0 + 2 = 2. - In the second hiring round we choose the worker from [<u>17,12,10,7</u>,<u>2,11,20,8</u>]. The lowest cost is 2 (index 4). The total cost = 2 + 2 = 4. - In the third hiring round we choose the worker from [<u>17,12,10,7,11,20,8</u>]. The lowest cost is 7 (index 3). The total cost = 4 + 7 = 11. Notice that the worker with index 3 was common in the first and last four workers. The total hiring cost is 11. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> costs = [1,2,4,1], k = 3, candidates = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0. - In the first hiring round we choose the worker from [<u>1,2,4,1</u>]. The lowest cost is 1, and we break the tie by the smallest index, which is 0. The total cost = 0 + 1 = 1. Notice that workers with index 1 and 2 are common in the first and last 3 workers. - In the second hiring round we choose the worker from [<u>2,4,1</u>]. The lowest cost is 1 (index 2). The total cost = 1 + 1 = 2. - In the third hiring round there are less than three candidates. We choose the worker from the remaining workers [<u>2,4</u>]. The lowest cost is 2 (index 0). The total cost = 2 + 2 = 4. The total hiring cost is 4. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= costs.length &lt;= 10<sup>5 </sup></code></li> <li><code>1 &lt;= costs[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k, candidates &lt;= costs.length</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n long long totalCost(vector<int>& costs, int k, int candidates) {\n priority_queue<int, vector<int>, greater<int>> pq, lpq, rpq;\n int n = costs.size();\n int i = 0;\n long long ans = 0;\n if (2 * candidates >= n) {\n for (auto &cost: costs) {\n pq.push(cost);\n }\n while (k--) {\n ans += pq.top();\n pq.pop();\n }\n } else {\n lpq.push(1e6);\n rpq.push(1e6);\n for (i = 0; i < candidates; i++) {\n lpq.push(costs[i]);\n rpq.push(costs[n - i - 1]);\n }\n int l = candidates;\n int r = n - candidates - 1;\n while (k--) {\n if (lpq.top() <= rpq.top()) {\n ans += lpq.top();\n lpq.pop();\n if (l <= r) {\n lpq.push(costs[l]);\n l++;\n }\n } else {\n ans += rpq.top();\n rpq.pop();\n if (l <= r) {\n rpq.push(costs[r]);\n r--;\n }\n }\n }\n }\n return ans;\n\n\n }\n};", "memory": "76615" }
2,553
<p>You are given a <strong>0-indexed</strong> integer array <code>costs</code> where <code>costs[i]</code> is the cost of hiring the <code>i<sup>th</sup></code> worker.</p> <p>You are also given two integers <code>k</code> and <code>candidates</code>. We want to hire exactly <code>k</code> workers according to the following rules:</p> <ul> <li>You will run <code>k</code> sessions and hire exactly one worker in each session.</li> <li>In each hiring session, choose the worker with the lowest cost from either the first <code>candidates</code> workers or the last <code>candidates</code> workers. Break the tie by the smallest index. <ul> <li>For example, if <code>costs = [3,2,7,7,1,2]</code> and <code>candidates = 2</code>, then in the first hiring session, we will choose the <code>4<sup>th</sup></code> worker because they have the lowest cost <code>[<u>3,2</u>,7,7,<u><strong>1</strong>,2</u>]</code>.</li> <li>In the second hiring session, we will choose <code>1<sup>st</sup></code> worker because they have the same lowest cost as <code>4<sup>th</sup></code> worker but they have the smallest index <code>[<u>3,<strong>2</strong></u>,7,<u>7,2</u>]</code>. Please note that the indexing may be changed in the process.</li> </ul> </li> <li>If there are fewer than candidates workers remaining, choose the worker with the lowest cost among them. Break the tie by the smallest index.</li> <li>A worker can only be chosen once.</li> </ul> <p>Return <em>the total cost to hire exactly </em><code>k</code><em> workers.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> costs = [17,12,10,2,7,2,11,20,8], k = 3, candidates = 4 <strong>Output:</strong> 11 <strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0. - In the first hiring round we choose the worker from [<u>17,12,10,2</u>,7,<u>2,11,20,8</u>]. The lowest cost is 2, and we break the tie by the smallest index, which is 3. The total cost = 0 + 2 = 2. - In the second hiring round we choose the worker from [<u>17,12,10,7</u>,<u>2,11,20,8</u>]. The lowest cost is 2 (index 4). The total cost = 2 + 2 = 4. - In the third hiring round we choose the worker from [<u>17,12,10,7,11,20,8</u>]. The lowest cost is 7 (index 3). The total cost = 4 + 7 = 11. Notice that the worker with index 3 was common in the first and last four workers. The total hiring cost is 11. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> costs = [1,2,4,1], k = 3, candidates = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0. - In the first hiring round we choose the worker from [<u>1,2,4,1</u>]. The lowest cost is 1, and we break the tie by the smallest index, which is 0. The total cost = 0 + 1 = 1. Notice that workers with index 1 and 2 are common in the first and last 3 workers. - In the second hiring round we choose the worker from [<u>2,4,1</u>]. The lowest cost is 1 (index 2). The total cost = 1 + 1 = 2. - In the third hiring round there are less than three candidates. We choose the worker from the remaining workers [<u>2,4</u>]. The lowest cost is 2 (index 0). The total cost = 2 + 2 = 4. The total hiring cost is 4. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= costs.length &lt;= 10<sup>5 </sup></code></li> <li><code>1 &lt;= costs[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k, candidates &lt;= costs.length</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n long long totalCost(vector<int>& costs, int k, int candidates) {\n long long output = 0;\n int left_index = candidates;\n int right_index = costs.size() - candidates - 1;\n priority_queue<int, vector<int>, greater<int>> left;\n if(left_index > right_index){\n for(int c : costs){\n left.push(c);\n }\n for(int i = 0; i < k; i++){\n output += left.top();\n left.pop();\n }\n return output;\n }\n\n priority_queue<int, vector<int>, greater<int>> right;\n for(int i = 0; i < candidates; i++){\n left.push(costs[i]);\n right.push(costs[costs.size() - i - 1]);\n }\n \n for(int i = 0; i < k; i++){\n if(right_index >= left_index){\n if(left.top() <= right.top()){\n output += left.top();\n cout << \"left \" << left.top() << endl;\n left.pop();\n\n left.push(costs[left_index]);\n left_index++;\n }\n else{\n output += right.top();\n cout << \"right \" << right.top() << endl;\n\n right.pop();\n right.push(costs[right_index]);\n right_index--;\n }\n }\n else{\n while(!right.empty()){\n left.push(right.top());\n right.pop();\n }\n cout << \"combined \" << left.top() << endl;\n output += left.top();\n left.pop();\n }\n }\n\n return output;\n }\n};", "memory": "76615" }
2,553
<p>You are given a <strong>0-indexed</strong> integer array <code>costs</code> where <code>costs[i]</code> is the cost of hiring the <code>i<sup>th</sup></code> worker.</p> <p>You are also given two integers <code>k</code> and <code>candidates</code>. We want to hire exactly <code>k</code> workers according to the following rules:</p> <ul> <li>You will run <code>k</code> sessions and hire exactly one worker in each session.</li> <li>In each hiring session, choose the worker with the lowest cost from either the first <code>candidates</code> workers or the last <code>candidates</code> workers. Break the tie by the smallest index. <ul> <li>For example, if <code>costs = [3,2,7,7,1,2]</code> and <code>candidates = 2</code>, then in the first hiring session, we will choose the <code>4<sup>th</sup></code> worker because they have the lowest cost <code>[<u>3,2</u>,7,7,<u><strong>1</strong>,2</u>]</code>.</li> <li>In the second hiring session, we will choose <code>1<sup>st</sup></code> worker because they have the same lowest cost as <code>4<sup>th</sup></code> worker but they have the smallest index <code>[<u>3,<strong>2</strong></u>,7,<u>7,2</u>]</code>. Please note that the indexing may be changed in the process.</li> </ul> </li> <li>If there are fewer than candidates workers remaining, choose the worker with the lowest cost among them. Break the tie by the smallest index.</li> <li>A worker can only be chosen once.</li> </ul> <p>Return <em>the total cost to hire exactly </em><code>k</code><em> workers.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> costs = [17,12,10,2,7,2,11,20,8], k = 3, candidates = 4 <strong>Output:</strong> 11 <strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0. - In the first hiring round we choose the worker from [<u>17,12,10,2</u>,7,<u>2,11,20,8</u>]. The lowest cost is 2, and we break the tie by the smallest index, which is 3. The total cost = 0 + 2 = 2. - In the second hiring round we choose the worker from [<u>17,12,10,7</u>,<u>2,11,20,8</u>]. The lowest cost is 2 (index 4). The total cost = 2 + 2 = 4. - In the third hiring round we choose the worker from [<u>17,12,10,7,11,20,8</u>]. The lowest cost is 7 (index 3). The total cost = 4 + 7 = 11. Notice that the worker with index 3 was common in the first and last four workers. The total hiring cost is 11. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> costs = [1,2,4,1], k = 3, candidates = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0. - In the first hiring round we choose the worker from [<u>1,2,4,1</u>]. The lowest cost is 1, and we break the tie by the smallest index, which is 0. The total cost = 0 + 1 = 1. Notice that workers with index 1 and 2 are common in the first and last 3 workers. - In the second hiring round we choose the worker from [<u>2,4,1</u>]. The lowest cost is 1 (index 2). The total cost = 1 + 1 = 2. - In the third hiring round there are less than three candidates. We choose the worker from the remaining workers [<u>2,4</u>]. The lowest cost is 2 (index 0). The total cost = 2 + 2 = 4. The total hiring cost is 4. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= costs.length &lt;= 10<sup>5 </sup></code></li> <li><code>1 &lt;= costs[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k, candidates &lt;= costs.length</code></li> </ul>
1
{ "code": "class Solution {\npublic:\n long long totalCost(vector<int>& costs, int k, int candidates) {\n int n = costs.size();\n long long ans = 0;\n priority_queue<int, vector<int>, greater<int>> pq1, pq2;\n int i = 0, j = n-1;\n int hired = 0;\n\n //T.C = O(K * logn)\n while(hired < k){ // O(K)\n while(pq1.size() < candidates && i <= j){ //O(log(candidaates))\n pq1.push(costs[i]);\n i++;\n }\n\n while(pq2.size() < candidates && j >= i){\n pq2.push(costs[j]);\n j--;\n }\n\n int min_pq1 = pq1.size() > 0 ? pq1.top() : INT_MAX;\n int min_pq2 = pq2.size() > 0 ? pq2.top() : INT_MAX;\n\n if(min_pq1 <= min_pq2){\n ans += min_pq1;\n pq1.pop(); //O(log(candidates))\n }\n else{\n ans += min_pq2;\n pq2.pop();\n }\n\n hired++;\n }\n\n return ans;\n }\n};", "memory": "77125" }
2,553
<p>You are given a <strong>0-indexed</strong> integer array <code>costs</code> where <code>costs[i]</code> is the cost of hiring the <code>i<sup>th</sup></code> worker.</p> <p>You are also given two integers <code>k</code> and <code>candidates</code>. We want to hire exactly <code>k</code> workers according to the following rules:</p> <ul> <li>You will run <code>k</code> sessions and hire exactly one worker in each session.</li> <li>In each hiring session, choose the worker with the lowest cost from either the first <code>candidates</code> workers or the last <code>candidates</code> workers. Break the tie by the smallest index. <ul> <li>For example, if <code>costs = [3,2,7,7,1,2]</code> and <code>candidates = 2</code>, then in the first hiring session, we will choose the <code>4<sup>th</sup></code> worker because they have the lowest cost <code>[<u>3,2</u>,7,7,<u><strong>1</strong>,2</u>]</code>.</li> <li>In the second hiring session, we will choose <code>1<sup>st</sup></code> worker because they have the same lowest cost as <code>4<sup>th</sup></code> worker but they have the smallest index <code>[<u>3,<strong>2</strong></u>,7,<u>7,2</u>]</code>. Please note that the indexing may be changed in the process.</li> </ul> </li> <li>If there are fewer than candidates workers remaining, choose the worker with the lowest cost among them. Break the tie by the smallest index.</li> <li>A worker can only be chosen once.</li> </ul> <p>Return <em>the total cost to hire exactly </em><code>k</code><em> workers.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> costs = [17,12,10,2,7,2,11,20,8], k = 3, candidates = 4 <strong>Output:</strong> 11 <strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0. - In the first hiring round we choose the worker from [<u>17,12,10,2</u>,7,<u>2,11,20,8</u>]. The lowest cost is 2, and we break the tie by the smallest index, which is 3. The total cost = 0 + 2 = 2. - In the second hiring round we choose the worker from [<u>17,12,10,7</u>,<u>2,11,20,8</u>]. The lowest cost is 2 (index 4). The total cost = 2 + 2 = 4. - In the third hiring round we choose the worker from [<u>17,12,10,7,11,20,8</u>]. The lowest cost is 7 (index 3). The total cost = 4 + 7 = 11. Notice that the worker with index 3 was common in the first and last four workers. The total hiring cost is 11. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> costs = [1,2,4,1], k = 3, candidates = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0. - In the first hiring round we choose the worker from [<u>1,2,4,1</u>]. The lowest cost is 1, and we break the tie by the smallest index, which is 0. The total cost = 0 + 1 = 1. Notice that workers with index 1 and 2 are common in the first and last 3 workers. - In the second hiring round we choose the worker from [<u>2,4,1</u>]. The lowest cost is 1 (index 2). The total cost = 1 + 1 = 2. - In the third hiring round there are less than three candidates. We choose the worker from the remaining workers [<u>2,4</u>]. The lowest cost is 2 (index 0). The total cost = 2 + 2 = 4. The total hiring cost is 4. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= costs.length &lt;= 10<sup>5 </sup></code></li> <li><code>1 &lt;= costs[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k, candidates &lt;= costs.length</code></li> </ul>
1
{ "code": "class Solution {\npublic:\n long long totalCost(vector<int>& costs, int k, int candidates) {\n priority_queue<int, vector<int> ,greater<int>>q1;\n priority_queue<int ,vector<int> ,greater<int>>q2;\n long long ans = 0;\n int n = costs.size();\n int i =0;int j = n-1;\n while(k--){\n while(q1.size()<candidates&&i<=j){\n q1.push(costs[i]);\n i++;\n }\n while(q2.size()<candidates&&j>=i){\n q2.push(costs[j]);\n j--;\n }\n int a = (!q1.empty())?q1.top():INT_MAX;\n int b = (!q2.empty())?q2.top():INT_MAX;\n if(a<=b){\n ans+=a;\n q1.pop();\n }\n else{\n ans+=b;\n q2.pop();\n }\n }\n return ans;\n }\n};", "memory": "77125" }