id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the ma...
3
{ "code": "/*\n# brute force: for every N, we do k comparisons, time O(N*k).k ~ N, so we could have O(N^2), at N~10^5, time would explode.\n# maintain a sorted array / heap. insertion O(logN), deletion O(logN). How do we delete a num in heap and re-heapify?\n# use hash to save location of val. on val replacement: \n#...
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the ma...
3
{ "code": "class Solution {\npublic:\n vector<int> maxSlidingWindow(vector<int>& n, int k) {\n map<int, int> m;\n vector<int> ans(n.size() - k + 1);\n int ind = 0, s = 0, e = k - 1;\n for (int i = 0; i < k; i++) {\n m[n[i]]++;\n }\n \n while(e < n.size())...
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the ma...
3
{ "code": "class Solution {\npublic:\n vector<int> maxSlidingWindow(vector<int>& nums, int k) {\n list<int> li;\n deque<int> dq;\n int n=nums.size();\n for(int i=0;i<n;i++){\n if(!dq.empty() && dq.front()<=i-k) dq.pop_front();\n while(!dq.empty() && nums[dq.back()]...
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the ma...
3
{ "code": "class Solution {\npublic:\n vector<int> maxSlidingWindow(vector<int>& nums, int k) {\n priority_queue<int>pq;\n vector<int>ans;\n unordered_multiset<int>deleted;\n for(int i=0;i<k;i++){\n pq.push(nums[i]);\n }\n int m=pq.top();\n ans.push_back(...
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the ma...
3
{ "code": "class Solution {\npublic:\n vector<int> maxSlidingWindow(vector<int>& nums, int k) {\n vector<int> ret;\n ret.reserve(nums.size() - k);\n map<int, int> mp;\n int i = 0;\n for (; i < k; ++i) {\n ++mp[nums[i]];\n }\n ret.push_back(mp.rbegin()->first);\n if (--mp[nums[0]] == 0)...
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the ma...
3
{ "code": "class Solution {\npublic:\n vector<int> maxSlidingWindow(vector<int>& nums, int k) {\n std::map<int, int> window;\n for(int i = 0; i < k; i++) {\n if(window.find(nums[i]) == window.end()) {\n window.insert({nums[i], 1});\n } else {\n window[nums[i]]++;\n ...
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the ma...
3
{ "code": "class Solution {\npublic:\n vector<int> maxSlidingWindow(vector<int>& nums, int k) {\n map<int, int> mp;\n\n for(int i = 0 ; i < k - 1 ; i++){\n if(mp.find(nums[i]) != mp.end()) mp[nums[i]]++;\n else mp.insert({nums[i], 1});\n }\n\n int n = nums.size();\...
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the ma...
3
{ "code": "class Solution {\npublic:\n vector<int> maxSlidingWindow(vector<int>& nums, int k) {\n std::map<int, int, std::greater<int>> my_map;\nstd::vector<int> vect;\nfor (int i = 0; i < k && i < nums.size(); i++)\n my_map[nums[i]]++;\n\nvect.push_back(my_map.begin()->first);\n\nfor (int i = k; i < num...
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the ma...
3
{ "code": "class Solution {\npublic:\n vector<int> maxSlidingWindow(vector<int>& nums, int k) {\n map<int,int> m;\n vector<int> v;\n for(int i=0;i<k;i++)\n {\n if(m[nums[i]])\n m[nums[i]]++;\n else\n m[nums[i]]=1;\n }\n auto it =...
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the ma...
3
{ "code": "class Solution {\npublic:\n vector<int> maxSlidingWindow(vector<int>& nums, int k) {\n std::map<int, std::size_t, std::greater<int>> window_elem_map;\n\n std::vector<int> result;\n result.reserve(nums.size()/k + 1);\n\n int right = std::min<int>(k, nums.size());\n\n //...
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the ma...
3
{ "code": "class Solution {\npublic:\n priority_queue<int> heap;\n map<int, int> numsToRemove; \n vector<int> ans;\n\n void toRemove(int v) {\n if (numsToRemove.find(v) != numsToRemove.end()) {\n numsToRemove[v]++;\n return;\n }\n numsToRemove[v] = 1;\n ...
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the ma...
3
{ "code": "class Solution {\npublic:\n vector<int> maxSlidingWindow(vector<int>& nums, int k) {\n vector<int>ans;\n priority_queue<int>pq;\n multiset<int> st;\n int i=0;\n int n=nums.size();\n for(int j=0;j<n;j++){\n pq.push(nums[j]);\n if(j-i+1==k){\n ...
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the ma...
3
{ "code": "typedef vector<int> vi;\nclass Solution {\npublic:\nvi maxSlidingWindow(vi& a, int k) {\n set<int> s;\n vi f(20001,0);\n int n=(int)a.size();\n for(int i=0;i<k;i++){\n s.insert(a[i]);\n if(a[i] >= 0) f[a[i]]++;\n else f[10000-a[i]]++;\n }\...
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the ma...
3
{ "code": "#define ll int \nclass Solution {\npublic:\n vector<int> maxSlidingWindow(vector<int>& nums, int k) {\n vector<ll>v = nums;\n ll n = v.size();\n map<ll,ll>mp;\n ll ans = -1e9;\n vector<ll>f;\n for(int i = 0;i<k;i++){\n mp[v[i]]++;\n ...
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the ma...
3
{ "code": "class Solution {\npublic:\n vector<int> maxSlidingWindow(vector<int>& nums, int k) {\n queue<int> q;\n map<int, int> m;\n vector<int> res;\n for(int i = 0 ; i<k ; i++)\n {\n q.push(nums[i]);\n m[nums[i]]++;\n }\n res.emplace_back(m.r...
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the ma...
3
{ "code": "class Solution {\npublic:\n vector<int> maxSlidingWindow(vector<int>& nums, int k) {\n\n #ifndef WakandaForever\n #define WakandaForever 1\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n #endif\n\n deque<int> q;\n map<int,...
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the ma...
3
{ "code": "class Solution {\npublic:\n vector<int> maxSlidingWindow(vector<int>& nums, int k) {\n\n #ifndef WakandaForever\n #define WakandaForever 1\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n #endif\n\n deque<int> q;\n map<int,...
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the ma...
3
{ "code": "class Solution {\npublic:\n vector<int> maxSlidingWindow(vector<int>& nums, int k) {\n deque<int> q;\n map<int, int> m;\n vector<int> res;\n for(int i = 0 ; i<k ; i++)\n {\n q.push_back(nums[i]);\n m[nums[i]]++;\n }\n res.emplace_bac...
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the ma...
3
{ "code": "class Solution {\npublic:\n vector<int> maxSlidingWindow(vector<int>& nums, int k) {\n int n=nums.size();\n \n int maxi=-1e5;\n set<int>st; map<int,int>mp;\n vector<int>ans;\n \n for(int r=0;r<k;r++){\n maxi=max(maxi,nums[r]);\n st.i...
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the ma...
3
{ "code": "class Solution {\npublic:\n vector<int> maxSlidingWindow(vector<int>& nums, int k) {\n vector<int> v1 ;\n // multiset<int> s1(nums.begin(), nums.begin() + k ); \n // v1.push_back(*s1.rbegin()) ;\n\n // for ( int i = k ; i<nums.size() ; i++) { \n // auto itr = s1.fi...
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the ma...
3
{ "code": "class MonoQueue {\npublic:\n list<int> data;\n\npublic:\n void push (int v) {\n while (!data.empty() && data.front () < v) {\n data.pop_front ();\n }\n\n data.push_front (v);\n }\n\n void pop (int v) {\n if (v == data.back ()) {\n data.pop_back ...
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the ma...
3
{ "code": "class MonoQueue {\npublic:\n list<int> data;\n\npublic:\n void push (int v) {\n while (!data.empty() && data.front () < v) {\n data.pop_front ();\n }\n\n data.push_front (v);\n }\n\n void pop (int v) {\n if (v == data.back ()) {\n data.pop_back ...
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the ma...
3
{ "code": "class Solution {\npublic:\n void printList(list<int> l)\n {\n list<int> dummy = l;\n while(dummy.size() > 0)\n {\n cout<<dummy.front()<<\" \";\n dummy.pop_front();\n }\n }\n vector<int> maxSlidingWindow(vector<int>& nums, int k) \n {\n ...
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the ma...
3
{ "code": "class Solution {\npublic:\n vector<int> maxSlidingWindow(vector<int>& nums, int k) {\n int n = size(nums), maxi = 1e-8;\n set<int> s; map<int,int> mp;\n for(int i=0;i<k;i++){\n mp[nums[i]]++;\n s.insert(nums[i]);\n maxi = max(maxi, nums[i]);\n ...
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the ma...
3
{ "code": "class Solution {\npublic:\nvector<int> maxSlidingWindow(vector<int>& nums, int k) {\n int n = nums.size();\n set<int> st;\n unordered_map<int,int> freq;\n\n for(int i=0;i<k;i++){\n freq[nums[i]]++;\n st.insert(nums[i]);\n }\n\n vector<int> ans...
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the ma...
3
{ "code": "class Solution {\npublic:\n vector<int> maxSlidingWindow(vector<int>& nums, int k) {\n vector<int> out;\n set<int> recent;\n unordered_map<int, int> map;\n\n int maxi = nums[0];\n for(int i = 0; i < k; i++){\n maxi = max(nums[i], maxi);\n if(recen...
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the ma...
3
{ "code": "class Solution {\npublic:\n vector<int> maxSlidingWindow(vector<int>& nums, int k) {\n vector<int> ans;\n unordered_map<int,int> m;\n set<int> s;\n for(int i=0;i<k;i++){\n m[nums[i]]++;\n s.insert(nums[i]);\n }\n ans.push_back(*(s.rbegin())...
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the ma...
3
{ "code": "class Solution {\npublic:\n vector<int> maxSlidingWindow(vector<int>& nums, int k) {\n vector<int> ret;\n ret.reserve(nums.size() - k);\n list<pair<int, int>> li;\n int i = 0;\n for (; i < k; ++i) {\n while (li.size() && nums[i] >= li.back().first) {\n li.pop_back();\n }\...
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the ma...
3
{ "code": "struct Pair\n{\n int value;\n int index;\n\npublic:\n Pair(int value, int index) : value(value), index(index) {}\n};\n\nclass Heap\n{\n vector<Pair> heap;\n unordered_map<int, int> indexes;\n\n void bubbleUp(int pos)\n {\n while (pos > 0)\n {\n int parent = (po...
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the ma...
3
{ "code": "struct Pair\n{\n int value;\n int index;\n};\n\nclass Heap\n{\n vector<Pair> heap;\n unordered_map<int, int> indexes;\n\npublic:\n void swapHeapNode(int pos1, int pos2)\n {\n if (pos1 == pos2)\n {\n return;\n }\n\n Pair p = Pair({heap[pos1].value, he...
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the ma...
3
{ "code": "class Solution {\npublic:\n vector<int> maxSlidingWindow(vector<int>& arr, int k) {\n int i=0,j=0;\n list<int> temp;\n vector<int> ans;\n\n int n =arr.size();\n\n while(j<n)\n {\n //calculation-Maintaining list ..remove all elements smaller than curre...
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the ma...
3
{ "code": "class Solution {\npublic:\n vector<int> maxSlidingWindow(vector<int>& nums, int k) {\n int i=0,j=0,sum=0,maxEle=INT_MIN,size=nums.size();\n list<int> l;\n vector<int> ans;\n while(j<size){\n if(!l.empty()){\n while(!l.empty() && nums[j]>l.back()) l.p...
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the ma...
3
{ "code": "class Solution {\npublic:\n vector<int> maxSlidingWindow(vector<int>& nums, int k) {\n deque<vector<int>> q;\n vector<int> ret;\n for (int i = 0; i < nums.size(); i++)\n {\n while (q.size() > 0 && q.back()[0] <= nums[i])\n {\n q.pop_back()...
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the ma...
3
{ "code": "class Solution {\npublic:\n vector<int> maxSlidingWindow(vector<int>& nums, int k) {\n priority_queue<int> pq;\n unordered_multiset<int> memo;\n for(int i=0;i<k;++i) pq.push(nums[i]),memo.insert(nums[i]);\n int last=0;\n vector<int> ans = {pq.top()};\n for(int i...
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the ma...
3
{ "code": "class Solution {\npublic:\n vector<int> maxSlidingWindow(vector<int>& nums, int k) {\n // stores {value, index of occurence}, strictly decreasing by value\n deque<vector<int>> dec;\n\n int n = nums.size();\n\n vector<int> ans;\n \n for (int i = 0; i < n; ++i) {\...
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the ma...
3
{ "code": "class Solution {\npublic:\n vector<int> maxSlidingWindow(vector<int>& nums, int k) {\n // stores {value, index of occurence}, strictly decreasing by value\n deque<vector<int>> dec;\n\n int n = nums.size();\n\n vector<int> ans;\n \n for (int i = 0; i < n; ++i) {\...
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the ma...
3
{ "code": "class Solution {\npublic:\n vector<int> maxSlidingWindow(vector<int>& nums, int k) {\n priority_queue<int> pq;\n multiset<int> s;\n vector<int> res;\n for(int i=0; i<k; ++i) pq.push(nums[i]);\n res.push_back(pq.top());\n for(int i=k; i<nums.size(); ++i){\n ...
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the ma...
3
{ "code": "class Solution {\npublic:\n int answer = 0;\n vector<int> ans;\n vector<int> maxSlidingWindow(vector<int>& nums, int k) {\n int i = 0, j = 0;\n set<int, greater<int>> st;\n map<int, int> mp;\n\n while(j < nums.size()){\n if(j - i + 1 <= k){\n s...
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the ma...
3
{ "code": "class Solution {\npublic:\n vector<int> maxSlidingWindow(vector<int>& nums, int k) {\n deque<vector<int>> window;\n vector<int> ans;\n int currMax = 0;\n for(int i = 0; i < nums.size(); i++) {\n while (window.size() != 0 && window.front()[1] <= i - k) {\n ...
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the ma...
3
{ "code": "class Solution {\npublic:\n vector<int> maxSlidingWindow(vector<int>& nums, int k) {\n deque<vector<int>> window;\n vector<int> ans;\n int currMax = 0;\n\n for(int i = 0; i < nums.size(); i++) {\n while (window.size() > 0 && window.front()[1] <= i - k) {\n ...
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the ma...
3
{ "code": "class Solution {\npublic:\n vector<int> maxSlidingWindow(vector<int>& nums, int k) {\n deque<vector<int>> window;\n vector<int> ans;\n int currMax = 0;\n\n for(int i = 0; i < nums.size(); i++) {\n while (window.size() > 0 && window.front()[1] <= i - k) {\n ...
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the ma...
3
{ "code": "class Solution {\npublic:\n vector<int> maxSlidingWindow(vector<int>& nums, int k) {\n deque<vector<int>> window;\n vector<int> ans;\n int currMax = 0;\n for(int i = 0; i < k; i++) {\n while(window.size() > 0 && window.back()[0] < nums[i]) {\n window...
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the ma...
3
{ "code": "class Solution {\npublic:\n vector<int> maxSlidingWindow(vector<int>& nums, int k) {\n int n=nums.size();\n if(n==k)return {*max_element(nums.begin(),nums.end())};\n priority_queue<pair<int,int>> pq;\n unordered_map<int,int> mp;\n vector<int> ans;\n int j=0;\n ...
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the ma...
3
{ "code": "class Solution {\npublic:\n class Queue {\n private:\n const size_t size;\n int* arr;\n int front;\n map<int, int> arrMap;\n public:\n Queue(const size_t &sizeP): size(sizeP), front(0), arr(new int[size]()) {}\n ~Queue() {de...
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the ma...
3
{ "code": "class Solution {\npublic:\n class Queue {\n private:\n const size_t size;\n int* arr;\n int front;\n map<int, int> arrMap;\n public:\n Queue(const size_t &sizeP): size(sizeP), front(0), arr(new int[size]()) {}\n ~Queue() {de...
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the ma...
3
{ "code": "class Solution {\npublic:\n vector<int> maxSlidingWindow(vector<int>& nums, int k) {\n\n // int start=0;\n // int end=0;\n // int n = nums.size();\n // vector<int> res;\n // set<int> st;\n // int sum=0;\n // while(end<n)\n // {\n // st.i...
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the ma...
3
{ "code": "class Solution {\npublic:\n vector<int> maxSlidingWindow(vector<int>& nums, int k) {\n vector<int> result;\n priority_queue<pair<int, int>> maxHeap;\n unordered_map<int,int> umap;\n\n for (int i = 0; i < k; ++i) {\n maxHeap.push({nums[i], i});\n }\n resul...
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the ma...
3
{ "code": "class Solution {\npublic:\n vector<int> maxSlidingWindow(vector<int>& arr, int k) {\n int n=arr.size();\n priority_queue<pair<int,int>> pq;\n map<int,int> m;\n vector<int> ans;\n for(int i=0;i<k;i++)\n pq.push({arr[i],i});\n\n ans.push_back(pq.top().first...
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the ma...
3
{ "code": "#include<bits/stdc++.h>\n#include <queue>\nusing namespace std;\n\nclass Solution {\nprivate:\n class Compare{\n public : \n bool operator()( pair<int,int> a, pair<int,int> b){\n if ( a.first > b.first ){\n return true;\n } else if ( a.first == b.first ){\n...
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the ma...
3
{ "code": "class Solution {\npublic:\n vector<int> maxSlidingWindow(const vector<int>& nums, const int k) const {\n std::multiset<int> set(nums.cbegin(), std::next(nums.cbegin(), k));\n std::vector<int> res;\n res.reserve(nums.size() - k + 1);\n for(auto i = k; i < nums.size(); ++i) {\n...
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the ma...
3
{ "code": "class Solution {\npublic:\n vector<int> maxSlidingWindow(vector<int>& nums, int k) {\n multiset<int> vals;\n vector<int> res(nums.size()-k+1);\n for (int i = 0; i < nums.size(); i++) {\n vals.insert(nums[i]);\n if (i >= k) {\n vals.erase(vals.fin...
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the ma...
3
{ "code": "class Solution {\npublic:\n vector<int> maxSlidingWindow(const vector<int>& nums, size_t k) {\n\n std::multiset<int> window_nums_set;\n for (size_t i = 0; i < k; ++i)\n window_nums_set.insert(nums[i]);\n\n std::vector<int> res;\n res.resize(nums.size() + 1 - k);\n ...
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the ma...
3
{ "code": "// #include <vector>\n// #include <queue>\n// #include <map>\n\n// using namespace std;\n\nclass Solution {\npublic:\n vector<int> maxSlidingWindow(vector<int>& nums, int k) {\n int n = nums.size();\n priority_queue<pair<int, int>> maxi; // Pair to store both value and index\n map<i...
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the ma...
3
{ "code": "class Solution {\npublic:\n vector<int> maxSlidingWindow(vector<int>& nums, int k) {\n\n int n=nums.size();\n vector<int>ans;\n priority_queue<pair<int,int>>pq;\n map<pair<int,int>,int>m;\n int j=0;\n for(int i=0;i<n;i++){\n \n int ele=nums...
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the ma...
3
{ "code": "class Solution {\npublic:\n \n\n vector<int> maxSlidingWindow(vector<int>& nums, int k) {\n deque <vector<int>> dq;\n int n = nums.size();\n for(int i = 0 ; i < k ; i++ ){\n if(!dq.empty()){\n while(!dq.empty() && dq.back()[1] <= nums[i])\n ...
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the ma...
3
{ "code": "class Solution {\npublic:\n vector<int> maxSlidingWindow(vector<int>& nums, int k) {\n int i, j, length = nums.size();\n vector<short int> lg(length + 1);\n vector<int> res(length - k + 1);\n lg[1] = 0;\n for(i = 2; i <= length; ++i)\n lg[i] = lg[i >> 1] + 1...
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the ma...
3
{ "code": "class Solution {\npublic:\n vector<int> maxSlidingWindow(vector<int>& nums, int k) {\n vector<int> result;\n result.reserve(nums.size()-k);\n\n multiset<int> window_sum;\n for(int i =0; i < k; ++i)\n window_sum.insert(nums[i]);\n\n\n result.push_back( (*win...
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the ma...
3
{ "code": "class Solution {\npublic:\n vector<int> maxSlidingWindow(vector<int>& nums, int k) {\n multiset<int> s;\n vector<int> ans;\n for(int i=0; i<k; i++) s.insert(nums[i]);\n ans.push_back(*s.rbegin());\n for(int i=k; i<nums.size(); i++){\n s.erase(s.find(nums[i-k...
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the ma...
3
{ "code": "class Solution {\npublic:\n vector<int> maxSlidingWindow(vector<int>& nums, int k) {\n vector<int>ans;\n multiset<int>m;\n int j=0;\n for(int i=0; i<nums.size(); i++){\n m.insert(nums[i]);\n if(i-j+1==k){\n ans.push_back(*m.rbegin());\n ...
240
<p>Write an efficient algorithm that searches for a value <code>target</code> in an <code>m x n</code> integer matrix <code>matrix</code>. This matrix has the following properties:</p> <ul> <li>Integers in each row are sorted in ascending from left to right.</li> <li>Integers in each column are sorted in ascending f...
0
{ "code": "class Solution {\npublic:\n bool searchMatrix(vector<vector<int>>& matrix, int target) {\n ios_base::sync_with_stdio(false);\n cin.tie(0);\n cout.tie(0);\n\n int row=matrix.size();\n for (int i = 0; i < row; i++) {\n\n int l = 0, r = matrix[0].size() - 1;\n\...
240
<p>Write an efficient algorithm that searches for a value <code>target</code> in an <code>m x n</code> integer matrix <code>matrix</code>. This matrix has the following properties:</p> <ul> <li>Integers in each row are sorted in ascending from left to right.</li> <li>Integers in each column are sorted in ascending f...
0
{ "code": "class Solution {\npublic:\n bool searchMatrix(vector<vector<int>>& matrix, int target) {\n \n int m=matrix.size();\n int n=matrix[0].size();\n int i=0;\n int j=n-1;\n \n while(j>=0 && i<m){\n if(matrix[i][j]==target) return true;\n e...
240
<p>Write an efficient algorithm that searches for a value <code>target</code> in an <code>m x n</code> integer matrix <code>matrix</code>. This matrix has the following properties:</p> <ul> <li>Integers in each row are sorted in ascending from left to right.</li> <li>Integers in each column are sorted in ascending f...
0
{ "code": "class Solution {\npublic:\n bool searchMatrix(vector<vector<int>>& matrix, int target) {\n int row=matrix.size();\n int col=matrix[0].size();\n\n int rowIndex=0;\n int colIndex=col-1;\n\n while(rowIndex<row && colIndex>=0){\n int element=matrix[rowIndex][col...
240
<p>Write an efficient algorithm that searches for a value <code>target</code> in an <code>m x n</code> integer matrix <code>matrix</code>. This matrix has the following properties:</p> <ul> <li>Integers in each row are sorted in ascending from left to right.</li> <li>Integers in each column are sorted in ascending f...
0
{ "code": "class Solution {\npublic:\n bool searchMatrix(vector<vector<int>> const& matrix, int const target) {\n size_t const M = matrix.size();\n size_t const N = matrix[0].size();\n\n for (size_t i = 0; i < M; ++i) {\n if (matrix[i][0] <= target && matrix[i][N - 1] >= target) {\n...
240
<p>Write an efficient algorithm that searches for a value <code>target</code> in an <code>m x n</code> integer matrix <code>matrix</code>. This matrix has the following properties:</p> <ul> <li>Integers in each row are sorted in ascending from left to right.</li> <li>Integers in each column are sorted in ascending f...
0
{ "code": "class Solution {\npublic:\n int solve(int l, int r, vector<vector<int>>& mat,int col, int x){\n int res = -1;\n while(l <= r){\n int m = l + (r - l)/2;\n if(mat[m][col] == x) return m;\n else if(mat[m][col] > x) r = m - 1;\n else{\n ...
240
<p>Write an efficient algorithm that searches for a value <code>target</code> in an <code>m x n</code> integer matrix <code>matrix</code>. This matrix has the following properties:</p> <ul> <li>Integers in each row are sorted in ascending from left to right.</li> <li>Integers in each column are sorted in ascending f...
0
{ "code": "class Solution {\npublic:\n bool searchMatrix(vector<vector<int>>& matrix, int target) {\n int rows=matrix.size();\n int cols=matrix[0].size();\n int row=0;\n int col=cols-1;\n while(row<rows &&col>=0){\n int current=matrix[row][col];\n if(current...
240
<p>Write an efficient algorithm that searches for a value <code>target</code> in an <code>m x n</code> integer matrix <code>matrix</code>. This matrix has the following properties:</p> <ul> <li>Integers in each row are sorted in ascending from left to right.</li> <li>Integers in each column are sorted in ascending f...
0
{ "code": "class Solution {\npublic:\n bool searchMatrix(vector<vector<int>>& matrix, int target) {\n int row=matrix.size();\n int col=matrix[0].size();\n\n int rowIndex=0;\n int colIndex=col-1;\n while(rowIndex<row && colIndex>=0){\n int element=matrix[rowIndex][colIn...
240
<p>Write an efficient algorithm that searches for a value <code>target</code> in an <code>m x n</code> integer matrix <code>matrix</code>. This matrix has the following properties:</p> <ul> <li>Integers in each row are sorted in ascending from left to right.</li> <li>Integers in each column are sorted in ascending f...
0
{ "code": "class Solution {\npublic:\n bool searchMatrix(vector<vector<int>>& matrix, int target) {\n int n = matrix.size();\n int m = matrix[0].size();\n int i = 0;\n int j = m- 1;\n while( (i<n) && (j>=0)){\n if(matrix[i][j] == target){\n return tru...
240
<p>Write an efficient algorithm that searches for a value <code>target</code> in an <code>m x n</code> integer matrix <code>matrix</code>. This matrix has the following properties:</p> <ul> <li>Integers in each row are sorted in ascending from left to right.</li> <li>Integers in each column are sorted in ascending f...
0
{ "code": "class Solution {\npublic:\n bool searchMatrix(vector<vector<int>>& matrix, int target) {\n int row=matrix.size();\n int col=matrix[0].size();\n int i=0,j=col-1;\n while(i>=0 && i<row && j>=0 && j<col)\n {\n int value=matrix[i][j];\n if(value>targe...
240
<p>Write an efficient algorithm that searches for a value <code>target</code> in an <code>m x n</code> integer matrix <code>matrix</code>. This matrix has the following properties:</p> <ul> <li>Integers in each row are sorted in ascending from left to right.</li> <li>Integers in each column are sorted in ascending f...
0
{ "code": "class Solution {\npublic:\n bool searchMatrix(vector<vector<int>>& matrix, int target) {\n int row_start = 0, col_start = matrix[0].size()-1;\n while(col_start >=0 && row_start <= matrix.size()-1)\n {\n if(matrix[row_start][col_start] == target)\n return tr...
2,914
<p>You are given a <strong>0-indexed</strong> 2D matrix <code>grid</code> of size <code>n x n</code>, where <code>(r, c)</code> represents:</p> <ul> <li>A cell containing a thief if <code>grid[r][c] = 1</code></li> <li>An empty cell if <code>grid[r][c] = 0</code></li> </ul> <p>You are initially positioned at cell <...
0
{ "code": "class Solution {\npublic:\n int getFactor(int i, int j, vector<pair<int,int>> &index){\n\n int factor = INT_MAX;\n for(auto it : index){ \n int r = it.first;\n int c = it.second;\n factor = min(factor, abs(i-r)+abs(j-c));\n }\n return factor;...
2,914
<p>You are given a <strong>0-indexed</strong> 2D matrix <code>grid</code> of size <code>n x n</code>, where <code>(r, c)</code> represents:</p> <ul> <li>A cell containing a thief if <code>grid[r][c] = 1</code></li> <li>An empty cell if <code>grid[r][c] = 0</code></li> </ul> <p>You are initially positioned at cell <...
0
{ "code": "class Solution {\npublic:\n int getFactor(int i, int j, vector<pair<int,int>> &index){\n\n int factor = INT_MAX;\n for(auto it : index){ \n int r = it.first;\n int c = it.second;\n factor = min(factor, abs(i-r)+abs(j-c));\n }\n return factor;...
2,914
<p>You are given a <strong>0-indexed</strong> 2D matrix <code>grid</code> of size <code>n x n</code>, where <code>(r, c)</code> represents:</p> <ul> <li>A cell containing a thief if <code>grid[r][c] = 1</code></li> <li>An empty cell if <code>grid[r][c] = 0</code></li> </ul> <p>You are initially positioned at cell <...
0
{ "code": "class Solution {\npublic:\n int maximumSafenessFactor(vector<vector<int>>& grid) {\n int n=grid.size();\n if(grid[0][0]==1 || grid[n-1][n-1]==1) return 0;\n int dir[4][2]={{0, 1}, {0, -1}, {1, 0}, {-1, 0}};\n queue<pair<int, int>> q;\n for(int i=0;i<n;i++){\n ...
2,914
<p>You are given a <strong>0-indexed</strong> 2D matrix <code>grid</code> of size <code>n x n</code>, where <code>(r, c)</code> represents:</p> <ul> <li>A cell containing a thief if <code>grid[r][c] = 1</code></li> <li>An empty cell if <code>grid[r][c] = 0</code></li> </ul> <p>You are initially positioned at cell <...
0
{ "code": "class Solution {\npublic:\n void cal_dist(vector<vector<int>>& grid){\n int n = grid.size();\n int di[] = {0,-1,0,1};\n int dj[] = {1,0,-1,0};\n queue<pair<int,int>> queue;\n for(int i = 0; i < n; ++i){\n for(int j = 0; j < n; ++j)\n if(grid[i...
2,914
<p>You are given a <strong>0-indexed</strong> 2D matrix <code>grid</code> of size <code>n x n</code>, where <code>(r, c)</code> represents:</p> <ul> <li>A cell containing a thief if <code>grid[r][c] = 1</code></li> <li>An empty cell if <code>grid[r][c] = 0</code></li> </ul> <p>You are initially positioned at cell <...
0
{ "code": "class Solution {\npublic:\n\n const vector<pair<int,int>> directions = {{0,1},{0,-1},{1,0},{-1,0}};\n\n bool isValid(int x, int y, int n) {\n return x >= 0 && x < n && y >= 0 && y < n;\n }\n \n int maximumSafenessFactor(vector<vector<int>>& grid) {\n int n = grid.size();\n\n ...
2,914
<p>You are given a <strong>0-indexed</strong> 2D matrix <code>grid</code> of size <code>n x n</code>, where <code>(r, c)</code> represents:</p> <ul> <li>A cell containing a thief if <code>grid[r][c] = 1</code></li> <li>An empty cell if <code>grid[r][c] = 0</code></li> </ul> <p>You are initially positioned at cell <...
0
{ "code": "#include <vector>\n#include <queue>\n#include <tuple>\n#include <algorithm>\nusing namespace std;\n\nclass Solution {\npublic:\n int maximumSafenessFactor(vector<vector<int>>& grid) {\n int nRows = grid.size();\n int nCols = grid[0].size();\n vector<pair<int, int>> directions = {{0,...
2,914
<p>You are given a <strong>0-indexed</strong> 2D matrix <code>grid</code> of size <code>n x n</code>, where <code>(r, c)</code> represents:</p> <ul> <li>A cell containing a thief if <code>grid[r][c] = 1</code></li> <li>An empty cell if <code>grid[r][c] = 0</code></li> </ul> <p>You are initially positioned at cell <...
0
{ "code": "class Solution {\npublic:\n bool is_neighbor(int x, int y, int n) {\n return x >= 0 && x < n && y >= 0 && y < n;\n }\n int bfs(vector<vector<int>> grid) {\n\n int n = grid.size();\n priority_queue<pair<int, pair<int, int>>> Q;\n pair<int, int> q;\n pair<int, int>...
2,914
<p>You are given a <strong>0-indexed</strong> 2D matrix <code>grid</code> of size <code>n x n</code>, where <code>(r, c)</code> represents:</p> <ul> <li>A cell containing a thief if <code>grid[r][c] = 1</code></li> <li>An empty cell if <code>grid[r][c] = 0</code></li> </ul> <p>You are initially positioned at cell <...
0
{ "code": "class Solution {\npublic:\n int findManhattan(vector<vector<int>> &grid, int pr, int pc) {\n int n = grid.size();\n int ans = 3*n;\n for(int i = 0; i<n; i++) {\n for(int j = 0; j<n; j++) {\n if (grid[i][j] == 1) ans = min(ans, abs(i-pr)+abs(j-pc));\n ...
2,914
<p>You are given a <strong>0-indexed</strong> 2D matrix <code>grid</code> of size <code>n x n</code>, where <code>(r, c)</code> represents:</p> <ul> <li>A cell containing a thief if <code>grid[r][c] = 1</code></li> <li>An empty cell if <code>grid[r][c] = 0</code></li> </ul> <p>You are initially positioned at cell <...
0
{ "code": "const int INF = 1e8;\ntypedef pair <int,int> pi;\ntypedef pair <int, pi> ppi;\nconst vector <pi> mover = {{1,0},{0,1},{0,-1},{-1,0}};\n\nclass Solution {\npublic:\n\n void print2D(vector<vector<int>> &grid){\n int n = grid.size(), m = grid[0].size();\n for( int i = 0 ; i < n ; i++){\n ...
2,914
<p>You are given a <strong>0-indexed</strong> 2D matrix <code>grid</code> of size <code>n x n</code>, where <code>(r, c)</code> represents:</p> <ul> <li>A cell containing a thief if <code>grid[r][c] = 1</code></li> <li>An empty cell if <code>grid[r][c] = 0</code></li> </ul> <p>You are initially positioned at cell <...
0
{ "code": "class Solution {\npublic:\n int maximumSafenessFactor(vector<vector<int>>& grid) {\n // do bfs twice on grid\n // first bfs on grid: build matrix[i][j], meaning the safe score of that node\n\n int n = grid.size();\n vector<vector<int>> matrix = grid;\n queue<pair<int, ...
2,914
<p>You are given a <strong>0-indexed</strong> 2D matrix <code>grid</code> of size <code>n x n</code>, where <code>(r, c)</code> represents:</p> <ul> <li>A cell containing a thief if <code>grid[r][c] = 1</code></li> <li>An empty cell if <code>grid[r][c] = 0</code></li> </ul> <p>You are initially positioned at cell <...
1
{ "code": "class Solution {\npublic:\n vector<int> roww = {0, 0, -1, 1};\n vector<int> coll = {-1, 1, 0, 0};\n\n void bfs(vector<vector<int>>& grid, vector<vector<int>>& score, int n) {\n queue<pair<int, int>> q;\n\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n ...
2,914
<p>You are given a <strong>0-indexed</strong> 2D matrix <code>grid</code> of size <code>n x n</code>, where <code>(r, c)</code> represents:</p> <ul> <li>A cell containing a thief if <code>grid[r][c] = 1</code></li> <li>An empty cell if <code>grid[r][c] = 0</code></li> </ul> <p>You are initially positioned at cell <...
1
{ "code": "class Solution {\npublic:\n\n // void buildDis(queue<pair<int,int>>& q, int n, vector<vector<int>>& dis, vector<vector<bool>>& vis){\n // vector<pair<int, int>> dir = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};\n // int lvl = 0;\n\n // while (!q.empty()) {\n // int siz = q.size()...
2,914
<p>You are given a <strong>0-indexed</strong> 2D matrix <code>grid</code> of size <code>n x n</code>, where <code>(r, c)</code> represents:</p> <ul> <li>A cell containing a thief if <code>grid[r][c] = 1</code></li> <li>An empty cell if <code>grid[r][c] = 0</code></li> </ul> <p>You are initially positioned at cell <...
1
{ "code": "struct Point {\n int r, c;\n};\n\nstruct Node {\n Point point;\n int score;\n\n bool operator<(const Node& other) const {\n return score < other.score;\n }\n bool operator>(const Node& other) const {\n return score > other.score;\n }\n};\n\nclass Solution {\npublic:\n ...
2,914
<p>You are given a <strong>0-indexed</strong> 2D matrix <code>grid</code> of size <code>n x n</code>, where <code>(r, c)</code> represents:</p> <ul> <li>A cell containing a thief if <code>grid[r][c] = 1</code></li> <li>An empty cell if <code>grid[r][c] = 0</code></li> </ul> <p>You are initially positioned at cell <...
1
{ "code": "class Solution {\n const vector<int> dx = {-1, 0, 1, 0};\n const vector<int> dy = {0, 1, 0, -1};\n\n vector<vector<int>> getSafenessFactor(vector<vector<int>> &grid) {\n\n int n = grid.size(), m = grid[0].size();\n\n vector<vector<int>> safeNessFactor(n, vector<int>(m, 1e9));\n ...
2,914
<p>You are given a <strong>0-indexed</strong> 2D matrix <code>grid</code> of size <code>n x n</code>, where <code>(r, c)</code> represents:</p> <ul> <li>A cell containing a thief if <code>grid[r][c] = 1</code></li> <li>An empty cell if <code>grid[r][c] = 0</code></li> </ul> <p>You are initially positioned at cell <...
1
{ "code": "class Solution {\npublic:\n int maximumSafenessFactor(vector<vector<int>>& grid) {\n\n int n = grid.size();\n int m = grid[0].size();\n if (grid[0][0] == 1 || grid[n - 1][m - 1] == 1) {\n return 0;\n }\n\n vector<vector<int>> v(n, vector<int>(m, 0));\n ...
2,914
<p>You are given a <strong>0-indexed</strong> 2D matrix <code>grid</code> of size <code>n x n</code>, where <code>(r, c)</code> represents:</p> <ul> <li>A cell containing a thief if <code>grid[r][c] = 1</code></li> <li>An empty cell if <code>grid[r][c] = 0</code></li> </ul> <p>You are initially positioned at cell <...
1
{ "code": "class Solution {\npublic:\n vector<int>delr={-1 ,0 ,1 ,0};\n vector<int>delc={0 ,-1 ,0 ,1};\n int maximumSafenessFactor(vector<vector<int>>& grid) {\n int r=grid.size();\n int c=grid[0].size();\n vector<vector<int>>dis(r ,vector<int>(c ,1e9));\n vector<vector<bool>>vis(...
2,914
<p>You are given a <strong>0-indexed</strong> 2D matrix <code>grid</code> of size <code>n x n</code>, where <code>(r, c)</code> represents:</p> <ul> <li>A cell containing a thief if <code>grid[r][c] = 1</code></li> <li>An empty cell if <code>grid[r][c] = 0</code></li> </ul> <p>You are initially positioned at cell <...
1
{ "code": "class Solution {\n void multisourceBFS(vector<vector<int>>& arr, vector<int> &dx, vector<int> &dy)\n {\n int m = arr.size(), n = arr[0].size();\n queue<pair<int,pair<int,int>>> q; //{dist, {x,y}}\n vector<vector<int>> vis(m, vector<int>(n,0));\n \n for(int i=0...
2,914
<p>You are given a <strong>0-indexed</strong> 2D matrix <code>grid</code> of size <code>n x n</code>, where <code>(r, c)</code> represents:</p> <ul> <li>A cell containing a thief if <code>grid[r][c] = 1</code></li> <li>An empty cell if <code>grid[r][c] = 0</code></li> </ul> <p>You are initially positioned at cell <...
1
{ "code": "class Solution {\npublic:\n int dx[4] = {0, 1, -1, 0};\n int dy[4] = {1, 0, 0, -1};\n bool valid(int x, int y, vector<vector<int>>& grid){\n return x > -1 && y > -1 && x < grid.size() && y < grid[0].size();\n }\n void bfs(int x, vector<vector<int>>& grid){\n int n = grid.size()...
2,914
<p>You are given a <strong>0-indexed</strong> 2D matrix <code>grid</code> of size <code>n x n</code>, where <code>(r, c)</code> represents:</p> <ul> <li>A cell containing a thief if <code>grid[r][c] = 1</code></li> <li>An empty cell if <code>grid[r][c] = 0</code></li> </ul> <p>You are initially positioned at cell <...
1
{ "code": "class Solution {\nprivate:\n vector<int> DIR = {0, 1, 0, -1, 0};\n int m = 0;\n int n = 0;\n // is there a valid path of safeness factor d from grid[r][c] to grid[m - 1][n - 1]\n // safeness factor d: every cell on the path >= d\n bool validPath(int d, int r, int c, vector<vector<int>>& d...
2,914
<p>You are given a <strong>0-indexed</strong> 2D matrix <code>grid</code> of size <code>n x n</code>, where <code>(r, c)</code> represents:</p> <ul> <li>A cell containing a thief if <code>grid[r][c] = 1</code></li> <li>An empty cell if <code>grid[r][c] = 0</code></li> </ul> <p>You are initially positioned at cell <...
1
{ "code": "class Solution {\nprivate:\n vector<int> DIR = {0, 1, 0, -1, 0};\n int m = 0;\n int n = 0;\n // is there a valid path of safeness factor d from grid[r][c] to grid[m - 1][n - 1]\n // safeness factor d: every cell on the path >= d\n bool validPath(int d, int r, int c, vector<vector<int>>& d...
2,914
<p>You are given a <strong>0-indexed</strong> 2D matrix <code>grid</code> of size <code>n x n</code>, where <code>(r, c)</code> represents:</p> <ul> <li>A cell containing a thief if <code>grid[r][c] = 1</code></li> <li>An empty cell if <code>grid[r][c] = 0</code></li> </ul> <p>You are initially positioned at cell <...
1
{ "code": "class Solution {\npublic:\n vector<pair<int, int>> move = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};\n\n bool isValidSafeness(vector<vector<int>>& grid, int minSafeness) {\n int n = grid.size();\n\n if (grid[0][0] < minSafeness || grid[n - 1][n - 1] < minSafeness) {\n return false;\...
2,914
<p>You are given a <strong>0-indexed</strong> 2D matrix <code>grid</code> of size <code>n x n</code>, where <code>(r, c)</code> represents:</p> <ul> <li>A cell containing a thief if <code>grid[r][c] = 1</code></li> <li>An empty cell if <code>grid[r][c] = 0</code></li> </ul> <p>You are initially positioned at cell <...
1
{ "code": "class Solution {\n\npublic:\n\n int maximumSafenessFactor(vector<vector<int>>& grid) {\n\n int n = grid.size();\n\n queue<pair<int, int>> multiSourceQueue;\n\n // Mark thieves as 0 and empty cells as -1, and push thieves to the\n\n // queue\n\n for (int i = 0; i < n; i...
2,914
<p>You are given a <strong>0-indexed</strong> 2D matrix <code>grid</code> of size <code>n x n</code>, where <code>(r, c)</code> represents:</p> <ul> <li>A cell containing a thief if <code>grid[r][c] = 1</code></li> <li>An empty cell if <code>grid[r][c] = 0</code></li> </ul> <p>You are initially positioned at cell <...
2
{ "code": "class Solution {\npublic:\n int maximumSafenessFactor(vector<vector<int>>& grid) {\n this->n = grid.size();\n vector<vector<int>> safenessGrid = getSafenessGrid(grid);\n return findSafestPath(safenessGrid);\n }\n\nprivate:\n struct Position {\n int r,c;\n Positio...
2,914
<p>You are given a <strong>0-indexed</strong> 2D matrix <code>grid</code> of size <code>n x n</code>, where <code>(r, c)</code> represents:</p> <ul> <li>A cell containing a thief if <code>grid[r][c] = 1</code></li> <li>An empty cell if <code>grid[r][c] = 0</code></li> </ul> <p>You are initially positioned at cell <...
2
{ "code": "#include <iostream>\n#include <vector>\n#include <queue>\n#include <algorithm> // For max function\n\nusing namespace std;\n\nclass Solution {\npublic:\n int maximumSafenessFactor(vector<vector<int>>& grid) {\n // Calculate safeness factors for each cell\n vector<vector<int>> safeness = ca...
2,914
<p>You are given a <strong>0-indexed</strong> 2D matrix <code>grid</code> of size <code>n x n</code>, where <code>(r, c)</code> represents:</p> <ul> <li>A cell containing a thief if <code>grid[r][c] = 1</code></li> <li>An empty cell if <code>grid[r][c] = 0</code></li> </ul> <p>You are initially positioned at cell <...
2
{ "code": "#include <iostream>\n#include <vector>\n#include <queue>\n#include <algorithm> \n\nusing namespace std;\n\nclass Solution {\npublic:\n int maximumSafenessFactor(vector<vector<int>>& grid) {\n // Calculate safeness factors for each cell\n vector<vector<int>> safeness = calculateSafenessFact...
2,914
<p>You are given a <strong>0-indexed</strong> 2D matrix <code>grid</code> of size <code>n x n</code>, where <code>(r, c)</code> represents:</p> <ul> <li>A cell containing a thief if <code>grid[r][c] = 1</code></li> <li>An empty cell if <code>grid[r][c] = 0</code></li> </ul> <p>You are initially positioned at cell <...
2
{ "code": "class Solution {\npublic:\n int maximumSafenessFactor(vector<vector<int>>& grid) {\n queue<pair<int,int>> q;\n int n=grid.size();\n for(int i=0;i<n;i++){\n for(int j=0;j<n;j++){\n if(grid[i][j]==1){\n q.push({i,j});\n ...
2,914
<p>You are given a <strong>0-indexed</strong> 2D matrix <code>grid</code> of size <code>n x n</code>, where <code>(r, c)</code> represents:</p> <ul> <li>A cell containing a thief if <code>grid[r][c] = 1</code></li> <li>An empty cell if <code>grid[r][c] = 0</code></li> </ul> <p>You are initially positioned at cell <...
2
{ "code": "class Solution {\npublic:\n int getCost(vector<vector<int>>& grid) {\n int n = grid.size(), m = grid[0].size();\n vector<vector<int>> cost(n, vector<int>(m, -1e9));\n cost[0][0] = grid[0][0];\n queue<pair<int, int>> q, q1;\n q.push({0, 0});\n\n vector<int> dirs ...
2,914
<p>You are given a <strong>0-indexed</strong> 2D matrix <code>grid</code> of size <code>n x n</code>, where <code>(r, c)</code> represents:</p> <ul> <li>A cell containing a thief if <code>grid[r][c] = 1</code></li> <li>An empty cell if <code>grid[r][c] = 0</code></li> </ul> <p>You are initially positioned at cell <...
2
{ "code": "class Solution {\n\n int dx[4]={1, -1, 0, 0};\n int dy[4]={0, 0, 1, -1};\n \n bool bfs(vector<vector<int>> &dist, int sf)\n {\n if(dist[0][0]<sf) return false;\n int n=dist.size();\n queue<pair<int, int>> q;\n vector<vector<bool>> vis(n, vector<bool> (n, false));\...
2,914
<p>You are given a <strong>0-indexed</strong> 2D matrix <code>grid</code> of size <code>n x n</code>, where <code>(r, c)</code> represents:</p> <ul> <li>A cell containing a thief if <code>grid[r][c] = 1</code></li> <li>An empty cell if <code>grid[r][c] = 0</code></li> </ul> <p>You are initially positioned at cell <...
2
{ "code": "class Solution {\n\n int dx[4]={1, -1, 0, 0};\n int dy[4]={0, 0, 1, -1};\n \n bool bfs(vector<vector<int>> &dist, int sf)\n {\n if(dist[0][0]<sf) return false;\n int n=dist.size();\n queue<pair<int, int>> q;\n vector<vector<bool>> vis(n, vector<bool> (n, false));\...
2,914
<p>You are given a <strong>0-indexed</strong> 2D matrix <code>grid</code> of size <code>n x n</code>, where <code>(r, c)</code> represents:</p> <ul> <li>A cell containing a thief if <code>grid[r][c] = 1</code></li> <li>An empty cell if <code>grid[r][c] = 0</code></li> </ul> <p>You are initially positioned at cell <...
2
{ "code": "class Solution {\npublic:\n vector<vector<int>> computeSafeDistances(vector<vector<int>>& grid, vector<pair<int, int>>& thieves) {\n int n = grid.size();\n vector<vector<int>> safe(n, vector<int>(n, INT_MAX));\n queue<pair<int, int>> q;\n \n // Initialize the BFS queue...
2,914
<p>You are given a <strong>0-indexed</strong> 2D matrix <code>grid</code> of size <code>n x n</code>, where <code>(r, c)</code> represents:</p> <ul> <li>A cell containing a thief if <code>grid[r][c] = 1</code></li> <li>An empty cell if <code>grid[r][c] = 0</code></li> </ul> <p>You are initially positioned at cell <...
2
{ "code": "class Solution {\npublic:\n using pp = pair<int, int>;\n\n pp M[4] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};\n\n bool f(int t, vector<vector<int>>& d, vector<vector<int>>& v){\n if(d[0][0] < t)\n return 0;\n \n queue<pp> q;\n q.push({0, 0});\n v[0][0] ...