id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
1,531
<p>There are <code>n</code> people and <code>40</code> types of hats labeled from <code>1</code> to <code>40</code>.</p> <p>Given a 2D integer array <code>hats</code>, where <code>hats[i]</code> is a list of all hats preferred by the <code>i<sup>th</sup></code> person.</p> <p>Return <em>the number of ways that the <c...
3
{ "code": "class Solution {\npublic:\n long long mod=1e9+7;\n int numberWays(vector<vector<int>>& hats) {\n int n=hats.size();\n vector<vector<long long > >dp((1<<n),vector<long long >(41));\n dp[0][0]=1;\n vector<int>used(1<<n);\n vector<int>bfs(1,0);\n while(bfs.size(...
1,531
<p>There are <code>n</code> people and <code>40</code> types of hats labeled from <code>1</code> to <code>40</code>.</p> <p>Given a 2D integer array <code>hats</code>, where <code>hats[i]</code> is a list of all hats preferred by the <code>i<sup>th</sup></code> person.</p> <p>Return <em>the number of ways that the <c...
3
{ "code": "class Solution {\npublic:\n long long mod=1e9+7;\n int numberWays(vector<vector<int>>& hats) {\n int n=hats.size();\n vector<vector<long long > >dp((1<<n),vector<long long >(41));\n dp[0][0]=1;\n vector<int>used(1<<n);\n vector<int>bfs(1,0);\n while(bfs.size(...
1,531
<p>There are <code>n</code> people and <code>40</code> types of hats labeled from <code>1</code> to <code>40</code>.</p> <p>Given a 2D integer array <code>hats</code>, where <code>hats[i]</code> is a list of all hats preferred by the <code>i<sup>th</sup></code> person.</p> <p>Return <em>the number of ways that the <c...
3
{ "code": "class Solution {\npublic:\n int mod = 1e9+7;\n int f(int ind,vector<int> &arr,vector<vector<int>>& hats,long long &mask,vector<vector<long long>> &dp)\n {\n int n = hats.size();\n long long val = (1<<n)-1; \n if(mask == val)\n return 1;\n if(ind==41)\n ret...
1,531
<p>There are <code>n</code> people and <code>40</code> types of hats labeled from <code>1</code> to <code>40</code>.</p> <p>Given a 2D integer array <code>hats</code>, where <code>hats[i]</code> is a list of all hats preferred by the <code>i<sup>th</sup></code> person.</p> <p>Return <em>the number of ways that the <c...
3
{ "code": "using LL = long long;\n\nclass Solution {\npublic:\n int numberWays(vector<vector<int>>& A) {\n int n = A.size();\n int MOD = 1e9+7;\n\n map<int, vector<int>> mp;\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < A[i].size(); j++) {\n int hat = A[i][j];\n mp[hat].push_ba...
1,531
<p>There are <code>n</code> people and <code>40</code> types of hats labeled from <code>1</code> to <code>40</code>.</p> <p>Given a 2D integer array <code>hats</code>, where <code>hats[i]</code> is a list of all hats preferred by the <code>i<sup>th</sup></code> person.</p> <p>Return <em>the number of ways that the <c...
3
{ "code": "class Solution {\nprivate:\n int solve(int capId, int mask, int n, int mod, vector<vector<int>>& dp,\n vector<vector<int>>& capToPeople) {\n if (mask == (1 << n) - 1)\n return 1;\n if (capId == 101)\n return 0;\n if (dp[capId][mask] != -1)\n ...
1,531
<p>There are <code>n</code> people and <code>40</code> types of hats labeled from <code>1</code> to <code>40</code>.</p> <p>Given a 2D integer array <code>hats</code>, where <code>hats[i]</code> is a list of all hats preferred by the <code>i<sup>th</sup></code> person.</p> <p>Return <em>the number of ways that the <c...
3
{ "code": "class Solution {\npublic:\n\n int mod = pow(10, 9) + 7;\n\n void findSol(map<int, vector<int>> &mp, vector<vector<long long>> &dp, int currWear, int maxHat) {\n if(currWear == dp[0].size() - 1) {\n dp[maxHat][currWear] = 1;\n return;\n }\n\n auto it = mp.upp...
1,531
<p>There are <code>n</code> people and <code>40</code> types of hats labeled from <code>1</code> to <code>40</code>.</p> <p>Given a 2D integer array <code>hats</code>, where <code>hats[i]</code> is a list of all hats preferred by the <code>i<sup>th</sup></code> person.</p> <p>Return <em>the number of ways that the <c...
3
{ "code": "class Solution \n{\nprivate:\n #define mod (long long)(pow(10, 9) + 7)\n long long solve(long long i, long long &n, long long mask, unordered_map<long long, unordered_map<long long, bool>> &mp, vector<vector<long long>> &dp)\n {\n if(i == 41)\n return (mask == ((1 << n) - 1));\n ...
1,531
<p>There are <code>n</code> people and <code>40</code> types of hats labeled from <code>1</code> to <code>40</code>.</p> <p>Given a 2D integer array <code>hats</code>, where <code>hats[i]</code> is a list of all hats preferred by the <code>i<sup>th</sup></code> person.</p> <p>Return <em>the number of ways that the <c...
3
{ "code": "class Solution {\npublic:\n const long MOD = 1e9 + 7;\n \n long count(int hat, unordered_map<int, vector<int>>& p, int people, vector<vector<long>>& dp) {\n if (people == dp.size()-1) return 1;\n if (hat > 40) return 0;\n if (dp[people][hat] != -1) return dp[people][hat];\n ...
1,531
<p>There are <code>n</code> people and <code>40</code> types of hats labeled from <code>1</code> to <code>40</code>.</p> <p>Given a 2D integer array <code>hats</code>, where <code>hats[i]</code> is a list of all hats preferred by the <code>i<sup>th</sup></code> person.</p> <p>Return <em>the number of ways that the <c...
3
{ "code": "class Solution {\npublic:\n long long mod=1e9+7;\n int numberWays(vector<vector<int>>& hats) {\n int n=hats.size();\n vector<vector<long long > >dp((1<<n),vector<long long >(41));\n dp[0][0]=1;\n unordered_set<int>bfs;\n bfs.insert(0);\n while(bfs.size()){\n ...
1,531
<p>There are <code>n</code> people and <code>40</code> types of hats labeled from <code>1</code> to <code>40</code>.</p> <p>Given a 2D integer array <code>hats</code>, where <code>hats[i]</code> is a list of all hats preferred by the <code>i<sup>th</sup></code> person.</p> <p>Return <em>the number of ways that the <c...
3
{ "code": "class Solution {\n unordered_map<int, vector<int>>map;\n int M = (int)(1e9) + 7;\npublic:\n int solve(int mask, int idx, int &n, vector<vector<int>>&dp){\n if(__builtin_popcount(mask) == n) return 1;\n if(idx > 40) return 0;\n if(dp[mask][idx] != -1) return dp[mask][idx];\n ...
1,531
<p>There are <code>n</code> people and <code>40</code> types of hats labeled from <code>1</code> to <code>40</code>.</p> <p>Given a 2D integer array <code>hats</code>, where <code>hats[i]</code> is a list of all hats preferred by the <code>i<sup>th</sup></code> person.</p> <p>Return <em>the number of ways that the <c...
3
{ "code": "class Solution {\n map<int, vector<int>>map;\n int M = (int)(1e9) + 7;\npublic:\n int solve(int mask, int idx, int &n, vector<vector<int>>&dp){\n if(__builtin_popcount(mask) == n) return 1;\n if(idx > 40) return 0;\n if(dp[mask][idx] != -1) return dp[mask][idx];\n int a...
1,531
<p>There are <code>n</code> people and <code>40</code> types of hats labeled from <code>1</code> to <code>40</code>.</p> <p>Given a 2D integer array <code>hats</code>, where <code>hats[i]</code> is a list of all hats preferred by the <code>i<sup>th</sup></code> person.</p> <p>Return <em>the number of ways that the <c...
3
{ "code": "class Solution {\n int n,m;\n const int M=1e9+7;\n private:\n int solve(int i, int msk, vector<int> &v, vector<vector<int>> &dp, map<int,vector<int>> &mp){\n if(msk==(1<<n)-1) return 1;\n if(i>=m)return 0;\n if(dp[i][msk]!=-1)return dp[i][msk];\n int res = solve(i + ...
1,531
<p>There are <code>n</code> people and <code>40</code> types of hats labeled from <code>1</code> to <code>40</code>.</p> <p>Given a 2D integer array <code>hats</code>, where <code>hats[i]</code> is a list of all hats preferred by the <code>i<sup>th</sup></code> person.</p> <p>Return <em>the number of ways that the <c...
3
{ "code": "const int mod = 1e9+7;\nclass Solution {\npublic:\n int n;\n map<int, map<int, int>> m;\n vector<vector<int>> dp;\n int solve(int mask, int hat){\n if(mask == ((1<<n)-1)) return 1;\n if(hat > 40) return 0;\n if(dp[mask][hat] != -1) return dp[mask][hat];\n\n int ans =...
1,531
<p>There are <code>n</code> people and <code>40</code> types of hats labeled from <code>1</code> to <code>40</code>.</p> <p>Given a 2D integer array <code>hats</code>, where <code>hats[i]</code> is a list of all hats preferred by the <code>i<sup>th</sup></code> person.</p> <p>Return <em>the number of ways that the <c...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> dp;\nint mod=1e9+7;\nint func(int mask, int currhat, int n, vector<vector<int>> &nums, int len)\n{\n if (currhat >= 40)\n {\n if (len == n)\n return 1;\n else\n {\n return -1;\n }\n }\n\n if (len...
1,531
<p>There are <code>n</code> people and <code>40</code> types of hats labeled from <code>1</code> to <code>40</code>.</p> <p>Given a 2D integer array <code>hats</code>, where <code>hats[i]</code> is a list of all hats preferred by the <code>i<sup>th</sup></code> person.</p> <p>Return <em>the number of ways that the <c...
3
{ "code": "class Solution {\npublic:\n int n;\n int mod = 1e9 + 7;\n int dfs(int pos,int mask,vector<vector<int>>& hat,vector<vector<int>>& dp)\n {\n if(pos>40)\n {\n return (mask == ((1<<n)-1));\n }\n\n if(dp[pos][mask]!=-1)\n return dp[pos][mask];\n\n ...
1,531
<p>There are <code>n</code> people and <code>40</code> types of hats labeled from <code>1</code> to <code>40</code>.</p> <p>Given a 2D integer array <code>hats</code>, where <code>hats[i]</code> is a list of all hats preferred by the <code>i<sup>th</sup></code> person.</p> <p>Return <em>the number of ways that the <c...
3
{ "code": "class Solution {\nprivate:\n const int mod = int(1e9) + 7;\npublic:\n\n int dphelper(int hat, int personsAssigned, const int& allAssigned, vector<vector<int>>& dp, const vector<vector<int>>& hat2people) {\n const int n_hats = hat2people.size();\n if(personsAssigned == allAssigned) retur...
1,531
<p>There are <code>n</code> people and <code>40</code> types of hats labeled from <code>1</code> to <code>40</code>.</p> <p>Given a 2D integer array <code>hats</code>, where <code>hats[i]</code> is a list of all hats preferred by the <code>i<sup>th</sup></code> person.</p> <p>Return <em>the number of ways that the <c...
3
{ "code": "class Solution {\n const int MOD = 1e9+7;\n int n;\n\nint helper (int curr, int mask, vector <vector <int>> &arr, vector <vector <int>> &dp){\n if (mask == (1 << n) - 1 ){\n return 1;\n }\n\n if (curr == 40) return 0;\n\n if (dp[curr][mask] != -1) return dp[curr][mask];\n\n int ...
1,531
<p>There are <code>n</code> people and <code>40</code> types of hats labeled from <code>1</code> to <code>40</code>.</p> <p>Given a 2D integer array <code>hats</code>, where <code>hats[i]</code> is a list of all hats preferred by the <code>i<sup>th</sup></code> person.</p> <p>Return <em>the number of ways that the <c...
3
{ "code": "class Solution {\npublic:\n int dp[41][100000];\n int mod = 1000000007;\n int getNumberOfWays(int start, int mask, vector<vector<int>> &assign, int n) {\n\n if(mask == (1 << n) - 1) {\n return 1;\n }\n if(start > 40) {\n return 0;\n }\n\n if...
1,531
<p>There are <code>n</code> people and <code>40</code> types of hats labeled from <code>1</code> to <code>40</code>.</p> <p>Given a 2D integer array <code>hats</code>, where <code>hats[i]</code> is a list of all hats preferred by the <code>i<sup>th</sup></code> person.</p> <p>Return <em>the number of ways that the <c...
3
{ "code": "class Solution {\npublic:\n const int MOD = 1e9 + 7 ;\n int f(vector<vector<int>>&a,int n,int mask,int i,vector<vector<int>>&dp){\n if(mask==(1<<n)-1) return 1;\n if(i>40) return 0;\n if(dp[mask][i]!=-1) return dp[mask][i];\n int ans=0;\n ans=f(a,n,mask,i+1,dp);\n ...
1,531
<p>There are <code>n</code> people and <code>40</code> types of hats labeled from <code>1</code> to <code>40</code>.</p> <p>Given a 2D integer array <code>hats</code>, where <code>hats[i]</code> is a list of all hats preferred by the <code>i<sup>th</sup></code> person.</p> <p>Return <em>the number of ways that the <c...
3
{ "code": "class Solution {\npublic:\n const int MOD = 1e9 + 7 ;\n int f(int ind,int mask,vector<vector<int>>&a,int n,vector<vector<int>>&dp){\n if(mask==(1<<n)-1) return 1;\n if(ind>40) return 0;\n if(dp[mask][ind]!=-1) return dp[mask][ind];\n int ans=f(ind+1,mask,a,n,dp);\n ...
1,531
<p>There are <code>n</code> people and <code>40</code> types of hats labeled from <code>1</code> to <code>40</code>.</p> <p>Given a 2D integer array <code>hats</code>, where <code>hats[i]</code> is a list of all hats preferred by the <code>i<sup>th</sup></code> person.</p> <p>Return <em>the number of ways that the <c...
3
{ "code": "class Solution {\npublic:\n static const int MOD = 1e9 + 7;\n unordered_map<int/*hat index*/, vector<int>/*people to satisfy*/> g_graph;\n unordered_map<int/*hat index*/, unordered_map<int/*state*/, int/*cnt*/>> dp;\n\n using GraphItr = decltype(g_graph.begin());\n\n int numberWays(vector<ve...
1,531
<p>There are <code>n</code> people and <code>40</code> types of hats labeled from <code>1</code> to <code>40</code>.</p> <p>Given a 2D integer array <code>hats</code>, where <code>hats[i]</code> is a list of all hats preferred by the <code>i<sup>th</sup></code> person.</p> <p>Return <em>the number of ways that the <c...
3
{ "code": "class Solution {\npublic:\n int mod = 1e9+7;\n int dp[41][1<<10];\n int f(int i,int mask,int m,unordered_map<int,vector<int>>&hatToP,int n) {\n if (mask == (1<<n)-1) return 1;\n if (i==m+1) return 0;\n if (dp[i][mask] != -1) return dp[i][mask];\n if (!hatToP.count(i)) r...
1,531
<p>There are <code>n</code> people and <code>40</code> types of hats labeled from <code>1</code> to <code>40</code>.</p> <p>Given a 2D integer array <code>hats</code>, where <code>hats[i]</code> is a list of all hats preferred by the <code>i<sup>th</sup></code> person.</p> <p>Return <em>the number of ways that the <c...
3
{ "code": "class Solution {\npublic:\n //3 -> 0\n //4 -> 0, 1\n //5 -> 1, 2\n static constexpr int MOD = 1e9+7;\n\n //ending case is where we have everyone with a hat, so the mask has all n people with hats, or the 1 is turned on\n int numberWays(vector<vector<int>>& hats) {\n int n = hats.si...
1,531
<p>There are <code>n</code> people and <code>40</code> types of hats labeled from <code>1</code> to <code>40</code>.</p> <p>Given a 2D integer array <code>hats</code>, where <code>hats[i]</code> is a list of all hats preferred by the <code>i<sup>th</sup></code> person.</p> <p>Return <em>the number of ways that the <c...
3
{ "code": "class Solution {\npublic:\n //3 -> 0\n //4 -> 0, 1\n //5 -> 1, 2\n static constexpr int MOD = 1e9+7;\n\n //ending case is where we have everyone with a hat, so the mask has all n people with hats, or the 1 is turned on\n int numberWays(vector<vector<int>>& hats) {\n int n = hats.si...
1,531
<p>There are <code>n</code> people and <code>40</code> types of hats labeled from <code>1</code> to <code>40</code>.</p> <p>Given a 2D integer array <code>hats</code>, where <code>hats[i]</code> is a list of all hats preferred by the <code>i<sup>th</sup></code> person.</p> <p>Return <em>the number of ways that the <c...
3
{ "code": "using ll = long long;\nclass Solution {\npublic:\n int mod = 1e9 + 7;\n int solve(vector<vector<int>> &h2p, vector<vector<ll>> &dp, int n, int mask, int i) {\n if (mask == ((1 << n) - 1))\n return 1;\n if (i > 40)\n return 0;\n if (dp[i][mask] != -1)\n ...
1,531
<p>There are <code>n</code> people and <code>40</code> types of hats labeled from <code>1</code> to <code>40</code>.</p> <p>Given a 2D integer array <code>hats</code>, where <code>hats[i]</code> is a list of all hats preferred by the <code>i<sup>th</sup></code> person.</p> <p>Return <em>the number of ways that the <c...
3
{ "code": "typedef long long ll;\n\nusing vi = vector<ll>;\nusing vvi = vector<vi>;\nclass Solution {\npublic:\n ll MOD = 1e9 + 7;\n // ll dp[41][(1LL<<10)] = {0};\n vvi dp;\n\n vvi adj;\n\n int n;\n\n int solve(int hat, int state){\n if(state == ((1<<n) - 1)) return 1;\n\n if(hat > 40...
1,548
<p>Given an binary array <code>nums</code> and an integer <code>k</code>, return <code>true</code><em> if all </em><code>1</code><em>&#39;s are at least </em><code>k</code><em> places away from each other, otherwise return </em><code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <i...
0
{ "code": "class Solution {\npublic:\n bool kLengthApart(vector<int>& nums, int k) {\n if(!k) return true;\n int j = nums[0] ? 0 : -1;\n for (int i=1; i<nums.size(); i++) {\n if(nums[i] == 1) {\n if(i-j-1<k && j != -1) return false; \n j=i;\n ...
1,548
<p>Given an binary array <code>nums</code> and an integer <code>k</code>, return <code>true</code><em> if all </em><code>1</code><em>&#39;s are at least </em><code>k</code><em> places away from each other, otherwise return </em><code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <i...
0
{ "code": "class Solution {\npublic:\n bool kLengthApart(vector<int>& nums, int k) {\n int j=-1;\n for(int i=0;i<nums.size();i++){\n if(nums[i]==1){\n if(i-j-1>=k || j==-1)\n j=i;\n else\n return false;\n }\n ...
1,548
<p>Given an binary array <code>nums</code> and an integer <code>k</code>, return <code>true</code><em> if all </em><code>1</code><em>&#39;s are at least </em><code>k</code><em> places away from each other, otherwise return </em><code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <i...
0
{ "code": "class Solution {\npublic:\n bool kLengthApart(vector<int>& nums, int k) {\n bool satisfy=true;\n int count=k;\n for(int i=0;i<nums.size();i++){\n if(!nums[i]){\n count++;\n }\n else{\n if(count<k)\n re...
1,548
<p>Given an binary array <code>nums</code> and an integer <code>k</code>, return <code>true</code><em> if all </em><code>1</code><em>&#39;s are at least </em><code>k</code><em> places away from each other, otherwise return </em><code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <i...
0
{ "code": "class Solution {\npublic:\n bool kLengthApart(vector<int>& nums, int k) {\n int n=nums.size();\n int count=0;\n int old;\n int flag=true;\n for(int i=0;i<n;i++)\n {\n if(nums[i]==1&&flag==true)\n {\n old=i;\n f...
1,548
<p>Given an binary array <code>nums</code> and an integer <code>k</code>, return <code>true</code><em> if all </em><code>1</code><em>&#39;s are at least </em><code>k</code><em> places away from each other, otherwise return </em><code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <i...
0
{ "code": "class Solution {\npublic:\n bool kLengthApart(vector<int>& nums, int k) {\n int count=0,start,end;\n for(int i=0;i<nums.size();i++)\n {\n if(nums[i]==1){\n count++;\n if(count==1){\n start=i+1;\n }\n ...
1,548
<p>Given an binary array <code>nums</code> and an integer <code>k</code>, return <code>true</code><em> if all </em><code>1</code><em>&#39;s are at least </em><code>k</code><em> places away from each other, otherwise return </em><code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <i...
0
{ "code": "class Solution {\npublic:\n bool kLengthApart(vector<int>& nums, int k) {\n int prev = INT_MIN/100;\n for(int i=0; i < nums.size(); i++){\n if(nums[i] ==1 && i - prev > k){\n prev = i;\n }else if(nums[i] == 1){\n return false;\n ...
1,548
<p>Given an binary array <code>nums</code> and an integer <code>k</code>, return <code>true</code><em> if all </em><code>1</code><em>&#39;s are at least </em><code>k</code><em> places away from each other, otherwise return </em><code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <i...
0
{ "code": "class Solution {\npublic:\n bool kLengthApart(vector<int>& nums, int k) {\n bool satisfy=true;\n int count=k;\n for(int i=0;i<nums.size();i++){\n if(!nums[i]){\n count++;\n }\n else{\n if(count<k)\n re...
1,548
<p>Given an binary array <code>nums</code> and an integer <code>k</code>, return <code>true</code><em> if all </em><code>1</code><em>&#39;s are at least </em><code>k</code><em> places away from each other, otherwise return </em><code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <i...
1
{ "code": "class Solution {\npublic:\n bool kLengthApart(vector<int>& nums, int k) {\n int count=k;\n for(int i=0;i<nums.size();i++){\n if(!nums[i]){\n count++;\n }\n else{\n if(count<k)\n return false;\n ...
1,548
<p>Given an binary array <code>nums</code> and an integer <code>k</code>, return <code>true</code><em> if all </em><code>1</code><em>&#39;s are at least </em><code>k</code><em> places away from each other, otherwise return </em><code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <i...
1
{ "code": "class Solution {\npublic:\n bool kLengthApart(vector<int>& nums, int k) {\n int count=k;\n for(int i=0;i<nums.size();i++){\n if(!nums[i]){\n count++;\n }\n else{\n if(count<k){\n return false;\n ...
1,548
<p>Given an binary array <code>nums</code> and an integer <code>k</code>, return <code>true</code><em> if all </em><code>1</code><em>&#39;s are at least </em><code>k</code><em> places away from each other, otherwise return </em><code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <i...
3
{ "code": "class Solution {\r\npublic:\r\n bool kLengthApart(vector<int>& nums, int k) {\r\n bool satisfy=true;\r\n int count=k;\r\n for(int i=0;i<nums.size();i++){\r\n if(!nums[i]){\r\n count++;\r\n }\r\n else{\r\n if(count<k)\r\n...
1,548
<p>Given an binary array <code>nums</code> and an integer <code>k</code>, return <code>true</code><em> if all </em><code>1</code><em>&#39;s are at least </em><code>k</code><em> places away from each other, otherwise return </em><code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <i...
3
{ "code": "class Solution {\npublic:\n bool kLengthApart(vector<int>& nums, int k) {\n int count=k;\n for(int i=0;i<nums.size();i++){\n if(!nums[i]){\n count++;\n }\n else{\n if(count<k)\n return false;\n ...
1,548
<p>Given an binary array <code>nums</code> and an integer <code>k</code>, return <code>true</code><em> if all </em><code>1</code><em>&#39;s are at least </em><code>k</code><em> places away from each other, otherwise return </em><code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <i...
3
{ "code": "class Solution {\npublic:\n bool kLengthApart(vector<int>& nums, int k) {\n int n=nums.size();\n int cnt=0;\n for(int i=0;i<nums.size();i++){\n if(nums[i]==1) cnt++;\n }\n vector<int> arr2(cnt);\n int j=0;\n for(int i=0;i<n;i++){\n i...
1,548
<p>Given an binary array <code>nums</code> and an integer <code>k</code>, return <code>true</code><em> if all </em><code>1</code><em>&#39;s are at least </em><code>k</code><em> places away from each other, otherwise return </em><code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <i...
3
{ "code": "class Solution {\npublic:\n bool kLengthApart(vector<int>& nums, int k) {\n int n=nums.size();\n int cnt=0;\n for(int i=0;i<nums.size();i++){\n if(nums[i]==1) cnt++;\n }\n vector<int> arr2(cnt);\n int j=0;\n for(int i=0;i<n;i++){\n i...
1,548
<p>Given an binary array <code>nums</code> and an integer <code>k</code>, return <code>true</code><em> if all </em><code>1</code><em>&#39;s are at least </em><code>k</code><em> places away from each other, otherwise return </em><code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <i...
3
{ "code": "class Solution {\npublic:\n bool kLengthApart(vector<int>& nums, int k) {\n int n=nums.size();\n int cnt=0;\n for(int i=0;i<nums.size();i++){\n if(nums[i]==1) cnt++;\n }\n vector<int> arr2(cnt);\n int j=0;\n for(int i=0;i<n;i++){\n i...
1,548
<p>Given an binary array <code>nums</code> and an integer <code>k</code>, return <code>true</code><em> if all </em><code>1</code><em>&#39;s are at least </em><code>k</code><em> places away from each other, otherwise return </em><code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <i...
3
{ "code": "class Solution {\npublic:\n bool kLengthApart(vector<int>& nums, int k) {\n unordered_map<int, int> mp;\n // int k = 0;\n for (int i = 0; i < nums.size(); i++)\n {\n if (nums[i] == 1 && mp.find(nums[i]) == mp.end())\n {\n mp[nums[i]] = i;\n }\n else...
1,548
<p>Given an binary array <code>nums</code> and an integer <code>k</code>, return <code>true</code><em> if all </em><code>1</code><em>&#39;s are at least </em><code>k</code><em> places away from each other, otherwise return </em><code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <i...
3
{ "code": "class Solution {\npublic:\n bool kLengthApart(vector<int>& nums, int k) {\n int n=nums.size();\n int cnt=0;\n for(int i=0;i<nums.size();i++){\n if(nums[i]==1) cnt++;\n }\n vector<int> arr2(cnt);\n int j=0;\n for(int i=0;i<n;i++){\n i...
1,548
<p>Given an binary array <code>nums</code> and an integer <code>k</code>, return <code>true</code><em> if all </em><code>1</code><em>&#39;s are at least </em><code>k</code><em> places away from each other, otherwise return </em><code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <i...
3
{ "code": "class Solution {\npublic:\n bool kLengthApart(vector<int>& nums, int k) {\n int n=nums.size();\n int cnt=0;\n for(int i=0;i<nums.size();i++){\n if(nums[i]==1) cnt++;\n }\n vector<int> arr2(cnt);\n int j=0;\n for(int i=0;i<n;i++){\n i...
1,548
<p>Given an binary array <code>nums</code> and an integer <code>k</code>, return <code>true</code><em> if all </em><code>1</code><em>&#39;s are at least </em><code>k</code><em> places away from each other, otherwise return </em><code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <i...
3
{ "code": "class Solution {\npublic:\n bool kLengthApart(vector<int>& nums, int k) {\n \n unordered_map<int,int>mpp;\n\n\n for(int i=0;i<nums.size();i++)\n {\n int currEl = nums[i];\n if(currEl == 1 && mpp.find(currEl) != mpp.end() && (i-mpp[currEl])<=k)\n ...
1,548
<p>Given an binary array <code>nums</code> and an integer <code>k</code>, return <code>true</code><em> if all </em><code>1</code><em>&#39;s are at least </em><code>k</code><em> places away from each other, otherwise return </em><code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <i...
3
{ "code": "class Solution {\npublic:\n bool kLengthApart(vector<int>& nums, int k) {\n if(k == 0) return true;\n vector<int> v;\n for(int i = 0 ; i < nums.size() ; ++i){\n if(nums[i] == 1) v.push_back(i);\n }\n for(int i = 0 ; i < v.size() ; ++i){\n for(int ...
1,548
<p>Given an binary array <code>nums</code> and an integer <code>k</code>, return <code>true</code><em> if all </em><code>1</code><em>&#39;s are at least </em><code>k</code><em> places away from each other, otherwise return </em><code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <i...
3
{ "code": "class Solution {\npublic:\n bool kLengthApart(vector<int>& nums, int k) {\n vector<int> indices;\n\n // Collect indices of all 1's\n for (int i = 0; i < nums.size(); i++) {\n if (nums[i] == 1) {\n indices.push_back(i);\n }\n }\n\n /...
1,548
<p>Given an binary array <code>nums</code> and an integer <code>k</code>, return <code>true</code><em> if all </em><code>1</code><em>&#39;s are at least </em><code>k</code><em> places away from each other, otherwise return </em><code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <i...
3
{ "code": "class Solution {\npublic:\n bool kLengthApart(vector<int>& nums, int k) {\n vector<int> positions;\n for (int i = 0; i < nums.size(); ++i)\n {\n if (nums[i] == 1)\n {\n positions.push_back(i);\n }\n }\n if (positions.empt...
1,548
<p>Given an binary array <code>nums</code> and an integer <code>k</code>, return <code>true</code><em> if all </em><code>1</code><em>&#39;s are at least </em><code>k</code><em> places away from each other, otherwise return </em><code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <i...
3
{ "code": "class Solution {\npublic:\n bool kLengthApart(vector<int>& nums, int k) {\n vector<int> v;\n for(int i = 0; i < nums.size() ; i++)\n {\n if(nums[i] == 1)\n {\n v.push_back(i);\n }\n }\n for(int i = 1 ; i < v.size() ; i++)...
1,549
<p>Given an array of integers <code>nums</code> and an integer <code>limit</code>, return the size of the longest <strong>non-empty</strong> subarray such that the absolute difference between any two elements of this subarray is less than or equal to <code>limit</code><em>.</em></p> <p>&nbsp;</p> <p><strong class="exa...
0
{ "code": "class Solution {\npublic:\n int longestSubarray(vector<int>& nums, int limit) {\n int i = 0, j = 0, ans = 1, diff = -1;\n for (i = 0; i < nums.size() - 1; i++) {\n if(nums.size()-i<ans){\n break;\n }\n int mini = nums[i];\n int max...
1,549
<p>Given an array of integers <code>nums</code> and an integer <code>limit</code>, return the size of the longest <strong>non-empty</strong> subarray such that the absolute difference between any two elements of this subarray is less than or equal to <code>limit</code><em>.</em></p> <p>&nbsp;</p> <p><strong class="exa...
0
{ "code": "class Solution {\npublic:\n int longestSubarray(vector<int>& nums, int limit) {\n int n = nums.size();\n\n if (n==1) {\n return 1;\n }\n\n int result = 1;\n\n for (int i = 0; i <n; i++) {\n if(result>n-i) {\n break;\n }\n...
1,549
<p>Given an array of integers <code>nums</code> and an integer <code>limit</code>, return the size of the longest <strong>non-empty</strong> subarray such that the absolute difference between any two elements of this subarray is less than or equal to <code>limit</code><em>.</em></p> <p>&nbsp;</p> <p><strong class="exa...
0
{ "code": "class Solution {\npublic:\n int longestSubarray(vector<int>& nums, int limit) {\n int n=nums.size(),res=0;\n int mins[n],maxes[n],lmin=0,rmin=-1,lmax=0,rmax=-1;\n for(int l=0,r=0;r<n;r++){\n while(lmin<=rmin&&mins[rmin]>nums[r]) --rmin;\n mins[++rmin]=nums[r];\...
1,549
<p>Given an array of integers <code>nums</code> and an integer <code>limit</code>, return the size of the longest <strong>non-empty</strong> subarray such that the absolute difference between any two elements of this subarray is less than or equal to <code>limit</code><em>.</em></p> <p>&nbsp;</p> <p><strong class="exa...
0
{ "code": "class Solution {\npublic:\n int longestSubarray(vector<int>& nums, int limit) {\n int n=nums.size(),res=0;\n int mins[n],maxes[n],lmin=0,rmin=-1,lmax=0,rmax=-1;\n for(int l=0,r=0;r<n;r++){\n while(lmin<=rmin&&mins[rmin]>nums[r]) --rmin;\n mins[++rmin]=nums[r];\...
1,549
<p>Given an array of integers <code>nums</code> and an integer <code>limit</code>, return the size of the longest <strong>non-empty</strong> subarray such that the absolute difference between any two elements of this subarray is less than or equal to <code>limit</code><em>.</em></p> <p>&nbsp;</p> <p><strong class="exa...
0
{ "code": "class Solution {\npublic:\n int longestSubarray(vector<int>& nums, int limit) {\n deque<int> maxDeque, minDeque;\n int start = 0, max_len = 0;\n\n for (int end = 0; end < nums.size(); ++end) {\n // Maintain the max deque\n while (!maxDeque.empty() && nums[maxD...
1,549
<p>Given an array of integers <code>nums</code> and an integer <code>limit</code>, return the size of the longest <strong>non-empty</strong> subarray such that the absolute difference between any two elements of this subarray is less than or equal to <code>limit</code><em>.</em></p> <p>&nbsp;</p> <p><strong class="exa...
0
{ "code": "class Solution {\npublic:\n int longestSubarray(vector<int>& nums, int limit) {\n deque<int>mindq;\n deque<int>maxdq;\n int n=nums.size();\n int i=0,j=0;\n int ans=0;\n while(j<n){\n // max deque\n while(!maxdq.empty()&&maxdq.front()<i){\n ...
1,549
<p>Given an array of integers <code>nums</code> and an integer <code>limit</code>, return the size of the longest <strong>non-empty</strong> subarray such that the absolute difference between any two elements of this subarray is less than or equal to <code>limit</code><em>.</em></p> <p>&nbsp;</p> <p><strong class="exa...
0
{ "code": "class Solution {\npublic:\n int longestSubarray(vector<int>& nums, int limit) {\n int n = nums.size(), start = 0, ans = 0;\n deque<int>inc,dec;\n for(int end = 0; end < n; end++) {\n while(!inc.empty() and nums[inc.back()] <= nums[end]) inc.pop_back();\n while(...
1,549
<p>Given an array of integers <code>nums</code> and an integer <code>limit</code>, return the size of the longest <strong>non-empty</strong> subarray such that the absolute difference between any two elements of this subarray is less than or equal to <code>limit</code><em>.</em></p> <p>&nbsp;</p> <p><strong class="exa...
0
{ "code": "class Solution {\npublic:\n int longestSubarray(vector<int>& nums, int limit) {\n deque<int> maxDeque, minDeque;\n int left = 0, maxLength = 0;\n\n for (int right = 0; right < nums.size(); ++right) {\n // Maintain the maxDeque in decreasing order\n while (!maxD...
1,549
<p>Given an array of integers <code>nums</code> and an integer <code>limit</code>, return the size of the longest <strong>non-empty</strong> subarray such that the absolute difference between any two elements of this subarray is less than or equal to <code>limit</code><em>.</em></p> <p>&nbsp;</p> <p><strong class="exa...
0
{ "code": "class Solution {\npublic:\n int longestSubarray(vector<int>& nums, int limit) {\n int ans=INT_MIN;\n deque<int>minQ;\n deque<int>maxQ;\n int s=0,e=0;\n while(e<nums.size())\n {\n int x=nums[e];\n while(!minQ.empty() && nums[minQ.back()]>=x)...
1,549
<p>Given an array of integers <code>nums</code> and an integer <code>limit</code>, return the size of the longest <strong>non-empty</strong> subarray such that the absolute difference between any two elements of this subarray is less than or equal to <code>limit</code><em>.</em></p> <p>&nbsp;</p> <p><strong class="exa...
0
{ "code": "class Solution {\npublic:\n int longestSubarray(vector<int>& nums, int limit) {\n vector<int> maxQueue(nums.size(), 0);\n int maxl = 0, maxr = 0;\n vector<int> minQueue(nums.size(), 0);\n int minl = 0, minr = 0;\n\n auto push = [&](int index){\n // push to m...
1,549
<p>Given an array of integers <code>nums</code> and an integer <code>limit</code>, return the size of the longest <strong>non-empty</strong> subarray such that the absolute difference between any two elements of this subarray is less than or equal to <code>limit</code><em>.</em></p> <p>&nbsp;</p> <p><strong class="exa...
0
{ "code": "class Solution {\npublic:\n int longestSubarray(vector<int>& nums, int limit) {\n deque<pair<int,int>> maxDeque, minDeque;\n maxDeque.emplace_back(nums[0],0); // add from front/back, pop from back\n minDeque.emplace_back(nums[0],0); // 7 10 39 96\n int res = 1, s = 0;\n ...
1,549
<p>Given an array of integers <code>nums</code> and an integer <code>limit</code>, return the size of the longest <strong>non-empty</strong> subarray such that the absolute difference between any two elements of this subarray is less than or equal to <code>limit</code><em>.</em></p> <p>&nbsp;</p> <p><strong class="exa...
0
{ "code": "class Solution {\npublic:\n int longestSubarray(vector<int>& nums, int limit) {\n deque<int> minQ;\n deque<int> maxQ;\n\n int begin = 0;\n int end = 0;\n int maxCount = 0;\n while (begin < nums.size())\n {\n while (end < nums.size())\n ...
1,549
<p>Given an array of integers <code>nums</code> and an integer <code>limit</code>, return the size of the longest <strong>non-empty</strong> subarray such that the absolute difference between any two elements of this subarray is less than or equal to <code>limit</code><em>.</em></p> <p>&nbsp;</p> <p><strong class="exa...
0
{ "code": "class Solution {\npublic:\n deque<int> maxq;\n deque<int> minq;\n\n void enquemax(int x) {\n // Maintain elements in decreasing order in maxq\n while (!maxq.empty() && x > maxq.back()) {\n maxq.pop_back();\n }\n maxq.push_back(x);\n }\n\n void enquemin(...
1,549
<p>Given an array of integers <code>nums</code> and an integer <code>limit</code>, return the size of the longest <strong>non-empty</strong> subarray such that the absolute difference between any two elements of this subarray is less than or equal to <code>limit</code><em>.</em></p> <p>&nbsp;</p> <p><strong class="exa...
0
{ "code": "class Solution {\npublic:\n int longestSubarray(vector<int>& A, int limit) {\n deque<int> maxd, mind;\n int i = 0, j;\n for (j = 0; j < A.size(); ++j) {\n while (!maxd.empty() && A[j] > maxd.back()) maxd.pop_back();\n while (!mind.empty() && A[j] < mind.back())...
1,549
<p>Given an array of integers <code>nums</code> and an integer <code>limit</code>, return the size of the longest <strong>non-empty</strong> subarray such that the absolute difference between any two elements of this subarray is less than or equal to <code>limit</code><em>.</em></p> <p>&nbsp;</p> <p><strong class="exa...
0
{ "code": "class Solution {\npublic:\n int longestSubarray(vector<int>& nums, int limit) {\n deque<int> maxDeque, minDeque;\n int left = 0, right;\n int maxLength = 0;\n\n for (right = 0; right < nums.size(); ++right) {\n // Maintain the maxDeque in decreasing order\n ...
1,549
<p>Given an array of integers <code>nums</code> and an integer <code>limit</code>, return the size of the longest <strong>non-empty</strong> subarray such that the absolute difference between any two elements of this subarray is less than or equal to <code>limit</code><em>.</em></p> <p>&nbsp;</p> <p><strong class="exa...
0
{ "code": "class MonotonicQueue {\nprivate:\n deque<int> minq;\n deque<int> maxq;\n\npublic:\n void push(int x) {\n while (!minq.empty() && minq.back() > x) {\n minq.pop_back();\n }\n\n minq.push_back(x);\n\n while (!maxq.empty() && maxq.back() < x) {\n maxq....
1,549
<p>Given an array of integers <code>nums</code> and an integer <code>limit</code>, return the size of the longest <strong>non-empty</strong> subarray such that the absolute difference between any two elements of this subarray is less than or equal to <code>limit</code><em>.</em></p> <p>&nbsp;</p> <p><strong class="exa...
0
{ "code": "class Solution {\npublic:\n /*\n */\n // heaps\n int longestSubarray(vector<int>& nums, int limit) {\n auto maxCmp = [&nums](int lhs, int rhs){ return nums[lhs] < nums[rhs]; };\n auto minCmp = [&nums](int lhs, int rhs){ return nums[lhs] > nums[rhs]; };\n std::priority_queue<int, std::vector<in...
1,549
<p>Given an array of integers <code>nums</code> and an integer <code>limit</code>, return the size of the longest <strong>non-empty</strong> subarray such that the absolute difference between any two elements of this subarray is less than or equal to <code>limit</code><em>.</em></p> <p>&nbsp;</p> <p><strong class="exa...
0
{ "code": "class Solution {\npublic:\n int longestSubarray(vector<int>& nums, int limit) {\n auto maxCmp = [&nums](int lhs, int rhs) { return nums[lhs] < nums[rhs]; };\n auto minCmp = [&nums](int lhs, int rhs) { return nums[lhs] > nums[rhs]; };\n std::priority_queue<int, std::vector<int>, decltype(maxCmp)> ...
1,549
<p>Given an array of integers <code>nums</code> and an integer <code>limit</code>, return the size of the longest <strong>non-empty</strong> subarray such that the absolute difference between any two elements of this subarray is less than or equal to <code>limit</code><em>.</em></p> <p>&nbsp;</p> <p><strong class="exa...
0
{ "code": "class Solution {\npublic:\n /*\n */\n // heaps\n int longestSubarray(vector<int>& nums, int limit) {\n auto maxCmp = [&nums](int lhs, int rhs){ return nums[lhs] < nums[rhs]; };\n auto minCmp = [&nums](int lhs, int rhs){ return nums[lhs] > nums[rhs]; };\n std::priority_queue<int, std::vector<in...
1,549
<p>Given an array of integers <code>nums</code> and an integer <code>limit</code>, return the size of the longest <strong>non-empty</strong> subarray such that the absolute difference between any two elements of this subarray is less than or equal to <code>limit</code><em>.</em></p> <p>&nbsp;</p> <p><strong class="exa...
0
{ "code": "class Solution {\npublic:\n int longestSubarray(vector<int>& nums, int limit) {\n int n = nums.size();\n int l = 0;\n int r = 0;\n deque<int> ma;\n deque<int> mi;\n int ans = 0;\n while(r < n){\n if(ma.empty()) ma.push_back(r);\n els...
1,549
<p>Given an array of integers <code>nums</code> and an integer <code>limit</code>, return the size of the longest <strong>non-empty</strong> subarray such that the absolute difference between any two elements of this subarray is less than or equal to <code>limit</code><em>.</em></p> <p>&nbsp;</p> <p><strong class="exa...
0
{ "code": "class Solution {\npublic:\n int longestSubarray(vector<int>& nums, int limit) {\n int n = nums.size();\n int l = 0;\n int r = 0;\n deque<int> ma;\n deque<int> mi;\n int ans = 0;\n while(r < n){\n if(ma.empty()) ma.push_back(r);\n els...
1,549
<p>Given an array of integers <code>nums</code> and an integer <code>limit</code>, return the size of the longest <strong>non-empty</strong> subarray such that the absolute difference between any two elements of this subarray is less than or equal to <code>limit</code><em>.</em></p> <p>&nbsp;</p> <p><strong class="exa...
0
{ "code": "class Solution {\npublic:\n int longestSubarray(vector<int>& nums, int limit) {\n deque<int> mini;\n deque<int> maxi;\n\n int i=0;\n int j =0;\n int ans = 0;\n\n while(j<nums.size()){\n \n while(mini.size() && nums[mini.back()] > nums[j]){\...
1,549
<p>Given an array of integers <code>nums</code> and an integer <code>limit</code>, return the size of the longest <strong>non-empty</strong> subarray such that the absolute difference between any two elements of this subarray is less than or equal to <code>limit</code><em>.</em></p> <p>&nbsp;</p> <p><strong class="exa...
0
{ "code": "class Solution {\npublic:\n int longestSubarray(vector<int>& nums, int limit) {\n int ans = 0;\n int left = 0;\n int right = 0;\n\n int maxi = INT_MIN;\n int mini = INT_MAX;\n unordered_map<int, int> mp;\n\n while(right < nums.size()) {\n maxi ...
1,549
<p>Given an array of integers <code>nums</code> and an integer <code>limit</code>, return the size of the longest <strong>non-empty</strong> subarray such that the absolute difference between any two elements of this subarray is less than or equal to <code>limit</code><em>.</em></p> <p>&nbsp;</p> <p><strong class="exa...
0
{ "code": "class Solution {\npublic:\n int longestSubarray(vector<int>& nums, int k) {\n int i=0;\n int j=0;\n int ans=0;\n unordered_map<int,int>m;\n int maxi=INT_MIN;\n int mini=INT_MAX;\n while(j<nums.size()){\n m[nums[j]]++;\n maxi=max(maxi...
1,549
<p>Given an array of integers <code>nums</code> and an integer <code>limit</code>, return the size of the longest <strong>non-empty</strong> subarray such that the absolute difference between any two elements of this subarray is less than or equal to <code>limit</code><em>.</em></p> <p>&nbsp;</p> <p><strong class="exa...
0
{ "code": "class Solution {\npublic:\n int longestSubarray(vector<int>& nums, int limit) {\n int n=nums.size(),lmin=0,rmin=-1,lmax=0,rmax=-1,res=0;\n int mins[n],maxes[n];\n for(int l=0,r=0;r<n;r++){\n while(lmin<=rmin&&mins[rmin]>nums[r]) --rmin;\n mins[++rmin]=nums[r];\...
1,549
<p>Given an array of integers <code>nums</code> and an integer <code>limit</code>, return the size of the longest <strong>non-empty</strong> subarray such that the absolute difference between any two elements of this subarray is less than or equal to <code>limit</code><em>.</em></p> <p>&nbsp;</p> <p><strong class="exa...
0
{ "code": "class Solution {\npublic:\n /*\n */\n // heaps\n int longestSubarray(vector<int>& nums, int limit) {\n auto maxCmp = [&nums](int lhs, int rhs){ return nums[lhs] < nums[rhs]; };\n auto minCmp = [&nums](int lhs, int rhs){ return nums[lhs] > nums[rhs]; };\n std::priority_queue<int, std::vector<in...
1,549
<p>Given an array of integers <code>nums</code> and an integer <code>limit</code>, return the size of the longest <strong>non-empty</strong> subarray such that the absolute difference between any two elements of this subarray is less than or equal to <code>limit</code><em>.</em></p> <p>&nbsp;</p> <p><strong class="exa...
0
{ "code": "// auto init = []()\n// { \n// ios::sync_with_stdio(0);\n// cin.tie(0);\n// cout.tie(0);\n// return 'c';\n// }();\n\nclass Solution {\npublic:\n int longestSubarray(vector<int>& nums, int limit) {\n int left = 0, right = 0, n = nums.size();\n deque<pair<int, int>> maxi, min...
1,549
<p>Given an array of integers <code>nums</code> and an integer <code>limit</code>, return the size of the longest <strong>non-empty</strong> subarray such that the absolute difference between any two elements of this subarray is less than or equal to <code>limit</code><em>.</em></p> <p>&nbsp;</p> <p><strong class="exa...
0
{ "code": "static const int iofast = [](){\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n return 0;\n}();\n\nint q0[100000+1];\nint q1[100000+1];\nint l0 = {};\nint r0 = {};\nint l1 = {};\nint r1 = {};\n\nclass Solution {\npublic:\n int longestSubarray(vector<int>& a, int ...
1,549
<p>Given an array of integers <code>nums</code> and an integer <code>limit</code>, return the size of the longest <strong>non-empty</strong> subarray such that the absolute difference between any two elements of this subarray is less than or equal to <code>limit</code><em>.</em></p> <p>&nbsp;</p> <p><strong class="exa...
0
{ "code": "class Solution {\npublic:\n deque<int> Qu_mx;\n deque<int> Qu_mi;\n vector<int> nums;\n\n void move_right(int r) {\n while (!Qu_mx.empty() && nums[r] >= nums[Qu_mx.back()]) {\n Qu_mx.pop_back();\n }\n while (!Qu_mi.empty() && nums[r] <= nums[Qu_mi.back()]) {\n ...
1,549
<p>Given an array of integers <code>nums</code> and an integer <code>limit</code>, return the size of the longest <strong>non-empty</strong> subarray such that the absolute difference between any two elements of this subarray is less than or equal to <code>limit</code><em>.</em></p> <p>&nbsp;</p> <p><strong class="exa...
0
{ "code": "const static auto _ = []() {\n cin.tie(nullptr)->sync_with_stdio(false);\n return nullptr;\n}();\n\nclass Solution {\npublic:\n int longestSubarray(vector<int>& nums, int limit) {\n deque<int> min_q;\n deque<int> max_q;\n int left = -1;\n int ans = 0;\n\n for (int i ...
1,549
<p>Given an array of integers <code>nums</code> and an integer <code>limit</code>, return the size of the longest <strong>non-empty</strong> subarray such that the absolute difference between any two elements of this subarray is less than or equal to <code>limit</code><em>.</em></p> <p>&nbsp;</p> <p><strong class="exa...
0
{ "code": "class Solution {\npublic:\n int longestSubarray(vector<int>& nums, int limit) {\n deque<int> maxQue;\n deque<int> minQue;\n int right = 0;\n int left = 0;\n int result = 0;\n\n while(right < nums.size())\n {\n while(!maxQue.empty() && nums[maxQ...
1,549
<p>Given an array of integers <code>nums</code> and an integer <code>limit</code>, return the size of the longest <strong>non-empty</strong> subarray such that the absolute difference between any two elements of this subarray is less than or equal to <code>limit</code><em>.</em></p> <p>&nbsp;</p> <p><strong class="exa...
0
{ "code": "class Solution {\npublic:\n int longestSubarray(vector<int>& nums, int limit) {\n int ans=1 ,l =0, r=0;\n deque<int> minq;\n deque<int> maxq;\n\n while(r<nums.size()){\n int val= nums[r];\n\n while(!minq.empty() && val <=nums[minq.back()]) \n ...
1,549
<p>Given an array of integers <code>nums</code> and an integer <code>limit</code>, return the size of the longest <strong>non-empty</strong> subarray such that the absolute difference between any two elements of this subarray is less than or equal to <code>limit</code><em>.</em></p> <p>&nbsp;</p> <p><strong class="exa...
0
{ "code": "class Solution {\npublic:\n int longestSubarray(vector<int>& nums, int limit) {\n // sort(nums.begin(),nums.end());\n int s=0,e=0,ans=0;\n int mini=nums[s],maxe=nums[0];\n priority_queue<pair<int,int>> max_heap;\n priority_queue<pair<int,int>,vector<pair<int,int>>,grea...
1,549
<p>Given an array of integers <code>nums</code> and an integer <code>limit</code>, return the size of the longest <strong>non-empty</strong> subarray such that the absolute difference between any two elements of this subarray is less than or equal to <code>limit</code><em>.</em></p> <p>&nbsp;</p> <p><strong class="exa...
0
{ "code": "class Solution {\npublic:\n int longestSubarray(vector<int>& nums, int limit) {\n /*\n 2,4,7,8\n 2 last index which is less than equal to (2+limit)\n ar[i] last idx <= (limit + ar[i])\n i=j=0;\n mx-mi>=limit\n */\n\n int n = nums.size();...
1,549
<p>Given an array of integers <code>nums</code> and an integer <code>limit</code>, return the size of the longest <strong>non-empty</strong> subarray such that the absolute difference between any two elements of this subarray is less than or equal to <code>limit</code><em>.</em></p> <p>&nbsp;</p> <p><strong class="exa...
0
{ "code": "#include <deque>\n#include <algorithm>\n\nclass Solution {\npublic:\n int longestSubarray(vector<int>& nums, int limit) {\n deque<int> increasing;\n deque<int> decreasing;\n int left = 0, ans = 0;\n \n for (int right = 0; right < nums.size(); right++) {\n wh...
1,549
<p>Given an array of integers <code>nums</code> and an integer <code>limit</code>, return the size of the longest <strong>non-empty</strong> subarray such that the absolute difference between any two elements of this subarray is less than or equal to <code>limit</code><em>.</em></p> <p>&nbsp;</p> <p><strong class="exa...
0
{ "code": "class Solution {\npublic:\n int longestSubarray(vector<int>& nums, int limit) {\n deque<int> maxDeque, minDeque;\n int left = 0, right;\n int maxLength = 0;\n\n for (right = 0; right < nums.size(); ++right) {\n // Maintain the maxDeque in decreasing order\n ...
1,549
<p>Given an array of integers <code>nums</code> and an integer <code>limit</code>, return the size of the longest <strong>non-empty</strong> subarray such that the absolute difference between any two elements of this subarray is less than or equal to <code>limit</code><em>.</em></p> <p>&nbsp;</p> <p><strong class="exa...
1
{ "code": "class Solution {\npublic:\n int longestSubarray(vector<int>& nums, int limit) {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n \n deque<int> maxi, mini;\n\n int i = 0, j = 0, longest = 0;\n\n for (int i = 0; i < nums.size() && j < nums.size()-longest; ++...
1,549
<p>Given an array of integers <code>nums</code> and an integer <code>limit</code>, return the size of the longest <strong>non-empty</strong> subarray such that the absolute difference between any two elements of this subarray is less than or equal to <code>limit</code><em>.</em></p> <p>&nbsp;</p> <p><strong class="exa...
1
{ "code": "\nclass Solution {\npublic:\n int longestSubarray(vector<int>& nums, int limit) {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n deque<int> maxi, mini;\n int j = 0, i = 0;\n int ans = 0;\n \n for (i = 0; i < nums.size(); i++) {\n // Maintai...
1,549
<p>Given an array of integers <code>nums</code> and an integer <code>limit</code>, return the size of the longest <strong>non-empty</strong> subarray such that the absolute difference between any two elements of this subarray is less than or equal to <code>limit</code><em>.</em></p> <p>&nbsp;</p> <p><strong class="exa...
1
{ "code": "class Solution {\npublic:\n int longestSubarray(vector<int>& nums, int limit) {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n map<int, int> hmap;\n\n int ans = 1;\n int l = 0, r = -1;\n int maxWindowLength = 0;\n // cout<<r<<' '<<(r>(int)nums.siz...
1,549
<p>Given an array of integers <code>nums</code> and an integer <code>limit</code>, return the size of the longest <strong>non-empty</strong> subarray such that the absolute difference between any two elements of this subarray is less than or equal to <code>limit</code><em>.</em></p> <p>&nbsp;</p> <p><strong class="exa...
1
{ "code": "class Solution {\npublic:\n struct monotone_deque {\n deque<int> dq;\n bool _mini;\n\n monotone_deque(bool mini) : _mini(mini) {}\n\n void insert(int x) {\n if (_mini) {\n while (!dq.empty() && dq.back() > x) {\n dq.pop_back();\n ...
1,549
<p>Given an array of integers <code>nums</code> and an integer <code>limit</code>, return the size of the longest <strong>non-empty</strong> subarray such that the absolute difference between any two elements of this subarray is less than or equal to <code>limit</code><em>.</em></p> <p>&nbsp;</p> <p><strong class="exa...
1
{ "code": "const int inf = 1e9 + 7;\nvector<int> v;\n\nstruct mnx_stk {\n deque<int> mx_dq, mn_dq;\n\n void push(int i) {\n while (!mx_dq.empty() && v[mx_dq.back()] <= v[i])\n mx_dq.pop_back();\n while (!mn_dq.empty() && v[mn_dq.back()] >= v[i])\n mn_dq.pop_back();\n m...
1,549
<p>Given an array of integers <code>nums</code> and an integer <code>limit</code>, return the size of the longest <strong>non-empty</strong> subarray such that the absolute difference between any two elements of this subarray is less than or equal to <code>limit</code><em>.</em></p> <p>&nbsp;</p> <p><strong class="exa...
1
{ "code": "class Solution {\npublic:\n int longestSubarray(vector<int>& nums, int limit) {\n priority_queue<pair<int, int>> maxHeap;\n priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> minHeap;\n\n int left = 0, curr = 0;\n int max_len = -1;\n while(...
1,549
<p>Given an array of integers <code>nums</code> and an integer <code>limit</code>, return the size of the longest <strong>non-empty</strong> subarray such that the absolute difference between any two elements of this subarray is less than or equal to <code>limit</code><em>.</em></p> <p>&nbsp;</p> <p><strong class="exa...
1
{ "code": "class Solution {\npublic:\n int longestSubarray(vector<int>& nums, int limit) {\n priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>ls;\n priority_queue<pair<int,int>,vector<pair<int,int>>,less<pair<int,int>>>gr;\n\n int j = 0;\n int i = 0;\n in...
1,549
<p>Given an array of integers <code>nums</code> and an integer <code>limit</code>, return the size of the longest <strong>non-empty</strong> subarray such that the absolute difference between any two elements of this subarray is less than or equal to <code>limit</code><em>.</em></p> <p>&nbsp;</p> <p><strong class="exa...
1
{ "code": "class Solution {\npublic:\n int longestSubarray(vector<int>& nums, int limit) {\n priority_queue<int, vector<int>, function<bool(int, int)>> maxh(\n [&](int i, int j) {\n return nums[i] < nums[j];\n }\n );\n priority_queue<int, vector<int>, funct...
1,549
<p>Given an array of integers <code>nums</code> and an integer <code>limit</code>, return the size of the longest <strong>non-empty</strong> subarray such that the absolute difference between any two elements of this subarray is less than or equal to <code>limit</code><em>.</em></p> <p>&nbsp;</p> <p><strong class="exa...
1
{ "code": "class Solution {\npublic:\n int longestSubarray(vector<int>& nums, int limit) {\n \n int ans = 0;\n deque<int> mindq, maxdq;\n \n int left = 0, right = 0;\n \n while(right < nums.size()) {\n \n while(!mindq.empty() && mindq.front() <...
1,549
<p>Given an array of integers <code>nums</code> and an integer <code>limit</code>, return the size of the longest <strong>non-empty</strong> subarray such that the absolute difference between any two elements of this subarray is less than or equal to <code>limit</code><em>.</em></p> <p>&nbsp;</p> <p><strong class="exa...
1
{ "code": "#include <algorithm>\n#include <vector>\n#include <queue>\n#include <deque>\n\nclass MonotonicQueue {\n deque<int> q; // Normal queue\n deque<int> maxQ; // Front is the largest\n deque<int> minQ; // Front is the smallest\n\n\npublic:\n // When pushing the new value into the qu...
1,549
<p>Given an array of integers <code>nums</code> and an integer <code>limit</code>, return the size of the longest <strong>non-empty</strong> subarray such that the absolute difference between any two elements of this subarray is less than or equal to <code>limit</code><em>.</em></p> <p>&nbsp;</p> <p><strong class="exa...
1
{ "code": "#include <deque>\nclass Solution {\npublic:\n int longestSubarray(vector<int>& nums, int limit) {\n int ans = 0;\n deque<pair<int,int>> max_dq,min_dq;\n for(int r=0,l=0;r<nums.size();r++)\n {\n while(!max_dq.empty() && nums[r] > max_dq.back().first)\n ma...
1,549
<p>Given an array of integers <code>nums</code> and an integer <code>limit</code>, return the size of the longest <strong>non-empty</strong> subarray such that the absolute difference between any two elements of this subarray is less than or equal to <code>limit</code><em>.</em></p> <p>&nbsp;</p> <p><strong class="exa...
1
{ "code": "#include <deque>\nclass Solution {\npublic:\n int longestSubarray(vector<int>& nums, int limit) {\n int ans = 0;\n deque<pair<int,int>> max_dq,min_dq;\n for(int r=0,l=0;r<nums.size();r++)\n {\n while(!max_dq.empty() && nums[r] > max_dq.back().first)\n ma...
1,549
<p>Given an array of integers <code>nums</code> and an integer <code>limit</code>, return the size of the longest <strong>non-empty</strong> subarray such that the absolute difference between any two elements of this subarray is less than or equal to <code>limit</code><em>.</em></p> <p>&nbsp;</p> <p><strong class="exa...
1
{ "code": "class Solution {\npublic:\n int longestSubarray(vector<int>& nums, int limit) {\n deque<pair<int, int>> inc, dec;\n int start = 0;\n int result = 0;\n \n for (int end = 0; end < nums.size(); end++) {\n while (!inc.empty() && inc.back().first > nums[end]) {\n...
1,549
<p>Given an array of integers <code>nums</code> and an integer <code>limit</code>, return the size of the longest <strong>non-empty</strong> subarray such that the absolute difference between any two elements of this subarray is less than or equal to <code>limit</code><em>.</em></p> <p>&nbsp;</p> <p><strong class="exa...
1
{ "code": "#define pi pair<int, int>\nclass Solution {\n public:\n int longestSubarray(vector<int>& nums, int limit) {\n deque<pair<int, int>> minDq, maxDq;\n int left = 0, right = 0, len = nums.size(), ans = 1;\n while (right < len) {\n // set minDQ\n while (minDq.size...
1,549
<p>Given an array of integers <code>nums</code> and an integer <code>limit</code>, return the size of the longest <strong>non-empty</strong> subarray such that the absolute difference between any two elements of this subarray is less than or equal to <code>limit</code><em>.</em></p> <p>&nbsp;</p> <p><strong class="exa...
1
{ "code": "#define pi pair<int, int>\nclass Solution {\n public:\n int longestSubarray(vector<int>& nums, int limit) {\n cin.tie(), cout.tie();\n ios_base::sync_with_stdio(false);\n deque<pair<int, int>> minDq, maxDq;\n int left = 0, right = 0, len = nums.size(), ans = 1;\n whi...
1,549
<p>Given an array of integers <code>nums</code> and an integer <code>limit</code>, return the size of the longest <strong>non-empty</strong> subarray such that the absolute difference between any two elements of this subarray is less than or equal to <code>limit</code><em>.</em></p> <p>&nbsp;</p> <p><strong class="exa...
1
{ "code": "#include <vector>\n#include <map>\n#include <queue>\n#include <algorithm>\n\nclass Solution {\npublic:\n int longestSubarray(std::vector<int>& nums, int limit) {\n int n = nums.size();\n std::map<int, int> mp;\n std::priority_queue<int> maxHeap;\n std::priority_queue<int, std...