id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
373
<p>You are given two integer arrays <code>nums1</code> and <code>nums2</code> sorted in <strong>non-decreasing&nbsp;order</strong> and an integer <code>k</code>.</p> <p>Define a pair <code>(u, v)</code> which consists of one element from the first array and one element from the second array.</p> <p>Return <em>the</em...
3
{ "code": "int lower(int x,vector<int>& arr){\n int l=0;\n int r=arr.size()-1;\n while(l<=r){\n int m=l+(r-l)/2;\n if(arr[m] <= x){\n l = m+1;\n }\n else{\n r=m-1;\n }\n }\n return r;\n}\nbool check(int s,int k,vector<int>& nums1, vector<int>& nu...
373
<p>You are given two integer arrays <code>nums1</code> and <code>nums2</code> sorted in <strong>non-decreasing&nbsp;order</strong> and an integer <code>k</code>.</p> <p>Define a pair <code>(u, v)</code> which consists of one element from the first array and one element from the second array.</p> <p>Return <em>the</em...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> kSmallestPairs(vector<int>& nums1, vector<int>& nums2, int k) {\n vector<vector<int>> res;\n priority_queue<pair<int,vector<int>>> pq;\n \n int n = nums1.size(), m = nums2.size();\n for(int i=0;i<n;i++){\n\n for(int j =0;j<m;j++){\...
373
<p>You are given two integer arrays <code>nums1</code> and <code>nums2</code> sorted in <strong>non-decreasing&nbsp;order</strong> and an integer <code>k</code>.</p> <p>Define a pair <code>(u, v)</code> which consists of one element from the first array and one element from the second array.</p> <p>Return <em>the</em...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> kSmallestPairs(vector<int>& nums1, vector<int>& nums2, int k) {\n vector<vector<int>> res;\n priority_queue<pair<int, vector<int>>> pq;\n for (int num1 : nums1) {\n for (int num2 : nums2) {\n int sum = num1 + ...
373
<p>You are given two integer arrays <code>nums1</code> and <code>nums2</code> sorted in <strong>non-decreasing&nbsp;order</strong> and an integer <code>k</code>.</p> <p>Define a pair <code>(u, v)</code> which consists of one element from the first array and one element from the second array.</p> <p>Return <em>the</em...
3
{ "code": "typedef vector<int> vi;\ntypedef pair<int,vector<int>> pi;\nclass Solution {\npublic:\n vector<vector<int>> kSmallestPairs(vector<int>& nums1, vector<int>& nums2, int k) {\n vector<vi> Ans;\n priority_queue<pi> pq;\n int i=0,j=0;\n while(i<nums1.size() && j<nums2.size()){\n ...
373
<p>You are given two integer arrays <code>nums1</code> and <code>nums2</code> sorted in <strong>non-decreasing&nbsp;order</strong> and an integer <code>k</code>.</p> <p>Define a pair <code>(u, v)</code> which consists of one element from the first array and one element from the second array.</p> <p>Return <em>the</em...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> kSmallestPairs(vector<int>& nums1, vector<int>& nums2, int k) {\n vector<vector<int>> res {};\n auto comparator = [](const pair<int, vector<int>>& a, const pair<int, vector<int>>& b) { return a.first < b.first; };\n priority_queue<pair...
373
<p>You are given two integer arrays <code>nums1</code> and <code>nums2</code> sorted in <strong>non-decreasing&nbsp;order</strong> and an integer <code>k</code>.</p> <p>Define a pair <code>(u, v)</code> which consists of one element from the first array and one element from the second array.</p> <p>Return <em>the</em...
3
{ "code": "class Solution {\npublic:\n typedef pair<int, vector<int>> P;\n vector<vector<int>> kSmallestPairs(vector<int>& nums1, vector<int>& nums2, int k)\n {\n priority_queue<P, vector<P>, greater<P>> min_heap;\n set<vector<int>> s1;\n min_heap.push({nums1[0]+nums2[0], {0, 0}});\n ...
373
<p>You are given two integer arrays <code>nums1</code> and <code>nums2</code> sorted in <strong>non-decreasing&nbsp;order</strong> and an integer <code>k</code>.</p> <p>Define a pair <code>(u, v)</code> which consists of one element from the first array and one element from the second array.</p> <p>Return <em>the</em...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> kSmallestPairs(vector<int>& nums1, vector<int>& nums2, int k) {\n int n=nums1.size();\n int m=nums2.size();\n priority_queue<pair<int,vector<int>>,vector<pair<int,vector<int>>>>pq;\n vector<vector<int>>ans;\n int cnt=0...
373
<p>You are given two integer arrays <code>nums1</code> and <code>nums2</code> sorted in <strong>non-decreasing&nbsp;order</strong> and an integer <code>k</code>.</p> <p>Define a pair <code>(u, v)</code> which consists of one element from the first array and one element from the second array.</p> <p>Return <em>the</em...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> kSmallestPairs(vector<int>& nums1, vector<int>& nums2, int k) {\n vector<vector<int>> ans;\n\n priority_queue<pair<int, vector<int>>> heap;\n for(int i = 0; i < nums1.size(); i++)\n {\n for(int j = 0; j < nums2.size()...
373
<p>You are given two integer arrays <code>nums1</code> and <code>nums2</code> sorted in <strong>non-decreasing&nbsp;order</strong> and an integer <code>k</code>.</p> <p>Define a pair <code>(u, v)</code> which consists of one element from the first array and one element from the second array.</p> <p>Return <em>the</em...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> kSmallestPairs(vector<int>& nums1, vector<int>& nums2, int k) {\n vector<vector<int>> ans;\n multimap <int,vector<int> > candidates;\n for(int i=0;i<k && i<nums1.size();i++){\n vector<int> current;\n current.push_...
373
<p>You are given two integer arrays <code>nums1</code> and <code>nums2</code> sorted in <strong>non-decreasing&nbsp;order</strong> and an integer <code>k</code>.</p> <p>Define a pair <code>(u, v)</code> which consists of one element from the first array and one element from the second array.</p> <p>Return <em>the</em...
3
{ "code": "class Solution {\n vector<vector<int>> minheap;\n\npublic:\n vector<vector<int>> kSmallestPairs(vector<int>& nums1, vector<int>& nums2,\n int k) {\n vector<vector<int>> ans;\n const int m = nums1.size();\n const int n = nums2.size();\n ...
373
<p>You are given two integer arrays <code>nums1</code> and <code>nums2</code> sorted in <strong>non-decreasing&nbsp;order</strong> and an integer <code>k</code>.</p> <p>Define a pair <code>(u, v)</code> which consists of one element from the first array and one element from the second array.</p> <p>Return <em>the</em...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> kSmallestPairs(vector<int>& nums1, vector<int>& nums2, int k) {\n int ln1 = nums1.size();\n int ln2 = nums2.size();\n vector<vector<int>> ret(k);\n priority_queue<vector<int>, vector<vector<int>>, greater<>> pq;\n for(int...
373
<p>You are given two integer arrays <code>nums1</code> and <code>nums2</code> sorted in <strong>non-decreasing&nbsp;order</strong> and an integer <code>k</code>.</p> <p>Define a pair <code>(u, v)</code> which consists of one element from the first array and one element from the second array.</p> <p>Return <em>the</em...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> kSmallestPairs(vector<int>& nums1, vector<int>& nums2, int k) {\n int ln1 = nums1.size();\n int ln2 = nums2.size();\n vector<vector<int>> ret(k);\n priority_queue<vector<int>, vector<vector<int>>, greater<>> pq;\n for(int...
373
<p>You are given two integer arrays <code>nums1</code> and <code>nums2</code> sorted in <strong>non-decreasing&nbsp;order</strong> and an integer <code>k</code>.</p> <p>Define a pair <code>(u, v)</code> which consists of one element from the first array and one element from the second array.</p> <p>Return <em>the</em...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> kSmallestPairs(vector<int>& nums1, vector<int>& nums2, int k) {\n int ln1 = nums1.size();\n int ln2 = nums2.size();\n priority_queue<vector<int>, vector<vector<int>>, greater<>> pq;\n for(int i=0;i<ln2;i++){\n pq.push...
373
<p>You are given two integer arrays <code>nums1</code> and <code>nums2</code> sorted in <strong>non-decreasing&nbsp;order</strong> and an integer <code>k</code>.</p> <p>Define a pair <code>(u, v)</code> which consists of one element from the first array and one element from the second array.</p> <p>Return <em>the</em...
3
{ "code": "class CompareHeaps {\n public:\n bool operator()(vector<int> &pair1, vector<int> &pair2){\n int sum1 = pair1[0] + pair1[1];\n int sum2 = pair2[0] + pair2[1];\n return sum1 < sum2;\n }\n};\n\n\n\nclass Solution {\npublic:\n vector<vector<int>> kSmallestPairs(vector<int>& nums1, vector<int...
373
<p>You are given two integer arrays <code>nums1</code> and <code>nums2</code> sorted in <strong>non-decreasing&nbsp;order</strong> and an integer <code>k</code>.</p> <p>Define a pair <code>(u, v)</code> which consists of one element from the first array and one element from the second array.</p> <p>Return <em>the</em...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> kSmallestPairs(vector<int>& nums1, vector<int>& nums2, int k) {\n int ln1 = nums1.size();\n int ln2 = nums2.size();\n priority_queue<vector<int>, vector<vector<int>>, greater<>> pq;\n for(int i=0;i<ln2;i++){\n pq.push...
373
<p>You are given two integer arrays <code>nums1</code> and <code>nums2</code> sorted in <strong>non-decreasing&nbsp;order</strong> and an integer <code>k</code>.</p> <p>Define a pair <code>(u, v)</code> which consists of one element from the first array and one element from the second array.</p> <p>Return <em>the</em...
3
{ "code": "class Solution {\npublic:\n string stringify(vector <int> curr){\n return to_string(curr[0]) + \",\" + to_string(curr[1]);\n }\n vector<vector<int>> kSmallestPairs(vector<int>& nums1, vector<int>& nums2, int k) {\n priority_queue <pair <int, vector <int>>, vector<pair <int, vector <i...
373
<p>You are given two integer arrays <code>nums1</code> and <code>nums2</code> sorted in <strong>non-decreasing&nbsp;order</strong> and an integer <code>k</code>.</p> <p>Define a pair <code>(u, v)</code> which consists of one element from the first array and one element from the second array.</p> <p>Return <em>the</em...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> kSmallestPairs(vector<int>& nums1, vector<int>& nums2, int k) {\n // Maintain a max-heap containing the smallest k elements currently.\n vector< vector<int> > maxHeap;\n make_heap(maxHeap.begin(), maxHeap.end());\n for(int i=0; ...
373
<p>You are given two integer arrays <code>nums1</code> and <code>nums2</code> sorted in <strong>non-decreasing&nbsp;order</strong> and an integer <code>k</code>.</p> <p>Define a pair <code>(u, v)</code> which consists of one element from the first array and one element from the second array.</p> <p>Return <em>the</em...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> kSmallestPairs(vector<int>& nums1, vector<int>& nums2, int k) {\n // Maintain a max-heap containing the smallest k elements currently.\n vector< vector<int> > maxHeap;\n make_heap(maxHeap.begin(), maxHeap.end());\n for(int i=0; ...
373
<p>You are given two integer arrays <code>nums1</code> and <code>nums2</code> sorted in <strong>non-decreasing&nbsp;order</strong> and an integer <code>k</code>.</p> <p>Define a pair <code>(u, v)</code> which consists of one element from the first array and one element from the second array.</p> <p>Return <em>the</em...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> kSmallestPairs(vector<int>& nums1, vector<int>& nums2, int k) {\n // Maintain a max-heap containing the smallest k elements currently.\n vector< vector<int> > maxHeap;\n make_heap(maxHeap.begin(), maxHeap.end());\n for(int i=0; ...
373
<p>You are given two integer arrays <code>nums1</code> and <code>nums2</code> sorted in <strong>non-decreasing&nbsp;order</strong> and an integer <code>k</code>.</p> <p>Define a pair <code>(u, v)</code> which consists of one element from the first array and one element from the second array.</p> <p>Return <em>the</em...
3
{ "code": "class Solution {\n struct comp{\n bool operator()(vector<int>&a, vector<int>&b){\n return a[0]+ a[1] < b[0] + b[1];\n }\n };\npublic:\n vector<vector<int>> kSmallestPairs(vector<int>& nums1, vector<int>& nums2, int k) {\n int lCtr= 0;\n int rCtr =0;\n ...
373
<p>You are given two integer arrays <code>nums1</code> and <code>nums2</code> sorted in <strong>non-decreasing&nbsp;order</strong> and an integer <code>k</code>.</p> <p>Define a pair <code>(u, v)</code> which consists of one element from the first array and one element from the second array.</p> <p>Return <em>the</em...
3
{ "code": "class Solution {\npublic:\n typedef pair<long, vector<int>> pvi;\n vector<vector<int>> kSmallestPairs(vector<int>& nums1, vector<int>& nums2, int k) {\n priority_queue<pvi> maxh; \n int n = nums1.size();\n int m = nums2.size();\n for(int i=0; i<n; i++){\n ...
373
<p>You are given two integer arrays <code>nums1</code> and <code>nums2</code> sorted in <strong>non-decreasing&nbsp;order</strong> and an integer <code>k</code>.</p> <p>Define a pair <code>(u, v)</code> which consists of one element from the first array and one element from the second array.</p> <p>Return <em>the</em...
3
{ "code": "class Solution {\npublic:\n //brute force is O(n^2)\n //till the greatest element is smaller than the next element\n vector<vector<int>> kSmallestPairs(vector<int>& nums1, vector<int>& nums2, int k) {\n int s1 = nums1.size(), s2 = nums2.size();\n priority_queue< vector<int>, vector<v...
373
<p>You are given two integer arrays <code>nums1</code> and <code>nums2</code> sorted in <strong>non-decreasing&nbsp;order</strong> and an integer <code>k</code>.</p> <p>Define a pair <code>(u, v)</code> which consists of one element from the first array and one element from the second array.</p> <p>Return <em>the</em...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> kSmallestPairs(vector<int>& v1, vector<int>& v2, int k) {\n priority_queue<vector<int>,vector<vector<int>>,greater<vector<int>>>pq;\n for(int i=0;i<v2.size();i++) pq.push({v1[0]+v2[i],0,i});\n vector<vector<int>>ans;\n while(k--...
373
<p>You are given two integer arrays <code>nums1</code> and <code>nums2</code> sorted in <strong>non-decreasing&nbsp;order</strong> and an integer <code>k</code>.</p> <p>Define a pair <code>(u, v)</code> which consists of one element from the first array and one element from the second array.</p> <p>Return <em>the</em...
3
{ "code": "template<class T>\nusing min_heap = priority_queue<T, vector<T>, greater<T>>;\n\nclass Solution {\npublic:\n vector<vector<int>> kSmallestPairs(vector<int>& nums1, vector<int>& nums2, int k) {\n int m = nums1.size(), n = nums2.size();\n\n vector<vector<int>> res;\n\n min_heap<vector...
373
<p>You are given two integer arrays <code>nums1</code> and <code>nums2</code> sorted in <strong>non-decreasing&nbsp;order</strong> and an integer <code>k</code>.</p> <p>Define a pair <code>(u, v)</code> which consists of one element from the first array and one element from the second array.</p> <p>Return <em>the</em...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> kSmallestPairs(vector<int>& v1, vector<int>& v2, int k) {\n priority_queue<vector<int>,vector<vector<int>>,greater<vector<int>>>pq;\n for(int i=0;i<v2.size();i++) pq.push({v1[0]+v2[i],0,i});\n vector<vector<int>>ans;\n int v,i,j...
373
<p>You are given two integer arrays <code>nums1</code> and <code>nums2</code> sorted in <strong>non-decreasing&nbsp;order</strong> and an integer <code>k</code>.</p> <p>Define a pair <code>(u, v)</code> which consists of one element from the first array and one element from the second array.</p> <p>Return <em>the</em...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> kSmallestPairs(vector<int>& nums1, vector<int>& nums2, int k) {\n priority_queue<vector<int>, vector<vector<int>>, greater<vector<int>>>pq;\n int i,j;\n\n for(i=0;i<nums2.size();i++){\n int sum = nums1[0] + nums2[i];\n ...
374
<p>We are playing the Guess Game. The game is as follows:</p> <p>I pick a number from <code>1</code> to <code>n</code>. You have to guess which number I picked.</p> <p>Every time you guess wrong, I will tell you whether the number I picked is higher or lower than your guess.</p> <p>You call a pre-defined API <code>i...
0
{ "code": "/** \n * Forward declaration of guess API.\n * @param num your guess\n * @return \t -1 if num is higher than the picked number\n *\t\t\t 1 if num is lower than the picked number\n * otherwise return 0\n * int guess(int num);\n */\n\nclass Solution {\npublic:\n int guessNumber(i...
374
<p>We are playing the Guess Game. The game is as follows:</p> <p>I pick a number from <code>1</code> to <code>n</code>. You have to guess which number I picked.</p> <p>Every time you guess wrong, I will tell you whether the number I picked is higher or lower than your guess.</p> <p>You call a pre-defined API <code>i...
0
{ "code": "/** \n * Forward declaration of guess API.\n * @param num your guess\n * @return \t -1 if num is higher than the picked number\n *\t\t\t 1 if num is lower than the picked number\n * otherwise return 0\n * int guess(int num);\n */\n\nclass Solution {\npublic:\n int guessNumber(i...
374
<p>We are playing the Guess Game. The game is as follows:</p> <p>I pick a number from <code>1</code> to <code>n</code>. You have to guess which number I picked.</p> <p>Every time you guess wrong, I will tell you whether the number I picked is higher or lower than your guess.</p> <p>You call a pre-defined API <code>i...
0
{ "code": "/** \n * Forward declaration of guess API.\n * @param num your guess\n * @return \t -1 if num is higher than the picked number\n *\t\t\t 1 if num is lower than the picked number\n * otherwise return 0\n * int guess(int num);\n */\n\nclass Solution {\npublic:\n int guessNumber(i...
374
<p>We are playing the Guess Game. The game is as follows:</p> <p>I pick a number from <code>1</code> to <code>n</code>. You have to guess which number I picked.</p> <p>Every time you guess wrong, I will tell you whether the number I picked is higher or lower than your guess.</p> <p>You call a pre-defined API <code>i...
0
{ "code": "/** \n * Forward declaration of guess API.\n * @param num your guess\n * @return \t -1 if num is higher than the picked number\n *\t\t\t 1 if num is lower than the picked number\n * otherwise return 0\n * int guess(int num);\n */\n\nclass Solution {\npublic:\n int guessNumber(i...
375
<p>We are playing the Guessing Game. The game will work as follows:</p> <ol> <li>I pick a number between&nbsp;<code>1</code>&nbsp;and&nbsp;<code>n</code>.</li> <li>You guess a number.</li> <li>If you guess the right number, <strong>you win the game</strong>.</li> <li>If you guess the wrong number, then I will tell...
0
{ "code": "class Solution {\npublic:\n\n int dp[201][201];\n\n int rec(int l, int r){\n if(l>=r) return 0;\n if(dp[l][r]!=-1) return dp[l][r];\n int ans = 1e9;\n for(int i=l;i<=r;i++){\n ans = min(ans,max(rec(l,i-1),rec(i+1,r))+i);\n }\n // cout<<l<<\" \"<<r<...
375
<p>We are playing the Guessing Game. The game will work as follows:</p> <ol> <li>I pick a number between&nbsp;<code>1</code>&nbsp;and&nbsp;<code>n</code>.</li> <li>You guess a number.</li> <li>If you guess the right number, <strong>you win the game</strong>.</li> <li>If you guess the wrong number, then I will tell...
0
{ "code": "class Solution {\npublic:\n int dp[201][201];\n int solve(int l, int r)\n {\n if(l>=r)\n return 0;\n\n if(dp[l][r]!=-1)\n return dp[l][r];\n\n int a = INT_MAX;\n\n for(int i=l;i<=r;i++)\n a = min(a, max(i + solve(l, i-1), i+solve(i+1, r)));\n\n ...
375
<p>We are playing the Guessing Game. The game will work as follows:</p> <ol> <li>I pick a number between&nbsp;<code>1</code>&nbsp;and&nbsp;<code>n</code>.</li> <li>You guess a number.</li> <li>If you guess the right number, <strong>you win the game</strong>.</li> <li>If you guess the wrong number, then I will tell...
0
{ "code": "class Solution {\npublic:\n int dp[210][210];\n int rec(int l, int r){\n if(l>=r)return 0;\n\n int ans=1e9;\n if(dp[l][r]!=-1)return dp[l][r];\n for(int i=l;i<=r;i++){\n ans=min(ans,i+max(rec(l,i-1),rec(i+1,r)));\n }\n return dp[l][r]=ans;\n }\n...
375
<p>We are playing the Guessing Game. The game will work as follows:</p> <ol> <li>I pick a number between&nbsp;<code>1</code>&nbsp;and&nbsp;<code>n</code>.</li> <li>You guess a number.</li> <li>If you guess the right number, <strong>you win the game</strong>.</li> <li>If you guess the wrong number, then I will tell...
0
{ "code": "/*\n * @lc app=leetcode id=375 lang=cpp\n *\n * [375] Guess Number Higher or Lower II\n */\n\n// @lc code=start\nclass Solution {\n\npublic:\n typedef long long ll;\n ll dp[205][205];\n\n ll cnt(int l,int r){\n if(l + 1 == r)\n return dp[l][r] = l;\n if(dp[l][r] < INT_MAX)...
375
<p>We are playing the Guessing Game. The game will work as follows:</p> <ol> <li>I pick a number between&nbsp;<code>1</code>&nbsp;and&nbsp;<code>n</code>.</li> <li>You guess a number.</li> <li>If you guess the right number, <strong>you win the game</strong>.</li> <li>If you guess the wrong number, then I will tell...
0
{ "code": "/*\n * @lc app=leetcode id=375 lang=cpp\n *\n * [375] Guess Number Higher or Lower II\n */\n\n// @lc code=start\nclass Solution {\n\npublic:\n typedef long long ll;\n ll dp[205][205];\n\n ll cnt(int l,int r){\n if(l + 1 == r)\n return dp[l][r] = l;\n if(dp[l][r] < INT_MAX)...
375
<p>We are playing the Guessing Game. The game will work as follows:</p> <ol> <li>I pick a number between&nbsp;<code>1</code>&nbsp;and&nbsp;<code>n</code>.</li> <li>You guess a number.</li> <li>If you guess the right number, <strong>you win the game</strong>.</li> <li>If you guess the wrong number, then I will tell...
0
{ "code": "class Solution {\nprivate:\n int solve(vector<int>& list, vector<vector<int>>& dp, int left, int right){\n if (right < left) return 0;\n if (left == right) return dp[left][right] = list[left];\n if (dp[left][right] != -1) return dp[left][right];\n int k = INT_MAX;\n fo...
375
<p>We are playing the Guessing Game. The game will work as follows:</p> <ol> <li>I pick a number between&nbsp;<code>1</code>&nbsp;and&nbsp;<code>n</code>.</li> <li>You guess a number.</li> <li>If you guess the right number, <strong>you win the game</strong>.</li> <li>If you guess the wrong number, then I will tell...
0
{ "code": "class Solution {\npublic:\n \n int dp[210][210];\n \n int helper(long l, long r, long n)\n {\n if(l==r) return 0;\n \n long ans = INT_MAX;\n \n if(dp[l][r]) return dp[l][r];\n \n // 3 cases possible;\n //either the guess is correct, gre...
375
<p>We are playing the Guessing Game. The game will work as follows:</p> <ol> <li>I pick a number between&nbsp;<code>1</code>&nbsp;and&nbsp;<code>n</code>.</li> <li>You guess a number.</li> <li>If you guess the right number, <strong>you win the game</strong>.</li> <li>If you guess the wrong number, then I will tell...
0
{ "code": "class Solution {\n int dp[210][210];\npublic:\n int helper(long i, long j, long n){\n if(i==j) return 0;\n if(dp[i][j]) return dp[i][j];\n long ans=INT_MAX;\n for(long k=i;k<=j;k++){\n long t1=0,t2=0,t3=0;\n t1=k;\n if(k<n){\n t2...
375
<p>We are playing the Guessing Game. The game will work as follows:</p> <ol> <li>I pick a number between&nbsp;<code>1</code>&nbsp;and&nbsp;<code>n</code>.</li> <li>You guess a number.</li> <li>If you guess the right number, <strong>you win the game</strong>.</li> <li>If you guess the wrong number, then I will tell...
0
{ "code": "class Solution {\npublic:\n int dp[210][210];\n \n // this helper function calculate the amount of money needed if the guessed number lies between \"l\" and \"r\". \n int helper(long l, long r, long n){\n if(l == r)return 0; // the guessed number lies between \"l\" and \"r\" and, if \"l\...
375
<p>We are playing the Guessing Game. The game will work as follows:</p> <ol> <li>I pick a number between&nbsp;<code>1</code>&nbsp;and&nbsp;<code>n</code>.</li> <li>You guess a number.</li> <li>If you guess the right number, <strong>you win the game</strong>.</li> <li>If you guess the wrong number, then I will tell...
0
{ "code": "class Solution {\nprivate:\n vector<vector<int>> cache; // cache[end][start] -> min amount needed to find any number in start..end\n\npublic:\n int getMoneyAmount(int n) {\n cache.emplace_back();\n\n for (int i = 1; i <= n; i++) {\n cache.emplace_back(i, -1);\n }\n\n ...
375
<p>We are playing the Guessing Game. The game will work as follows:</p> <ol> <li>I pick a number between&nbsp;<code>1</code>&nbsp;and&nbsp;<code>n</code>.</li> <li>You guess a number.</li> <li>If you guess the right number, <strong>you win the game</strong>.</li> <li>If you guess the wrong number, then I will tell...
0
{ "code": "class Solution {\npublic:\n long long dp[205][205];\n long long solve(long long l,long long r,long long n){\n if(l==r) return 0;\n if(dp[l][r] != -1) return dp[l][r];\n long long ans = INT_MAX;\n for(long long i = l;i<=r;i++){\n long long a = 0,b = 0 ,c = 0;\n ...
375
<p>We are playing the Guessing Game. The game will work as follows:</p> <ol> <li>I pick a number between&nbsp;<code>1</code>&nbsp;and&nbsp;<code>n</code>.</li> <li>You guess a number.</li> <li>If you guess the right number, <strong>you win the game</strong>.</li> <li>If you guess the wrong number, then I will tell...
0
{ "code": "class Solution {\npublic:\n int getMoneyAmount(int n) {\n vector<vector<int>> cost;\n for(int i=0; i<n; i++) {\n cost.push_back(vector<int>(n, 0));\n }\n return getMoneyAmount(1, n, cost);\n }\n\n int getMoneyAmount(int start, int end, vector<vector<int>> &co...
375
<p>We are playing the Guessing Game. The game will work as follows:</p> <ol> <li>I pick a number between&nbsp;<code>1</code>&nbsp;and&nbsp;<code>n</code>.</li> <li>You guess a number.</li> <li>If you guess the right number, <strong>you win the game</strong>.</li> <li>If you guess the wrong number, then I will tell...
0
{ "code": "class Solution {\npublic:\n\n int SolveUsingRec(int s, int e)\n {\n if(s >=e || s < 0)\n {\n return 0;\n }\n \n int final_ans = INT_MAX;\n for(int i = s; i<=e;i++)\n {\n int leftKaAns = SolveUsingRec(s, i - 1);\n int ri...
375
<p>We are playing the Guessing Game. The game will work as follows:</p> <ol> <li>I pick a number between&nbsp;<code>1</code>&nbsp;and&nbsp;<code>n</code>.</li> <li>You guess a number.</li> <li>If you guess the right number, <strong>you win the game</strong>.</li> <li>If you guess the wrong number, then I will tell...
0
{ "code": "class Solution {\npublic:\nint solve(int start, int end) {\n if(start>=end)\n return 0;\n int maxi=INT_MAX;\n for(int i=start; i<=end; i++) {\n maxi=min(maxi, i+max(solve(start,i-1), solve(i+1, end)));\n }\n return maxi;\n}\nint solveMem(int start, int end, vector<vector<int>...
375
<p>We are playing the Guessing Game. The game will work as follows:</p> <ol> <li>I pick a number between&nbsp;<code>1</code>&nbsp;and&nbsp;<code>n</code>.</li> <li>You guess a number.</li> <li>If you guess the right number, <strong>you win the game</strong>.</li> <li>If you guess the wrong number, then I will tell...
0
{ "code": "class Solution {\nprivate:\n //1 : recursion : tle\n int solveRec(int start, int end){\n if(start >= end){\n return 0;\n }\n\n int ans = INT_MAX;\n for(int i=start; i<=end; i++){\n ans = min(ans, i + max(solveRec(start, i-1), solveRec(i+1, end)));\n ...
375
<p>We are playing the Guessing Game. The game will work as follows:</p> <ol> <li>I pick a number between&nbsp;<code>1</code>&nbsp;and&nbsp;<code>n</code>.</li> <li>You guess a number.</li> <li>If you guess the right number, <strong>you win the game</strong>.</li> <li>If you guess the wrong number, then I will tell...
0
{ "code": "class Solution {\npublic:\n\n int calculateCost(int start,int end,vector<vector<int>> &dp){\n if(start>=end) return 0;\n if(dp[start][end]!=-1) return dp[start][end];\n int minCost=INT_MAX;\n for(int guess=(start+end)/2;guess<=end;guess++){\n int cost=guess+max(cal...
375
<p>We are playing the Guessing Game. The game will work as follows:</p> <ol> <li>I pick a number between&nbsp;<code>1</code>&nbsp;and&nbsp;<code>n</code>.</li> <li>You guess a number.</li> <li>If you guess the right number, <strong>you win the game</strong>.</li> <li>If you guess the wrong number, then I will tell...
1
{ "code": "class Solution {\npublic:\n\n // int solvebyrec(int start, int end){\n // if(start>=end){\n // return 0;\n // }\n // int ans=INT_MAX;\n // for(int i=start;i<=end;i++){\n // ans=min(ans,i+max(solvebyrec(start,i-1),solvebyrec(i+1,end)));\n // }\n ...
375
<p>We are playing the Guessing Game. The game will work as follows:</p> <ol> <li>I pick a number between&nbsp;<code>1</code>&nbsp;and&nbsp;<code>n</code>.</li> <li>You guess a number.</li> <li>If you guess the right number, <strong>you win the game</strong>.</li> <li>If you guess the wrong number, then I will tell...
1
{ "code": "class Solution {\npublic:\n int solve(int s,int e,vector<vector<int>>&dp){\n if(s>=e){\n return 0;\n }\n if(dp[s][e]!=-1){\n return dp[s][e];\n }\n int ans=INT_MAX;\n for(int i=s;i<e;i++){\n ans=min(ans,i+max(solve(s,i-1,dp),solv...
375
<p>We are playing the Guessing Game. The game will work as follows:</p> <ol> <li>I pick a number between&nbsp;<code>1</code>&nbsp;and&nbsp;<code>n</code>.</li> <li>You guess a number.</li> <li>If you guess the right number, <strong>you win the game</strong>.</li> <li>If you guess the wrong number, then I will tell...
2
{ "code": "class Solution {\n int helper(int l, int r, vector<vector<int>> &dp) {\n if(l >= r) return 0;\n\n if(dp[l][r] != -1) return dp[l][r];\n\n int m = 0, ans = INT_MAX;\n\n for(int i=l;i<=r;i++) {\n ans = min(ans, i + max(helper(i+1, r, dp), helper(l, i-1, dp)));\n ...
375
<p>We are playing the Guessing Game. The game will work as follows:</p> <ol> <li>I pick a number between&nbsp;<code>1</code>&nbsp;and&nbsp;<code>n</code>.</li> <li>You guess a number.</li> <li>If you guess the right number, <strong>you win the game</strong>.</li> <li>If you guess the wrong number, then I will tell...
2
{ "code": "class Solution {\npublic:\n int solve(int s,int e,vector<vector<int>> &dp){\n if(s>=e) return 0;\n int ans = INT_MAX;\n if(dp[s][e]!=-1) return dp[s][e];\n for(int i=s;i<=e;i++){\n ans = min(ans,i+max(solve(s,i-1,dp),solve(i+1,e,dp)));\n }\n dp[s][e]=...
375
<p>We are playing the Guessing Game. The game will work as follows:</p> <ol> <li>I pick a number between&nbsp;<code>1</code>&nbsp;and&nbsp;<code>n</code>.</li> <li>You guess a number.</li> <li>If you guess the right number, <strong>you win the game</strong>.</li> <li>If you guess the wrong number, then I will tell...
3
{ "code": "class Solution {\npublic:\nint findanswer(int lb,int ub, vector<vector<int>>&dp)\n{\n if(lb>=ub)\n {\n return 0;\n }\n if(dp[lb][ub]!=INT_MAX)\n {\n return dp[lb][ub];\n }\n int ans=INT_MAX;\n for(int i=lb;i<=ub;i++)\n {\n int ans1=i+findanswer(lb,i-1,dp);\n...
375
<p>We are playing the Guessing Game. The game will work as follows:</p> <ol> <li>I pick a number between&nbsp;<code>1</code>&nbsp;and&nbsp;<code>n</code>.</li> <li>You guess a number.</li> <li>If you guess the right number, <strong>you win the game</strong>.</li> <li>If you guess the wrong number, then I will tell...
3
{ "code": "class Solution {\npublic:\n\n vector<vector<int>>dp;\n int f(int start,int end)\n {\n int ans=1e9;\n if(start==end)return 0;\n if(dp[start][end]!=-1)return dp[start][end];\n for(int i=start;i<=end;i++)\n {\n int curr=0;\n if(i-1>=start)\n ...
375
<p>We are playing the Guessing Game. The game will work as follows:</p> <ol> <li>I pick a number between&nbsp;<code>1</code>&nbsp;and&nbsp;<code>n</code>.</li> <li>You guess a number.</li> <li>If you guess the right number, <strong>you win the game</strong>.</li> <li>If you guess the wrong number, then I will tell...
3
{ "code": "class Solution {\npublic:\n int get(int &n,int start,int end,vector<vector<int>>&dp){\n if( end==start or start>end ){\n return 0;\n }\n int a=1000000;\n if(dp[start][end]!=-1){\n return dp[start][end];\n }\n // cout<<start<<endl;\n fo...
375
<p>We are playing the Guessing Game. The game will work as follows:</p> <ol> <li>I pick a number between&nbsp;<code>1</code>&nbsp;and&nbsp;<code>n</code>.</li> <li>You guess a number.</li> <li>If you guess the right number, <strong>you win the game</strong>.</li> <li>If you guess the wrong number, then I will tell...
3
{ "code": "class Solution {\npublic:\n int getMoneyAmount(int n) {\n vector<vector<int>> dp(n + 1, vector<int>(n + 1, 0));\n\n function<int(int, int)> calculateCost = [&](int start, int end) {\n if (start >= end) return 0;\n if (dp[start][end] != 0) return dp[start][end];\n\n ...
375
<p>We are playing the Guessing Game. The game will work as follows:</p> <ol> <li>I pick a number between&nbsp;<code>1</code>&nbsp;and&nbsp;<code>n</code>.</li> <li>You guess a number.</li> <li>If you guess the right number, <strong>you win the game</strong>.</li> <li>If you guess the wrong number, then I will tell...
3
{ "code": "class Solution {\npublic:\n int getMoneyAmount(int n) {\n vector<vector<int>> dp(n + 1, vector<int>(n + 1, 0));\n\n function<int(int, int)> calculateCost = [&](int start, int end) {\n if (start >= end) return 0;\n if (dp[start][end] != 0) return dp[start][end];\n\n ...
375
<p>We are playing the Guessing Game. The game will work as follows:</p> <ol> <li>I pick a number between&nbsp;<code>1</code>&nbsp;and&nbsp;<code>n</code>.</li> <li>You guess a number.</li> <li>If you guess the right number, <strong>you win the game</strong>.</li> <li>If you guess the wrong number, then I will tell...
3
{ "code": "class Solution {\npublic:\n int getMoneyAmount(int n) {\n vector<vector<int>> dp(n + 1, vector<int>(n + 1, 0));\n\n function<int(int, int)> calculateCost = [&](int start, int end) {\n if (start >= end) return 0;\n if (dp[start][end] != 0) return dp[start][end];\n\n ...
375
<p>We are playing the Guessing Game. The game will work as follows:</p> <ol> <li>I pick a number between&nbsp;<code>1</code>&nbsp;and&nbsp;<code>n</code>.</li> <li>You guess a number.</li> <li>If you guess the right number, <strong>you win the game</strong>.</li> <li>If you guess the wrong number, then I will tell...
3
{ "code": "class Solution {\npublic:\n int solveUsingRec(int start, int end){\n if(start >= end) return 0;\n\n int ans = INT_MAX;\n\n for(int i = start; i <= end; i++){\n ans = min(ans, i + max(solveUsingRec(start, i-1), solveUsingRec(i+1, end)));\n }\n\n return ans;\n...
375
<p>We are playing the Guessing Game. The game will work as follows:</p> <ol> <li>I pick a number between&nbsp;<code>1</code>&nbsp;and&nbsp;<code>n</code>.</li> <li>You guess a number.</li> <li>If you guess the right number, <strong>you win the game</strong>.</li> <li>If you guess the wrong number, then I will tell...
3
{ "code": "class Solution {\n int fun(int l,int r, vector<vector<long long>>&dp){\n if(l>r) return INT_MAX;\n if(l==r) return 0;\n if(l+1==r) return l;\n if(l+2==r) return l+1;\n if(dp[l][r]!=-1) return dp[l][r];\n long long ans = INT_MAX;\n for(int j=l;j<=r;j++){\n...
375
<p>We are playing the Guessing Game. The game will work as follows:</p> <ol> <li>I pick a number between&nbsp;<code>1</code>&nbsp;and&nbsp;<code>n</code>.</li> <li>You guess a number.</li> <li>If you guess the right number, <strong>you win the game</strong>.</li> <li>If you guess the wrong number, then I will tell...
3
{ "code": "class Solution {\npublic:\n int solve(int start, int end, vector<vector<int>>& dp) {\n if (start >= end)\n return 0;\n if (dp[start][end] != -1)\n return dp[start][end];\n int ans = INT_MAX;\n for (int i = start; i <= end; i++) {\n ans = min(a...
375
<p>We are playing the Guessing Game. The game will work as follows:</p> <ol> <li>I pick a number between&nbsp;<code>1</code>&nbsp;and&nbsp;<code>n</code>.</li> <li>You guess a number.</li> <li>If you guess the right number, <strong>you win the game</strong>.</li> <li>If you guess the wrong number, then I will tell...
3
{ "code": "class Solution {\npublic:\n long rec(int s,int e,vector<vector<long>>& dp){\n if(s>=e)return 0;\n if(dp[s][e]!= -1)return dp[s][e];\n long ans=INT_MAX;\n for(int i=s;i<=e;i++)ans=min(ans,i+max(rec(s,i-1,dp),rec(i+1,e,dp)));\n return dp[s][e]=ans;\n }\n int getMon...
376
<p>A <strong>wiggle sequence</strong> is a sequence where the differences between successive numbers strictly alternate between positive and negative. The first difference (if one exists) may be either positive or negative. A sequence with one element and a sequence with two non-equal elements are trivially wiggle sequ...
0
{ "code": "class Solution {\npublic:\n int wiggleMaxLength(vector<int>& nums) {\n int len = nums.size();\n int flag1 = 1;\n int flag2 = -1;\n int ans1=0, ans2=0;\n\n for(int i=1 ; i<len ; i++){\n if((nums[i]-nums[i-1]) * flag1 > 0){\n ans1++;\n ...
376
<p>A <strong>wiggle sequence</strong> is a sequence where the differences between successive numbers strictly alternate between positive and negative. The first difference (if one exists) may be either positive or negative. A sequence with one element and a sequence with two non-equal elements are trivially wiggle sequ...
0
{ "code": "class Solution {\npublic:\n int wiggleMaxLength(vector<int>& nums) {\n int n=nums.size(),maxi=0,mini=0;\n\n for(int i=1;i<n;i++){\n if(nums[i]<nums[i-1]) mini=maxi+1;\n if(nums[i]>nums[i-1]) maxi=mini+1; \n } \n return max(mini,maxi)+1;\n }\n};", ...
376
<p>A <strong>wiggle sequence</strong> is a sequence where the differences between successive numbers strictly alternate between positive and negative. The first difference (if one exists) may be either positive or negative. A sequence with one element and a sequence with two non-equal elements are trivially wiggle sequ...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> dp; // Memoization cache\n \n int maxLen(vector<char>& order, int ind, char prev) {\n if (ind >= order.size()) return 0;\n \n if (dp[ind][prev-'A'] != -1) {\n return dp[ind][prev-'A'];\n }\n \n int...
376
<p>A <strong>wiggle sequence</strong> is a sequence where the differences between successive numbers strictly alternate between positive and negative. The first difference (if one exists) may be either positive or negative. A sequence with one element and a sequence with two non-equal elements are trivially wiggle sequ...
3
{ "code": "class Solution {\npublic:\n // Analysis\n // method1,地推法,就是看和前面的值对比,然后再覆盖。\n // 其实也是一个DP算法拉。。就是假设前面[已知】\n int wiggleMaxLength(vector<int>& nums) {\n vector<pair<string, int>> v1; // head\n vector<pair<string, int>> v2; // tail\n\n for (int i = 0; i < nums.size(); i++) {\n ...
376
<p>A <strong>wiggle sequence</strong> is a sequence where the differences between successive numbers strictly alternate between positive and negative. The first difference (if one exists) may be either positive or negative. A sequence with one element and a sequence with two non-equal elements are trivially wiggle sequ...
3
{ "code": "class Solution {\npublic:\n int memo[1001][1002];\n int wiggleMaxLength(vector<int>& nums) {\n memset(memo,-1,sizeof(memo));\n return dfs(nums,0,-1);\n }\n int dfs(auto& nums,int index,int prev) {\n int n = nums.size();\n\n if(index == n)\n return 0;\n ...
376
<p>A <strong>wiggle sequence</strong> is a sequence where the differences between successive numbers strictly alternate between positive and negative. The first difference (if one exists) may be either positive or negative. A sequence with one element and a sequence with two non-equal elements are trivially wiggle sequ...
3
{ "code": "class Solution {\npublic:\n int n = 0;\n int dp[1010][1010];\n int rec(int level,int prevind,vector<int>& nums){\n\n if(level>=n)\n return 0;\n\n if(dp[level][prevind+1]!=-1)\n return dp[level][prevind+1]; \n\n int ans = -1e9;...
376
<p>A <strong>wiggle sequence</strong> is a sequence where the differences between successive numbers strictly alternate between positive and negative. The first difference (if one exists) may be either positive or negative. A sequence with one element and a sequence with two non-equal elements are trivially wiggle sequ...
3
{ "code": "class Solution {\npublic:\n int dp[1001][1001];\n int solve(int i,int prev,int n,vector<int> &temp){\n if(i==n)\n return 0;\n if(dp[i][prev]!=-1)\n return dp[i][prev];\n int ans=0;\n for(int j=i;j<n;j++){\n if(temp[prev]*temp[j]<0)\n ...
376
<p>A <strong>wiggle sequence</strong> is a sequence where the differences between successive numbers strictly alternate between positive and negative. The first difference (if one exists) may be either positive or negative. A sequence with one element and a sequence with two non-equal elements are trivially wiggle sequ...
3
{ "code": "class Solution {\npublic:\n int func(vector<int>& nums, int i, int prev, map<pair<int, int>, int>& dp){\n if(i == nums.size() - 1) return 0;\n if(dp.find({i, prev}) != dp.end()) return dp[{i, prev}];\n int diff = nums[i + 1] - nums[i];\n int take = 0;\n if((diff != 0) ...
376
<p>A <strong>wiggle sequence</strong> is a sequence where the differences between successive numbers strictly alternate between positive and negative. The first difference (if one exists) may be either positive or negative. A sequence with one element and a sequence with two non-equal elements are trivially wiggle sequ...
3
{ "code": "class Solution {\npublic:\n int func(vector<int>& nums, int i, int prev, map<pair<int, int>, int>& dp){\n if(i == nums.size() - 1) return 0;\n if(dp.find({i, prev}) != dp.end()) return dp[{i, prev}];\n\n int diff = nums[i + 1] - nums[i];\n int newprev = prev;\n if(diff...
376
<p>A <strong>wiggle sequence</strong> is a sequence where the differences between successive numbers strictly alternate between positive and negative. The first difference (if one exists) may be either positive or negative. A sequence with one element and a sequence with two non-equal elements are trivially wiggle sequ...
3
{ "code": "class Solution {\npublic:\n int f(int index, int prev, vector<int>& nums, vector<vector<int>>& dp) {\n int n = nums.size();\n if (index >= nums.size()) {\n return 0;\n }\n \n if (dp[index][prev+1] != -1) {\n return dp[index][prev+1];\n }\n ...
376
<p>A <strong>wiggle sequence</strong> is a sequence where the differences between successive numbers strictly alternate between positive and negative. The first difference (if one exists) may be either positive or negative. A sequence with one element and a sequence with two non-equal elements are trivially wiggle sequ...
3
{ "code": "class Solution {\npublic:\n int solve(int ind,int prevind,vector<int>&nums, vector<vector<int>>&dp){\n int n=nums.size();\n if(ind==n)return 0;\n if(dp[ind][prevind+1]!=-1)return dp[ind][prevind+1];\n int nottake=solve(ind+1,prevind,nums,dp);\n int take=INT_MIN;\n ...
376
<p>A <strong>wiggle sequence</strong> is a sequence where the differences between successive numbers strictly alternate between positive and negative. The first difference (if one exists) may be either positive or negative. A sequence with one element and a sequence with two non-equal elements are trivially wiggle sequ...
3
{ "code": "class Solution {\npublic:\n int solve(int ind,int prevind,vector<int>&nums, vector<vector<int>>&dp){\n int n=nums.size();\n if(ind==n)return 0;\n if(dp[ind][prevind+1]!=-1)return dp[ind][prevind+1];\n int nottake=solve(ind+1,prevind,nums,dp);\n int take=INT_MIN;\n ...
376
<p>A <strong>wiggle sequence</strong> is a sequence where the differences between successive numbers strictly alternate between positive and negative. The first difference (if one exists) may be either positive or negative. A sequence with one element and a sequence with two non-equal elements are trivially wiggle sequ...
3
{ "code": "int dp[1001][1002][2];\nclass Solution {\npublic:\n int sol(int i, int prv, int j, int n, vector<int>&a){\n if(i==n) return 0;\n if(dp[i][prv][j]!=-1) return dp[i][prv][j];\n if(j==0){\n int ans = sol(i+1,prv,j,n,a);\n if(prv==0||a[i]<a[prv-1]) ans = max(ans, 1...
376
<p>A <strong>wiggle sequence</strong> is a sequence where the differences between successive numbers strictly alternate between positive and negative. The first difference (if one exists) may be either positive or negative. A sequence with one element and a sequence with two non-equal elements are trivially wiggle sequ...
3
{ "code": "class Solution {\npublic:\n int f(vector<int> &nums,int prev,int i,vector<vector<int>> &dp){\n if(i>=nums.size()) return 0;\n if(dp[i][prev+1]!=-1) return dp[i][prev+1];\n int pick=0;\n if(prev==-1 || nums[prev]<0&&nums[i]>0 || nums[prev]>0 && nums[i]<0){\n pick=1+...
376
<p>A <strong>wiggle sequence</strong> is a sequence where the differences between successive numbers strictly alternate between positive and negative. The first difference (if one exists) may be either positive or negative. A sequence with one element and a sequence with two non-equal elements are trivially wiggle sequ...
3
{ "code": "class Solution {\npublic:\n // curr, prv, cond\n // curr > prv && cond == 1 || curr < prv && cond == 01 --> 1 + solve(i+1, nums[i], (cond == 1) ? -1 : 1)\n // max (solve(i+1, prv, cond), solve(i+1, num[i], cond))\n int dp[1001][1001][2];\n int solve(int i, int prv, int cond, vector<int>& num...
376
<p>A <strong>wiggle sequence</strong> is a sequence where the differences between successive numbers strictly alternate between positive and negative. The first difference (if one exists) may be either positive or negative. A sequence with one element and a sequence with two non-equal elements are trivially wiggle sequ...
3
{ "code": "class Solution {\npublic:\n // 0->nev\n // 1->pos\n int dp[1001][1002][2];\n int help(vector<int>&nums, int i, int j, int f){\n if(i==nums.size())return 0;\n if(dp[i][j+1][f]!=-1)return dp[i][j+1][f];\n int t=0,nt=0;\n nt=help(nums,i+1,j,f);\n if(j==-1){\n ...
376
<p>A <strong>wiggle sequence</strong> is a sequence where the differences between successive numbers strictly alternate between positive and negative. The first difference (if one exists) may be either positive or negative. A sequence with one element and a sequence with two non-equal elements are trivially wiggle sequ...
3
{ "code": "class Solution {\npublic:\n\nint dp[1001][1001][2];\nint solve(int i, int pre, vector<int>& nums, int flag){\n if(i>=nums.size()) return 0;\n \n if(dp[i][pre+1][flag] != -1) return dp[i][pre+1][flag];\n\n int start_with_positive = 0;\n int start_with_negative = 0;\n int skip_this_start = ...
376
<p>A <strong>wiggle sequence</strong> is a sequence where the differences between successive numbers strictly alternate between positive and negative. The first difference (if one exists) may be either positive or negative. A sequence with one element and a sequence with two non-equal elements are trivially wiggle sequ...
3
{ "code": "class Solution {\npublic:\n\nint n;\nvector<int> v;\n\nstatic const int N = 1e3 + 3;\nint memo[N][N][2];\n\nint dp(int i, int prev, bool sign) {\n if(i == n) return 0;\n\n int& ret = memo[i][prev][sign];\n if(ret != -1) return ret;\n\n int leave = dp(i + 1, prev, sign);\n if(prev == n) {\n ...
376
<p>A <strong>wiggle sequence</strong> is a sequence where the differences between successive numbers strictly alternate between positive and negative. The first difference (if one exists) may be either positive or negative. A sequence with one element and a sequence with two non-equal elements are trivially wiggle sequ...
3
{ "code": "class Solution {\npublic:\n int n;\n int dp[1001][1003][3];\n int find(int v,int p,int s,vector<int> &nums){\n if(v==n) return 0;\n if(dp[v][p+1][s]!=-1) return dp[v][p+1][s];\n int ans=find(v+1,p,s,nums);\n if(p==-1){\n ans=max(ans,1+find(v+1,v,s,nums));\n ...
376
<p>A <strong>wiggle sequence</strong> is a sequence where the differences between successive numbers strictly alternate between positive and negative. The first difference (if one exists) may be either positive or negative. A sequence with one element and a sequence with two non-equal elements are trivially wiggle sequ...
3
{ "code": "class Solution {\npublic:\n int dp[1001][3][1001];\n int gh(int i,int k,int prev,vector<int>& v){\n if(i>=v.size()){\n return 0;\n }\n if(i>=0 && k>=0 && prev>=0 && dp[i][k][prev]!=-1){\n return dp[i][k][prev];\n }\n if(k==2){\n retu...
376
<p>A <strong>wiggle sequence</strong> is a sequence where the differences between successive numbers strictly alternate between positive and negative. The first difference (if one exists) may be either positive or negative. A sequence with one element and a sequence with two non-equal elements are trivially wiggle sequ...
3
{ "code": "class Solution {\npublic:\n int wiggleMaxLength(vector<int>& nums) {\n int N=nums.size();\n if(N==1)\n {\n return 1;\n }\n if(N==2)\n {\n if(nums[1]!=nums[0])\n {\n return 2;\n }\n return 1;\n...
376
<p>A <strong>wiggle sequence</strong> is a sequence where the differences between successive numbers strictly alternate between positive and negative. The first difference (if one exists) may be either positive or negative. A sequence with one element and a sequence with two non-equal elements are trivially wiggle sequ...
3
{ "code": "class Solution {\npublic:\n int wiggleMaxLength(vector<int>& nums) {\n int N=nums.size();\n if(N==1)\n {\n return 1;\n }\n if(N==2)\n {\n if(nums[1]!=nums[0])\n {\n return 2;\n }\n return 1;\n...
376
<p>A <strong>wiggle sequence</strong> is a sequence where the differences between successive numbers strictly alternate between positive and negative. The first difference (if one exists) may be either positive or negative. A sequence with one element and a sequence with two non-equal elements are trivially wiggle sequ...
3
{ "code": "class Solution {\npublic:\n int wiggleMaxLength(vector<int>& nums) {\n int N=nums.size();\n if(N==1)\n {\n return 1;\n }\n if(N==2)\n {\n if(nums[1]!=nums[0])\n {\n return 2;\n }\n return 1;\n...
376
<p>A <strong>wiggle sequence</strong> is a sequence where the differences between successive numbers strictly alternate between positive and negative. The first difference (if one exists) may be either positive or negative. A sequence with one element and a sequence with two non-equal elements are trivially wiggle sequ...
3
{ "code": "class Solution {\n vector<vector<vector<int>>>dp;\npublic:\n Solution(){\n ios_base :: sync_with_stdio(false);\n }\n int solve(vector<int>&nums,int index,int n,bool pos,int lastIndex){\n if(index == n){\n return 0;\n }\n if(dp[index][pos][lastIndex+1] ...
376
<p>A <strong>wiggle sequence</strong> is a sequence where the differences between successive numbers strictly alternate between positive and negative. The first difference (if one exists) may be either positive or negative. A sequence with one element and a sequence with two non-equal elements are trivially wiggle sequ...
3
{ "code": "class Solution {\npublic:\nint dp[1005][1005][5];\n// sign - 1 -> positive\n\n int f(int i,int prev,int sign,vector<int>&nums){\n if(i>=nums.size()){\n return 0;\n }\n int take=0;\n int not_take=0;\n\n if(dp[i][prev][sign+1]!=-1){\n return dp[i][p...
376
<p>A <strong>wiggle sequence</strong> is a sequence where the differences between successive numbers strictly alternate between positive and negative. The first difference (if one exists) may be either positive or negative. A sequence with one element and a sequence with two non-equal elements are trivially wiggle sequ...
3
{ "code": "class Solution {\nprivate:\n int dp[1008][1008][5];\n //recusrive + memoized, with pick and not pick method\n // T.C = O(N*N*3);\n int helper(int ind,int sign,int prev,vector<int>&nums){\n if(ind>=nums.size())\n return 0;\n if(dp[ind][prev+2][sign+2]!=-1)\n r...
376
<p>A <strong>wiggle sequence</strong> is a sequence where the differences between successive numbers strictly alternate between positive and negative. The first difference (if one exists) may be either positive or negative. A sequence with one element and a sequence with two non-equal elements are trivially wiggle sequ...
3
{ "code": "class Solution {\nprivate:\n int dp[1008][1008][5];\n int helper(int ind,int sign,int prev,vector<int>&nums){\n if(ind>=nums.size())\n return 0;\n if(dp[ind][prev+2][sign+2]!=-1)\n return dp[ind][prev+2][sign+2];\n int ans = 0;\n if(prev==-1){\n ...
376
<p>A <strong>wiggle sequence</strong> is a sequence where the differences between successive numbers strictly alternate between positive and negative. The first difference (if one exists) may be either positive or negative. A sequence with one element and a sequence with two non-equal elements are trivially wiggle sequ...
3
{ "code": "class Solution {\nprivate:\n int dp[1008][1008][5];\n int helper(int ind,int sign,int prev,vector<int>&nums){\n if(ind>=nums.size())\n return 0;\n if(dp[ind][prev+1][sign+1]!=-1)\n return dp[ind][prev+1][sign+1];\n int ans = 0; int pick = 0,notPick = 0;\n ...
376
<p>A <strong>wiggle sequence</strong> is a sequence where the differences between successive numbers strictly alternate between positive and negative. The first difference (if one exists) may be either positive or negative. A sequence with one element and a sequence with two non-equal elements are trivially wiggle sequ...
3
{ "code": "class Solution {\nprivate:\n int dp[1008][1008][5];\n //recusrive + memoized, with pick and not pick method\n // T.C = O(N*N*3);\n //shorter version\n int helper(int ind,int sign,int prev,vector<int>&nums){\n if(ind>=nums.size())\n return 0;\n if(dp[ind][prev+1][sign...
376
<p>A <strong>wiggle sequence</strong> is a sequence where the differences between successive numbers strictly alternate between positive and negative. The first difference (if one exists) may be either positive or negative. A sequence with one element and a sequence with two non-equal elements are trivially wiggle sequ...
3
{ "code": "class Solution {\npublic:\n int solve(int ind,vector<int>&nums,int pos,int prev,vector<vector<vector<int>>>&dp){\n int n=nums.size();\n\n if(ind>=n)\n return 0;\n\n if(dp[ind][pos+2][prev]!=-1)\n return dp[ind][pos+2][prev];\n\n int ways=0;\n int val=nums...
376
<p>A <strong>wiggle sequence</strong> is a sequence where the differences between successive numbers strictly alternate between positive and negative. The first difference (if one exists) may be either positive or negative. A sequence with one element and a sequence with two non-equal elements are trivially wiggle sequ...
3
{ "code": "class Solution {\npublic:\n int solve(int i,int j,vector<int> nums,vector<vector<int>>& dp){\n if(i==nums.size()) return 1;\n\n if(dp[i][j]!=-1) return dp[i][j];\n int nt = solve(i+1,j,nums,dp);\n int t = 0;\n \n int a = nums[i]-nums[i-1];\n if(a<0) a = 1;\n ...
376
<p>A <strong>wiggle sequence</strong> is a sequence where the differences between successive numbers strictly alternate between positive and negative. The first difference (if one exists) may be either positive or negative. A sequence with one element and a sequence with two non-equal elements are trivially wiggle sequ...
3
{ "code": "class Solution {\npublic:\n\n int rec(vector<int>nums, int flag, int prev,int i,vector<vector<vector<int>>>& t) //flag =2(decrease) 1(increase)\n { if(t[flag][prev][i] != -1){return t[flag][prev][i];}\n if(i==nums.size())\n {\n t[flag][prev][i]=0;\n return 0;\n ...
376
<p>A <strong>wiggle sequence</strong> is a sequence where the differences between successive numbers strictly alternate between positive and negative. The first difference (if one exists) may be either positive or negative. A sequence with one element and a sequence with two non-equal elements are trivially wiggle sequ...
3
{ "code": "class Solution {\npublic:\n\n int rec(vector<int>nums, int flag, int prev,int i,vector<vector<vector<int>>>& t) //flag =2(decrease) 1(increase)\n { if(t[flag][prev][i] != -1){return t[flag][prev][i];}\n if(i==nums.size())\n {\n t[flag][prev][i]=0;\n return 0;\n ...
376
<p>A <strong>wiggle sequence</strong> is a sequence where the differences between successive numbers strictly alternate between positive and negative. The first difference (if one exists) may be either positive or negative. A sequence with one element and a sequence with two non-equal elements are trivially wiggle sequ...
3
{ "code": "#include <vector>\n#include <climits>\nusing namespace std;\n\nclass Solution {\npublic:\n int f(int index, int prev_diff, vector<int> &nums, vector<vector<int>>&dp) {\n if (index >= nums.size()) {\n return 0;\n }\n if(dp[index][prev_diff + 1000]!=-1)return dp[index][prev...
376
<p>A <strong>wiggle sequence</strong> is a sequence where the differences between successive numbers strictly alternate between positive and negative. The first difference (if one exists) may be either positive or negative. A sequence with one element and a sequence with two non-equal elements are trivially wiggle sequ...
3
{ "code": "class Solution {\npublic:\n int solve(vector<int>& nums, int i, int flag, int prev, vector<vector<vector<int>>>& dp){\n if(i == nums.size()) return 0;\n if(prev != -1 && dp[i][flag][prev] != -1) \n return dp[i][flag][prev];\n\n int take = 0;\n if(flag){\n ...
376
<p>A <strong>wiggle sequence</strong> is a sequence where the differences between successive numbers strictly alternate between positive and negative. The first difference (if one exists) may be either positive or negative. A sequence with one element and a sequence with two non-equal elements are trivially wiggle sequ...
3
{ "code": "class Solution {\npublic:\n int dp[1002][1002][3][3];\n\n int util(int i, int prev_i, int f, int prev_f, int n, vector<int> &nums){\n if(i==n)\n return 0;\n\n if(dp[i][prev_i][f][prev_f]!=-1)\n return dp[i][prev_i][f][prev_f];\n\n int p=0;\n int update_f=nums...
376
<p>A <strong>wiggle sequence</strong> is a sequence where the differences between successive numbers strictly alternate between positive and negative. The first difference (if one exists) may be either positive or negative. A sequence with one element and a sequence with two non-equal elements are trivially wiggle sequ...
3
{ "code": "class Solution {\npublic:\n int dp[1002][1002][3][3];\n\n int util(int i, int prev_i, int f, int prev_f, int n, vector<int> &nums){\n if(i==n)\n return 0;\n\n if(dp[i][prev_i][f][prev_f]!=-1)\n return dp[i][prev_i][f][prev_f];\n\n int p=0;\n int update_f=nums...
376
<p>A <strong>wiggle sequence</strong> is a sequence where the differences between successive numbers strictly alternate between positive and negative. The first difference (if one exists) may be either positive or negative. A sequence with one element and a sequence with two non-equal elements are trivially wiggle sequ...
3
{ "code": "class Solution {\npublic:\n int memo[1001][10002];\n int wiggleMaxLength(vector<int>& nums) {\n memset(memo,-1,sizeof(memo));\n return dfs(nums,0,-1);\n }\n int dfs(auto& nums,int index,int prev) {\n if(index == nums.size())\n return 0;\n \n if(memo...
376
<p>A <strong>wiggle sequence</strong> is a sequence where the differences between successive numbers strictly alternate between positive and negative. The first difference (if one exists) may be either positive or negative. A sequence with one element and a sequence with two non-equal elements are trivially wiggle sequ...
3
{ "code": "class Solution {\n private:\n int fn(vector<int>&nums,int n,int prev,int positive,int i,vector<vector<vector<int>>>&dp){\n if(i==n){\n return 0;\n }\n if(dp[i][positive][prev]!=-1){\n return dp[i][positive][prev];\n }\n if(positive==2){\n //pick\n if(nums[i]...
376
<p>A <strong>wiggle sequence</strong> is a sequence where the differences between successive numbers strictly alternate between positive and negative. The first difference (if one exists) may be either positive or negative. A sequence with one element and a sequence with two non-equal elements are trivially wiggle sequ...
3
{ "code": "class Solution {\npublic:\n int dp[1001][10001][2];\n int calc(int in,int n,int lastPick,bool flag,vector<int>&nums)\n {\n\n if(in==n)\n return 0;\n if(dp[in][lastPick][flag]!=-1)\n return dp[in][lastPick][flag];\n int pick=0,npick=0;\n int diff = ...