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...
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]...
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...
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=...
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...
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 ret...
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...
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 ...
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...
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...
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...
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...
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...
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 ...
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...
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...
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...
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, ...
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...
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 ...
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...
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 ...
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...
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 ...
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...
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...
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...
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(nullpt...
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...
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 mi...
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...
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 ...
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...
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...
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...
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...
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...
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 ...
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...
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...
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...
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...
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...
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...
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...
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 ListNod...
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...
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 ListNod...
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...
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...
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...
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 ListNod...
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...
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...
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...
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 ListNod...
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...
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...
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...
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...
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...
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...
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...
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...
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...
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...
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...
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 ...
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...
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 vecto...
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...
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...
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>...
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...
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>...
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...
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>...
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 ...
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>...
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 ...
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>...
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 ...
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>...
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 t...
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>...
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 ...
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>...
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;...
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>...
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<i...
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>...
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)s...
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>...
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...
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>...
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(aut...
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>...
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 r...
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>...
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 ...
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>...
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 ...
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>...
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 ...
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>...
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,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>...
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} requi...
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>...
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 ...
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>...
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 ...
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>...
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<...
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>...
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 ...
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>...
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...
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>...
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)r...
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>...
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][bi...
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>...
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,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>...
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((mas...
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>...
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 ...
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>...
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 ...
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>...
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 ...
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>...
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 ...
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>...
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,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>...
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 ...
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>...
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...
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>...
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...
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>...
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 ...
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>...
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...
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>...
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(i...
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>...
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<<...
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>...
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 ...
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>...
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...
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>...
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,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>...
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<<...
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>...
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 ...
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>...
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...
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>...
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=...
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>...
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(in...
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>...
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]...
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 fol...
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){...
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 fol...
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 ...
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 fol...
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--) a...
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 fol...
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 ...
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 fol...
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 ...
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 fol...
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...
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 fol...
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::...
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 fol...
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 ...
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 fol...
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 p...
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 fol...
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...
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 fol...
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 pr...
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 fol...
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 totalCo...
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 fol...
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 &co...
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 fol...
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...
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 fol...
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 ...
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 fol...
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 ...