id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
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 int n=nums.size();\n int maxi=0;\n int l=0;\n int r=0;\n unordered_map<int,int> m;\n set<int> s;\n while(l<n&&r<n){\n s.insert(nums[r]);\n m[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...
1
{ "code": "#include<algorithm>\n#include<queue>\n\nusing namespace std;\n\nclass Solution {\npublic:\n struct node{\n int v, max_v, min_v;\n };\n\n void push(stack<node>& s1, int v){\n node tmp;\n if (s1.empty()){\n tmp.v = v;\n tmp.max_v = v;\n tmp.min_v...
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<queue>\n\nusing namespace std;\n\nclass Solution {\npublic:\n struct node{\n int v, max_v, min_v;\n };\n\n void push(stack<node>& s1, int v){\n node tmp;\n if (s1.empty()){\n tmp.v = v;\n tmp.max_v = v;\n tmp.min_v...
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 {\n using pi = std::pair<int,int>;\npublic:\n int longestSubarray(vector<int>& nums, int limit) {\n std::priority_queue<pi> max_heap;\n std::priority_queue<pi, std::vector<pi>, std::greater<>> min_heap;\n int ret = 1;\n for (int start = 0, i = 0; i < nums.si...
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>>> mnpq;\n priority_queue<pair<int,int>> mxpq;\n\n int i=0,j=0,n=nums.size(),ans=0;\n while(j<n){\n mnpq.push(...
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 using P = pair<int, int>; // {num, idx}\n int longestSubarray(vector<int>& nums, int limit) {\n int n = nums.size();\n priority_queue<P> maxHeap;\n priority_queue<P, vector<P>, greater<P>> minHeap;\n int l = 0;\n int r = 0;\n int a...
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...
2
{ "code": "\nclass Solution {\n public:\n using Elt = pair<int, size_t>;\n\n int longestSubarray(vector<int>& nums, int limit) {\n priority_queue<Elt> max_queue;\n priority_queue<Elt, vector<Elt>, greater<>> min_queue;\n\n size_t start = 0;\n size_t end = 0;\n\n int max_length = 1;\n\n max_queue.e...
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...
2
{ "code": "class Solution {\npublic:\n struct MaxStack {\n stack<pair<int, int>> stk;\n bool empty() { return stk.empty(); }\n void push(int e) { stk.push(make_pair(e, max(e, this->getMax()))); }\n void pop() { stk.pop(); }\n int top() { return stk.top().first; }\n int get...
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...
2
{ "code": "class Solution {\npublic:\n int longestSubarray(vector<int>& nums, int limit) {\n priority_queue<pair<int,int>,vector<pair<int,int>>,greater<>>mini;\n priority_queue<pair<int,int>>maxi;\n int ans=0;\n int left=0;\n for (int i=0;i<nums.size();i++)\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...
2
{ "code": "struct revorder {\n bool operator()(const int& lhs, const int& rhs) const {\n return lhs > rhs;\n }\n };\nclass Solution {\npublic:\n int longestSubarray(vector<int>& nums, int limit) {\n int ans = 0;\n int l=0,r=0;\n map<int,int> mn;\n map<int,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...
2
{ "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\nstruct stack_max {\n vector<int> s = {};\n vector<int> smax = {};\n void push(int x) {\n s.push_back(x);\n if (smax.size() == 0 || x >= smax.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...
2
{ "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\nstruct stack_max {\n vector<int> s = {};\n vector<int> smax = {};\n void push(int x) {\n s.push_back(x);\n if (smax.size() == 0 || x >= smax.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...
2
{ "code": "\nclass Solution {\n \npublic:\n //here I have used pair to handle duplicate values and can be categoried by index\n int longestSubarray(vector<int>& nums, int limit) {\n int i=0,j=0,n=nums.size(),mx,mn,res=0;\n priority_queue<pair<int,int>> mxhp; //to find max value in window\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...
2
{ "code": "class Solution {\npublic:\n int longestSubarray(vector<int>& nums, int limit) {\n int i = 0, j = 0;\n int max = INT_MIN;\n int numMin = nums[0];\n int numMax = nums[0];\n int iMin = 0;\n int iMax = 0;\n priority_queue<pair<int, int>, vector<pair<int,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...
2
{ "code": "class Solution {\npublic:\n int longestSubarray(vector<int>& nums, int limit) {\n int i = 0, j = 0;\n int max = INT_MIN;\n int numMin = nums[0];\n int numMax = nums[0];\n int iMin = 0;\n int iMax = 0;\n priority_queue<pair<int, int>, vector<pair<int,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...
2
{ "code": "class Solution {\npublic:\n int longestSubarray(vector<int>& nums, int limit) {\n int n = nums.size();\n\n int i = 0, j = 0, cur_max = nums[0], cur_min = nums[0];\n std::map<int, int> nums_count;\n nums_count[nums[0]] = 1;\n int ans = 0;\n\n while (i < 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...
2
{ "code": "class Solution {\npublic:\n int longestSubarray(vector<int>& nums, int limit) {\n map<int, int> m;\n int l = 0;\n int out = 1;\n for (int r = 0; r < nums.size(); r++){\n m[nums[r]]++;\n while (abs(m.begin()->first - m.rbegin()->first) > limit){\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...
2
{ "code": "class Solution {\npublic:\n int longestSubarray(vector<int>& nums, int limit) {\n map<int, int> m;\n int l = 0;\n int out = 1;\n for (int r = 0; r < nums.size(); r++){\n m[nums[r]]++;\n while (abs(m.begin()->first - m.rbegin()->first) > limit){\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...
2
{ "code": "class Solution {\npublic:\n int longestSubarray(vector<int>&v, int l) {\n int i=0, j=0, n=v.size(), ans=0;\n unordered_map<int,int> m;\n deque<int> mxq, mnq;\n while(j<n && i <= j){\n m[v[j]]++;\n if(m[v[j]] == 1){\n while(!mxq.empty() && ...
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...
2
{ "code": "class Solution {\npublic:\n int longestSubarray(vector<int>&v, int l) {\n int i=0, j=0, n=v.size(), ans=0;\n unordered_map<int,int> m;\n priority_queue<int> mxpq;\n priority_queue<int, vector<int>, greater<int>> mnpq;\n while(j<n && i <= j){\n m[v[j]]++;\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...
2
{ "code": "class Solution {\npublic:\n int longestSubarray(vector<int>& nums, int limit) {\n // Can do a two-ptr approach w/ linear time solution\n // Keep track of the length between start/end ptrs, take max as solution.\n // Also keep track of min/max in current run. Key obs: \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...
2
{ "code": "class Solution {\npublic:\n class segmentTree{\n vector<int> tree;\n public:\n segmentTree(vector<int> nums){\n int n = nums.size();\n tree = vector<int>(4*n+1);\n build(nums);\n }\n\n void build(vector<int> &nums){\n int 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...
2
{ "code": "class Solution {\npublic:\n int longestSubarray(vector<int>& nums, int limit) {\n int ans = 1;\n int n = nums.size();\n int l = 0;\n int r = 0;\n \n // Priority queues to maintain max and min within the current window\n priority_queue<pair<int, 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...
2
{ "code": "class Solution {\npublic:\n // Function to find the longest subarray with elements within a specific limit\nint longestSubarray(vector<int>& nums, int limit) {\n // Initialize variables\n int i = 0, j = 0; // Pointers for sliding window (i - start, j - end)\n int n = nums.size(); // Size of the ...
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...
2
{ "code": "class Solution {\npublic:\n\n int calc(vector<int>& arr, int& limit) {\n int n = (int)arr.size();\n multiset<int> ms;\n int head = -1, tail = 0;\n int ans = 0;\n while(tail < n) {\n while(head+1 < n && (\n ms.empty() || (\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...
2
{ "code": "#include <vector>\n#include <set>\n#include <climits>\nusing namespace std;\n\nclass Solution {\npublic:\n int longestSubarray(vector<int>& nums, int limit) {\n int n = nums.size();\n multiset<int> s;\n int l = 0, r = 0, maxlen = 0;\n\n while (r < n) {\n s.insert(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...
2
{ "code": "class Solution {\npublic:\n int longestSubarray(vector<int>& nums, int limit) {\n int n=nums.size();\n multiset<int> ml;\n int maxlen=0, l=0, max= INT_MIN, min=INT_MAX;\n for(auto i:nums)\n {\n ml.insert(i);\n min=*ml.begin();\n max=*ml...
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...
2
{ "code": "class Solution {\npublic:\n int longestSubarray(vector<int>& nums, int limit) {\n multiset<int>window;\n int l=0,ans=0;\n for(int r=0;r<nums.size();r++){\n window.insert(nums[r]);\n while(*window.rbegin()-*window.begin()>limit){\n window.erase(wi...
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...
2
{ "code": "class Solution {\npublic:\n int longestSubarray(vector<int>& nums, int limit) {\n int n=nums.size(),maxLen=0;\n int left=0,right=0;\n multiset<int> st;\n\n while(right<n){\n st.insert(nums[right]);\n if(abs(*rbegin(st)-*begin(st))>limit){\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...
2
{ "code": "class Solution {\npublic:\n int longestSubarray(vector<int>& nums, int limit) {\n int longest= 0;\n int left =0;\n multiset<int,greater<int>>ml;\n for(int i =0;i<nums.size();i++){\n ml.insert(nums[i]);\n int large = *ml.begin();\n int small = ...
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...
2
{ "code": "class Solution {\npublic:\n int longestSubarray(vector<int>& nums, int limit) {\n int n = nums.size();\n multiset<int> st;\n\n int i =0, j=0, maxL =0;\n while(j<n){\n st.insert({nums[j]});\n\n while( *st.rbegin() - *st.begin() > limit){\n st.erase(st.find...
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...
2
{ "code": "class Solution {\npublic:\n int longestSubarray(vector<int>& nums, int limit) {\n std::multiset<int> ms;\n int min{ INT_MAX - 1 };\n int max{ 0 };\n\n int res{ 0 };\n int left{ 0 };\n int right{ 0 };\n\n ms.emplace(nums[0]);\n\n while (right < nums...
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...
2
{ "code": "class Solution {\npublic:\n int longestSubarray(vector<int>& nums, int limit) {\n int max_len=0;\n priority_queue<int> largest_nums;\n priority_queue<int,vector<int>,greater<int>> smallest_nums;\n unordered_map<int,int> nums_freq;\n for (int i=0, j=0; j<nums.size(); 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...
2
{ "code": "class Solution {\npublic:\n int longestSubarray(vector<int>& nums, int limit) {\n priority_queue<int> mx;\n priority_queue<int,vector<int>,greater<int>> mn;\n unordered_map<int,int> mp;\n int l=0,r=0,n=nums.size(),ans=0;\n\n while(l<=r && r<n){\n mp[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...
2
{ "code": "class Solution {\npublic:\n int longestSubarray(vector<int>& nums, int limit) {\n if(nums.size()==1) return 1;\n int s=0,e=1,res=1,n=nums.size();\n unordered_map<int,int> mp;\n priority_queue<int> mx;\n priority_queue<int,vector<int>,greater<int>> mi;\n mx.push(...
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...
2
{ "code": "class Solution {\npublic:\n\n int longestSubarray(vector<int>& nums, int limit) {\n //find highest/lowest in a range\n ios_base::sync_with_stdio(0);\n \n int n=nums.size();\n deque<int> mini,maxi;\n int ans=0;\n // mini.push_back(nums[0]);\n // maxi.pu...
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...
2
{ "code": "class Solution {\npublic:\n int longestSubarray(vector<int>& nums, int limit) {\n multiset<int>st;\n int i=0,j=-1,n=nums.size(),ans=0;\n while(i<n){\n if(i>j){j=i;st.insert(nums[j]);}\n while(j+1<n){\n auto it=st.begin(),itt=st.end();itt--;\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...
2
{ "code": "class Solution {\npublic:\n int longestSubarray(vector<int>& nums, int limit) {\n \n int i=0,j=0;\n multiset<int>st;\n int n=nums.size();\n int ans=1;\n for(int j=0;j<n;j++)\n {\n st.insert(nums[j]);\n\n while(st.size()>0 && (*st.rbe...
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...
2
{ "code": "class Solution {\npublic:\n int longestSubarray(vector<int>& nums, int limit) {\n int ans = 0;\n int left = 0, right = 0;\n multiset<int> ms;\n while (right < nums.size()) {\n ms.insert(nums[right]);\n while (!ms.empty() && *ms.rbegin() - *ms.begin() > l...
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...
3
{ "code": "class Solution {\npublic:\n int longestSubarray(vector<int>& nums, int limit) {\n multiset<int> m;\n int j = 0;\n int ans = 1;\n for (int i = 0; i < nums.size(); i++) {\n m.insert(nums[i]);\n\n while(!m.empty() && *m.rbegin()-*m.begin()>limit){\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...
3
{ "code": "class Solution {\npublic:\n int longestSubarray(vector<int>& nums, int limit) {\n int l=0,ans=1;\n multiset<int> s;\n for(int r=0;r<nums.size();++r)\n {\n s.insert(nums[r]);\n while(!s.empty() && *s.rbegin()-*s.begin()>limit)s.erase(s.find(nums[l++]));\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...
3
{ "code": "class Solution {\npublic:\n int longestSubarray(vector<int>& nums, int limit) {\n multiset<int> m;\n int j = 0;\n int ans = 1;\n for (int i = 0; i < nums.size(); i++) {\n m.insert(nums[i]);\n\n while(!m.empty() && *m.rbegin()-*m.begin()>limit){\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...
3
{ "code": "class Solution {\npublic:\n int longestSubarray(vector<int>& nums, int limit) {\n multiset<int> ms;\n int n = nums.size();\n int l = 0;\n int ans = 0;\n for(int r = 0;r<n;r++){\n ms.insert(nums[r]);\n while(!ms.empty() && *ms.rbegin()-*ms.begin()>l...
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...
3
{ "code": "class Solution {\npublic:\n int longestSubarray(vector<int>& nums, int limit) {\n int ans = 0;\n int left = 0, right = 0;\n multiset<int> ms;\n while (right < nums.size()) {\n ms.insert(nums[right]);\n while (*ms.rbegin() - *ms.begin() > limit) {\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...
3
{ "code": "class Solution {\npublic:\n int longestSubarray(vector<int>& nums, int limit) {\n int left = 0, right = 0, maxLen = 0;\n int maxValue = nums[0], minValue = nums[0];\n multiset<int>s;\n while (right < nums.size()){\n s.insert(nums[right]);\n while(*(s.rbegin(...
1,552
<p>You are given an integer array <code>target</code> and an integer <code>n</code>.</p> <p>You have an empty stack with the two following operations:</p> <ul> <li><strong><code>&quot;Push&quot;</code></strong>: pushes an integer to the top of the stack.</li> <li><strong><code>&quot;Pop&quot;</code></strong>: remov...
0
{ "code": "class Solution {\npublic:\n static inline void doPush(vector<string> &ans, int &n) {\n n++;\n ans.push_back(\"Push\");\n }\n static inline void doPop(vector<string> &ans) {\n ans.push_back(\"Pop\");\n }\n vector<string> buildArray(vector<int>& target, int n) {\n i...
1,552
<p>You are given an integer array <code>target</code> and an integer <code>n</code>.</p> <p>You have an empty stack with the two following operations:</p> <ul> <li><strong><code>&quot;Push&quot;</code></strong>: pushes an integer to the top of the stack.</li> <li><strong><code>&quot;Pop&quot;</code></strong>: remov...
0
{ "code": "class Solution {\npublic:\n vector<string> buildArray(vector<int>& target, int n) {\n int stream=1;\n int i=0;\n vector<string>ans;\n while(i<target.size() && stream<=n){\n ans.push_back(\"Push\");\n if(stream==target[i]){\n i++;\n ...
1,552
<p>You are given an integer array <code>target</code> and an integer <code>n</code>.</p> <p>You have an empty stack with the two following operations:</p> <ul> <li><strong><code>&quot;Push&quot;</code></strong>: pushes an integer to the top of the stack.</li> <li><strong><code>&quot;Pop&quot;</code></strong>: remov...
0
{ "code": "class Solution {\npublic:\n vector<string> buildArray(vector<int>& target, int n) {\n vector<string>ans;\n int j = 0;\n for(int i = 1;i<=n;i++){\n ans.push_back(\"Push\");\n if(i==target[j]){\n j++;\n }\n else ans.push_back(...
1,552
<p>You are given an integer array <code>target</code> and an integer <code>n</code>.</p> <p>You have an empty stack with the two following operations:</p> <ul> <li><strong><code>&quot;Push&quot;</code></strong>: pushes an integer to the top of the stack.</li> <li><strong><code>&quot;Pop&quot;</code></strong>: remov...
0
{ "code": "class Solution {\npublic:\n vector<string> buildArray(vector<int>& target, int n) {\n int sizee = target.size();\n \n int num = 1;\n vector<string> v;\n for(int i=0; i<sizee; ){\n if(target[i] == num){\n v.push_back(\"Push\");\n ...
1,552
<p>You are given an integer array <code>target</code> and an integer <code>n</code>.</p> <p>You have an empty stack with the two following operations:</p> <ul> <li><strong><code>&quot;Push&quot;</code></strong>: pushes an integer to the top of the stack.</li> <li><strong><code>&quot;Pop&quot;</code></strong>: remov...
0
{ "code": "class Solution {\npublic:\n vector<string> buildArray(vector<int>& target, int n) {\n vector<string> res;\n int i = 0;\n int size = 0, prev = 0;\n for(int k = 1; i <= n && i < target.size(); k ++)\n {\n if(target[i] == k)\n {\n if(s...
1,552
<p>You are given an integer array <code>target</code> and an integer <code>n</code>.</p> <p>You have an empty stack with the two following operations:</p> <ul> <li><strong><code>&quot;Push&quot;</code></strong>: pushes an integer to the top of the stack.</li> <li><strong><code>&quot;Pop&quot;</code></strong>: remov...
0
{ "code": "class Solution {\npublic:\n vector<string> buildArray(vector<int>& target, int n) \n {\n vector<string> result;\n int current = 1; \n\n for (int i = 0; i < target.size(); i++) \n {\n while (current < target[i]) \n {\n \n ...
1,552
<p>You are given an integer array <code>target</code> and an integer <code>n</code>.</p> <p>You have an empty stack with the two following operations:</p> <ul> <li><strong><code>&quot;Push&quot;</code></strong>: pushes an integer to the top of the stack.</li> <li><strong><code>&quot;Pop&quot;</code></strong>: remov...
0
{ "code": "class Solution {\npublic:\n vector<string> buildArray(vector<int>& target, int n) {\n\n int current = 1;\n vector<string>res;\n for(int i = 0 ; i < target.size();++i)\n {\n while(target[i] > current){\n res.push_back(\"Push\");\n res.p...
1,552
<p>You are given an integer array <code>target</code> and an integer <code>n</code>.</p> <p>You have an empty stack with the two following operations:</p> <ul> <li><strong><code>&quot;Push&quot;</code></strong>: pushes an integer to the top of the stack.</li> <li><strong><code>&quot;Pop&quot;</code></strong>: remov...
0
{ "code": "class Solution {\npublic:\n vector<string> buildArray(vector<int>& target, int n) {\n\n int current = 1;\n vector<string>res;\n for(int i = 0 ; i < target.size();i++)\n {\n while(target[i] > current){\n res.push_back(\"Push\");\n res.p...
1,552
<p>You are given an integer array <code>target</code> and an integer <code>n</code>.</p> <p>You have an empty stack with the two following operations:</p> <ul> <li><strong><code>&quot;Push&quot;</code></strong>: pushes an integer to the top of the stack.</li> <li><strong><code>&quot;Pop&quot;</code></strong>: remov...
1
{ "code": "class Solution {\npublic:\n vector<string> buildArray(vector<int>& target, int n) {\n int i = 0 , j = 1;\n vector<string> ans; \n while(i < target.size()){\n if(target[i] == j){\n ans.push_back(\"Push\");\n i++; j++;\n }\n else if(j != target[i]...
1,552
<p>You are given an integer array <code>target</code> and an integer <code>n</code>.</p> <p>You have an empty stack with the two following operations:</p> <ul> <li><strong><code>&quot;Push&quot;</code></strong>: pushes an integer to the top of the stack.</li> <li><strong><code>&quot;Pop&quot;</code></strong>: remov...
1
{ "code": "class Solution {\npublic:\n vector<string> buildArray(vector<int>& target, int n) {\n vector<string> ans;\n int idx = 0; \n\n for (int i = 1; i <= n && idx < target.size(); i++) {\n ans.push_back(\"Push\");\n \n if (i != target[idx]) {\n ...
1,553
<p>Given an array of integers <code>arr</code>.</p> <p>We want to select three indices <code>i</code>, <code>j</code> and <code>k</code> where <code>(0 &lt;= i &lt; j &lt;= k &lt; arr.length)</code>.</p> <p>Let&#39;s define <code>a</code> and <code>b</code> as follows:</p> <ul> <li><code>a = arr[i] ^ arr[i + 1] ^ ....
0
{ "code": "class Solution {\npublic:\n int countTriplets(vector<int>& arr) {\n\n int n=arr.size();\n\n int ans=0;\n\n for(int i=0;i<n;i++)\n {\n int t=arr[i];\n\n for(int j=i+1;j<n;j++)\n {\n t^=arr[j];\n if(t==0)\n ...
1,553
<p>Given an array of integers <code>arr</code>.</p> <p>We want to select three indices <code>i</code>, <code>j</code> and <code>k</code> where <code>(0 &lt;= i &lt; j &lt;= k &lt; arr.length)</code>.</p> <p>Let&#39;s define <code>a</code> and <code>b</code> as follows:</p> <ul> <li><code>a = arr[i] ^ arr[i + 1] ^ ....
0
{ "code": "class Solution {\npublic:\n int countTriplets(vector<int>& arr) {\n int n=arr.size();\n int result=0;\n for(int i=0;i<n;i++){\n int a = arr[i];\n for(int k=i+1;k<n;k++){\n a ^=(arr[k]);\n if(a==0){\n result += (k...
1,553
<p>Given an array of integers <code>arr</code>.</p> <p>We want to select three indices <code>i</code>, <code>j</code> and <code>k</code> where <code>(0 &lt;= i &lt; j &lt;= k &lt; arr.length)</code>.</p> <p>Let&#39;s define <code>a</code> and <code>b</code> as follows:</p> <ul> <li><code>a = arr[i] ^ arr[i + 1] ^ ....
0
{ "code": "class Solution {\npublic:\n int countTriplets(vector<int>& arr) {\n int cnt = 0;\n int n = arr.size();\n\n for (int i = 0; i < n; i++) {\n int first = 0;\n // XOR from i to mid (i <= mid < n)\n for (int mid = i; mid < n; mid++) {\n fir...
1,553
<p>Given an array of integers <code>arr</code>.</p> <p>We want to select three indices <code>i</code>, <code>j</code> and <code>k</code> where <code>(0 &lt;= i &lt; j &lt;= k &lt; arr.length)</code>.</p> <p>Let&#39;s define <code>a</code> and <code>b</code> as follows:</p> <ul> <li><code>a = arr[i] ^ arr[i + 1] ^ ....
0
{ "code": "class Solution {\npublic:\n int ans = 0;\n int countTriplets(vector<int>& arr) {\n int n = arr.size();\n for (int i = 0; i < n; ++i) {\n int val = arr[i];\n for (int k = i+1; k < n; ++k) {\n val ^= arr[k];\n if (val == 0) {\n ...
1,553
<p>Given an array of integers <code>arr</code>.</p> <p>We want to select three indices <code>i</code>, <code>j</code> and <code>k</code> where <code>(0 &lt;= i &lt; j &lt;= k &lt; arr.length)</code>.</p> <p>Let&#39;s define <code>a</code> and <code>b</code> as follows:</p> <ul> <li><code>a = arr[i] ^ arr[i + 1] ^ ....
1
{ "code": "class Solution {\npublic:\n int ans = 0;\n int countTriplets(vector<int>& arr) {\n int n = arr.size();\n for (int i = 0; i < n; ++i) {\n int val = arr[i];\n for (int k = i+1; k < n; ++k) {\n val ^= arr[k];\n if (val == 0) {\n ...
1,553
<p>Given an array of integers <code>arr</code>.</p> <p>We want to select three indices <code>i</code>, <code>j</code> and <code>k</code> where <code>(0 &lt;= i &lt; j &lt;= k &lt; arr.length)</code>.</p> <p>Let&#39;s define <code>a</code> and <code>b</code> as follows:</p> <ul> <li><code>a = arr[i] ^ arr[i + 1] ^ ....
1
{ "code": "\n\nclass Solution {\npublic:\n int countTriplets(vector<int>& arr) {\n int n = arr.size();\n vector<int> prefixXor(n + 1, 0); \n \n // Compute prefixXor array\n for (int i = 0; i < n; ++i) {\n prefixXor[i + 1] = prefixXor[i] ^ arr[i];\n }\n\n ...
1,553
<p>Given an array of integers <code>arr</code>.</p> <p>We want to select three indices <code>i</code>, <code>j</code> and <code>k</code> where <code>(0 &lt;= i &lt; j &lt;= k &lt; arr.length)</code>.</p> <p>Let&#39;s define <code>a</code> and <code>b</code> as follows:</p> <ul> <li><code>a = arr[i] ^ arr[i + 1] ^ ....
1
{ "code": "class Solution {\npublic:\n int countTriplets(vector<int>& a) {// xor property\n int n=a.size(),ans=0;\n vector<int>p(n+1,0);\n for(int i=1;i<=n;i++){\n p[i]=p[i-1]^a[i-1];//p[i] is prefix for i-1(including)\n }\n for(int i=0;i<n;i++){// i for prefix atmax n...
1,553
<p>Given an array of integers <code>arr</code>.</p> <p>We want to select three indices <code>i</code>, <code>j</code> and <code>k</code> where <code>(0 &lt;= i &lt; j &lt;= k &lt; arr.length)</code>.</p> <p>Let&#39;s define <code>a</code> and <code>b</code> as follows:</p> <ul> <li><code>a = arr[i] ^ arr[i + 1] ^ ....
2
{ "code": "class Solution {\npublic:\n int countTriplets(vector<int>& arr) {\n int cnt = 0;\n int n = arr.size();\n \n // Step 1: Build the prefix XOR array\n vector<int> prefix(n + 1, 0);\n for (int i = 0; i < n; i++) {\n prefix[i + 1] = prefix[i] ^ arr[i]; // XOR from the beginning to i\n...
1,553
<p>Given an array of integers <code>arr</code>.</p> <p>We want to select three indices <code>i</code>, <code>j</code> and <code>k</code> where <code>(0 &lt;= i &lt; j &lt;= k &lt; arr.length)</code>.</p> <p>Let&#39;s define <code>a</code> and <code>b</code> as follows:</p> <ul> <li><code>a = arr[i] ^ arr[i + 1] ^ ....
2
{ "code": "class Solution {\npublic:\n int countTriplets(vector<int>& arr) {\n int cnt = 0;\n int n = arr.size();\n \n\n vector<int> prefix(n + 1, 0);\n for (int i = 0; i < n; i++) {\n prefix[i + 1] = prefix[i] ^ arr[i]; \n }\n \n for (int i = 0; i < n; i++) {\n for (int j = i ...
1,553
<p>Given an array of integers <code>arr</code>.</p> <p>We want to select three indices <code>i</code>, <code>j</code> and <code>k</code> where <code>(0 &lt;= i &lt; j &lt;= k &lt; arr.length)</code>.</p> <p>Let&#39;s define <code>a</code> and <code>b</code> as follows:</p> <ul> <li><code>a = arr[i] ^ arr[i + 1] ^ ....
2
{ "code": "class Solution {\npublic:\n int countTriplets(vector<int>& arr) {\n int cnt = 0;\n int n = arr.size();\n \n vector<int> prefix(n);\n prefix[0] = arr[0];\n for (int i = 1; i < n; i++) {\n prefix[i] = prefix[i - 1] ^ arr[i];\n }\n \n for (int i = 0; i < n; i++) {\n ...
1,553
<p>Given an array of integers <code>arr</code>.</p> <p>We want to select three indices <code>i</code>, <code>j</code> and <code>k</code> where <code>(0 &lt;= i &lt; j &lt;= k &lt; arr.length)</code>.</p> <p>Let&#39;s define <code>a</code> and <code>b</code> as follows:</p> <ul> <li><code>a = arr[i] ^ arr[i + 1] ^ ....
3
{ "code": "class Solution {\npublic:\n int countTriplets(vector<int>& arr) {\n unordered_map<int,vector<int>>Map;\n int xorsum = 0;\n int ret = 0;\n Map[0].push_back(-1);\n for (int i = 0; i < arr.size(); i++) {\n xorsum ^= arr[i];\n for (int k: Map[xorsum])...
1,553
<p>Given an array of integers <code>arr</code>.</p> <p>We want to select three indices <code>i</code>, <code>j</code> and <code>k</code> where <code>(0 &lt;= i &lt; j &lt;= k &lt; arr.length)</code>.</p> <p>Let&#39;s define <code>a</code> and <code>b</code> as follows:</p> <ul> <li><code>a = arr[i] ^ arr[i + 1] ^ ....
3
{ "code": "class Solution {\npublic:\n int countTriplets(vector<int>& arr) {\n unordered_map<int, vector<int>> map; // pre_xor_sum -> {idx}\n int ans = 0, sum = 0;\n map[0].push_back({-1});\n\n for(int k = 0; k < arr.size(); ++k)\n {\n sum ^= arr[k];\n if(ma...
1,553
<p>Given an array of integers <code>arr</code>.</p> <p>We want to select three indices <code>i</code>, <code>j</code> and <code>k</code> where <code>(0 &lt;= i &lt; j &lt;= k &lt; arr.length)</code>.</p> <p>Let&#39;s define <code>a</code> and <code>b</code> as follows:</p> <ul> <li><code>a = arr[i] ^ arr[i + 1] ^ ....
3
{ "code": "class Solution {\npublic:\n int countTriplets(vector<int>& arr) {\n int psum = 0;\n unordered_map<int, pair<int, int>> m; // Map storing <prefix XOR, <frequency, index sum>>\n m[0] = {1, -1}; // Initialize for prefix XOR 0 at index -1\n int count = 0;\n \n for (int i = 0; i < arr.siz...
1,553
<p>Given an array of integers <code>arr</code>.</p> <p>We want to select three indices <code>i</code>, <code>j</code> and <code>k</code> where <code>(0 &lt;= i &lt; j &lt;= k &lt; arr.length)</code>.</p> <p>Let&#39;s define <code>a</code> and <code>b</code> as follows:</p> <ul> <li><code>a = arr[i] ^ arr[i + 1] ^ ....
3
{ "code": "class Solution {\npublic:\n int countTriplets(vector<int>& arr) {\n int psum = 0;\n unordered_map<int, pair<int, int>> m; // Map storing <prefix XOR, <frequency, index sum>>\n m[0] = {1, -1}; // Initialize for prefix XOR 0 at index -1\n int count = 0;\n \n for...
1,553
<p>Given an array of integers <code>arr</code>.</p> <p>We want to select three indices <code>i</code>, <code>j</code> and <code>k</code> where <code>(0 &lt;= i &lt; j &lt;= k &lt; arr.length)</code>.</p> <p>Let&#39;s define <code>a</code> and <code>b</code> as follows:</p> <ul> <li><code>a = arr[i] ^ arr[i + 1] ^ ....
3
{ "code": "class Solution {\npublic:\n int countTriplets(vector<int>& arr) {\n int n=arr.size();\n vector<int> pxor(n);\n pxor[0]=arr[0];\n for(int i=1;i<n;i++){\n pxor[i]=pxor[i-1]^arr[i];\n }\n unordered_map<int,vector<int>> um;\n um[0]={-1};\n i...
1,553
<p>Given an array of integers <code>arr</code>.</p> <p>We want to select three indices <code>i</code>, <code>j</code> and <code>k</code> where <code>(0 &lt;= i &lt; j &lt;= k &lt; arr.length)</code>.</p> <p>Let&#39;s define <code>a</code> and <code>b</code> as follows:</p> <ul> <li><code>a = arr[i] ^ arr[i + 1] ^ ....
3
{ "code": "class Solution {\npublic:\n int countTriplets(vector<int>& arr) {\n map<int, vector<int>> mpp;\n int xor_ = 0;\n int ans = 0;\n mpp[0].push_back(-1);\n for(int i = 0; i < arr.size(); i++)\n {\n xor_ ^= arr[i];\n \n if(mpp.find(xo...
1,553
<p>Given an array of integers <code>arr</code>.</p> <p>We want to select three indices <code>i</code>, <code>j</code> and <code>k</code> where <code>(0 &lt;= i &lt; j &lt;= k &lt; arr.length)</code>.</p> <p>Let&#39;s define <code>a</code> and <code>b</code> as follows:</p> <ul> <li><code>a = arr[i] ^ arr[i + 1] ^ ....
3
{ "code": "class Solution {\npublic:\n int countTriplets(vector<int>& arr) {\n // find a subarray of 0 xor, all splits within it should be valid\n // we can find using a hashmap prefix xor approach\n map<int, int> m = {{0, 1}}, tm;\n int pref = 0;\n int n = arr.size();\n i...
1,553
<p>Given an array of integers <code>arr</code>.</p> <p>We want to select three indices <code>i</code>, <code>j</code> and <code>k</code> where <code>(0 &lt;= i &lt; j &lt;= k &lt; arr.length)</code>.</p> <p>Let&#39;s define <code>a</code> and <code>b</code> as follows:</p> <ul> <li><code>a = arr[i] ^ arr[i + 1] ^ ....
3
{ "code": "class Solution {\npublic:\n int countTriplets(vector<int>& arr) {\n map<int,vector<int>> mp;\n int curr=0;\n int ans=0;\n mp[0].push_back(-1);\n for(int i=0;i<arr.size();i++){\n curr=curr^arr[i];\n for(int j=0;j<mp[curr].size();j++){\n ...
2,808
<p>You are given two <strong>0-indexed</strong> integer arrays,&nbsp;<code>cost</code> and <code>time</code>, of size <code>n</code> representing the costs and the time taken to paint <code>n</code> different walls respectively. There are two painters available:</p> <ul> <li>A<strong>&nbsp;paid painter</strong>&nbsp;...
0
{ "code": "class Solution {\npublic:\n int paintWalls(vector<int>& cost, vector<int>& time) {\n int n = cost.size();\n int dp[n + 1];\n for(int i = 0; i < n + 1; i++){\n dp[i] = 1e9;\n }\n dp[0] = 0;\n for(int i = 0; i < n; i++){\n for(int j = n; j >=...
2,808
<p>You are given two <strong>0-indexed</strong> integer arrays,&nbsp;<code>cost</code> and <code>time</code>, of size <code>n</code> representing the costs and the time taken to paint <code>n</code> different walls respectively. There are two painters available:</p> <ul> <li>A<strong>&nbsp;paid painter</strong>&nbsp;...
0
{ "code": "// class Solution {\n// public:\n// int solve(vector<int>&cost,vector<int>&time,int i ,int k,vector<int>&dp){\n// if(k <= 0)return 0;\n// if(i<0)return 100000000;\n// if(dp[i][k] != -1)return dp[i][k];\n// int t = time;\n// int take = \n\n// } \n// public...
2,808
<p>You are given two <strong>0-indexed</strong> integer arrays,&nbsp;<code>cost</code> and <code>time</code>, of size <code>n</code> representing the costs and the time taken to paint <code>n</code> different walls respectively. There are two painters available:</p> <ul> <li>A<strong>&nbsp;paid painter</strong>&nbsp;...
1
{ "code": "class Solution {\npublic:\n int dp[1001][1001];\n int f(int ind, int rem, vector<int>&cost, vector<int>& time){\n if(rem<=0) return 0;\n if(ind<0){\n return 1e9;\n }\n if(dp[ind][rem]!=-1) return dp[ind][rem];\n int take = cost[ind]+f(ind-1, rem-time[ind]...
2,808
<p>You are given two <strong>0-indexed</strong> integer arrays,&nbsp;<code>cost</code> and <code>time</code>, of size <code>n</code> representing the costs and the time taken to paint <code>n</code> different walls respectively. There are two painters available:</p> <ul> <li>A<strong>&nbsp;paid painter</strong>&nbsp;...
1
{ "code": "class Solution {\npublic:\n int dp[1001][1001];\n int f(int ind, int rem, vector<int>&cost, vector<int>& time){\n if(ind<0 || rem<=0){\n if(rem<=0) return 0;\n return 1e9;\n }\n if(dp[ind][rem]!=-1) return dp[ind][rem];\n int take = cost[ind]+f(ind-1,...
2,808
<p>You are given two <strong>0-indexed</strong> integer arrays,&nbsp;<code>cost</code> and <code>time</code>, of size <code>n</code> representing the costs and the time taken to paint <code>n</code> different walls respectively. There are two painters available:</p> <ul> <li>A<strong>&nbsp;paid painter</strong>&nbsp;...
1
{ "code": "class Solution {\npublic:\n int paintWalls(vector<int>& cost, vector<int>& time) {\n int d = 0;\n for(auto e:time){\n d += e;\n }\n const int INF = 1e9;\n vector<array<int,2>> dp(d+1,{INF,INF}),ndp(d+1,{INF,INF});\n dp[time[0]][0]=cost[0];\n dp...
2,808
<p>You are given two <strong>0-indexed</strong> integer arrays,&nbsp;<code>cost</code> and <code>time</code>, of size <code>n</code> representing the costs and the time taken to paint <code>n</code> different walls respectively. There are two painters available:</p> <ul> <li>A<strong>&nbsp;paid painter</strong>&nbsp;...
1
{ "code": "class Solution {\npublic:\n int paintWalls(vector<int>& cost, vector<int>& time) {\n for ( int i = 0 ; i < time.size() ; i ++ ){\n time[i] ++ ; \n }\n vector < int > dp ( 1005 );\n for ( int i = 0 ; i < 1005 ; i ++ ){ dp[i] = INT_MAX/2 ; }\n for ( int i =...
2,808
<p>You are given two <strong>0-indexed</strong> integer arrays,&nbsp;<code>cost</code> and <code>time</code>, of size <code>n</code> representing the costs and the time taken to paint <code>n</code> different walls respectively. There are two painters available:</p> <ul> <li>A<strong>&nbsp;paid painter</strong>&nbsp;...
1
{ "code": "class Solution {\npublic:\n int solve(int i, vector<int>& cost, vector<int>& time, int comp,\n vector<vector<int>>& dp) {\n if (comp <= 0) {\n return 0;\n }\n if (i < 0) {\n return 1e8;\n }\n if (dp[i][comp] != -1) {\n retu...
2,808
<p>You are given two <strong>0-indexed</strong> integer arrays,&nbsp;<code>cost</code> and <code>time</code>, of size <code>n</code> representing the costs and the time taken to paint <code>n</code> different walls respectively. There are two painters available:</p> <ul> <li>A<strong>&nbsp;paid painter</strong>&nbsp;...
1
{ "code": "class Solution {\npublic:\n int solve(int i, vector<int>& cost, vector<int>& time, int comp,\n vector<vector<int>>& dp) {\n if (comp <= 0) {\n return 0;\n }\n if (i < 0) {\n return 1e8;\n }\n if (dp[i][comp] != -1) {\n retu...
2,808
<p>You are given two <strong>0-indexed</strong> integer arrays,&nbsp;<code>cost</code> and <code>time</code>, of size <code>n</code> representing the costs and the time taken to paint <code>n</code> different walls respectively. There are two painters available:</p> <ul> <li>A<strong>&nbsp;paid painter</strong>&nbsp;...
1
{ "code": "class Solution {\npublic:\n int solve(int i, vector<int>& cost, vector<int>& time, int comp,\n vector<vector<int>>& dp) {\n if (comp <= 0) {\n return 0;\n }\n if (i < 0) {\n return 1e8;\n }\n if (dp[i][comp] != -1) {\n retu...
2,808
<p>You are given two <strong>0-indexed</strong> integer arrays,&nbsp;<code>cost</code> and <code>time</code>, of size <code>n</code> representing the costs and the time taken to paint <code>n</code> different walls respectively. There are two painters available:</p> <ul> <li>A<strong>&nbsp;paid painter</strong>&nbsp;...
1
{ "code": "class Solution {\npublic:\n int paintWalls(vector<int>& cost, vector<int>& time) {\n int n = cost.size();\n vector<int> dp(n + 1, 1e9);\n dp[0] = 0;\n for(int i = n - 1; i>=0; i--){\n vector<int> temp = dp;\n for(int j = 1; j <= n; j++){\n ...
2,808
<p>You are given two <strong>0-indexed</strong> integer arrays,&nbsp;<code>cost</code> and <code>time</code>, of size <code>n</code> representing the costs and the time taken to paint <code>n</code> different walls respectively. There are two painters available:</p> <ul> <li>A<strong>&nbsp;paid painter</strong>&nbsp;...
1
{ "code": "class Solution {\npublic:\nint n;\nint func(int i,int pt, vector<int> &cost, vector<int> &time, vector<vector<int>> &dp){\n if(pt>=n) return 0;\n if(i>=n) return 1e9;\n if(dp[i][pt]!=-1) return dp[i][pt];\n int tk = cost[i]+ func(i+1,pt+1+time[i],cost,time,dp);\n int nt = func(i+1,pt,cost,...
2,808
<p>You are given two <strong>0-indexed</strong> integer arrays,&nbsp;<code>cost</code> and <code>time</code>, of size <code>n</code> representing the costs and the time taken to paint <code>n</code> different walls respectively. There are two painters available:</p> <ul> <li>A<strong>&nbsp;paid painter</strong>&nbsp;...
1
{ "code": "class Solution {\npublic:\n int paintWalls(vector<int>& cost, vector<int>& time) {\n int n = cost.size();\n vector<int> dp(n + 1, 1e9);\n dp[0] = 0;\n for(int i = n - 1; i >= 0; i--){\n vector<int> temp = dp;\n for(int j = 1; j <= n; j++){\n ...
2,808
<p>You are given two <strong>0-indexed</strong> integer arrays,&nbsp;<code>cost</code> and <code>time</code>, of size <code>n</code> representing the costs and the time taken to paint <code>n</code> different walls respectively. There are two painters available:</p> <ul> <li>A<strong>&nbsp;paid painter</strong>&nbsp;...
1
{ "code": "class Solution {\npublic:\n int paintWalls(vector<int>& cost, vector<int>& time) {\n int lens = cost.size();\n vector<int> dp(lens+1, 1e9);\n dp[0] = 0;\n for(int r=lens-1; r>=0; r--)\n {\n vector<int> nxt(lens+1, 1e9);\n nxt[0] = 0;\n ...
2,808
<p>You are given two <strong>0-indexed</strong> integer arrays,&nbsp;<code>cost</code> and <code>time</code>, of size <code>n</code> representing the costs and the time taken to paint <code>n</code> different walls respectively. There are two painters available:</p> <ul> <li>A<strong>&nbsp;paid painter</strong>&nbsp;...
1
{ "code": "class Solution {\npublic:\n int solve(int n,vector<int> &cost,vector<int> &time,int count,vector<vector<int> > &dp){\n // cout<<n<<\" \"<<count<<endl;\n if(count >= cost.size()) return 0;\n if(n==cost.size()) return 1e9;\n if(dp[n][count]!=-1) return dp[n][count];\n in...
2,808
<p>You are given two <strong>0-indexed</strong> integer arrays,&nbsp;<code>cost</code> and <code>time</code>, of size <code>n</code> representing the costs and the time taken to paint <code>n</code> different walls respectively. There are two painters available:</p> <ul> <li>A<strong>&nbsp;paid painter</strong>&nbsp;...
3
{ "code": "class Solution {\npublic:\n // if the paid guy paints x walls\n // the free guy should paint n - x walls\n // what's the minimum cost to paint x walls so that their time is at least (n - x)?\n int solve1(vector<int>& cost, vector<int>& time) {\n const int inf = 1e9;\n const int n = cost.size();\n...
2,808
<p>You are given two <strong>0-indexed</strong> integer arrays,&nbsp;<code>cost</code> and <code>time</code>, of size <code>n</code> representing the costs and the time taken to paint <code>n</code> different walls respectively. There are two painters available:</p> <ul> <li>A<strong>&nbsp;paid painter</strong>&nbsp;...
3
{ "code": "class Solution {\npublic:\n // if the paid guy paints x walls\n // the free guy should paint n - x walls\n // what's the minimum cost to paint x walls so that their time is at least (n - x)?\n int solve1(vector<int>& cost, vector<int>& time) {\n const int inf = 1e9;\n const int n = cost.size();\n...
2,808
<p>You are given two <strong>0-indexed</strong> integer arrays,&nbsp;<code>cost</code> and <code>time</code>, of size <code>n</code> representing the costs and the time taken to paint <code>n</code> different walls respectively. There are two painters available:</p> <ul> <li>A<strong>&nbsp;paid painter</strong>&nbsp;...
3
{ "code": "class Solution {\n void better(int &x, int y) {\n if (y >= 0 && (x < 0 || x > y)) {\n x = y;\n }\n }\npublic:\n int paintWalls(vector<int>& cost, vector<int>& time) {\n const int n = cost.size(), limit = n << 1, m = limit | 1;\n vector<vector<int>> dp(2, vect...
2,808
<p>You are given two <strong>0-indexed</strong> integer arrays,&nbsp;<code>cost</code> and <code>time</code>, of size <code>n</code> representing the costs and the time taken to paint <code>n</code> different walls respectively. There are two painters available:</p> <ul> <li>A<strong>&nbsp;paid painter</strong>&nbsp;...
3
{ "code": "class Solution {\nprivate:\n long long f(int ind,int rem,vector<int>& cost,vector<int>& time,vector<vector<long long>>& dp)\n {\n if(rem<=0)return 0;\n if(ind<0)return INT_MAX;\n if(dp[ind][rem]!=-1)return dp[ind][rem];\n long long p=cost[ind]+f(ind-1,rem-1-time[ind],cost,time,dp);\n lon...
2,808
<p>You are given two <strong>0-indexed</strong> integer arrays,&nbsp;<code>cost</code> and <code>time</code>, of size <code>n</code> representing the costs and the time taken to paint <code>n</code> different walls respectively. There are two painters available:</p> <ul> <li>A<strong>&nbsp;paid painter</strong>&nbsp;...
3
{ "code": "class Solution {\npublic:\n vector<vector<long long >> dp;\n long long solve(int i,int t,int n,vector<int>& cost,vector<int>& time){\n // base\n if(n<=0){\n return 0;\n }\n if(i>=cost.size()){\n return INT_MAX;\n }\n if(dp[i][n]!=-1) ret...
2,808
<p>You are given two <strong>0-indexed</strong> integer arrays,&nbsp;<code>cost</code> and <code>time</code>, of size <code>n</code> representing the costs and the time taken to paint <code>n</code> different walls respectively. There are two painters available:</p> <ul> <li>A<strong>&nbsp;paid painter</strong>&nbsp;...
3
{ "code": "#define v vector\n#define vi v<int>\n#define ll long long\n\nconst int N = 501;\nll dp[N][N];\n\nclass Solution {\npublic:\n\n ll solve(vi &cost, vi &time, int i, int remWalls){\n\n // base case(s)\n if(remWalls <= 0) return 0;\n if(i == cost.size()) return INT_MAX; // we made choic...
2,808
<p>You are given two <strong>0-indexed</strong> integer arrays,&nbsp;<code>cost</code> and <code>time</code>, of size <code>n</code> representing the costs and the time taken to paint <code>n</code> different walls respectively. There are two painters available:</p> <ul> <li>A<strong>&nbsp;paid painter</strong>&nbsp;...
3
{ "code": "class Solution {\npublic:\n\n long long int fun(vector<int>& cost, vector<int>& time , long long int idx, long long int wall , vector<vector<long long int>> &dp )\n {\n\n if(wall<=0)\n {\n return 0;\n }\n\n if(idx<0)\n return INT_MAX;\n\n if(dp[idx...
2,808
<p>You are given two <strong>0-indexed</strong> integer arrays,&nbsp;<code>cost</code> and <code>time</code>, of size <code>n</code> representing the costs and the time taken to paint <code>n</code> different walls respectively. There are two painters available:</p> <ul> <li>A<strong>&nbsp;paid painter</strong>&nbsp;...
3
{ "code": "typedef int ll;\n#define vll vector<ll>\n#define vvll vector<vll>\nclass Solution {\npublic:\n // cost, time\n ll f(ll i, ll tsum, ll &n, vll &c, vll &t, vvll &dp) {\n \n if (i == n) {\n if (tsum-n < 0) {\n return dp[i][tsum] = INT_MAX;\n }\n ...
2,808
<p>You are given two <strong>0-indexed</strong> integer arrays,&nbsp;<code>cost</code> and <code>time</code>, of size <code>n</code> representing the costs and the time taken to paint <code>n</code> different walls respectively. There are two painters available:</p> <ul> <li>A<strong>&nbsp;paid painter</strong>&nbsp;...
3
{ "code": "class Solution {\n public:\nint n;\n\nint solve(vector<pair<int,int>>&v,vector<vector<int>>&dp,int idx,int end)\n{\n if(idx>end) return 0;\n if(idx==n) return 1e9;\n if(dp[idx][end]!=-1) return dp[idx][end];\n int t=0,nt=0;\n t=v[idx].first+solve(v,dp,idx+1,end-v[idx].second);\n nt=sol...
2,808
<p>You are given two <strong>0-indexed</strong> integer arrays,&nbsp;<code>cost</code> and <code>time</code>, of size <code>n</code> representing the costs and the time taken to paint <code>n</code> different walls respectively. There are two painters available:</p> <ul> <li>A<strong>&nbsp;paid painter</strong>&nbsp;...
3
{ "code": "typedef int ll;\n#define vll vector<ll>\n#define vvll vector<vll>\nclass Solution {\npublic:\n // cost, time\n vvll dp;\n static bool cmp(const vll &a, const vll &b) {\n if (a[0] == b[0]) {\n return a[1] > b[1];\n }\n return a[0]<b[0];\n }\n ll f(ll i, ll tsum...
2,808
<p>You are given two <strong>0-indexed</strong> integer arrays,&nbsp;<code>cost</code> and <code>time</code>, of size <code>n</code> representing the costs and the time taken to paint <code>n</code> different walls respectively. There are two painters available:</p> <ul> <li>A<strong>&nbsp;paid painter</strong>&nbsp;...
3
{ "code": "typedef int ll;\n#define vll vector<ll>\n#define vvll vector<vll>\nclass Solution {\npublic:\n // cost, time\n vvll dp;\n static bool cmp(const vll &a, const vll &b) {\n if (a[0] == b[0]) {\n return a[1] > b[1];\n }\n return a[0]<b[0];\n }\n ll f(ll i, ll tsum...
2,808
<p>You are given two <strong>0-indexed</strong> integer arrays,&nbsp;<code>cost</code> and <code>time</code>, of size <code>n</code> representing the costs and the time taken to paint <code>n</code> different walls respectively. There are two painters available:</p> <ul> <li>A<strong>&nbsp;paid painter</strong>&nbsp;...
3
{ "code": "class Solution {\npublic:\nint n,cnst;\n int solve(vector<int> &cost,vector<int> &time , int sum,int ind,vector<vector<int>>&dp){\n if(ind==-1){\n if(sum<=0)return 0;\n else return 6e8;\n }\n if(dp[sum+cnst][ind]!=-1)return dp[sum+cnst][ind];\n int f1 = ...
2,808
<p>You are given two <strong>0-indexed</strong> integer arrays,&nbsp;<code>cost</code> and <code>time</code>, of size <code>n</code> representing the costs and the time taken to paint <code>n</code> different walls respectively. There are two painters available:</p> <ul> <li>A<strong>&nbsp;paid painter</strong>&nbsp;...
3
{ "code": "class Solution {\npublic:\n\n int solve(vector<int>&cost, vector<int>&time, int sum, int i,vector<vector<int>>&dp){\n if(sum <= 0) return 0;\n if(i >= cost.size()){\n return 1e9;\n }\n if(dp[i][sum] != -1) return dp[i][sum];\n int t1 = solve(cost, time, sum,...