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 <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 max sliding window</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,-1,-3,5,3,6,7], k = 3
<strong>Output:</strong> [3,3,5,5,6,7]
<strong>Explanation:</strong>
Window position Max
--------------- -----
[1 3 -1] -3 5 3 6 7 <strong>3</strong>
1 [3 -1 -3] 5 3 6 7 <strong>3</strong>
1 3 [-1 -3 5] 3 6 7 <strong> 5</strong>
1 3 -1 [-3 5 3] 6 7 <strong>5</strong>
1 3 -1 -3 [5 3 6] 7 <strong>6</strong>
1 3 -1 -3 5 [3 6 7] <strong>7</strong>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1], k = 1
<strong>Output:</strong> [1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 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# heap: how to re-heapify with new val?\n\nUse BST: every time we locate the old val, delete it if count == 0, and insert new val / increase count.\nprocedure: 1. set up std::map: {val : count}. init map with first k values. for any subsequent insertion, locate old value, decrease count / remove it, and then insert new node / increase count.\n*/\n\n\nclass Solution {\npublic:\n void printMap(map<int, int> m) {\n cout << \"[ \";\n for(auto const& i : m) {\n cout << \"(\" << i.first << \", \" << i.second << \") \";\n }\n cout << \"]\" << endl;\n }\n\n vector<int> maxSlidingWindow(vector<int>& nums, int k) {\n vector<int> ret(nums.size() - k + 1, 0); // init return vector\n map<int, int> bst;\n // init\n for(size_t i = 0; i < k; ++i) {\n int num = nums[i];\n auto it = bst.find(num);\n if(it != bst.end()) {\n it->second ++;\n }\n else {\n bst.insert(make_pair(num, 1));\n }\n }\n size_t j = 0;\n ret[j] = bst.rbegin()->first; // set last in bst to be max\n j++;\n\n for(size_t i = k; i < nums.size(); ++i, ++j) {\n size_t i_remove = i - k;\n int num_remove = nums[i_remove];\n int num = nums[i];\n if(num_remove == num) { // same number, no change\n ret[j] = ret[j-1];\n continue;\n }\n auto it_remove = bst.find(num_remove);\n if(it_remove->second > 1) {\n it_remove->second--;\n }\n else{\n bst.erase(it_remove);\n }\n auto it_add = bst.find(num);\n if(it_add != bst.end()) {\n it_add->second++;\n }\n else {\n bst.insert(make_pair(num, 1));\n }\n ret[j] = bst.rbegin()->first; // set last in bst to be max\n\n //cout << \"i = \" << i << \", j = \" << j << \", max: \" << ret[j] << \", bst: \";\n //printMap(bst);\n }\n\n return ret;\n }\n};",
"memory": "174623"
} |
239 | <p>You are given an array of integers <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 max sliding window</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,-1,-3,5,3,6,7], k = 3
<strong>Output:</strong> [3,3,5,5,6,7]
<strong>Explanation:</strong>
Window position Max
--------------- -----
[1 3 -1] -3 5 3 6 7 <strong>3</strong>
1 [3 -1 -3] 5 3 6 7 <strong>3</strong>
1 3 [-1 -3 5] 3 6 7 <strong> 5</strong>
1 3 -1 [-3 5 3] 6 7 <strong>5</strong>
1 3 -1 -3 [5 3 6] 7 <strong>6</strong>
1 3 -1 -3 5 [3 6 7] <strong>7</strong>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1], k = 1
<strong>Output:</strong> [1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 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()) {\n auto it = m.rbegin();\n cout << it->first << ' ' << it->second << '\\n';\n ans[ind++] = it->first;\n m[n[s]]--;\n if (!m[n[s]]) m.erase(n[s]);\n s++;e++;\n if (e < n.size()) m[n[e]]++;\n }\n\n return ans;\n }\n};",
"memory": "174623"
} |
239 | <p>You are given an array of integers <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 max sliding window</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,-1,-3,5,3,6,7], k = 3
<strong>Output:</strong> [3,3,5,5,6,7]
<strong>Explanation:</strong>
Window position Max
--------------- -----
[1 3 -1] -3 5 3 6 7 <strong>3</strong>
1 [3 -1 -3] 5 3 6 7 <strong>3</strong>
1 3 [-1 -3 5] 3 6 7 <strong> 5</strong>
1 3 -1 [-3 5 3] 6 7 <strong>5</strong>
1 3 -1 -3 [5 3 6] 7 <strong>6</strong>
1 3 -1 -3 5 [3 6 7] <strong>7</strong>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1], k = 1
<strong>Output:</strong> [1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 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()]<=nums[i]) dq.pop_back();\n dq.push_back(i);\n if(i>=k-1) li.push_back(nums[dq.front()]);\n }\n return vector<int>(li.begin(),li.end());\n }\n};",
"memory": "175716"
} |
239 | <p>You are given an array of integers <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 max sliding window</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,-1,-3,5,3,6,7], k = 3
<strong>Output:</strong> [3,3,5,5,6,7]
<strong>Explanation:</strong>
Window position Max
--------------- -----
[1 3 -1] -3 5 3 6 7 <strong>3</strong>
1 [3 -1 -3] 5 3 6 7 <strong>3</strong>
1 3 [-1 -3 5] 3 6 7 <strong> 5</strong>
1 3 -1 [-3 5 3] 6 7 <strong>5</strong>
1 3 -1 -3 [5 3 6] 7 <strong>6</strong>
1 3 -1 -3 5 [3 6 7] <strong>7</strong>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1], k = 1
<strong>Output:</strong> [1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 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(m);\n for(int j=1;j+k-1<nums.size();j++){\n if(pq.top()==nums[j-1]){\n pq.pop();\n }\n else{\n deleted.insert(nums[j-1]);\n }\n pq.push(nums[j+k-1]);\n \n while(deleted.find(pq.top()) != deleted.end()){\n deleted.erase(deleted.find(pq.top()));\n pq.pop();\n }\n m=pq.top();\n ans.push_back(m);\n }\n return ans;\n \n }\n};",
"memory": "175716"
} |
239 | <p>You are given an array of integers <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 max sliding window</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,-1,-3,5,3,6,7], k = 3
<strong>Output:</strong> [3,3,5,5,6,7]
<strong>Explanation:</strong>
Window position Max
--------------- -----
[1 3 -1] -3 5 3 6 7 <strong>3</strong>
1 [3 -1 -3] 5 3 6 7 <strong>3</strong>
1 3 [-1 -3 5] 3 6 7 <strong> 5</strong>
1 3 -1 [-3 5 3] 6 7 <strong>5</strong>
1 3 -1 -3 [5 3 6] 7 <strong>6</strong>
1 3 -1 -3 5 [3 6 7] <strong>7</strong>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1], k = 1
<strong>Output:</strong> [1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 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) {\n mp.erase(nums[0]);\n }\n for (; i < nums.size(); ++i) {\n ++mp[nums[i]];\n ret.push_back(mp.rbegin()->first);\n if (--mp[nums[i - k + 1]] == 0) {\n mp.erase(nums[i - k + 1]);\n }\n }\n return ret;\n }\n};",
"memory": "176808"
} |
239 | <p>You are given an array of integers <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 max sliding window</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,-1,-3,5,3,6,7], k = 3
<strong>Output:</strong> [3,3,5,5,6,7]
<strong>Explanation:</strong>
Window position Max
--------------- -----
[1 3 -1] -3 5 3 6 7 <strong>3</strong>
1 [3 -1 -3] 5 3 6 7 <strong>3</strong>
1 3 [-1 -3 5] 3 6 7 <strong> 5</strong>
1 3 -1 [-3 5 3] 6 7 <strong>5</strong>
1 3 -1 -3 [5 3 6] 7 <strong>6</strong>
1 3 -1 -3 5 [3 6 7] <strong>7</strong>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1], k = 1
<strong>Output:</strong> [1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 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 }\n }\n std::vector<int> output = {(*std::prev(window.end())).first};\n for(int i = 1; i + k <= nums.size(); i++) {\n window[nums[i - 1]]--;\n if(window[nums[i - 1]] == 0)\n window.erase(nums[i - 1]);\n\n if(window.find(nums[i + k - 1]) == window.end()) {\n window.insert({nums[i + k - 1], 1});\n } else {\n window[nums[i + k - 1]]++;\n }\n\n output.emplace_back((*std::prev(window.end())).first);\n }\n return output;\n }\n};",
"memory": "177901"
} |
239 | <p>You are given an array of integers <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 max sliding window</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,-1,-3,5,3,6,7], k = 3
<strong>Output:</strong> [3,3,5,5,6,7]
<strong>Explanation:</strong>
Window position Max
--------------- -----
[1 3 -1] -3 5 3 6 7 <strong>3</strong>
1 [3 -1 -3] 5 3 6 7 <strong>3</strong>
1 3 [-1 -3 5] 3 6 7 <strong> 5</strong>
1 3 -1 [-3 5 3] 6 7 <strong>5</strong>
1 3 -1 -3 [5 3 6] 7 <strong>6</strong>
1 3 -1 -3 5 [3 6 7] <strong>7</strong>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1], k = 1
<strong>Output:</strong> [1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 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();\n vector<int> result;\n\n for(int i = k - 1 ; i < nums.size() ; i++){\n int indexDelete = i - k;\n if(indexDelete >= 0){\n mp[nums[indexDelete]]--;\n if(mp[nums[indexDelete]] == 0) mp.erase(nums[indexDelete]);\n }\n if(mp.find(nums[i]) != mp.end()) mp[nums[i]]++;\n else mp.insert({nums[i], 1});\n\n auto it = mp.end();\n it--;\n result.push_back(it->first);\n }\n\n return result;\n }\n};",
"memory": "177901"
} |
239 | <p>You are given an array of integers <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 max sliding window</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,-1,-3,5,3,6,7], k = 3
<strong>Output:</strong> [3,3,5,5,6,7]
<strong>Explanation:</strong>
Window position Max
--------------- -----
[1 3 -1] -3 5 3 6 7 <strong>3</strong>
1 [3 -1 -3] 5 3 6 7 <strong>3</strong>
1 3 [-1 -3 5] 3 6 7 <strong> 5</strong>
1 3 -1 [-3 5 3] 6 7 <strong>5</strong>
1 3 -1 -3 [5 3 6] 7 <strong>6</strong>
1 3 -1 -3 5 [3 6 7] <strong>7</strong>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1], k = 1
<strong>Output:</strong> [1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 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 < nums.size(); i++)\n{\n my_map[nums[i - k]]--;\n if (my_map[nums[i - k]] == 0)\n my_map.erase(nums[i - k]);\n my_map[nums[i]]++;\n vect.push_back(my_map.begin()->first);\n}\nreturn vect;\n\n }\n};",
"memory": "178993"
} |
239 | <p>You are given an array of integers <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 max sliding window</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,-1,-3,5,3,6,7], k = 3
<strong>Output:</strong> [3,3,5,5,6,7]
<strong>Explanation:</strong>
Window position Max
--------------- -----
[1 3 -1] -3 5 3 6 7 <strong>3</strong>
1 [3 -1 -3] 5 3 6 7 <strong>3</strong>
1 3 [-1 -3 5] 3 6 7 <strong> 5</strong>
1 3 -1 [-3 5 3] 6 7 <strong>5</strong>
1 3 -1 -3 [5 3 6] 7 <strong>6</strong>
1 3 -1 -3 5 [3 6 7] <strong>7</strong>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1], k = 1
<strong>Output:</strong> [1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 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 = m.rbegin();\n v.push_back(it->first);\n for(int i=k;i<nums.size();i++)\n {\n if(m[nums[i-k]]>1)\n {\n m[nums[i-k]]--; \n }\n else\n {\n m.erase(nums[i-k]);\n }\n if(m[nums[i]])\n m[nums[i]]++;\n else\n m[nums[i]]=1;\n auto it = m.rbegin();\n v.push_back(it->first);\n }\n return v;\n }\n};",
"memory": "178993"
} |
239 | <p>You are given an array of integers <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 max sliding window</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,-1,-3,5,3,6,7], k = 3
<strong>Output:</strong> [3,3,5,5,6,7]
<strong>Explanation:</strong>
Window position Max
--------------- -----
[1 3 -1] -3 5 3 6 7 <strong>3</strong>
1 [3 -1 -3] 5 3 6 7 <strong>3</strong>
1 3 [-1 -3 5] 3 6 7 <strong> 5</strong>
1 3 -1 [-3 5 3] 6 7 <strong>5</strong>
1 3 -1 -3 [5 3 6] 7 <strong>6</strong>
1 3 -1 -3 5 [3 6 7] <strong>7</strong>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1], k = 1
<strong>Output:</strong> [1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 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 // populate initial window\n for(std::size_t i=0; i < right; i++)\n {\n window_elem_map[nums[i]] += 1;\n }\n\n result.push_back(window_elem_map.begin()->first);\n right++;\n\n while(right <= nums.size()) {\n const int left = nums[right-k-1];\n\n std::size_t& count = window_elem_map[left];\n count -= 1;\n\n if(count == 0) {\n window_elem_map.erase(left);\n }\n\n window_elem_map[nums[right-1]] += 1;\n\n const int max_element = window_elem_map.begin()->first;\n result.push_back(max_element);\n\n right += 1;\n }\n\n\n return result;\n }\n};",
"memory": "180086"
} |
239 | <p>You are given an array of integers <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 max sliding window</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,-1,-3,5,3,6,7], k = 3
<strong>Output:</strong> [3,3,5,5,6,7]
<strong>Explanation:</strong>
Window position Max
--------------- -----
[1 3 -1] -3 5 3 6 7 <strong>3</strong>
1 [3 -1 -3] 5 3 6 7 <strong>3</strong>
1 3 [-1 -3 5] 3 6 7 <strong> 5</strong>
1 3 -1 [-3 5 3] 6 7 <strong>5</strong>
1 3 -1 -3 [5 3 6] 7 <strong>6</strong>
1 3 -1 -3 5 [3 6 7] <strong>7</strong>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1], k = 1
<strong>Output:</strong> [1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 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 }\n\n void removed(int v) {\n numsToRemove[v]--;\n if (numsToRemove[v] == 0) numsToRemove.erase(v);\n }\n\n vector<int> maxSlidingWindow(vector<int>& nums, int k) {\n for(int i=0;i<k;++i) heap.push(nums[i]);\n ans.push_back(heap.top());\n for(int i=k;i<nums.size();++i) {\n heap.push(nums[i]);\n toRemove(nums[i-k]);\n while(numsToRemove.find(heap.top()) != numsToRemove.end()) {\n removed(heap.top());\n heap.pop();\n }\n ans.push_back(heap.top());\n }\n return ans;\n }\n};",
"memory": "180086"
} |
239 | <p>You are given an array of integers <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 max sliding window</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,-1,-3,5,3,6,7], k = 3
<strong>Output:</strong> [3,3,5,5,6,7]
<strong>Explanation:</strong>
Window position Max
--------------- -----
[1 3 -1] -3 5 3 6 7 <strong>3</strong>
1 [3 -1 -3] 5 3 6 7 <strong>3</strong>
1 3 [-1 -3 5] 3 6 7 <strong> 5</strong>
1 3 -1 [-3 5 3] 6 7 <strong>5</strong>
1 3 -1 -3 [5 3 6] 7 <strong>6</strong>
1 3 -1 -3 5 [3 6 7] <strong>7</strong>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1], k = 1
<strong>Output:</strong> [1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 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 ans.push_back(pq.top());\n if(nums[i]==pq.top()){\n pq.pop();\n while(pq.size()&&st.count(pq.top())){\n st.erase(st.find(pq.top()));\n pq.pop();\n }\n }\n else st.insert(nums[i]);\n i++;\n }\n \n }\n return ans;\n }\n};",
"memory": "181178"
} |
239 | <p>You are given an array of integers <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 max sliding window</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,-1,-3,5,3,6,7], k = 3
<strong>Output:</strong> [3,3,5,5,6,7]
<strong>Explanation:</strong>
Window position Max
--------------- -----
[1 3 -1] -3 5 3 6 7 <strong>3</strong>
1 [3 -1 -3] 5 3 6 7 <strong>3</strong>
1 3 [-1 -3 5] 3 6 7 <strong> 5</strong>
1 3 -1 [-3 5 3] 6 7 <strong>5</strong>
1 3 -1 -3 [5 3 6] 7 <strong>6</strong>
1 3 -1 -3 5 [3 6 7] <strong>7</strong>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1], k = 1
<strong>Output:</strong> [1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 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 }\n vi ans;\n ans.push_back(*--s.end());\n for(int i=k;i<n;i++){\n int next=a[i];\n int prev=a[i-k];\n s.insert(next);\n if(next >= 0) f[next]++;\n else f[10000-next]++;\n if(prev >= 0) f[prev]--;\n else f[10000-prev]--;\n if(prev >= 0) {\n if(f[prev]==0) s.erase(prev);\n }\n else {\n if(f[10000-prev]==0) s.erase(prev);\n }\n ans.push_back(*--s.end());\n }\n return ans;\n}\n};",
"memory": "181178"
} |
239 | <p>You are given an array of integers <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 max sliding window</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,-1,-3,5,3,6,7], k = 3
<strong>Output:</strong> [3,3,5,5,6,7]
<strong>Explanation:</strong>
Window position Max
--------------- -----
[1 3 -1] -3 5 3 6 7 <strong>3</strong>
1 [3 -1 -3] 5 3 6 7 <strong>3</strong>
1 3 [-1 -3 5] 3 6 7 <strong> 5</strong>
1 3 -1 [-3 5 3] 6 7 <strong>5</strong>
1 3 -1 -3 [5 3 6] 7 <strong>6</strong>
1 3 -1 -3 5 [3 6 7] <strong>7</strong>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1], k = 1
<strong>Output:</strong> [1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 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 }\n ans = max(ans,(--mp.end())->first);\n f.push_back(ans);\n for(int i = k;i<n;i++){\n mp[v[i-k]]--;\n mp[v[i]]++;\n if(mp[v[i-k]]==0){\n mp.erase(v[i-k]);\n }\n f.push_back((--mp.end())->first);\n }\n return f;\n }\n};",
"memory": "182271"
} |
239 | <p>You are given an array of integers <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 max sliding window</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,-1,-3,5,3,6,7], k = 3
<strong>Output:</strong> [3,3,5,5,6,7]
<strong>Explanation:</strong>
Window position Max
--------------- -----
[1 3 -1] -3 5 3 6 7 <strong>3</strong>
1 [3 -1 -3] 5 3 6 7 <strong>3</strong>
1 3 [-1 -3 5] 3 6 7 <strong> 5</strong>
1 3 -1 [-3 5 3] 6 7 <strong>5</strong>
1 3 -1 -3 [5 3 6] 7 <strong>6</strong>
1 3 -1 -3 5 [3 6 7] <strong>7</strong>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1], k = 1
<strong>Output:</strong> [1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 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.rbegin()->first);\n for(int i = k ; i<nums.size() ; i++)\n {\n q.push(nums[i]);;\n m[q.front()]--;\n if(m[q.front()]==0) m.erase(q.front());\n q.pop();\n m[nums[i]]++;\n res.emplace_back(m.rbegin()->first);\n }\n return res;\n }\n};",
"memory": "183363"
} |
239 | <p>You are given an array of integers <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 max sliding window</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,-1,-3,5,3,6,7], k = 3
<strong>Output:</strong> [3,3,5,5,6,7]
<strong>Explanation:</strong>
Window position Max
--------------- -----
[1 3 -1] -3 5 3 6 7 <strong>3</strong>
1 [3 -1 -3] 5 3 6 7 <strong>3</strong>
1 3 [-1 -3 5] 3 6 7 <strong> 5</strong>
1 3 -1 [-3 5 3] 6 7 <strong>5</strong>
1 3 -1 -3 [5 3 6] 7 <strong>6</strong>
1 3 -1 -3 5 [3 6 7] <strong>7</strong>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1], k = 1
<strong>Output:</strong> [1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 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, 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_back(m.rbegin()->first);\n for(int i = k ; i<nums.size() ; i++)\n {\n q.push_back(nums[i]);\n m[q.front()]--;\n if(m[q.front()]==0) m.erase(q.front());\n q.pop_front();\n m[nums[i]]++;\n res.emplace_back(m.rbegin()->first);\n }\n return res;\n }\n};",
"memory": "183363"
} |
239 | <p>You are given an array of integers <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 max sliding window</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,-1,-3,5,3,6,7], k = 3
<strong>Output:</strong> [3,3,5,5,6,7]
<strong>Explanation:</strong>
Window position Max
--------------- -----
[1 3 -1] -3 5 3 6 7 <strong>3</strong>
1 [3 -1 -3] 5 3 6 7 <strong>3</strong>
1 3 [-1 -3 5] 3 6 7 <strong> 5</strong>
1 3 -1 [-3 5 3] 6 7 <strong>5</strong>
1 3 -1 -3 [5 3 6] 7 <strong>6</strong>
1 3 -1 -3 5 [3 6 7] <strong>7</strong>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1], k = 1
<strong>Output:</strong> [1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 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, 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_back(m.rbegin()->first);\n for(int i = k ; i<nums.size() ; i++)\n {\n q.push_back(nums[i]);\n m[q.front()]--;\n if(m[q.front()]==0) m.erase(q.front());\n q.pop_front();\n m[nums[i]]++;\n res.emplace_back(m.rbegin()->first);\n }\n return res;\n }\n};",
"memory": "184456"
} |
239 | <p>You are given an array of integers <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 max sliding window</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,-1,-3,5,3,6,7], k = 3
<strong>Output:</strong> [3,3,5,5,6,7]
<strong>Explanation:</strong>
Window position Max
--------------- -----
[1 3 -1] -3 5 3 6 7 <strong>3</strong>
1 [3 -1 -3] 5 3 6 7 <strong>3</strong>
1 3 [-1 -3 5] 3 6 7 <strong> 5</strong>
1 3 -1 [-3 5 3] 6 7 <strong>5</strong>
1 3 -1 -3 [5 3 6] 7 <strong>6</strong>
1 3 -1 -3 5 [3 6 7] <strong>7</strong>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1], k = 1
<strong>Output:</strong> [1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 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_back(m.rbegin()->first);\n for(int i = k ; i<nums.size() ; i++)\n {\n q.push_back(nums[i]);\n m[q.front()]--;\n if(m[q.front()]==0) m.erase(q.front());\n q.pop_front();\n m[nums[i]]++;\n res.emplace_back(m.rbegin()->first);\n }\n return res;\n }\n};",
"memory": "185548"
} |
239 | <p>You are given an array of integers <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 max sliding window</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,-1,-3,5,3,6,7], k = 3
<strong>Output:</strong> [3,3,5,5,6,7]
<strong>Explanation:</strong>
Window position Max
--------------- -----
[1 3 -1] -3 5 3 6 7 <strong>3</strong>
1 [3 -1 -3] 5 3 6 7 <strong>3</strong>
1 3 [-1 -3 5] 3 6 7 <strong> 5</strong>
1 3 -1 [-3 5 3] 6 7 <strong>5</strong>
1 3 -1 -3 [5 3 6] 7 <strong>6</strong>
1 3 -1 -3 5 [3 6 7] <strong>7</strong>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1], k = 1
<strong>Output:</strong> [1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 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.insert(nums[r]); mp[nums[r]]++;\n // cout<<maxi<<endl;\n }\n // for(auto it:st){cout<<it<<\" \";}\n // cout<<endl;\n int l=0,r=k-1;\n // cout<<l<<\" \"<<r<<endl;\n while(r<n)\n {\n // cout<<l<<\" \"<<r<<\" \"<<ans.size()<<endl;\n ans.push_back(maxi);\n mp[nums[l]]--; if(mp[nums[l]]==0){mp.erase(nums[l]);}\n // if(maxi==nums[l]){\n // if(mp[nums[l]]==0){\n // if(!st.empty())\n // {auto it=st.end();\n // it--;\n // maxi=*it;}\n // else{maxi=-1e5;}\n // }\n \n // }\n if(mp.empty()){maxi=-1e5;}\n // for(auto it:st){cout<<it<<\" st \";}\n // cout<<endl;\n // cout<<maxi<<\" \"<<endl;\n l++; r++;\n if(r<n){\n // maxi=max(maxi,nums[r]);\n mp[nums[r]]++;\n auto it=mp.end();\n it--;\n maxi=it->first;\n }\n // cout<<maxi<<\" \"<<endl;\n \n }\n return ans;\n }\n};",
"memory": "189918"
} |
239 | <p>You are given an array of integers <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 max sliding window</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,-1,-3,5,3,6,7], k = 3
<strong>Output:</strong> [3,3,5,5,6,7]
<strong>Explanation:</strong>
Window position Max
--------------- -----
[1 3 -1] -3 5 3 6 7 <strong>3</strong>
1 [3 -1 -3] 5 3 6 7 <strong>3</strong>
1 3 [-1 -3 5] 3 6 7 <strong> 5</strong>
1 3 -1 [-3 5 3] 6 7 <strong>5</strong>
1 3 -1 -3 [5 3 6] 7 <strong>6</strong>
1 3 -1 -3 5 [3 6 7] <strong>7</strong>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1], k = 1
<strong>Output:</strong> [1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 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.find(nums[i-k]);\n // s1.erase(itr) ;\n // s1.insert(nums[i]) ;\n // v1.push_back(*s1.rbegin()) ;\n\n // }\n priority_queue<int> pq(nums.begin(), nums.begin() + k ); \n unordered_multiset s1(nums.begin() , nums.begin() + k) ;\n v1.push_back(pq.top()) ;\n for ( int i = k ; i<nums.size() ; i++) { \n if ( nums[i-k] == pq.top() ) { pq.pop() ; }\n auto it = s1.find(nums[i-k]) ;\n s1.erase(it) ;\n pq.push(nums[i]) ;\n s1.insert(nums[i]) ;\n while ( s1.find(pq.top()) == s1.end()) { \n pq.pop() ;\n }\n v1.push_back(pq.top()) ;\n }\n return v1 ;\n\n return v1 ;\n \n }\n\n \n};",
"memory": "189918"
} |
239 | <p>You are given an array of integers <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 max sliding window</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,-1,-3,5,3,6,7], k = 3
<strong>Output:</strong> [3,3,5,5,6,7]
<strong>Explanation:</strong>
Window position Max
--------------- -----
[1 3 -1] -3 5 3 6 7 <strong>3</strong>
1 [3 -1 -3] 5 3 6 7 <strong>3</strong>
1 3 [-1 -3 5] 3 6 7 <strong> 5</strong>
1 3 -1 [-3 5 3] 6 7 <strong>5</strong>
1 3 -1 -3 [5 3 6] 7 <strong>6</strong>
1 3 -1 -3 5 [3 6 7] <strong>7</strong>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1], k = 1
<strong>Output:</strong> [1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 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 ();\n }\n }\n\n int maxv () {\n return data.back ();\n }\n};\n\nclass Solution {\npublic:\n vector<int> maxSlidingWindow(vector<int>& nums, int k) {\n MonoQueue* window = new MonoQueue ();\n vector<int> res;\n res.resize (nums.size () - k + 1);\n\n for (int i = 0; i < nums.size (); i++) {\n if (i < k - 1) {\n window->push (nums [i]);\n continue;\n }\n\n window->push (nums [i]);\n res [i - k + 1] = window->maxv ();\n window->pop (nums [i - k + 1]);\n }\n\n return res; \n }\n};",
"memory": "191011"
} |
239 | <p>You are given an array of integers <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 max sliding window</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,-1,-3,5,3,6,7], k = 3
<strong>Output:</strong> [3,3,5,5,6,7]
<strong>Explanation:</strong>
Window position Max
--------------- -----
[1 3 -1] -3 5 3 6 7 <strong>3</strong>
1 [3 -1 -3] 5 3 6 7 <strong>3</strong>
1 3 [-1 -3 5] 3 6 7 <strong> 5</strong>
1 3 -1 [-3 5 3] 6 7 <strong>5</strong>
1 3 -1 -3 [5 3 6] 7 <strong>6</strong>
1 3 -1 -3 5 [3 6 7] <strong>7</strong>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1], k = 1
<strong>Output:</strong> [1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 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 ();\n }\n }\n\n int maxv () {\n return data.back ();\n }\n};\n\nclass Solution {\npublic:\n vector<int> maxSlidingWindow(vector<int>& nums, int k) {\n MonoQueue* window = new MonoQueue ();\n vector<int> res;\n res.resize (nums.size () - k + 1);\n\n for (int i = 0; i < nums.size (); i++) {\n if (i < k - 1) {\n window->push (nums [i]);\n continue;\n }\n\n window->push (nums [i]);\n res [i - k + 1] = window->maxv ();\n window->pop (nums [i - k + 1]);\n }\n\n return res; \n }\n};",
"memory": "191011"
} |
239 | <p>You are given an array of integers <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 max sliding window</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,-1,-3,5,3,6,7], k = 3
<strong>Output:</strong> [3,3,5,5,6,7]
<strong>Explanation:</strong>
Window position Max
--------------- -----
[1 3 -1] -3 5 3 6 7 <strong>3</strong>
1 [3 -1 -3] 5 3 6 7 <strong>3</strong>
1 3 [-1 -3 5] 3 6 7 <strong> 5</strong>
1 3 -1 [-3 5 3] 6 7 <strong>5</strong>
1 3 -1 -3 [5 3 6] 7 <strong>6</strong>
1 3 -1 -3 5 [3 6 7] <strong>7</strong>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1], k = 1
<strong>Output:</strong> [1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 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 int n = nums.size();\n vector<int> ans(n-k+1,0);\n list<int> list;\n int i,j,l;\n i=0;\n j=0;\n l=0;\n while( i < n)\n {\n while(list.size() > 0 && nums[i] > list.back())\n {\n list.pop_back();\n } \n list.push_back(nums[i]);\n if(i - j + 1 == k)\n { \n ans[l++]=list.front();\n if(nums[j] == list.front())\n { \n list.pop_front();\n }\n j++;\n }\n i++;\n } \n return ans;\n }\n};",
"memory": "192103"
} |
239 | <p>You are given an array of integers <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 max sliding window</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,-1,-3,5,3,6,7], k = 3
<strong>Output:</strong> [3,3,5,5,6,7]
<strong>Explanation:</strong>
Window position Max
--------------- -----
[1 3 -1] -3 5 3 6 7 <strong>3</strong>
1 [3 -1 -3] 5 3 6 7 <strong>3</strong>
1 3 [-1 -3 5] 3 6 7 <strong> 5</strong>
1 3 -1 [-3 5 3] 6 7 <strong>5</strong>
1 3 -1 -3 [5 3 6] 7 <strong>6</strong>
1 3 -1 -3 5 [3 6 7] <strong>7</strong>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1], k = 1
<strong>Output:</strong> [1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 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 }\n vector<int> ans(n-k+1);\n ans[0] = maxi;\n for(int i=k;i<n;i++){\n mp[nums[i-k]]--;\n if(mp[nums[i-k]] == 0) s.erase(nums[i-k]);\n mp[nums[i]]++;\n s.insert(nums[i]);\n ans[i-k+1] = *s.rbegin();\n }\n return ans;\n }\n};",
"memory": "192103"
} |
239 | <p>You are given an array of integers <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 max sliding window</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,-1,-3,5,3,6,7], k = 3
<strong>Output:</strong> [3,3,5,5,6,7]
<strong>Explanation:</strong>
Window position Max
--------------- -----
[1 3 -1] -3 5 3 6 7 <strong>3</strong>
1 [3 -1 -3] 5 3 6 7 <strong>3</strong>
1 3 [-1 -3 5] 3 6 7 <strong> 5</strong>
1 3 -1 [-3 5 3] 6 7 <strong>5</strong>
1 3 -1 -3 [5 3 6] 7 <strong>6</strong>
1 3 -1 -3 5 [3 6 7] <strong>7</strong>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1], k = 1
<strong>Output:</strong> [1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 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;\n ans.push_back(*st.rbegin());\n\n for(int i=k;i<n;i++){\n freq[ nums[i-k] ]--;\n if(freq[nums[i-k]] == 0){\n st.erase(nums[i-k]);\n }\n\n freq[nums[i]]++;\n st.insert(nums[i]);\n\n int val = *st.rbegin();\n ans.push_back(val);\n }\n return ans;\n }\n};",
"memory": "193196"
} |
239 | <p>You are given an array of integers <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 max sliding window</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,-1,-3,5,3,6,7], k = 3
<strong>Output:</strong> [3,3,5,5,6,7]
<strong>Explanation:</strong>
Window position Max
--------------- -----
[1 3 -1] -3 5 3 6 7 <strong>3</strong>
1 [3 -1 -3] 5 3 6 7 <strong>3</strong>
1 3 [-1 -3 5] 3 6 7 <strong> 5</strong>
1 3 -1 [-3 5 3] 6 7 <strong>5</strong>
1 3 -1 -3 [5 3 6] 7 <strong>6</strong>
1 3 -1 -3 5 [3 6 7] <strong>7</strong>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1], k = 1
<strong>Output:</strong> [1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 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(recent.contains(nums[i])){\n map[nums[i]]++;\n }\n else{\n recent.insert(nums[i]);\n map[nums[i]] = 1;\n }\n \n }\n out.push_back(maxi);\n for(int i = k; i < nums.size(); i++){\n map[nums[i-k]]--;\n if(map[nums[i-k]] == 0){\n recent.erase(nums[i-k]);\n }\n if(recent.contains(nums[i])){\n map[nums[i]]++;\n }\n else{\n recent.insert(nums[i]);\n map[nums[i]] = 1;\n }\n out.push_back(*recent.rbegin());\n }\n return out;\n }\n};",
"memory": "193196"
} |
239 | <p>You are given an array of integers <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 max sliding window</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,-1,-3,5,3,6,7], k = 3
<strong>Output:</strong> [3,3,5,5,6,7]
<strong>Explanation:</strong>
Window position Max
--------------- -----
[1 3 -1] -3 5 3 6 7 <strong>3</strong>
1 [3 -1 -3] 5 3 6 7 <strong>3</strong>
1 3 [-1 -3 5] 3 6 7 <strong> 5</strong>
1 3 -1 [-3 5 3] 6 7 <strong>5</strong>
1 3 -1 -3 [5 3 6] 7 <strong>6</strong>
1 3 -1 -3 5 [3 6 7] <strong>7</strong>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1], k = 1
<strong>Output:</strong> [1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 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()));\n int i=0;\n for(int j=k;j<nums.size();j++){\n m[nums[i]]--;\n if(m[nums[i]]==0){\n s.erase(nums[i]);\n }\n m[nums[j]]++;\n s.insert(nums[j]);\n ans.push_back(*(s.rbegin()));\n i++;\n }\n return ans;\n }\n};",
"memory": "194288"
} |
239 | <p>You are given an array of integers <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 max sliding window</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,-1,-3,5,3,6,7], k = 3
<strong>Output:</strong> [3,3,5,5,6,7]
<strong>Explanation:</strong>
Window position Max
--------------- -----
[1 3 -1] -3 5 3 6 7 <strong>3</strong>
1 [3 -1 -3] 5 3 6 7 <strong>3</strong>
1 3 [-1 -3 5] 3 6 7 <strong> 5</strong>
1 3 -1 [-3 5 3] 6 7 <strong>5</strong>
1 3 -1 -3 [5 3 6] 7 <strong>6</strong>
1 3 -1 -3 5 [3 6 7] <strong>7</strong>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1], k = 1
<strong>Output:</strong> [1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 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 }\n li.emplace_back(make_pair(nums[i], i));\n }\n ret.push_back(li.front().first);\n\n for (; i < nums.size(); ++i) {\n if (li.front().second < i - k + 1)\n li.pop_front();\n\n while (li.size() && nums[i] >= li.back().first) {\n li.pop_back();\n }\n li.emplace_back(make_pair(nums[i], i));\n\n ret.push_back(li.front().first);\n }\n\n return ret;\n }\n};",
"memory": "194288"
} |
239 | <p>You are given an array of integers <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 max sliding window</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,-1,-3,5,3,6,7], k = 3
<strong>Output:</strong> [3,3,5,5,6,7]
<strong>Explanation:</strong>
Window position Max
--------------- -----
[1 3 -1] -3 5 3 6 7 <strong>3</strong>
1 [3 -1 -3] 5 3 6 7 <strong>3</strong>
1 3 [-1 -3 5] 3 6 7 <strong> 5</strong>
1 3 -1 [-3 5 3] 6 7 <strong>5</strong>
1 3 -1 -3 [5 3 6] 7 <strong>6</strong>
1 3 -1 -3 5 [3 6 7] <strong>7</strong>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1], k = 1
<strong>Output:</strong> [1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 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 = (pos - 1) / 2;\n if (heap[parent].value >= heap[pos].value)\n {\n break;\n }\n\n // Swap pos and parent.\n this->swap(pos, parent);\n pos = parent;\n }\n }\n\n void bubbleDown(int pos)\n {\n while (true)\n {\n int left = pos * 2 + 1;\n int right = pos * 2 + 2;\n if (left >= this->heap.size())\n {\n return;\n }\n\n if (this->heap[pos].value < this->heap[left].value && (right >= heap.size() || heap[right].value <= heap[left].value))\n {\n // Go left.\n this->swap(left, pos);\n pos = left;\n }\n else if (right < heap.size() && heap[right].value > heap[pos].value)\n {\n this->swap(right, pos);\n pos = right;\n }\n else\n {\n break;\n }\n }\n }\n\n void swap(int pos1, int pos2)\n {\n Pair p = Pair(heap[pos1].value, heap[pos1].index);\n heap[pos1] = heap[pos2];\n heap[pos2] = p;\n\n this->indexes[heap[pos1].index] = pos1;\n this->indexes[heap[pos2].index] = pos2;\n }\n\npublic:\n void insert(int value, int index)\n {\n Pair p(value, index);\n heap.push_back(p);\n indexes[index] = heap.size() - 1;\n\n this->bubbleUp(heap.size() - 1);\n }\n\n int getMax()\n {\n return heap[0].value;\n }\n\n void removeIndex(int index)\n {\n if (!indexes.count(index))\n {\n throw std::invalid_argument(\"Cannot find index\");\n }\n\n int pos = indexes[index];\n // Swap this and the last element.\n // cout << \"swap 111111 \" << pos << \" \" << heap.size() << \" \" << indexes.size() << endl;\n this->swap(pos, heap.size() - 1);\n // Remove the last element.\n this->heap.pop_back();\n this->indexes.erase(index);\n\n if (heap.size() == 0)\n {\n return;\n }\n\n // Check if we can bubble up at position\n int parent = (pos - 1) / 2;\n if (pos > 0 && heap[parent].value < heap[pos].value)\n {\n // bubble up\n this->bubbleUp(pos);\n }\n else\n {\n // bubble down.\n this->bubbleDown(pos);\n }\n }\n\n void print()\n {\n for (auto x : this->heap)\n {\n cout << x.value << \" \";\n }\n cout << endl\n << endl;\n }\n};\n\nclass Solution\n{\npublic:\n vector<int> maxSlidingWindow(vector<int> &nums, int k)\n {\n Heap heap;\n vector<int> result;\n for (int i = 0; i < k; i++)\n {\n heap.insert(nums[i], i);\n }\n result.push_back(heap.getMax());\n\n for (int i = k; i < nums.size(); i++)\n {\n heap.removeIndex(i - k);\n heap.insert(nums[i], i);\n result.push_back(heap.getMax());\n }\n\n return result;\n }\n};",
"memory": "195381"
} |
239 | <p>You are given an array of integers <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 max sliding window</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,-1,-3,5,3,6,7], k = 3
<strong>Output:</strong> [3,3,5,5,6,7]
<strong>Explanation:</strong>
Window position Max
--------------- -----
[1 3 -1] -3 5 3 6 7 <strong>3</strong>
1 [3 -1 -3] 5 3 6 7 <strong>3</strong>
1 3 [-1 -3 5] 3 6 7 <strong> 5</strong>
1 3 -1 [-3 5 3] 6 7 <strong>5</strong>
1 3 -1 -3 [5 3 6] 7 <strong>6</strong>
1 3 -1 -3 5 [3 6 7] <strong>7</strong>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1], k = 1
<strong>Output:</strong> [1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 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, heap[pos1].index});\n heap[pos1] = heap[pos2];\n heap[pos2] = p;\n\n // Update the map\n this->indexes[heap[pos1].index] = pos1;\n this->indexes[heap[pos2].index] = pos2;\n }\n\n void bubbleUp(int pos)\n {\n while (pos > 0)\n {\n int parent = (pos - 1) / 2;\n if (heap[parent].value >= heap[pos].value)\n {\n break;\n }\n\n this->swapHeapNode(pos, parent);\n pos = parent;\n }\n }\n\n void bubbleDown(int pos)\n {\n // Buble down the position.\n while (true)\n {\n int left = pos * 2 + 1;\n int right = pos * 2 + 2;\n if (left >= heap.size())\n {\n break;\n }\n\n if (heap[left].value > heap[pos].value && ((right < heap.size() && heap[left].value >= heap[right].value) || right >= heap.size()))\n {\n this->swapHeapNode(left, pos);\n pos = left;\n }\n else if (right < heap.size() && heap[right].value > heap[pos].value)\n {\n this->swapHeapNode(right, pos);\n pos = right;\n }\n else\n {\n break;\n }\n }\n }\n\n void insert(int value, int index)\n {\n heap.push_back(Pair{value, index});\n this->indexes[index] = heap.size() - 1;\n\n int pos = heap.size() - 1;\n this->bubbleUp(pos);\n }\n\n int getMax()\n {\n return this->heap[0].value;\n }\n\n void removeIndex(int index)\n {\n if (!this->indexes.count(index))\n {\n throw std::invalid_argument(\"Cannot find index\");\n }\n\n // position in the heap\n int pos = this->indexes[index];\n\n // if (index == 75)\n // {\n // cout << \"AAAAA \" << pos << \" \" << heap[pos].value << endl;\n // this->print();\n // }\n\n // swap this position with the last element.\n this->swapHeapNode(pos, this->heap.size() - 1);\n\n // Remove the last element.\n this->indexes.erase(heap[heap.size() - 1].index);\n heap.pop_back();\n if (heap.size() == 0)\n {\n return;\n }\n\n // if (index == 75)\n // {\n // cout << \"AAAAA \" << pos << \" \" << heap[pos].value << endl;\n // this->print();\n // }\n\n int parent = (pos - 1) / 2;\n if (pos > 0 && heap[parent].value < heap[pos].value)\n {\n // bubble up.\n this->bubbleUp(pos);\n }\n else\n {\n this->bubbleDown(pos);\n }\n }\n\n void print()\n {\n for (auto x : this->heap)\n {\n cout << x.value << \" \";\n }\n cout << endl\n << endl;\n }\n};\n\nclass Solution\n{\npublic:\n vector<int> maxSlidingWindow(vector<int> &nums, int k)\n {\n Heap heap;\n for (int i = 0; i < k; i++)\n {\n heap.insert(nums[i], i);\n }\n\n vector<int> result;\n result.push_back(heap.getMax());\n\n for (int i = k; i < nums.size(); i++)\n {\n // int t = 81;\n // if (nums[i - k] == 7954)\n // {\n // cout << \"BBBBB \" << i << endl;\n // }\n // if (i == t)\n // {\n // heap.print();\n // }\n heap.removeIndex(i - k);\n // if (i == t)\n // {\n // heap.print();\n // }\n\n heap.insert(nums[i], i);\n result.push_back(heap.getMax());\n\n // if (i == t)\n // {\n // heap.print();\n // }\n }\n\n return result;\n }\n};",
"memory": "195381"
} |
239 | <p>You are given an array of integers <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 max sliding window</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,-1,-3,5,3,6,7], k = 3
<strong>Output:</strong> [3,3,5,5,6,7]
<strong>Explanation:</strong>
Window position Max
--------------- -----
[1 3 -1] -3 5 3 6 7 <strong>3</strong>
1 [3 -1 -3] 5 3 6 7 <strong>3</strong>
1 3 [-1 -3 5] 3 6 7 <strong> 5</strong>
1 3 -1 [-3 5 3] 6 7 <strong>5</strong>
1 3 -1 -3 [5 3 6] 7 <strong>6</strong>
1 3 -1 -3 5 [3 6 7] <strong>7</strong>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1], k = 1
<strong>Output:</strong> [1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 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 current element\n while(temp.size()!=0 && temp.back()<arr[j]){\n temp.pop_back();\n }\n temp.push_back(arr[j]);\n \n //if(j-i+1<k)\n // {\n //j++;\n // }\n \n if(j-i+1==k)\n {\n ans.push_back(temp.front());\n if(temp.front() == arr[i]) temp.pop_front(); //remove element abount to be out of window\n i++;\n //j++;\n }\n j++;\n }\n return ans;\n }\n};",
"memory": "196473"
} |
239 | <p>You are given an array of integers <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 max sliding window</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,-1,-3,5,3,6,7], k = 3
<strong>Output:</strong> [3,3,5,5,6,7]
<strong>Explanation:</strong>
Window position Max
--------------- -----
[1 3 -1] -3 5 3 6 7 <strong>3</strong>
1 [3 -1 -3] 5 3 6 7 <strong>3</strong>
1 3 [-1 -3 5] 3 6 7 <strong> 5</strong>
1 3 -1 [-3 5 3] 6 7 <strong>5</strong>
1 3 -1 -3 [5 3 6] 7 <strong>6</strong>
1 3 -1 -3 5 [3 6 7] <strong>7</strong>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1], k = 1
<strong>Output:</strong> [1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 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.pop_back();\n l.push_back(nums[j]);\n }else l.push_front(nums[j]);\n\n if(j-i+1==k){\n if(!l.empty()){\n int ele = l.front();\n ans.push_back(ele);\n if(nums[i]==ele) l.pop_front();\n }\n i++;\n }\n j++;\n }\n return ans;\n\n // deque<int> dq;\n // vector<int> ans;\n\n // for (int i = 0; i < nums.size(); i++) {\n // if (!dq.empty() && dq.front() == (i - k)) {\n // dq.pop_front();\n // }\n // while (!dq.empty() && nums[dq.back()] < nums[i]) {\n // dq.pop_back();\n // }\n // dq.push_back(i);\n // if (i >= (k - 1)) {\n // ans.push_back(nums[dq.front()]);\n // }\n // }\n return ans;\n }\n};",
"memory": "196473"
} |
239 | <p>You are given an array of integers <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 max sliding window</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,-1,-3,5,3,6,7], k = 3
<strong>Output:</strong> [3,3,5,5,6,7]
<strong>Explanation:</strong>
Window position Max
--------------- -----
[1 3 -1] -3 5 3 6 7 <strong>3</strong>
1 [3 -1 -3] 5 3 6 7 <strong>3</strong>
1 3 [-1 -3 5] 3 6 7 <strong> 5</strong>
1 3 -1 [-3 5 3] 6 7 <strong>5</strong>
1 3 -1 -3 [5 3 6] 7 <strong>6</strong>
1 3 -1 -3 5 [3 6 7] <strong>7</strong>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1], k = 1
<strong>Output:</strong> [1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 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();\n }\n\n while (q.size() > 0 && i - q.front()[1] + 1 > k)\n {\n q.pop_front();\n }\n\n q.push_back({nums[i], i});\n\n if (i >= k - 1)\n ret.push_back(q.front()[0]);\n }\n\n return ret;\n }\n};",
"memory": "197566"
} |
239 | <p>You are given an array of integers <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 max sliding window</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,-1,-3,5,3,6,7], k = 3
<strong>Output:</strong> [3,3,5,5,6,7]
<strong>Explanation:</strong>
Window position Max
--------------- -----
[1 3 -1] -3 5 3 6 7 <strong>3</strong>
1 [3 -1 -3] 5 3 6 7 <strong>3</strong>
1 3 [-1 -3 5] 3 6 7 <strong> 5</strong>
1 3 -1 [-3 5 3] 6 7 <strong>5</strong>
1 3 -1 -3 [5 3 6] 7 <strong>6</strong>
1 3 -1 -3 5 [3 6 7] <strong>7</strong>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1], k = 1
<strong>Output:</strong> [1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 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=k;i<nums.size();++i) {\n memo.extract(nums[last++]);\n pq.push(nums[i]);\n memo.insert(nums[i]);\n while(memo.count(pq.top())==0) pq.pop();\n ans.push_back(pq.top());\n }\n return ans;\n }\n};",
"memory": "197566"
} |
239 | <p>You are given an array of integers <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 max sliding window</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,-1,-3,5,3,6,7], k = 3
<strong>Output:</strong> [3,3,5,5,6,7]
<strong>Explanation:</strong>
Window position Max
--------------- -----
[1 3 -1] -3 5 3 6 7 <strong>3</strong>
1 [3 -1 -3] 5 3 6 7 <strong>3</strong>
1 3 [-1 -3 5] 3 6 7 <strong> 5</strong>
1 3 -1 [-3 5 3] 6 7 <strong>5</strong>
1 3 -1 -3 [5 3 6] 7 <strong>6</strong>
1 3 -1 -3 5 [3 6 7] <strong>7</strong>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1], k = 1
<strong>Output:</strong> [1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 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) {\n int x = nums[i];\n\n while (!dec.empty() && dec[0][1] <= i-k) {\n dec.pop_front();\n }\n\n while (!dec.empty() && dec.back()[0] <= x) {\n dec.pop_back();\n }\n\n dec.push_back({x, i});\n\n if (i >= k-1) {\n ans.push_back(dec.front()[0]);\n }\n }\n\n return ans;\n }\n};",
"memory": "198658"
} |
239 | <p>You are given an array of integers <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 max sliding window</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,-1,-3,5,3,6,7], k = 3
<strong>Output:</strong> [3,3,5,5,6,7]
<strong>Explanation:</strong>
Window position Max
--------------- -----
[1 3 -1] -3 5 3 6 7 <strong>3</strong>
1 [3 -1 -3] 5 3 6 7 <strong>3</strong>
1 3 [-1 -3 5] 3 6 7 <strong> 5</strong>
1 3 -1 [-3 5 3] 6 7 <strong>5</strong>
1 3 -1 -3 [5 3 6] 7 <strong>6</strong>
1 3 -1 -3 5 [3 6 7] <strong>7</strong>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1], k = 1
<strong>Output:</strong> [1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 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) {\n int x = nums[i];\n\n while (!dec.empty() && dec.front()[1] <= i-k) {\n dec.pop_front();\n }\n\n while (!dec.empty() && dec.back()[0] <= x) {\n dec.pop_back();\n }\n\n dec.push_back({x, i});\n\n if (i >= k-1) {\n ans.push_back(dec.front()[0]);\n }\n }\n\n return ans;\n }\n};",
"memory": "198658"
} |
239 | <p>You are given an array of integers <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 max sliding window</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,-1,-3,5,3,6,7], k = 3
<strong>Output:</strong> [3,3,5,5,6,7]
<strong>Explanation:</strong>
Window position Max
--------------- -----
[1 3 -1] -3 5 3 6 7 <strong>3</strong>
1 [3 -1 -3] 5 3 6 7 <strong>3</strong>
1 3 [-1 -3 5] 3 6 7 <strong> 5</strong>
1 3 -1 [-3 5 3] 6 7 <strong>5</strong>
1 3 -1 -3 [5 3 6] 7 <strong>6</strong>
1 3 -1 -3 5 [3 6 7] <strong>7</strong>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1], k = 1
<strong>Output:</strong> [1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 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 pq.push(nums[i]);\n s.insert(nums[i-k]);\n while(s.find(pq.top())!=s.end()){\n s.erase(s.find(pq.top()));\n pq.pop(); \n }\n res.push_back(pq.top());\n }\n return res;\n }\n};",
"memory": "199751"
} |
239 | <p>You are given an array of integers <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 max sliding window</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,-1,-3,5,3,6,7], k = 3
<strong>Output:</strong> [3,3,5,5,6,7]
<strong>Explanation:</strong>
Window position Max
--------------- -----
[1 3 -1] -3 5 3 6 7 <strong>3</strong>
1 [3 -1 -3] 5 3 6 7 <strong>3</strong>
1 3 [-1 -3 5] 3 6 7 <strong> 5</strong>
1 3 -1 [-3 5 3] 6 7 <strong>5</strong>
1 3 -1 -3 [5 3 6] 7 <strong>6</strong>
1 3 -1 -3 5 [3 6 7] <strong>7</strong>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1], k = 1
<strong>Output:</strong> [1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 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 st.insert(nums[j]); \n mp[nums[j]]++;\n answer = max(answer, nums[j]);\n j++;\n }\n else{\n ans.push_back(answer);\n mp[nums[i]]--;\n if(mp[nums[i]] == 0){\n st.erase(nums[i]);\n }\n mp[nums[j]]++;\n st.insert(nums[j]);\n auto it = st.begin();\n answer = *it;\n i++, j++;\n }\n }\n ans.push_back(answer);\n return ans;\n }\n};",
"memory": "199751"
} |
239 | <p>You are given an array of integers <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 max sliding window</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,-1,-3,5,3,6,7], k = 3
<strong>Output:</strong> [3,3,5,5,6,7]
<strong>Explanation:</strong>
Window position Max
--------------- -----
[1 3 -1] -3 5 3 6 7 <strong>3</strong>
1 [3 -1 -3] 5 3 6 7 <strong>3</strong>
1 3 [-1 -3 5] 3 6 7 <strong> 5</strong>
1 3 -1 [-3 5 3] 6 7 <strong>5</strong>
1 3 -1 -3 [5 3 6] 7 <strong>6</strong>
1 3 -1 -3 5 [3 6 7] <strong>7</strong>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1], k = 1
<strong>Output:</strong> [1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 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 window.pop_front();\n }\n while(window.size() > 0 && window.back()[0] < nums[i]) {\n window.pop_back();\n }\n window.push_back({nums[i], i});\n if(i >= k-1) {\n ans.push_back(window.front()[0]);\n }\n }\n return ans;\n }\n};",
"memory": "200843"
} |
239 | <p>You are given an array of integers <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 max sliding window</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,-1,-3,5,3,6,7], k = 3
<strong>Output:</strong> [3,3,5,5,6,7]
<strong>Explanation:</strong>
Window position Max
--------------- -----
[1 3 -1] -3 5 3 6 7 <strong>3</strong>
1 [3 -1 -3] 5 3 6 7 <strong>3</strong>
1 3 [-1 -3 5] 3 6 7 <strong> 5</strong>
1 3 -1 [-3 5 3] 6 7 <strong>5</strong>
1 3 -1 -3 [5 3 6] 7 <strong>6</strong>
1 3 -1 -3 5 [3 6 7] <strong>7</strong>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1], k = 1
<strong>Output:</strong> [1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 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 window.pop_front();\n }\n while(window.size() > 0 && window.back()[0] < nums[i]) {\n window.pop_back();\n }\n window.push_back({nums[i], i});\n if(i >= k-1) {\n ans.push_back(window.front()[0]);\n }\n }\n return ans;\n }\n};",
"memory": "201936"
} |
239 | <p>You are given an array of integers <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 max sliding window</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,-1,-3,5,3,6,7], k = 3
<strong>Output:</strong> [3,3,5,5,6,7]
<strong>Explanation:</strong>
Window position Max
--------------- -----
[1 3 -1] -3 5 3 6 7 <strong>3</strong>
1 [3 -1 -3] 5 3 6 7 <strong>3</strong>
1 3 [-1 -3 5] 3 6 7 <strong> 5</strong>
1 3 -1 [-3 5 3] 6 7 <strong>5</strong>
1 3 -1 -3 [5 3 6] 7 <strong>6</strong>
1 3 -1 -3 5 [3 6 7] <strong>7</strong>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1], k = 1
<strong>Output:</strong> [1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 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 window.pop_front();\n }\n while(window.size() > 0 && window.back()[0] < nums[i]) {\n window.pop_back();\n }\n window.push_back({nums[i], i});\n if(i >= k-1) {\n ans.push_back(window.front()[0]);\n }\n }\n return ans;\n }\n};",
"memory": "203028"
} |
239 | <p>You are given an array of integers <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 max sliding window</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,-1,-3,5,3,6,7], k = 3
<strong>Output:</strong> [3,3,5,5,6,7]
<strong>Explanation:</strong>
Window position Max
--------------- -----
[1 3 -1] -3 5 3 6 7 <strong>3</strong>
1 [3 -1 -3] 5 3 6 7 <strong>3</strong>
1 3 [-1 -3 5] 3 6 7 <strong> 5</strong>
1 3 -1 [-3 5 3] 6 7 <strong>5</strong>
1 3 -1 -3 [5 3 6] 7 <strong>6</strong>
1 3 -1 -3 5 [3 6 7] <strong>7</strong>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1], k = 1
<strong>Output:</strong> [1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 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.pop_back();\n }\n window.push_back({nums[i], i});\n }\n ans.push_back(window.front()[0]);\n\n for(int i = k; i < nums.size(); i++) {\n while (window.size() > 0 && window.front()[1] <= i - k) {\n window.pop_front();\n }\n while(window.size() > 0 && window.back()[0] < nums[i]) {\n window.pop_back();\n }\n window.push_back({nums[i], i});\n ans.push_back(window.front()[0]);\n }\n return ans;\n }\n};",
"memory": "203028"
} |
239 | <p>You are given an array of integers <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 max sliding window</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,-1,-3,5,3,6,7], k = 3
<strong>Output:</strong> [3,3,5,5,6,7]
<strong>Explanation:</strong>
Window position Max
--------------- -----
[1 3 -1] -3 5 3 6 7 <strong>3</strong>
1 [3 -1 -3] 5 3 6 7 <strong>3</strong>
1 3 [-1 -3 5] 3 6 7 <strong> 5</strong>
1 3 -1 [-3 5 3] 6 7 <strong>5</strong>
1 3 -1 -3 [5 3 6] 7 <strong>6</strong>
1 3 -1 -3 5 [3 6 7] <strong>7</strong>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1], k = 1
<strong>Output:</strong> [1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 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 for(int i=0;i<n;i++){\n if(i<k){\n pq.push({nums[i],i});\n continue;\n }\n else{\n if(i==k)\n ans.push_back(pq.top().first);\n pq.push({nums[i],i});\n mp[j]++;\n j++;\n while(!pq.empty()&&mp.find(pq.top().second)!=mp.end())\n pq.pop();\n ans.push_back(pq.top().first);\n }\n }\n return ans;\n }\n};",
"memory": "204121"
} |
239 | <p>You are given an array of integers <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 max sliding window</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,-1,-3,5,3,6,7], k = 3
<strong>Output:</strong> [3,3,5,5,6,7]
<strong>Explanation:</strong>
Window position Max
--------------- -----
[1 3 -1] -3 5 3 6 7 <strong>3</strong>
1 [3 -1 -3] 5 3 6 7 <strong>3</strong>
1 3 [-1 -3 5] 3 6 7 <strong> 5</strong>
1 3 -1 [-3 5 3] 6 7 <strong>5</strong>
1 3 -1 -3 [5 3 6] 7 <strong>6</strong>
1 3 -1 -3 5 [3 6 7] <strong>7</strong>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1], k = 1
<strong>Output:</strong> [1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 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() {delete[] arr;}\n void add(const int &e) {\n arrMap[arr[front]]--;\n if (arrMap[arr[front]] <= 0) arrMap.erase(arr[front]);\n arrMap[e]++;\n\n arr[front] = e;\n \n front = (front + 1) % size;\n }\n\n int max() {\n return arrMap.rbegin()->first;\n }\n\n void print() {\n for (int i = 0; i < size; ++i) {\n cout << arr[i] << \" \";\n }\n cout << endl;\n }\n\n void printMap(){\n for (const auto& pair : arrMap) {\n cout << \"{\" << pair.first << \": \" << pair.second << \"} \";\n }\n cout << endl;\n }\n\n };\n\n vector<int> maxSlidingWindow(vector<int>& nums, int k) {\n int size = nums.size();\n vector<int> maxResults;\n Queue queue(k);\n\n for (int i = 0; i < k; ++i) {\n queue.add(nums[i]);\n // queue.print();\n // queue.printMap();\n }\n\n maxResults.push_back(queue.max());\n\n for (int i = k; i < size; ++i) {\n queue.add(nums[i]);\n // queue.print();\n // queue.printMap();\n maxResults.push_back(queue.max());\n }\n return maxResults;\n }\n};",
"memory": "205213"
} |
239 | <p>You are given an array of integers <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 max sliding window</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,-1,-3,5,3,6,7], k = 3
<strong>Output:</strong> [3,3,5,5,6,7]
<strong>Explanation:</strong>
Window position Max
--------------- -----
[1 3 -1] -3 5 3 6 7 <strong>3</strong>
1 [3 -1 -3] 5 3 6 7 <strong>3</strong>
1 3 [-1 -3 5] 3 6 7 <strong> 5</strong>
1 3 -1 [-3 5 3] 6 7 <strong>5</strong>
1 3 -1 -3 [5 3 6] 7 <strong>6</strong>
1 3 -1 -3 5 [3 6 7] <strong>7</strong>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1], k = 1
<strong>Output:</strong> [1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 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() {delete[] arr;}\n void add(const int &e) {\n arrMap[arr[front]]--;\n if (arrMap[arr[front]] <= 0) arrMap.erase(arr[front]);\n arrMap[e]++;\n\n arr[front] = e;\n \n front = (front + 1) % size;\n }\n\n int max() {\n return arrMap.rbegin()->first;\n }\n\n // void print() {\n // for (int i = 0; i < size; ++i) {\n // cout << arr[i] << \" \";\n // }\n // cout << endl;\n // }\n\n // void printMap(){\n // for (const auto& pair : arrMap) {\n // cout << \"{\" << pair.first << \": \" << pair.second << \"} \";\n // }\n // cout << endl;\n // }\n\n };\n\n vector<int> maxSlidingWindow(vector<int>& nums, int k) {\n int size = nums.size();\n vector<int> maxResults;\n Queue queue(k);\n\n for (int i = 0; i < k; ++i) {\n queue.add(nums[i]);\n // queue.print();\n // queue.printMap();\n }\n\n maxResults.push_back(queue.max());\n\n for (int i = k; i < size; ++i) {\n queue.add(nums[i]);\n // queue.print();\n // queue.printMap();\n maxResults.push_back(queue.max());\n }\n return maxResults;\n }\n};",
"memory": "205213"
} |
239 | <p>You are given an array of integers <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 max sliding window</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,-1,-3,5,3,6,7], k = 3
<strong>Output:</strong> [3,3,5,5,6,7]
<strong>Explanation:</strong>
Window position Max
--------------- -----
[1 3 -1] -3 5 3 6 7 <strong>3</strong>
1 [3 -1 -3] 5 3 6 7 <strong>3</strong>
1 3 [-1 -3 5] 3 6 7 <strong> 5</strong>
1 3 -1 [-3 5 3] 6 7 <strong>5</strong>
1 3 -1 -3 [5 3 6] 7 <strong>6</strong>
1 3 -1 -3 5 [3 6 7] <strong>7</strong>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1], k = 1
<strong>Output:</strong> [1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 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.insert(nums[end]);\n // if((end-start+1)<k)\n // {\n \n // end++;\n // }\n\n \n\n // else if((end-start+1)==k)\n // {\n \n \n \n // res.push_back(*st.rbegin());\n // st.clear();\n // start++;\n \n // end++;\n // }\n \n // }\n // return res;\n\n vector<int> res;\n set<int> st;\n unordered_map<int, int> count; // Map to keep track of element counts\n\n for (int end = 0; end < nums.size(); ++end) {\n st.insert(nums[end]);\n count[nums[end]]++;\n\n // Remove the element that is sliding out of the window\n if (end >= k - 1) {\n // Add the maximum element to the result\n res.push_back(*st.rbegin());\n\n int start = end - k + 1;\n count[nums[start]]--;\n if (count[nums[start]] == 0) {\n st.erase(nums[start]);\n count.erase(nums[start]);\n }\n }\n }\n\n return res;\n }\n};",
"memory": "206306"
} |
239 | <p>You are given an array of integers <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 max sliding window</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,-1,-3,5,3,6,7], k = 3
<strong>Output:</strong> [3,3,5,5,6,7]
<strong>Explanation:</strong>
Window position Max
--------------- -----
[1 3 -1] -3 5 3 6 7 <strong>3</strong>
1 [3 -1 -3] 5 3 6 7 <strong>3</strong>
1 3 [-1 -3 5] 3 6 7 <strong> 5</strong>
1 3 -1 [-3 5 3] 6 7 <strong>5</strong>
1 3 -1 -3 [5 3 6] 7 <strong>6</strong>
1 3 -1 -3 5 [3 6 7] <strong>7</strong>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1], k = 1
<strong>Output:</strong> [1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 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 result.push_back(maxHeap.top().first);\n for(int i=k;i<nums.size();i++){\n maxHeap.push({nums[i],i});\n\n umap[i-k]++;\n while (!maxHeap.empty() && umap[maxHeap.top().second] == 1) {\n umap[maxHeap.top().second]--;\n maxHeap.pop();\n }\n result.push_back(maxHeap.top().first);\n }\n\n return result;\n }\n};",
"memory": "206306"
} |
239 | <p>You are given an array of integers <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 max sliding window</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,-1,-3,5,3,6,7], k = 3
<strong>Output:</strong> [3,3,5,5,6,7]
<strong>Explanation:</strong>
Window position Max
--------------- -----
[1 3 -1] -3 5 3 6 7 <strong>3</strong>
1 [3 -1 -3] 5 3 6 7 <strong>3</strong>
1 3 [-1 -3 5] 3 6 7 <strong> 5</strong>
1 3 -1 [-3 5 3] 6 7 <strong>5</strong>
1 3 -1 -3 [5 3 6] 7 <strong>6</strong>
1 3 -1 -3 5 [3 6 7] <strong>7</strong>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1], k = 1
<strong>Output:</strong> [1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 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);\n\n for(int i=k;i<n;i++)\n {\n m[i-k]=1;\n while(!pq.empty() && m.find(pq.top().second)!=m.end())\n pq.pop();\n pq.push({arr[i],i});\n ans.push_back(pq.top().first);\n }\n\n return ans;\n }\n};",
"memory": "207398"
} |
239 | <p>You are given an array of integers <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 max sliding window</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,-1,-3,5,3,6,7], k = 3
<strong>Output:</strong> [3,3,5,5,6,7]
<strong>Explanation:</strong>
Window position Max
--------------- -----
[1 3 -1] -3 5 3 6 7 <strong>3</strong>
1 [3 -1 -3] 5 3 6 7 <strong>3</strong>
1 3 [-1 -3 5] 3 6 7 <strong> 5</strong>
1 3 -1 [-3 5 3] 6 7 <strong>5</strong>
1 3 -1 -3 [5 3 6] 7 <strong>6</strong>
1 3 -1 -3 5 [3 6 7] <strong>7</strong>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1], k = 1
<strong>Output:</strong> [1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 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 return a.second > b.second ;\n }\n return false;\n }\n };\npublic:\n vector<int> maxSlidingWindow(vector<int>& nums, int k) {\n priority_queue<pair<int,int>, vector<pair<int,int>>, Compare> window;\n set<int> removed;\n vector<int> maxRange;\n \n for ( int i = 0; i < (int)nums.size(); i++ )\n nums[i] *= -1;\n\n int n = nums.size();\n\n for ( int i = 0; i < k; i++ )\n window.emplace(nums[i],i);\n\n for ( int i = k; i <= n; i++ ){\n while (!window.empty() && removed.find(window.top().second) != removed.end()) {\n pair<int, int> data = window.top();\n cout << data.first << \" \" << data.second << endl;\n window.pop();\n }\n pair<int,int> data = window.top();\n\n maxRange.emplace_back(-data.first);\n \n if ( nums[i-k] == data.first ){\n window.pop();\n }\n\n removed.emplace(i - k);\n if ( i < n )\n window.emplace(nums[i],i);\n }\n\n return maxRange;\n }\n};",
"memory": "207398"
} |
239 | <p>You are given an array of integers <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 max sliding window</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,-1,-3,5,3,6,7], k = 3
<strong>Output:</strong> [3,3,5,5,6,7]
<strong>Explanation:</strong>
Window position Max
--------------- -----
[1 3 -1] -3 5 3 6 7 <strong>3</strong>
1 [3 -1 -3] 5 3 6 7 <strong>3</strong>
1 3 [-1 -3 5] 3 6 7 <strong> 5</strong>
1 3 -1 [-3 5 3] 6 7 <strong>5</strong>
1 3 -1 -3 [5 3 6] 7 <strong>6</strong>
1 3 -1 -3 5 [3 6 7] <strong>7</strong>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1], k = 1
<strong>Output:</strong> [1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 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 res.push_back(*set.crbegin());\n set.erase(set.find(nums[i-k]));\n set.insert(nums[i]);\n }\n res.push_back(*set.crbegin());\n return res;\n }\n};",
"memory": "208491"
} |
239 | <p>You are given an array of integers <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 max sliding window</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,-1,-3,5,3,6,7], k = 3
<strong>Output:</strong> [3,3,5,5,6,7]
<strong>Explanation:</strong>
Window position Max
--------------- -----
[1 3 -1] -3 5 3 6 7 <strong>3</strong>
1 [3 -1 -3] 5 3 6 7 <strong>3</strong>
1 3 [-1 -3 5] 3 6 7 <strong> 5</strong>
1 3 -1 [-3 5 3] 6 7 <strong>5</strong>
1 3 -1 -3 [5 3 6] 7 <strong>6</strong>
1 3 -1 -3 5 [3 6 7] <strong>7</strong>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1], k = 1
<strong>Output:</strong> [1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 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.find(nums[i-k]));\n }\n if (i+1 >= k) {\n res[i-k+1] = *prev(vals.end());\n }\n }\n return res;\n }\n};",
"memory": "209583"
} |
239 | <p>You are given an array of integers <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 max sliding window</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,-1,-3,5,3,6,7], k = 3
<strong>Output:</strong> [3,3,5,5,6,7]
<strong>Explanation:</strong>
Window position Max
--------------- -----
[1 3 -1] -3 5 3 6 7 <strong>3</strong>
1 [3 -1 -3] 5 3 6 7 <strong>3</strong>
1 3 [-1 -3 5] 3 6 7 <strong> 5</strong>
1 3 -1 [-3 5 3] 6 7 <strong>5</strong>
1 3 -1 -3 [5 3 6] 7 <strong>6</strong>
1 3 -1 -3 5 [3 6 7] <strong>7</strong>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1], k = 1
<strong>Output:</strong> [1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 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 res[0] = *window_nums_set.rbegin();\n\n for (size_t i = k; i < nums.size(); ++i) {\n window_nums_set.erase(window_nums_set.find(nums[i - k]));\n window_nums_set.insert(nums[i]);\n res[i + 1 - k] = *window_nums_set.rbegin();\n }\n\n return res;\n }\n};",
"memory": "209583"
} |
239 | <p>You are given an array of integers <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 max sliding window</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,-1,-3,5,3,6,7], k = 3
<strong>Output:</strong> [3,3,5,5,6,7]
<strong>Explanation:</strong>
Window position Max
--------------- -----
[1 3 -1] -3 5 3 6 7 <strong>3</strong>
1 [3 -1 -3] 5 3 6 7 <strong>3</strong>
1 3 [-1 -3 5] 3 6 7 <strong> 5</strong>
1 3 -1 [-3 5 3] 6 7 <strong>5</strong>
1 3 -1 -3 [5 3 6] 7 <strong>6</strong>
1 3 -1 -3 5 [3 6 7] <strong>7</strong>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1], k = 1
<strong>Output:</strong> [1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 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<int, int> mp; // Map to keep track of invalid elements\n vector<int> ans;\n int second = 0, first = 0;\n\n while (second < n) {\n // Push current element along with its index to the priority queue\n maxi.push({nums[second], second});\n \n // Move the window forward\n second++;\n \n // If the window size reaches 'k'\n if (second - first == k) {\n // Ensure the top of the heap is the maximum of the current window\n while (!maxi.empty() && mp[maxi.top().second] > 0) {\n mp[maxi.top().second]--;\n maxi.pop();\n }\n\n // The top element is now the maximum in the current window\n ans.push_back(maxi.top().first);\n\n // Mark the element going out of the window as invalid in the map\n mp[first]++;\n \n // Move the start of the window forward\n first++;\n }\n }\n\n return ans;\n }\n};\n",
"memory": "210676"
} |
239 | <p>You are given an array of integers <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 max sliding window</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,-1,-3,5,3,6,7], k = 3
<strong>Output:</strong> [3,3,5,5,6,7]
<strong>Explanation:</strong>
Window position Max
--------------- -----
[1 3 -1] -3 5 3 6 7 <strong>3</strong>
1 [3 -1 -3] 5 3 6 7 <strong>3</strong>
1 3 [-1 -3 5] 3 6 7 <strong> 5</strong>
1 3 -1 [-3 5 3] 6 7 <strong>5</strong>
1 3 -1 -3 [5 3 6] 7 <strong>6</strong>
1 3 -1 -3 5 [3 6 7] <strong>7</strong>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1], k = 1
<strong>Output:</strong> [1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 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[i];\n pq.push({ele,i});\n \n\n if(i-j+1==k){\n pair<int,int> top=pq.top();\n cout<<top.first<<\" \"<<top.second<<endl;\n while(m[top]){\n pq.pop();\n top=pq.top();\n }\n\n\n ans.push_back(top.first);\n m[{nums[j],j}]=1;\n j++;\n \n\n }\n \n \n\n }\n return ans;\n }\n};",
"memory": "210676"
} |
239 | <p>You are given an array of integers <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 max sliding window</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,-1,-3,5,3,6,7], k = 3
<strong>Output:</strong> [3,3,5,5,6,7]
<strong>Explanation:</strong>
Window position Max
--------------- -----
[1 3 -1] -3 5 3 6 7 <strong>3</strong>
1 [3 -1 -3] 5 3 6 7 <strong>3</strong>
1 3 [-1 -3 5] 3 6 7 <strong> 5</strong>
1 3 -1 [-3 5 3] 6 7 <strong>5</strong>
1 3 -1 -3 [5 3 6] 7 <strong>6</strong>
1 3 -1 -3 5 [3 6 7] <strong>7</strong>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1], k = 1
<strong>Output:</strong> [1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 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 dq.pop_back();\n // if(!dq.empty() && dq.back()[1] <= nums[i])\n // dq.push_back({i,nums[i]});\n {\n while(!dq.empty() && dq.front()[1]<=nums[i])\n dq.pop_front();\n dq.push_front({i,nums[i]});\n } \n }\n else\n dq.push_back({i,nums[i]});\n }\n // print(dq);\n\n if(n < k)\n return dq.back();\n\n vector<int> ans;\n ans.push_back(dq.back()[1]);\n for(int i = k ; i < n ; i++ ){\n if(!dq.empty()){\n while(!dq.empty() && (dq.back()[1] <= nums[i] || (i - dq.back()[0]) >= k))\n dq.pop_back();\n if(!dq.empty() && dq.back()[1] <= nums[i] )\n dq.push_back({i,nums[i]});\n else{\n while(!dq.empty() && (dq.front()[1]<=nums[i]||(i - dq.back()[0]) >= k))\n dq.pop_front();\n dq.push_front({i,nums[i]});\n } \n }\n else\n dq.push_back({i,nums[i]});\n \n ans.push_back(dq.back()[1]);\n\n }\n \n\n return ans;\n }\n};",
"memory": "211768"
} |
239 | <p>You are given an array of integers <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 max sliding window</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,-1,-3,5,3,6,7], k = 3
<strong>Output:</strong> [3,3,5,5,6,7]
<strong>Explanation:</strong>
Window position Max
--------------- -----
[1 3 -1] -3 5 3 6 7 <strong>3</strong>
1 [3 -1 -3] 5 3 6 7 <strong>3</strong>
1 3 [-1 -3 5] 3 6 7 <strong> 5</strong>
1 3 -1 [-3 5 3] 6 7 <strong>5</strong>
1 3 -1 -3 [5 3 6] 7 <strong>6</strong>
1 3 -1 -3 5 [3 6 7] <strong>7</strong>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1], k = 1
<strong>Output:</strong> [1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 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;\n vector<vector<int>> sp_table(lg[length] + 1, vector<int>(length));\n for(i = 0; i < length; ++i)\n sp_table[0][i] = nums[i];\n for(i = 1; i <= lg[length]; ++i)\n for(j = 0; j + (1 << (i - 1)) < length; ++j)\n sp_table[i][j] = max(sp_table[i - 1][j], sp_table[i - 1][j + (1 << (i - 1))]);\n int l = 0, r = k - 1;\n int log = lg[r - l + 1];\n while(r < length) {\n res[r - k + 1] = max(sp_table[log][l], sp_table[log][r + 1 - (1 << log)]);\n ++l;\n ++r;\n }\n return res;\n }\n};",
"memory": "212861"
} |
239 | <p>You are given an array of integers <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 max sliding window</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,-1,-3,5,3,6,7], k = 3
<strong>Output:</strong> [3,3,5,5,6,7]
<strong>Explanation:</strong>
Window position Max
--------------- -----
[1 3 -1] -3 5 3 6 7 <strong>3</strong>
1 [3 -1 -3] 5 3 6 7 <strong>3</strong>
1 3 [-1 -3 5] 3 6 7 <strong> 5</strong>
1 3 -1 [-3 5 3] 6 7 <strong>5</strong>
1 3 -1 -3 [5 3 6] 7 <strong>6</strong>
1 3 -1 -3 5 [3 6 7] <strong>7</strong>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1], k = 1
<strong>Output:</strong> [1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 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( (*window_sum.rbegin()));\n const int max_num = nums.size()-k;\n for( int i = 1; i <= max_num; ++i)\n {\n window_sum.erase(window_sum.find(nums[i - 1]));\n window_sum.insert( nums[i-1+k] );\n result.push_back( (*window_sum.rbegin()));\n }\n return result;\n }\n};",
"memory": "212861"
} |
239 | <p>You are given an array of integers <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 max sliding window</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,-1,-3,5,3,6,7], k = 3
<strong>Output:</strong> [3,3,5,5,6,7]
<strong>Explanation:</strong>
Window position Max
--------------- -----
[1 3 -1] -3 5 3 6 7 <strong>3</strong>
1 [3 -1 -3] 5 3 6 7 <strong>3</strong>
1 3 [-1 -3 5] 3 6 7 <strong> 5</strong>
1 3 -1 [-3 5 3] 6 7 <strong>5</strong>
1 3 -1 -3 [5 3 6] 7 <strong>6</strong>
1 3 -1 -3 5 [3 6 7] <strong>7</strong>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1], k = 1
<strong>Output:</strong> [1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 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]));\n s.insert(nums[i]);\n ans.push_back(*s.rbegin());\n }\n return ans;\n }\n};",
"memory": "213953"
} |
239 | <p>You are given an array of integers <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 max sliding window</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,-1,-3,5,3,6,7], k = 3
<strong>Output:</strong> [3,3,5,5,6,7]
<strong>Explanation:</strong>
Window position Max
--------------- -----
[1 3 -1] -3 5 3 6 7 <strong>3</strong>
1 [3 -1 -3] 5 3 6 7 <strong>3</strong>
1 3 [-1 -3 5] 3 6 7 <strong> 5</strong>
1 3 -1 [-3 5 3] 6 7 <strong>5</strong>
1 3 -1 -3 [5 3 6] 7 <strong>6</strong>
1 3 -1 -3 5 [3 6 7] <strong>7</strong>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1], k = 1
<strong>Output:</strong> [1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 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 m.erase(m.find(nums[j]));\n j++;\n }\n }\n return ans;\n }\n};",
"memory": "213953"
} |
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 from top to bottom.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/11/24/searchgrid2.jpg" style="width: 300px; height: 300px;" />
<pre>
<strong>Input:</strong> matrix = [[1,4,7,11,15],[2,5,8,12,19],[3,6,9,16,22],[10,13,14,17,24],[18,21,23,26,30]], target = 5
<strong>Output:</strong> true
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/11/24/searchgrid.jpg" style="width: 300px; height: 300px;" />
<pre>
<strong>Input:</strong> matrix = [[1,4,7,11,15],[2,5,8,12,19],[3,6,9,16,22],[10,13,14,17,24],[18,21,23,26,30]], target = 20
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == matrix.length</code></li>
<li><code>n == matrix[i].length</code></li>
<li><code>1 <= n, m <= 300</code></li>
<li><code>-10<sup>9</sup> <= matrix[i][j] <= 10<sup>9</sup></code></li>
<li>All the integers in each row are <strong>sorted</strong> in ascending order.</li>
<li>All the integers in each column are <strong>sorted</strong> in ascending order.</li>
<li><code>-10<sup>9</sup> <= target <= 10<sup>9</sup></code></li>
</ul>
| 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\n if (target >= matrix[i][l] && target <= matrix[i][r]) {\n while (l <= r) {\n int mid = (r + l) / 2;\n\n if (matrix[i][mid] == target) return true;\n \n else if (matrix[i][mid] > target) r = mid - 1;\n \n else l = mid + 1;\n }\n }\n }\n return false;\n }\n};\n \n",
"memory": "16500"
} |
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 from top to bottom.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/11/24/searchgrid2.jpg" style="width: 300px; height: 300px;" />
<pre>
<strong>Input:</strong> matrix = [[1,4,7,11,15],[2,5,8,12,19],[3,6,9,16,22],[10,13,14,17,24],[18,21,23,26,30]], target = 5
<strong>Output:</strong> true
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/11/24/searchgrid.jpg" style="width: 300px; height: 300px;" />
<pre>
<strong>Input:</strong> matrix = [[1,4,7,11,15],[2,5,8,12,19],[3,6,9,16,22],[10,13,14,17,24],[18,21,23,26,30]], target = 20
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == matrix.length</code></li>
<li><code>n == matrix[i].length</code></li>
<li><code>1 <= n, m <= 300</code></li>
<li><code>-10<sup>9</sup> <= matrix[i][j] <= 10<sup>9</sup></code></li>
<li>All the integers in each row are <strong>sorted</strong> in ascending order.</li>
<li>All the integers in each column are <strong>sorted</strong> in ascending order.</li>
<li><code>-10<sup>9</sup> <= target <= 10<sup>9</sup></code></li>
</ul>
| 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 else if(matrix[i][j]>target) j--;\n else i++;\n }\n return false;\n }\n};\n#pragma GCC optimize(\"Ofast,unroll-loops\")\n#pragma GCC target(\"avx2,tune=native\")\n\nstatic int x = []() {\n std::ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n return 0;\n}();",
"memory": "17000"
} |
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 from top to bottom.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/11/24/searchgrid2.jpg" style="width: 300px; height: 300px;" />
<pre>
<strong>Input:</strong> matrix = [[1,4,7,11,15],[2,5,8,12,19],[3,6,9,16,22],[10,13,14,17,24],[18,21,23,26,30]], target = 5
<strong>Output:</strong> true
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/11/24/searchgrid.jpg" style="width: 300px; height: 300px;" />
<pre>
<strong>Input:</strong> matrix = [[1,4,7,11,15],[2,5,8,12,19],[3,6,9,16,22],[10,13,14,17,24],[18,21,23,26,30]], target = 20
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == matrix.length</code></li>
<li><code>n == matrix[i].length</code></li>
<li><code>1 <= n, m <= 300</code></li>
<li><code>-10<sup>9</sup> <= matrix[i][j] <= 10<sup>9</sup></code></li>
<li>All the integers in each row are <strong>sorted</strong> in ascending order.</li>
<li>All the integers in each column are <strong>sorted</strong> in ascending order.</li>
<li><code>-10<sup>9</sup> <= target <= 10<sup>9</sup></code></li>
</ul>
| 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][colIndex];\n\n if(element == target){\n return 1;\n }\n if(element<target){\n rowIndex++;\n }\n else{\n colIndex--;\n }\n }\n return 0;\n }\n};",
"memory": "17200"
} |
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 from top to bottom.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/11/24/searchgrid2.jpg" style="width: 300px; height: 300px;" />
<pre>
<strong>Input:</strong> matrix = [[1,4,7,11,15],[2,5,8,12,19],[3,6,9,16,22],[10,13,14,17,24],[18,21,23,26,30]], target = 5
<strong>Output:</strong> true
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/11/24/searchgrid.jpg" style="width: 300px; height: 300px;" />
<pre>
<strong>Input:</strong> matrix = [[1,4,7,11,15],[2,5,8,12,19],[3,6,9,16,22],[10,13,14,17,24],[18,21,23,26,30]], target = 20
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == matrix.length</code></li>
<li><code>n == matrix[i].length</code></li>
<li><code>1 <= n, m <= 300</code></li>
<li><code>-10<sup>9</sup> <= matrix[i][j] <= 10<sup>9</sup></code></li>
<li>All the integers in each row are <strong>sorted</strong> in ascending order.</li>
<li>All the integers in each column are <strong>sorted</strong> in ascending order.</li>
<li><code>-10<sup>9</sup> <= target <= 10<sup>9</sup></code></li>
</ul>
| 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 // we found row where target could be located\n\n ssize_t l = 0;\n ssize_t r = N - 1;\n while (l <= r) {\n ssize_t const mid = (l + r) / 2;\n int val = matrix[i][mid];\n\n if (val == target) {\n return true;\n }\n\n if (val < target) {\n l = mid + 1;\n } else {\n r = mid - 1;\n }\n }\n }\n }\n return false;\n }\n};",
"memory": "17300"
} |
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 from top to bottom.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/11/24/searchgrid2.jpg" style="width: 300px; height: 300px;" />
<pre>
<strong>Input:</strong> matrix = [[1,4,7,11,15],[2,5,8,12,19],[3,6,9,16,22],[10,13,14,17,24],[18,21,23,26,30]], target = 5
<strong>Output:</strong> true
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/11/24/searchgrid.jpg" style="width: 300px; height: 300px;" />
<pre>
<strong>Input:</strong> matrix = [[1,4,7,11,15],[2,5,8,12,19],[3,6,9,16,22],[10,13,14,17,24],[18,21,23,26,30]], target = 20
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == matrix.length</code></li>
<li><code>n == matrix[i].length</code></li>
<li><code>1 <= n, m <= 300</code></li>
<li><code>-10<sup>9</sup> <= matrix[i][j] <= 10<sup>9</sup></code></li>
<li>All the integers in each row are <strong>sorted</strong> in ascending order.</li>
<li>All the integers in each column are <strong>sorted</strong> in ascending order.</li>
<li><code>-10<sup>9</sup> <= target <= 10<sup>9</sup></code></li>
</ul>
| 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 res = m; l = m + 1;\n }\n }\n return res;\n }\n bool searchMatrix(vector<vector<int>>& mat, int target) {\n int n = mat.size();\n int m = mat[0].size();\n int p = solve(0, n-1, mat, 0, target);\n\n int q = - 1;\n int l = 0, r = m - 1;\n while(l <= r){\n int m = l + (r-l)/2;\n if(mat[0][m] == target) {return true; break;}\n else if(mat[0][m] > target) r = m - 1;\n else{\n q = m; l = m + 1;\n }\n }\n \n if(p == -1 || q == -1) return false;\n\n for(int i = 0; i <= q; i++){\n int temp = solve(0,p,mat,i,target);\n if(mat[temp][i] == target) return true;\n }\n\n return false;\n }\n\n};",
"memory": "17400"
} |
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 from top to bottom.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/11/24/searchgrid2.jpg" style="width: 300px; height: 300px;" />
<pre>
<strong>Input:</strong> matrix = [[1,4,7,11,15],[2,5,8,12,19],[3,6,9,16,22],[10,13,14,17,24],[18,21,23,26,30]], target = 5
<strong>Output:</strong> true
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/11/24/searchgrid.jpg" style="width: 300px; height: 300px;" />
<pre>
<strong>Input:</strong> matrix = [[1,4,7,11,15],[2,5,8,12,19],[3,6,9,16,22],[10,13,14,17,24],[18,21,23,26,30]], target = 20
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == matrix.length</code></li>
<li><code>n == matrix[i].length</code></li>
<li><code>1 <= n, m <= 300</code></li>
<li><code>-10<sup>9</sup> <= matrix[i][j] <= 10<sup>9</sup></code></li>
<li>All the integers in each row are <strong>sorted</strong> in ascending order.</li>
<li>All the integers in each column are <strong>sorted</strong> in ascending order.</li>
<li><code>-10<sup>9</sup> <= target <= 10<sup>9</sup></code></li>
</ul>
| 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==target){\n return true;\n }else if(current>target){\n col--;\n }else{\n row++;\n }\n\n }\n return false;\n\n }\n};",
"memory": "17400"
} |
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 from top to bottom.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/11/24/searchgrid2.jpg" style="width: 300px; height: 300px;" />
<pre>
<strong>Input:</strong> matrix = [[1,4,7,11,15],[2,5,8,12,19],[3,6,9,16,22],[10,13,14,17,24],[18,21,23,26,30]], target = 5
<strong>Output:</strong> true
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/11/24/searchgrid.jpg" style="width: 300px; height: 300px;" />
<pre>
<strong>Input:</strong> matrix = [[1,4,7,11,15],[2,5,8,12,19],[3,6,9,16,22],[10,13,14,17,24],[18,21,23,26,30]], target = 20
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == matrix.length</code></li>
<li><code>n == matrix[i].length</code></li>
<li><code>1 <= n, m <= 300</code></li>
<li><code>-10<sup>9</sup> <= matrix[i][j] <= 10<sup>9</sup></code></li>
<li>All the integers in each row are <strong>sorted</strong> in ascending order.</li>
<li>All the integers in each column are <strong>sorted</strong> in ascending order.</li>
<li><code>-10<sup>9</sup> <= target <= 10<sup>9</sup></code></li>
</ul>
| 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][colIndex];\n if(element==target){\n return 1;\n }\n if(element<target){\n rowIndex++;\n }\n else{\n colIndex--;\n }\n }\n return 0;\n }\n};",
"memory": "17500"
} |
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 from top to bottom.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/11/24/searchgrid2.jpg" style="width: 300px; height: 300px;" />
<pre>
<strong>Input:</strong> matrix = [[1,4,7,11,15],[2,5,8,12,19],[3,6,9,16,22],[10,13,14,17,24],[18,21,23,26,30]], target = 5
<strong>Output:</strong> true
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/11/24/searchgrid.jpg" style="width: 300px; height: 300px;" />
<pre>
<strong>Input:</strong> matrix = [[1,4,7,11,15],[2,5,8,12,19],[3,6,9,16,22],[10,13,14,17,24],[18,21,23,26,30]], target = 20
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == matrix.length</code></li>
<li><code>n == matrix[i].length</code></li>
<li><code>1 <= n, m <= 300</code></li>
<li><code>-10<sup>9</sup> <= matrix[i][j] <= 10<sup>9</sup></code></li>
<li>All the integers in each row are <strong>sorted</strong> in ascending order.</li>
<li>All the integers in each column are <strong>sorted</strong> in ascending order.</li>
<li><code>-10<sup>9</sup> <= target <= 10<sup>9</sup></code></li>
</ul>
| 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 true;\n }\n else if(matrix[i][j] > target){\n j--;\n }\n else{\n i++;\n }\n }\n return false;\n }\n\n bool helper(vector<vector<int>>& matrix,int target){\n int n = matrix.size();\n int m = matrix[0].size();\n for(int i=0;i<n;i++){\n for(int j=0;j<m;j++){\n if(matrix[i][j] == target){\n return true;\n }\n }\n }\n return false;\n }\n};",
"memory": "17500"
} |
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 from top to bottom.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/11/24/searchgrid2.jpg" style="width: 300px; height: 300px;" />
<pre>
<strong>Input:</strong> matrix = [[1,4,7,11,15],[2,5,8,12,19],[3,6,9,16,22],[10,13,14,17,24],[18,21,23,26,30]], target = 5
<strong>Output:</strong> true
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/11/24/searchgrid.jpg" style="width: 300px; height: 300px;" />
<pre>
<strong>Input:</strong> matrix = [[1,4,7,11,15],[2,5,8,12,19],[3,6,9,16,22],[10,13,14,17,24],[18,21,23,26,30]], target = 20
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == matrix.length</code></li>
<li><code>n == matrix[i].length</code></li>
<li><code>1 <= n, m <= 300</code></li>
<li><code>-10<sup>9</sup> <= matrix[i][j] <= 10<sup>9</sup></code></li>
<li>All the integers in each row are <strong>sorted</strong> in ascending order.</li>
<li>All the integers in each column are <strong>sorted</strong> in ascending order.</li>
<li><code>-10<sup>9</sup> <= target <= 10<sup>9</sup></code></li>
</ul>
| 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>target)\n {\n j--;\n }\n else if(value<target)\n {\n i++;\n }\n else return true;\n }\n return false;\n }\n};",
"memory": "17600"
} |
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 from top to bottom.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/11/24/searchgrid2.jpg" style="width: 300px; height: 300px;" />
<pre>
<strong>Input:</strong> matrix = [[1,4,7,11,15],[2,5,8,12,19],[3,6,9,16,22],[10,13,14,17,24],[18,21,23,26,30]], target = 5
<strong>Output:</strong> true
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/11/24/searchgrid.jpg" style="width: 300px; height: 300px;" />
<pre>
<strong>Input:</strong> matrix = [[1,4,7,11,15],[2,5,8,12,19],[3,6,9,16,22],[10,13,14,17,24],[18,21,23,26,30]], target = 20
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == matrix.length</code></li>
<li><code>n == matrix[i].length</code></li>
<li><code>1 <= n, m <= 300</code></li>
<li><code>-10<sup>9</sup> <= matrix[i][j] <= 10<sup>9</sup></code></li>
<li>All the integers in each row are <strong>sorted</strong> in ascending order.</li>
<li>All the integers in each column are <strong>sorted</strong> in ascending order.</li>
<li><code>-10<sup>9</sup> <= target <= 10<sup>9</sup></code></li>
</ul>
| 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 true;\n if(target <matrix[row_start][col_start])\n col_start--;\n else\n row_start++;\n }\n return false;\n }\n};",
"memory": "17600"
} |
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 <code>(0, 0)</code>. In one move, you can move to any adjacent cell in the grid, including cells containing thieves.</p>
<p>The <strong>safeness factor</strong> of a path on the grid is defined as the <strong>minimum</strong> manhattan distance from any cell in the path to any thief in the grid.</p>
<p>Return <em>the <strong>maximum safeness factor</strong> of all paths leading to cell </em><code>(n - 1, n - 1)</code><em>.</em></p>
<p>An <strong>adjacent</strong> cell of cell <code>(r, c)</code>, is one of the cells <code>(r, c + 1)</code>, <code>(r, c - 1)</code>, <code>(r + 1, c)</code> and <code>(r - 1, c)</code> if it exists.</p>
<p>The <strong>Manhattan distance</strong> between two cells <code>(a, b)</code> and <code>(x, y)</code> is equal to <code>|a - x| + |b - y|</code>, where <code>|val|</code> denotes the absolute value of val.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example1.png" style="width: 362px; height: 242px;" />
<pre>
<strong>Input:</strong> grid = [[1,0,0],[0,0,0],[0,0,1]]
<strong>Output:</strong> 0
<strong>Explanation:</strong> All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example2.png" style="width: 362px; height: 242px;" />
<pre>
<strong>Input:</strong> grid = [[0,0,1],[0,0,0],[0,0,0]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since:
- The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2.
It can be shown that there are no other paths with a higher safeness factor.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example3.png" style="width: 362px; height: 242px;" />
<pre>
<strong>Input:</strong> grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since:
- The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2.
- The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2.
It can be shown that there are no other paths with a higher safeness factor.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length == n <= 400</code></li>
<li><code>grid[i].length == n</code></li>
<li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li>
<li>There is at least one thief in the <code>grid</code>.</li>
</ul>
| 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;\n }\n\n int getMinPath( vector<vector<int>> &grid){\n\n int n = grid.size();\n priority_queue<pair<int, pair<int, int>>> pq;\n pq.push({grid[0][0], {0, 0}});\n\n vector<vector<bool>> visited(n, vector<bool>(n, false));\n visited[0][0] = true;\n\n int dx[4] = {0,0,1,-1};\n int dy[4] = {-1,1,0,0};\n\n while(!pq.empty()){\n auto [val, cell] = pq.top();\n pq.pop();\n int x = cell.first;\n int y = cell.second;\n\n if (x == n - 1 && y == n - 1) {\n return val;\n }\n\n for(int i=0; i<4; i++){\n int nx = x + dx[i];\n int ny = y + dy[i];\n\n if (nx >= 0 && nx < n && ny >= 0 && ny < n && !visited[nx][ny]) {\n visited[nx][ny] = true;\n pq.push({min(val, grid[nx][ny]), {nx, ny}});\n }\n }\n }\n return -1;\n }\n\n int maximumSafenessFactor(vector<vector<int>>& grid) {\n vector<pair<int,int>> oneIndex;\n int n = grid.size();\n\n if(grid[0][0] == 1 || grid[n-1][n-1] == 1) return 0;\n\n for(int i=0; i<n; i++){\n for(int j=0; j<n; j++){\n if(grid[i][j] == 1)\n oneIndex.push_back({i,j});\n }\n }\n\n vector<vector<int>> safeNess = grid;\n\n for(int i=0; i<n; i++){\n for(int j=0; j<n; j++){\n if(grid[i][j] == 0){\n int safe = getFactor(i, j, oneIndex);\n safeNess[i][j] = safe;\n }\n else safeNess[i][j] = 0;\n }\n }\n return getMinPath(safeNess);\n }\n};",
"memory": "110123"
} |
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 <code>(0, 0)</code>. In one move, you can move to any adjacent cell in the grid, including cells containing thieves.</p>
<p>The <strong>safeness factor</strong> of a path on the grid is defined as the <strong>minimum</strong> manhattan distance from any cell in the path to any thief in the grid.</p>
<p>Return <em>the <strong>maximum safeness factor</strong> of all paths leading to cell </em><code>(n - 1, n - 1)</code><em>.</em></p>
<p>An <strong>adjacent</strong> cell of cell <code>(r, c)</code>, is one of the cells <code>(r, c + 1)</code>, <code>(r, c - 1)</code>, <code>(r + 1, c)</code> and <code>(r - 1, c)</code> if it exists.</p>
<p>The <strong>Manhattan distance</strong> between two cells <code>(a, b)</code> and <code>(x, y)</code> is equal to <code>|a - x| + |b - y|</code>, where <code>|val|</code> denotes the absolute value of val.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example1.png" style="width: 362px; height: 242px;" />
<pre>
<strong>Input:</strong> grid = [[1,0,0],[0,0,0],[0,0,1]]
<strong>Output:</strong> 0
<strong>Explanation:</strong> All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example2.png" style="width: 362px; height: 242px;" />
<pre>
<strong>Input:</strong> grid = [[0,0,1],[0,0,0],[0,0,0]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since:
- The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2.
It can be shown that there are no other paths with a higher safeness factor.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example3.png" style="width: 362px; height: 242px;" />
<pre>
<strong>Input:</strong> grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since:
- The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2.
- The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2.
It can be shown that there are no other paths with a higher safeness factor.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length == n <= 400</code></li>
<li><code>grid[i].length == n</code></li>
<li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li>
<li>There is at least one thief in the <code>grid</code>.</li>
</ul>
| 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;\n }\n\n int getMinPath( vector<vector<int>> &grid){\n\n int n = grid.size();\n priority_queue<pair<int, pair<int, int>>> pq;\n pq.push({grid[0][0], {0, 0}});\n\n vector<vector<bool>> visited(n, vector<bool>(n, false));\n visited[0][0] = true;\n\n int dx[4] = {0,0,1,-1};\n int dy[4] = {-1,1,0,0};\n\n while(!pq.empty()){\n auto [val, cell] = pq.top();\n pq.pop();\n int x = cell.first;\n int y = cell.second;\n\n if (x == n - 1 && y == n - 1) {\n return val;\n }\n\n for(int i=0; i<4; i++){\n int nx = x + dx[i];\n int ny = y + dy[i];\n\n if (nx >= 0 && nx < n && ny >= 0 && ny < n && !visited[nx][ny]) {\n visited[nx][ny] = true;\n pq.push({min(val, grid[nx][ny]), {nx, ny}});\n }\n }\n }\n return -1;\n }\n\n int maximumSafenessFactor(vector<vector<int>>& grid) {\n vector<pair<int,int>> oneIndex;\n int n = grid.size();\n\n if(grid[0][0] == 1 || grid[n-1][n-1] == 1) return 0;\n\n for(int i=0; i<n; i++){\n for(int j=0; j<n; j++){\n if(grid[i][j] == 1)\n oneIndex.push_back({i,j});\n }\n }\n\n vector<vector<int>> safeNess = grid;\n\n for(int i=0; i<n; i++){\n for(int j=0; j<n; j++){\n if(grid[i][j] == 0){\n int safe = getFactor(i, j, oneIndex);\n safeNess[i][j] = safe;\n }\n else safeNess[i][j] = 0;\n }\n }\n return getMinPath(safeNess);\n }\n};",
"memory": "110123"
} |
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 <code>(0, 0)</code>. In one move, you can move to any adjacent cell in the grid, including cells containing thieves.</p>
<p>The <strong>safeness factor</strong> of a path on the grid is defined as the <strong>minimum</strong> manhattan distance from any cell in the path to any thief in the grid.</p>
<p>Return <em>the <strong>maximum safeness factor</strong> of all paths leading to cell </em><code>(n - 1, n - 1)</code><em>.</em></p>
<p>An <strong>adjacent</strong> cell of cell <code>(r, c)</code>, is one of the cells <code>(r, c + 1)</code>, <code>(r, c - 1)</code>, <code>(r + 1, c)</code> and <code>(r - 1, c)</code> if it exists.</p>
<p>The <strong>Manhattan distance</strong> between two cells <code>(a, b)</code> and <code>(x, y)</code> is equal to <code>|a - x| + |b - y|</code>, where <code>|val|</code> denotes the absolute value of val.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example1.png" style="width: 362px; height: 242px;" />
<pre>
<strong>Input:</strong> grid = [[1,0,0],[0,0,0],[0,0,1]]
<strong>Output:</strong> 0
<strong>Explanation:</strong> All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example2.png" style="width: 362px; height: 242px;" />
<pre>
<strong>Input:</strong> grid = [[0,0,1],[0,0,0],[0,0,0]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since:
- The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2.
It can be shown that there are no other paths with a higher safeness factor.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example3.png" style="width: 362px; height: 242px;" />
<pre>
<strong>Input:</strong> grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since:
- The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2.
- The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2.
It can be shown that there are no other paths with a higher safeness factor.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length == n <= 400</code></li>
<li><code>grid[i].length == n</code></li>
<li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li>
<li>There is at least one thief in the <code>grid</code>.</li>
</ul>
| 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 for(int j=0;j<n;j++){\n if(grid[i][j]){\n q.push({i, j});\n grid[i][j]=0;\n }\n else grid[i][j]=-1;\n }\n }\n while(!q.empty()){\n auto[i, j]=q.front();\n q.pop();\n for(auto &ele:dir){\n int ni=i+ele[0], nj=j+ele[1];\n if(ni>=0 && nj>=0 && ni<n && nj<n && grid[ni][nj]==-1){\n grid[ni][nj]=grid[i][j]+1;\n q.push({ni, nj});\n }\n }\n }\n priority_queue<pair<int, pair<int, int>>> pq;\n pq.push({grid[0][0], {0, 0}});\n vector<vector<bool>> visited(n, vector<bool>(n, false));\n visited[0][0]=true;\n\n while(!pq.empty()){\n auto top=pq.top();\n int safe=top.first, i=top.second.first, j=top.second.second;\n pq.pop();\n if(i==n-1 && j==n-1) return safe;\n for(const auto &ele:dir){\n int ni=i+ele[0], nj=j+ele[1];\n if(ni>=0 && nj>=0 && ni<n && nj<n && !visited[ni][nj]){\n visited[ni][nj]=true;\n pq.push({min(safe, grid[ni][nj]), {ni, nj}});\n }\n }\n }\n return -1;\n }\n};",
"memory": "114806"
} |
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 <code>(0, 0)</code>. In one move, you can move to any adjacent cell in the grid, including cells containing thieves.</p>
<p>The <strong>safeness factor</strong> of a path on the grid is defined as the <strong>minimum</strong> manhattan distance from any cell in the path to any thief in the grid.</p>
<p>Return <em>the <strong>maximum safeness factor</strong> of all paths leading to cell </em><code>(n - 1, n - 1)</code><em>.</em></p>
<p>An <strong>adjacent</strong> cell of cell <code>(r, c)</code>, is one of the cells <code>(r, c + 1)</code>, <code>(r, c - 1)</code>, <code>(r + 1, c)</code> and <code>(r - 1, c)</code> if it exists.</p>
<p>The <strong>Manhattan distance</strong> between two cells <code>(a, b)</code> and <code>(x, y)</code> is equal to <code>|a - x| + |b - y|</code>, where <code>|val|</code> denotes the absolute value of val.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example1.png" style="width: 362px; height: 242px;" />
<pre>
<strong>Input:</strong> grid = [[1,0,0],[0,0,0],[0,0,1]]
<strong>Output:</strong> 0
<strong>Explanation:</strong> All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example2.png" style="width: 362px; height: 242px;" />
<pre>
<strong>Input:</strong> grid = [[0,0,1],[0,0,0],[0,0,0]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since:
- The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2.
It can be shown that there are no other paths with a higher safeness factor.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example3.png" style="width: 362px; height: 242px;" />
<pre>
<strong>Input:</strong> grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since:
- The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2.
- The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2.
It can be shown that there are no other paths with a higher safeness factor.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length == n <= 400</code></li>
<li><code>grid[i].length == n</code></li>
<li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li>
<li>There is at least one thief in the <code>grid</code>.</li>
</ul>
| 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][j] == 1) queue.push({i,j});\n }\n\n while(queue.size() > 0){\n auto [cur_i, cur_j] = queue.front(); queue.pop();\n for(int cnt = 0; cnt < 4; ++cnt){\n int I = cur_i + di[cnt], J = cur_j + dj[cnt];\n if(I < 0 || I >= n || J < 0 || J >= n) continue;\n if(grid[I][J] == 0 || grid[I][J] > grid[cur_i][cur_j] + 1){\n grid[I][J] = grid[cur_i][cur_j] + 1;\n queue.push({I,J});\n }\n }\n }\n }\n int find_path(vector<vector<int>>& grid){\n priority_queue<tuple<int,int,int>> queue;\n queue.push({grid[0][0],0,0});\n int di[] = {0,-1,0,1};\n int dj[] = {1,0,-1,0};\n int n = grid.size(), min_dist = n;\n vector<vector<bool>> visit(n, vector<bool>(n,false));\n visit[0][0] = true;\n while(queue.size() > 0){\n auto [dist, cur_i, cur_j] = queue.top(); queue.pop();\n min_dist = min(min_dist,grid[cur_i][cur_j]);\n if(cur_i == n-1 && cur_j == n-1) return min_dist-1;\n for(int cnt = 0; cnt < 4; ++cnt){\n int I = cur_i + di[cnt], J = cur_j + dj[cnt];\n if(I < 0 || I >= n || J < 0 || J >= n) continue;\n if(visit[I][J]) continue; \n queue.push({grid[I][J],I,J});\n visit[I][J] = true;\n }\n }\n\n return min_dist-1;\n }\n int maximumSafenessFactor(vector<vector<int>>& grid) {\n int n = grid.size();\n if(grid[0][0] || grid[n - 1][n - 1]) return 0;\n cal_dist(grid);\n \n // for(int i = 0; i < n; ++i){\n // for(int j = 0; j < n; ++j)\n // cout << \" \" << grid[i][j];\n // cout << endl;\n // }\n return find_path(grid);\n }\n};",
"memory": "114806"
} |
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 <code>(0, 0)</code>. In one move, you can move to any adjacent cell in the grid, including cells containing thieves.</p>
<p>The <strong>safeness factor</strong> of a path on the grid is defined as the <strong>minimum</strong> manhattan distance from any cell in the path to any thief in the grid.</p>
<p>Return <em>the <strong>maximum safeness factor</strong> of all paths leading to cell </em><code>(n - 1, n - 1)</code><em>.</em></p>
<p>An <strong>adjacent</strong> cell of cell <code>(r, c)</code>, is one of the cells <code>(r, c + 1)</code>, <code>(r, c - 1)</code>, <code>(r + 1, c)</code> and <code>(r - 1, c)</code> if it exists.</p>
<p>The <strong>Manhattan distance</strong> between two cells <code>(a, b)</code> and <code>(x, y)</code> is equal to <code>|a - x| + |b - y|</code>, where <code>|val|</code> denotes the absolute value of val.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example1.png" style="width: 362px; height: 242px;" />
<pre>
<strong>Input:</strong> grid = [[1,0,0],[0,0,0],[0,0,1]]
<strong>Output:</strong> 0
<strong>Explanation:</strong> All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example2.png" style="width: 362px; height: 242px;" />
<pre>
<strong>Input:</strong> grid = [[0,0,1],[0,0,0],[0,0,0]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since:
- The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2.
It can be shown that there are no other paths with a higher safeness factor.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example3.png" style="width: 362px; height: 242px;" />
<pre>
<strong>Input:</strong> grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since:
- The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2.
- The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2.
It can be shown that there are no other paths with a higher safeness factor.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length == n <= 400</code></li>
<li><code>grid[i].length == n</code></li>
<li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li>
<li>There is at least one thief in the <code>grid</code>.</li>
</ul>
| 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 // Initialize BFS queue with thieves and set their safeness to zero\n queue<pair<int,int>> q;\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n if (grid[i][j] == 0) {\n grid[i][j] = -1;\n } else {\n q.emplace(i, j);\n grid[i][j] = 0;\n }\n }\n }\n\n // Perform BFS inplace and update the safeness factor of each grid\n while(!q.empty()) {\n auto [i, j] = q.front();\n q.pop();\n int currDist = grid[i][j] + 1;\n for (auto [dx,dy] : directions) {\n if (isValid(i + dx, j + dy, n) && grid[i+dx][j+dy] == -1) {\n grid[i+dx][j+dy] = currDist;\n q.emplace(i+dx,j+dy);\n }\n }\n }\n \n // To find max safe path, greedily choose max safeness factor based on neighbors\n typedef pair<int,pair<int,int>> entry;\n priority_queue<entry> pq;\n pq.push({grid[0][0], {0,0}});\n \n while (!pq.empty()) {\n int maxSafeness = pq.top().first;\n auto [i,j] = pq.top().second;\n pq.pop();\n\n if (grid[i][j] == -1) continue;\n\n grid[i][j] = -1;\n if (i == n-1 && j == n-1) {\n return maxSafeness;\n }\n \n for (auto [dx,dy] : directions) {\n int newX = i+dx;\n int newY = j+dy;\n if (isValid(newX, newY, n) && grid[newX][newY] != -1) {\n pq.push({min(maxSafeness, grid[newX][newY]), {newX,newY}});\n }\n }\n }\n\n return -1;\n }\n};",
"memory": "119488"
} |
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 <code>(0, 0)</code>. In one move, you can move to any adjacent cell in the grid, including cells containing thieves.</p>
<p>The <strong>safeness factor</strong> of a path on the grid is defined as the <strong>minimum</strong> manhattan distance from any cell in the path to any thief in the grid.</p>
<p>Return <em>the <strong>maximum safeness factor</strong> of all paths leading to cell </em><code>(n - 1, n - 1)</code><em>.</em></p>
<p>An <strong>adjacent</strong> cell of cell <code>(r, c)</code>, is one of the cells <code>(r, c + 1)</code>, <code>(r, c - 1)</code>, <code>(r + 1, c)</code> and <code>(r - 1, c)</code> if it exists.</p>
<p>The <strong>Manhattan distance</strong> between two cells <code>(a, b)</code> and <code>(x, y)</code> is equal to <code>|a - x| + |b - y|</code>, where <code>|val|</code> denotes the absolute value of val.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example1.png" style="width: 362px; height: 242px;" />
<pre>
<strong>Input:</strong> grid = [[1,0,0],[0,0,0],[0,0,1]]
<strong>Output:</strong> 0
<strong>Explanation:</strong> All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example2.png" style="width: 362px; height: 242px;" />
<pre>
<strong>Input:</strong> grid = [[0,0,1],[0,0,0],[0,0,0]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since:
- The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2.
It can be shown that there are no other paths with a higher safeness factor.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example3.png" style="width: 362px; height: 242px;" />
<pre>
<strong>Input:</strong> grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since:
- The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2.
- The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2.
It can be shown that there are no other paths with a higher safeness factor.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length == n <= 400</code></li>
<li><code>grid[i].length == n</code></li>
<li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li>
<li>There is at least one thief in the <code>grid</code>.</li>
</ul>
| 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, 1}, {0, -1}, {1, 0}, {-1, 0}};\n\n queue<pair<int, int>> thievesGrids;\n for (int row = 0; row < nRows; row++) {\n for (int col = 0; col < nCols; col++) {\n if (grid[row][col] == 1) {\n thievesGrids.emplace(row, col);\n grid[row][col] = 0; // Mark thief cell with 0\n } else {\n grid[row][col] = -1; // Mark empty cell with -1\n }\n }\n }\n\n bfs(grid, thievesGrids, nRows, nCols, directions);\n return calculateSafeness(grid, nRows, nCols, directions);\n }\n\nprivate:\n void bfs(vector<vector<int>>& grid, queue<pair<int, int>>& thievesGrids, int nRows, int nCols, const vector<pair<int, int>>& directions) {\n while (!thievesGrids.empty()) {\n auto [row, col] = thievesGrids.front();\n thievesGrids.pop();\n int value = grid[row][col];\n\n for (const auto& direction : directions) {\n int newRow = row + direction.first;\n int newCol = col + direction.second;\n if (inBound(newRow, newCol, nRows, nCols) && grid[newRow][newCol] == -1) {\n grid[newRow][newCol] = value + 1;\n thievesGrids.emplace(newRow, newCol);\n }\n }\n }\n }\n\n int calculateSafeness(vector<vector<int>>& grid, int nRows, int nCols, const vector<pair<int, int>>& directions) {\n priority_queue<tuple<int, int, int>> priorityQueue;\n priorityQueue.emplace(grid[0][0], 0, 0);\n vector<vector<bool>> visited(nRows, vector<bool>(nCols, false));\n visited[0][0] = true;\n\n while (!priorityQueue.empty()) {\n auto [safeness, row, col] = priorityQueue.top();\n priorityQueue.pop();\n\n if (row == nRows - 1 && col == nCols - 1) {\n return safeness;\n }\n\n for (const auto& direction : directions) {\n int newRow = row + direction.first;\n int newCol = col + direction.second;\n if (inBound(newRow, newCol, nRows, nCols) && !visited[newRow][newCol]) {\n visited[newRow][newCol] = true;\n priorityQueue.emplace(min(safeness, grid[newRow][newCol]), newRow, newCol);\n }\n }\n }\n\n return -1;\n }\n\n bool inBound(int row, int col, int nRows, int nCols) {\n return 0 <= row && row < nRows && 0 <= col && col < nCols;\n }\n};",
"memory": "119488"
} |
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 <code>(0, 0)</code>. In one move, you can move to any adjacent cell in the grid, including cells containing thieves.</p>
<p>The <strong>safeness factor</strong> of a path on the grid is defined as the <strong>minimum</strong> manhattan distance from any cell in the path to any thief in the grid.</p>
<p>Return <em>the <strong>maximum safeness factor</strong> of all paths leading to cell </em><code>(n - 1, n - 1)</code><em>.</em></p>
<p>An <strong>adjacent</strong> cell of cell <code>(r, c)</code>, is one of the cells <code>(r, c + 1)</code>, <code>(r, c - 1)</code>, <code>(r + 1, c)</code> and <code>(r - 1, c)</code> if it exists.</p>
<p>The <strong>Manhattan distance</strong> between two cells <code>(a, b)</code> and <code>(x, y)</code> is equal to <code>|a - x| + |b - y|</code>, where <code>|val|</code> denotes the absolute value of val.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example1.png" style="width: 362px; height: 242px;" />
<pre>
<strong>Input:</strong> grid = [[1,0,0],[0,0,0],[0,0,1]]
<strong>Output:</strong> 0
<strong>Explanation:</strong> All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example2.png" style="width: 362px; height: 242px;" />
<pre>
<strong>Input:</strong> grid = [[0,0,1],[0,0,0],[0,0,0]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since:
- The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2.
It can be shown that there are no other paths with a higher safeness factor.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example3.png" style="width: 362px; height: 242px;" />
<pre>
<strong>Input:</strong> grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since:
- The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2.
- The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2.
It can be shown that there are no other paths with a higher safeness factor.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length == n <= 400</code></li>
<li><code>grid[i].length == n</code></li>
<li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li>
<li>There is at least one thief in the <code>grid</code>.</li>
</ul>
| 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> e{n - 1, n - 1};\n int min_d, x, y;\n vector<vector<bool>> visited(n, vector<bool>(n));\n vector<pair<int, int>> dir{{0, 1}, {0, -1}, {1, 0}, {-1, 0}};\n\n Q.push({grid[0][0], {0, 0}});\n visited[0][0] = true;\n while (!Q.empty()) {\n min_d = Q.top().first;\n q = Q.top().second;\n cout << min_d << ' ' << q.first << ' ' << q.second << endl;\n\n Q.pop();\n\n if (q == e) {\n return min_d - 1;\n }\n for (auto d : dir) {\n x = q.first + d.first;\n y = q.second + d.second;\n if (is_neighbor(x, y, n)) {\n if (!visited[x][y]) {\n visited[x][y] = true;\n Q.push({min(min_d, grid[x][y]), {x, y}});\n }\n }\n }\n }\n return 0;\n }\n void label_shortest_dist(vector<vector<int>>& grid) {\n int x, y;\n int n = grid.size();\n\n queue<pair<int, int>> Q;\n pair<int, int> q;\n\n vector<pair<int, int>> dir{{0, 1}, {0, -1}, {1, 0}, {-1, 0}};\n\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 }\n }\n\n while (!Q.empty()) {\n q = Q.front();\n Q.pop();\n\n for (auto d : dir) {\n x = q.first + d.first;\n y = q.second + d.second;\n if (is_neighbor(x, y, n)) {\n if (grid[x][y] == 0) {\n grid[x][y] = grid[q.first][q.second] + 1;\n Q.push({x, y});\n }\n }\n }\n }\n }\n int maximumSafenessFactor(vector<vector<int>>& grid) {\n\n int n = grid.size();\n if (grid[0][0] == 1 || grid[n - 1][n - 1] == 1) {\n return 0;\n }\n\n label_shortest_dist(grid);\n\n return bfs(grid);\n }\n};",
"memory": "124171"
} |
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 <code>(0, 0)</code>. In one move, you can move to any adjacent cell in the grid, including cells containing thieves.</p>
<p>The <strong>safeness factor</strong> of a path on the grid is defined as the <strong>minimum</strong> manhattan distance from any cell in the path to any thief in the grid.</p>
<p>Return <em>the <strong>maximum safeness factor</strong> of all paths leading to cell </em><code>(n - 1, n - 1)</code><em>.</em></p>
<p>An <strong>adjacent</strong> cell of cell <code>(r, c)</code>, is one of the cells <code>(r, c + 1)</code>, <code>(r, c - 1)</code>, <code>(r + 1, c)</code> and <code>(r - 1, c)</code> if it exists.</p>
<p>The <strong>Manhattan distance</strong> between two cells <code>(a, b)</code> and <code>(x, y)</code> is equal to <code>|a - x| + |b - y|</code>, where <code>|val|</code> denotes the absolute value of val.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example1.png" style="width: 362px; height: 242px;" />
<pre>
<strong>Input:</strong> grid = [[1,0,0],[0,0,0],[0,0,1]]
<strong>Output:</strong> 0
<strong>Explanation:</strong> All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example2.png" style="width: 362px; height: 242px;" />
<pre>
<strong>Input:</strong> grid = [[0,0,1],[0,0,0],[0,0,0]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since:
- The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2.
It can be shown that there are no other paths with a higher safeness factor.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example3.png" style="width: 362px; height: 242px;" />
<pre>
<strong>Input:</strong> grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since:
- The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2.
- The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2.
It can be shown that there are no other paths with a higher safeness factor.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length == n <= 400</code></li>
<li><code>grid[i].length == n</code></li>
<li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li>
<li>There is at least one thief in the <code>grid</code>.</li>
</ul>
| 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 }\n }\n return ans;\n }\n int maximumSafenessFactor(vector<vector<int>>& grid) {\n int n = grid.size();\n vector<vector<int>> md(n, vector<int>(n, 3*n));\n md[0][0] = findManhattan(grid, 0, 0);\n for(int i = 0; i<n; i++) {\n for(int j = 0; j<n; j++) {\n int tmp = 3*n;\n if (grid[i][j] == 1) tmp = 0;\n if (i>0) tmp = min(tmp, md[i-1][j]+1);\n if (j>0) tmp = min(tmp, md[i][j-1]+1);\n md[i][j] = min(md[i][j], tmp);\n }\n }\n md[n-1][n-1] = findManhattan(grid, n-1, n-1);\n for(int i = n-1; i>=0; i--) {\n for(int j = n-1; j>=0; j--) {\n int tmp = 3*n;\n if (grid[i][j] == 1) tmp = 0;\n if (i<n-1) tmp = min(tmp, md[i+1][j]+1);\n if (j<n-1) tmp = min(tmp, md[i][j+1]+1);\n md[i][j] = min(md[i][j], tmp);\n }\n }\n vector<vector<int>> ans(n, vector<int>(n, -1));\n priority_queue<pair<int, pair<int,int>>> pq;\n pq.push({md[0][0], {0,0}});\n while(!pq.empty()) {\n pair<int, pair<int,int>> p = pq.top();\n pq.pop();\n int sf = p.first;\n int pr = p.second.first;\n int pc = p.second.second;\n if (sf <= ans[pr][pc]) continue;\n ans[pr][pc] = sf;\n if (pr > 0) pq.push({min(sf, md[pr-1][pc]),{pr-1, pc}});\n if (pc > 0) pq.push({min(sf, md[pr][pc-1]),{pr, pc-1}});\n if (pr < n-1) pq.push({min(sf, md[pr+1][pc]),{pr+1, pc}});\n if (pc < n-1) pq.push({min(sf, md[pr][pc+1]),{pr, pc+1}});\n }\n return ans[n-1][n-1];\n }\n};",
"memory": "124171"
} |
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 <code>(0, 0)</code>. In one move, you can move to any adjacent cell in the grid, including cells containing thieves.</p>
<p>The <strong>safeness factor</strong> of a path on the grid is defined as the <strong>minimum</strong> manhattan distance from any cell in the path to any thief in the grid.</p>
<p>Return <em>the <strong>maximum safeness factor</strong> of all paths leading to cell </em><code>(n - 1, n - 1)</code><em>.</em></p>
<p>An <strong>adjacent</strong> cell of cell <code>(r, c)</code>, is one of the cells <code>(r, c + 1)</code>, <code>(r, c - 1)</code>, <code>(r + 1, c)</code> and <code>(r - 1, c)</code> if it exists.</p>
<p>The <strong>Manhattan distance</strong> between two cells <code>(a, b)</code> and <code>(x, y)</code> is equal to <code>|a - x| + |b - y|</code>, where <code>|val|</code> denotes the absolute value of val.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example1.png" style="width: 362px; height: 242px;" />
<pre>
<strong>Input:</strong> grid = [[1,0,0],[0,0,0],[0,0,1]]
<strong>Output:</strong> 0
<strong>Explanation:</strong> All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example2.png" style="width: 362px; height: 242px;" />
<pre>
<strong>Input:</strong> grid = [[0,0,1],[0,0,0],[0,0,0]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since:
- The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2.
It can be shown that there are no other paths with a higher safeness factor.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example3.png" style="width: 362px; height: 242px;" />
<pre>
<strong>Input:</strong> grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since:
- The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2.
- The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2.
It can be shown that there are no other paths with a higher safeness factor.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length == n <= 400</code></li>
<li><code>grid[i].length == n</code></li>
<li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li>
<li>There is at least one thief in the <code>grid</code>.</li>
</ul>
| 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 for( int j = 0 ; j < m ; j++){\n cout << grid[i][j] << \" \";\n }\n cout << \"\\n\";\n }\n }\n\n int maximumSafenessFactor(vector<vector<int>>& grid) {\n int n = grid.size(), m = grid[0].size();\n vector<vector<int>> cost(n, vector <int> (m,-1));\n queue <pi> q;\n\n for( int i = 0 ; i < n ; i++){\n for( int j = 0 ; j < m ; j++){\n if( grid[i][j] == 1 ){\n grid[i][j] = 0;\n q.push({i,j});\n }else{\n grid[i][j] = -1;\n }\n }\n }\n\n // print2D(grid);\n // cout << q.size() << \"\\n\";\n int level = 1;\n\n while( !q.empty()){\n int sz = q.size();\n while( sz--){\n auto [y,x] = q.front();\n q.pop();\n // cout << y << \" \" << x << \" parent \\n\";\n\n for( auto &[dy,dx] : mover ){\n int i = y + dy , j = dx + x;\n // cout << i << \" \" << j << \"\\n\";\n if( min(i,j) < 0 || i >= n || j >= m || grid[i][j] != -1 )\n continue;\n grid[i][j] = level;\n q.push({i,j});\n }\n }\n level++;\n }\n \n priority_queue <ppi> pq;\n\n pq.push({grid[0][0], {0,0}});\n cost[0][0] = grid[0][0];\n\n // print2D(grid);\n\n while( !pq.empty()){\n auto [val, cr] = pq.top();\n pq.pop();\n\n auto [y,x] = cr;\n // cout << y << \" \" << x << \"\\n\";\n\n for( auto &[dy,dx] : mover ){\n int i = dy + y, j = x + dx;\n \n if( min(i,j) < 0 || i >= n || j >= m )\n continue;\n // cout << i << \" \" << j << \" child \";\n int newCost = min(grid[i][j], val);\n // cout << newCost << \"\\n\";\n if( cost[i][j] < newCost){\n cost[i][j] = newCost;\n pq.push({newCost,{i,j}});\n }\n }\n }\n\n // print2D(cost);\n return cost[n-1][m-1];\n\n }\n};",
"memory": "128853"
} |
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 <code>(0, 0)</code>. In one move, you can move to any adjacent cell in the grid, including cells containing thieves.</p>
<p>The <strong>safeness factor</strong> of a path on the grid is defined as the <strong>minimum</strong> manhattan distance from any cell in the path to any thief in the grid.</p>
<p>Return <em>the <strong>maximum safeness factor</strong> of all paths leading to cell </em><code>(n - 1, n - 1)</code><em>.</em></p>
<p>An <strong>adjacent</strong> cell of cell <code>(r, c)</code>, is one of the cells <code>(r, c + 1)</code>, <code>(r, c - 1)</code>, <code>(r + 1, c)</code> and <code>(r - 1, c)</code> if it exists.</p>
<p>The <strong>Manhattan distance</strong> between two cells <code>(a, b)</code> and <code>(x, y)</code> is equal to <code>|a - x| + |b - y|</code>, where <code>|val|</code> denotes the absolute value of val.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example1.png" style="width: 362px; height: 242px;" />
<pre>
<strong>Input:</strong> grid = [[1,0,0],[0,0,0],[0,0,1]]
<strong>Output:</strong> 0
<strong>Explanation:</strong> All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example2.png" style="width: 362px; height: 242px;" />
<pre>
<strong>Input:</strong> grid = [[0,0,1],[0,0,0],[0,0,0]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since:
- The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2.
It can be shown that there are no other paths with a higher safeness factor.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example3.png" style="width: 362px; height: 242px;" />
<pre>
<strong>Input:</strong> grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since:
- The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2.
- The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2.
It can be shown that there are no other paths with a higher safeness factor.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length == n <= 400</code></li>
<li><code>grid[i].length == n</code></li>
<li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li>
<li>There is at least one thief in the <code>grid</code>.</li>
</ul>
| 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, int>> q;\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 \n int dx[] = {0, 1, 0, -1};\n int dy[] = {1, 0, -1, 0};\n \n while(!q.empty()){\n auto [x, y] = q.front();\n q.pop();\n for(int i = 0; i < 4; i++){\n int nx = x + dx[i];\n int ny = y + dy[i];\n if(nx < 0 || nx >= n || ny < 0 || ny >= n || matrix[nx][ny])\n continue;\n matrix[nx][ny] = matrix[x][y] + 1;\n q.push({nx, ny});\n }\n }\n\n for(int i = 0; i < n; i++){\n for(int j = 0; j < n; j++){\n matrix[i][j] -= 1;\n }\n }\n\n // second bfs on matrix: find path with max score from (0,0) to (n-1,n-1)\n priority_queue<tuple<int, int, int>> pq;\n\n int max_score = matrix[0][0];\n pq.push({matrix[0][0], 0, 0});\n matrix[0][0] = -1;\n\n bool arrived = false;\n while(!arrived){\n auto [s, x, y] = pq.top();\n pq.pop();\n\n max_score = min(max_score, s);\n if(x == n - 1 && y == n - 1)\n break;\n\n for(int i = 0; i < 4; i++){\n int nx = x + dx[i];\n int ny = y + dy[i];\n if(nx < 0 || nx >= n || ny < 0 || ny >= n || matrix[nx][ny] == -1)\n continue;\n \n pq.push({matrix[nx][ny], nx, ny});\n matrix[nx][ny] = -1;\n }\n }\n\n return max_score;\n }\n};",
"memory": "128853"
} |
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 <code>(0, 0)</code>. In one move, you can move to any adjacent cell in the grid, including cells containing thieves.</p>
<p>The <strong>safeness factor</strong> of a path on the grid is defined as the <strong>minimum</strong> manhattan distance from any cell in the path to any thief in the grid.</p>
<p>Return <em>the <strong>maximum safeness factor</strong> of all paths leading to cell </em><code>(n - 1, n - 1)</code><em>.</em></p>
<p>An <strong>adjacent</strong> cell of cell <code>(r, c)</code>, is one of the cells <code>(r, c + 1)</code>, <code>(r, c - 1)</code>, <code>(r + 1, c)</code> and <code>(r - 1, c)</code> if it exists.</p>
<p>The <strong>Manhattan distance</strong> between two cells <code>(a, b)</code> and <code>(x, y)</code> is equal to <code>|a - x| + |b - y|</code>, where <code>|val|</code> denotes the absolute value of val.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example1.png" style="width: 362px; height: 242px;" />
<pre>
<strong>Input:</strong> grid = [[1,0,0],[0,0,0],[0,0,1]]
<strong>Output:</strong> 0
<strong>Explanation:</strong> All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example2.png" style="width: 362px; height: 242px;" />
<pre>
<strong>Input:</strong> grid = [[0,0,1],[0,0,0],[0,0,0]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since:
- The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2.
It can be shown that there are no other paths with a higher safeness factor.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example3.png" style="width: 362px; height: 242px;" />
<pre>
<strong>Input:</strong> grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since:
- The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2.
- The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2.
It can be shown that there are no other paths with a higher safeness factor.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length == n <= 400</code></li>
<li><code>grid[i].length == n</code></li>
<li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li>
<li>There is at least one thief in the <code>grid</code>.</li>
</ul>
| 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 if (grid[i][j]) {\n score[i][j] = 0;\n q.push({i, j});\n }\n }\n }\n\n while (!q.empty()) {\n auto t = q.front();\n q.pop();\n\n int x = t.first, y = t.second;\n int s = score[x][y];\n\n for (int i = 0; i < 4; i++) {\n int newX = x + roww[i];\n int newY = y + coll[i];\n\n if (newX >= 0 && newX < n && newY >= 0 && newY < n &&\n score[newX][newY] > 1 + s) {\n\n score[newX][newY] = 1 + s;\n q.push({newX, newY});\n }\n }\n }\n }\n int maximumSafenessFactor(vector<vector<int>>& grid) {\n int n = grid.size();\n if (grid[0][0] || grid[n - 1][n - 1])\n return 0;\n\n vector<vector<int>> score(n, vector<int>(n, INT_MAX));\n bfs(grid, score, n);\n vector<vector<bool>> vis(n, vector<bool>(n, false));\n priority_queue<pair<int, pair<int, int>>> pq;\n pq.push({score[0][0], {0, 0}});\n\n vector<vector<int>> safe(n, vector<int>(n, -1e8));\n safe[0][0] = score[0][0];\n while (!pq.empty()) {\n int dist = pq.top().first;\n int row = pq.top().second.first;\n int col = pq.top().second.second;\n pq.pop();\n if (row == n - 1 && col == n - 1)\n return dist;\n int drow[4] = {-1, 0, 1, 0};\n int dcol[4] = {0, 1, 0, -1};\n for (int k = 0; k < 4; k++) {\n int nr = row + drow[k];\n int nc = col + dcol[k];\n if (nr < n && nr >= 0 && nc >= 0 && nc < n) {\n\n int check = min(score[nr][nc], dist);\n if (check > safe[nr][nc]) {\n safe[nr][nc] = check;\n pq.push({safe[nr][nc], {nr, nc}});\n }\n }\n }\n }\n return -1;\n }\n};",
"memory": "133536"
} |
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 <code>(0, 0)</code>. In one move, you can move to any adjacent cell in the grid, including cells containing thieves.</p>
<p>The <strong>safeness factor</strong> of a path on the grid is defined as the <strong>minimum</strong> manhattan distance from any cell in the path to any thief in the grid.</p>
<p>Return <em>the <strong>maximum safeness factor</strong> of all paths leading to cell </em><code>(n - 1, n - 1)</code><em>.</em></p>
<p>An <strong>adjacent</strong> cell of cell <code>(r, c)</code>, is one of the cells <code>(r, c + 1)</code>, <code>(r, c - 1)</code>, <code>(r + 1, c)</code> and <code>(r - 1, c)</code> if it exists.</p>
<p>The <strong>Manhattan distance</strong> between two cells <code>(a, b)</code> and <code>(x, y)</code> is equal to <code>|a - x| + |b - y|</code>, where <code>|val|</code> denotes the absolute value of val.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example1.png" style="width: 362px; height: 242px;" />
<pre>
<strong>Input:</strong> grid = [[1,0,0],[0,0,0],[0,0,1]]
<strong>Output:</strong> 0
<strong>Explanation:</strong> All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example2.png" style="width: 362px; height: 242px;" />
<pre>
<strong>Input:</strong> grid = [[0,0,1],[0,0,0],[0,0,0]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since:
- The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2.
It can be shown that there are no other paths with a higher safeness factor.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example3.png" style="width: 362px; height: 242px;" />
<pre>
<strong>Input:</strong> grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since:
- The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2.
- The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2.
It can be shown that there are no other paths with a higher safeness factor.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length == n <= 400</code></li>
<li><code>grid[i].length == n</code></li>
<li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li>
<li>There is at least one thief in the <code>grid</code>.</li>
</ul>
| 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();\n // while(siz--){\n // auto [x,y] = q.front();\n // q.pop();\n // dis[x][y] = lvl;\n // for (int i=0; i<4; i++) {\n // int nx = x+dir[i].first, ny = y+dir[i].second;\n // if (nx>=0 && nx<n && ny>=0 && ny<n && !vis[nx][ny]) {\n // q.push({nx,ny});\n // vis[nx][ny] = true;\n // }\n // }\n // }\n // lvl++;\n // }\n // }\n\n // int findSf(vector<vector<int>>& dis, int n){\n // priority_queue<pair<int,pair<int,int>>> pq;\n // vector<vector<bool>> vis (n,vector<bool>(n));\n // pq.push({dis[0][0],{0,0}});\n // vis[0][0] = true;\n // vector<pair<int, int>> dir = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};\n // int ans = 1e9;\n // while(!pq.empty()){\n // auto [d,cord] = pq.top();\n // pq.pop();\n // int x = cord.first, y = cord.second;\n // ans = min(ans,dis[x][y]);\n // if(x==n-1 && y==n-1) return ans;\n // for(int i=0; i<4; i++){\n // int nx = x+dir[i].first, ny = y+dir[i].second;\n // if(nx>=0 && nx<n && ny>=0 && ny<n &&!vis[nx][ny]){\n // vis[nx][ny] = true;\n // pq.push({dis[nx][ny],{nx,ny}});\n // }\n // }\n // }\n // return ans;\n // }\n\n// int maximumSafenessFactor(vector<vector<int>>& grid) {\n// int n = grid.size();\n \n// queue<pair<int, int>> q;\n// vector<vector<bool>> vis (n,vector<bool>(n));\n\n// for (int r = 0; r < n; r++) {\n// for (int c = 0; c < n; c++) {\n// if (grid[r][c] == 1) {\n// vis[r][c] = true;\n// q.push({r, c});\n// }\n// }\n// }\n \n// vector<vector<int>> dis (n,vector<int>(n));\n\n// buildDis(q,n,dis,vis);\n// int ans = findSf(dis,n);\n\n// return ans;\n// }\n// };\n\n void buildDis(queue<pair<int,int>>& q, int n, vector<vector<int>>& dis, vector<vector<bool>>& vis){\n\n int dx[4] = {0,0,1,-1} ;\n int dy[4] = {1,-1,0,0} ;\n int dist = 0 ;\n\n while( !q.empty() ){\n int size = q.size() ;\n while( size-- ){\n int x = q.front().first ;\n int y = q.front().second ;\n dis[x][y] = dist ;\n q.pop() ;\n for( int i=0 ; i<4 ; i++ ){\n int new_x = x+dx[i] ;\n int new_y = y+dy[i] ;\n if( new_x>=0 && new_y>=0 && new_x < n && new_y < n && !vis[new_x][new_y] ){\n vis[new_x][new_y] = 1 ;\n q.push( { new_x , new_y } ) ;\n }\n }\n }\n dist++ ;\n }\n }\n\n int findSafest( vector<vector<int>>& dist , int n ){\n\n vector<vector<bool>> vis( n , vector<bool>( n , 0 ) ) ;\n\n int dx[4] = {0,0,-1,1} ;\n int dy[4] = {1,-1,0,0} ;\n\n priority_queue<pair<int,pair<int,int>>> pq ;\n pq.push( { dist[0][0] , {0,0} } ) ;\n vis[0][0] = true ;\n\n int ans = dist[0][0] ;\n\n while(!pq.empty()){\n\n int min_safe_dist = pq.top().first ;\n pair<int,int> curr = pq.top().second ;\n int x = curr.first ; int y = curr.second ;\n pq.pop() ;\n\n ans = min( ans , min_safe_dist ) ;\n if( curr.first == n-1 && curr.second == n-1 ) break ;\n\n for( int i=0 ; i<4 ; i++ ){\n int new_x = x+dx[i] ;\n int new_y = y+dy[i] ;\n if( new_x>=0 && new_y>=0 && new_x<n && new_y<n && !vis[new_x][new_y] ){\n vis[new_x][new_y] = 1 ;\n pq.push( { min( min_safe_dist , dist[new_x][new_y] ) , { new_x , new_y } } ) ;\n }\n }\n }\n return ans ;\n }\n\n int maximumSafenessFactor(vector<vector<int>>& grid) {\n int n = grid.size() ;\n\n queue<pair<int,int>> q ;\n vector<vector<bool>>vis( n , vector<bool> ( n , 0 ) ) ;\n\n for( int i=0 ; i<n ; i++ ){\n for( int j=0 ; j<n ; j++ ){\n if( grid[i][j] == 1 ){\n vis[i][j] = 1 ; \n q.push({i,j}) ;\n }\n }\n }\n\n vector<vector<int>> dis( n, vector<int>( n , 0 ) ) ;\n\n buildDis( q , n , dis , vis ) ;\n int ans = findSafest( dis , n ) ;\n\n return ans;\n }\n};\n",
"memory": "133536"
} |
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 <code>(0, 0)</code>. In one move, you can move to any adjacent cell in the grid, including cells containing thieves.</p>
<p>The <strong>safeness factor</strong> of a path on the grid is defined as the <strong>minimum</strong> manhattan distance from any cell in the path to any thief in the grid.</p>
<p>Return <em>the <strong>maximum safeness factor</strong> of all paths leading to cell </em><code>(n - 1, n - 1)</code><em>.</em></p>
<p>An <strong>adjacent</strong> cell of cell <code>(r, c)</code>, is one of the cells <code>(r, c + 1)</code>, <code>(r, c - 1)</code>, <code>(r + 1, c)</code> and <code>(r - 1, c)</code> if it exists.</p>
<p>The <strong>Manhattan distance</strong> between two cells <code>(a, b)</code> and <code>(x, y)</code> is equal to <code>|a - x| + |b - y|</code>, where <code>|val|</code> denotes the absolute value of val.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example1.png" style="width: 362px; height: 242px;" />
<pre>
<strong>Input:</strong> grid = [[1,0,0],[0,0,0],[0,0,1]]
<strong>Output:</strong> 0
<strong>Explanation:</strong> All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example2.png" style="width: 362px; height: 242px;" />
<pre>
<strong>Input:</strong> grid = [[0,0,1],[0,0,0],[0,0,0]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since:
- The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2.
It can be shown that there are no other paths with a higher safeness factor.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example3.png" style="width: 362px; height: 242px;" />
<pre>
<strong>Input:</strong> grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since:
- The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2.
- The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2.
It can be shown that there are no other paths with a higher safeness factor.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length == n <= 400</code></li>
<li><code>grid[i].length == n</code></li>
<li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li>
<li>There is at least one thief in the <code>grid</code>.</li>
</ul>
| 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 constexpr inline bool in_bounds(int r, int c, int rows, int cols) const noexcept {\n return r >= 0 && r < rows && c >= 0 && c < cols;\n }\n\n int maximumSafenessFactor(vector<vector<int>>& grid) {\n const int ROWS = grid.size();\n const int COLS = grid.at(0).size();\n \n const int DR[] = {-1, 1, 0, 0};\n const int DC[] = {0, 0, 1, -1};\n \n\n // {Point, bfs depth}\n queue<Point> q;\n vector<vector<int>> safeness(ROWS, vector<int>(COLS, -1));\n for(int r = 0; r < ROWS; r++) {\n for(int c = 0; c < COLS; c++) {\n if (grid[r][c] == 1) {\n q.push({r, c});\n safeness[r][c] = 0;\n }\n }\n }\n\n // safeness[r][c] is the minimum distaince from (r, c) to any thief \n // on the grid\n while(!q.empty()) {\n auto [r, c] = q.front(); q.pop();\n\n for(int i = 0; i < 4; i++) {\n int new_r = r + DR[i];\n int new_c = c + DC[i];\n\n if (in_bounds(new_r, new_c, ROWS, COLS) && safeness[new_r][new_c] == -1) {\n safeness[new_r][new_c] = safeness[r][c] + 1;\n q.push({new_r, new_c});\n }\n }\n }\n\n for(int r = 0; r < ROWS; r++) {\n for(int c = 0; c < COLS; c++) {\n cout << safeness[r][c];\n }\n cout << endl;\n }\n cout << \"----------\" << endl;\n\n\n // N^2 log N\n // want max heap\n priority_queue<Node, vector<Node>, less<Node>> pq{};\n vector<vector<bool>> visited(ROWS, vector<bool>(COLS, false));\n\n pq.push(Node{ Point{0, 0}, safeness[0][0]});\n\n while(!pq.empty()) {\n Node n = pq.top(); pq.pop();\n const int r = n.point.r;\n const int c = n.point.c;\n const int score = n.score;\n\n if (r == ROWS - 1 && c == COLS - 1) return score;\n\n for(int i = 0; i < 4; i++) {\n int new_r = r + DR[i];\n int new_c = c + DC[i];\n\n if (in_bounds(new_r, new_c, ROWS, COLS) && !visited[new_r][new_c]) {\n visited[new_r][new_c] = true;\n\n int new_score = min(score, safeness[new_r][new_c]);\n pq.push(Node{Point{new_r, new_c}, new_score});\n }\n }\n }\n\n return -1;\n }\n};",
"memory": "138218"
} |
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 <code>(0, 0)</code>. In one move, you can move to any adjacent cell in the grid, including cells containing thieves.</p>
<p>The <strong>safeness factor</strong> of a path on the grid is defined as the <strong>minimum</strong> manhattan distance from any cell in the path to any thief in the grid.</p>
<p>Return <em>the <strong>maximum safeness factor</strong> of all paths leading to cell </em><code>(n - 1, n - 1)</code><em>.</em></p>
<p>An <strong>adjacent</strong> cell of cell <code>(r, c)</code>, is one of the cells <code>(r, c + 1)</code>, <code>(r, c - 1)</code>, <code>(r + 1, c)</code> and <code>(r - 1, c)</code> if it exists.</p>
<p>The <strong>Manhattan distance</strong> between two cells <code>(a, b)</code> and <code>(x, y)</code> is equal to <code>|a - x| + |b - y|</code>, where <code>|val|</code> denotes the absolute value of val.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example1.png" style="width: 362px; height: 242px;" />
<pre>
<strong>Input:</strong> grid = [[1,0,0],[0,0,0],[0,0,1]]
<strong>Output:</strong> 0
<strong>Explanation:</strong> All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example2.png" style="width: 362px; height: 242px;" />
<pre>
<strong>Input:</strong> grid = [[0,0,1],[0,0,0],[0,0,0]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since:
- The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2.
It can be shown that there are no other paths with a higher safeness factor.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example3.png" style="width: 362px; height: 242px;" />
<pre>
<strong>Input:</strong> grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since:
- The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2.
- The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2.
It can be shown that there are no other paths with a higher safeness factor.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length == n <= 400</code></li>
<li><code>grid[i].length == n</code></li>
<li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li>
<li>There is at least one thief in the <code>grid</code>.</li>
</ul>
| 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 queue<pair<int,int>> q;\n\n for(int i=0; i<n; i++) {\n for(int j=0; j<m; j++) {\n if(grid[i][j] == 1) {\n safeNessFactor[i][j] = 0;\n q.push({i, j});\n } \n }\n }\n\n\n while(!q.empty()) {\n auto [i, j] = q.front();\n q.pop();\n\n for(int k=0; k<4; k++) {\n int _i = i + dx[k], _j = j + dy[k];\n if(_i >= 0 && _i < n && _j >= 0 && _j < m && safeNessFactor[_i][_j] == 1e9) {\n safeNessFactor[_i][_j] = safeNessFactor[i][j] + 1;\n q.push({_i, _j});\n }\n }\n \n }\n return safeNessFactor;\n }\n\n int getMaxSafenessFactor(vector<vector<int>> &safenessFactor) {\n int n = safenessFactor.size(), m = safenessFactor[0].size();\n \n deque<pair<int, pair<int,int>>> q;\n vector<vector<bool>> vis(n, vector<bool> (m, false));\n\n vis[0][0] = true;\n q.push_back({safenessFactor[0][0], {0, 0}});\n\n int currentSafeness = safenessFactor[0][0];\n\n while(!q.empty()) {\n int safeNess = q.front().first;\n auto [i, j] = q.front().second;\n q.pop_front();\n\n currentSafeness = min(currentSafeness, safeNess);\n\n if(i == n-1 && j == m-1) {\n return currentSafeness;\n }\n\n for(int k=0; k<4; k++) {\n int _i = i + dx[k], _j = j + dy[k];\n if(_i >= 0 && _i < n && _j >= 0 && _j < m && vis[_i][_j] == false) {\n vis[_i][_j] = true;\n if(safenessFactor[_i][_j] < currentSafeness) {\n q.push_back({min(safenessFactor[_i][_j], currentSafeness), {_i, _j}});\n } else {\n q.push_front({currentSafeness, {_i, _j}});\n }\n }\n }\n }\n return -1;\n }\n\npublic:\n int maximumSafenessFactor(vector<vector<int>>& grid) {\n \n vector<vector<int>> safenessFactor = getSafenessFactor(grid);\n return getMaxSafenessFactor(safenessFactor);\n }\n};",
"memory": "138218"
} |
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 <code>(0, 0)</code>. In one move, you can move to any adjacent cell in the grid, including cells containing thieves.</p>
<p>The <strong>safeness factor</strong> of a path on the grid is defined as the <strong>minimum</strong> manhattan distance from any cell in the path to any thief in the grid.</p>
<p>Return <em>the <strong>maximum safeness factor</strong> of all paths leading to cell </em><code>(n - 1, n - 1)</code><em>.</em></p>
<p>An <strong>adjacent</strong> cell of cell <code>(r, c)</code>, is one of the cells <code>(r, c + 1)</code>, <code>(r, c - 1)</code>, <code>(r + 1, c)</code> and <code>(r - 1, c)</code> if it exists.</p>
<p>The <strong>Manhattan distance</strong> between two cells <code>(a, b)</code> and <code>(x, y)</code> is equal to <code>|a - x| + |b - y|</code>, where <code>|val|</code> denotes the absolute value of val.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example1.png" style="width: 362px; height: 242px;" />
<pre>
<strong>Input:</strong> grid = [[1,0,0],[0,0,0],[0,0,1]]
<strong>Output:</strong> 0
<strong>Explanation:</strong> All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example2.png" style="width: 362px; height: 242px;" />
<pre>
<strong>Input:</strong> grid = [[0,0,1],[0,0,0],[0,0,0]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since:
- The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2.
It can be shown that there are no other paths with a higher safeness factor.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example3.png" style="width: 362px; height: 242px;" />
<pre>
<strong>Input:</strong> grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since:
- The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2.
- The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2.
It can be shown that there are no other paths with a higher safeness factor.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length == n <= 400</code></li>
<li><code>grid[i].length == n</code></li>
<li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li>
<li>There is at least one thief in the <code>grid</code>.</li>
</ul>
| 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 vector<vector<int>> distance(n, vector<int>(m, INT_MIN));\n vector<vector<bool>> visited(n, vector<bool>(m, false));\n queue<pair<int, int>> q;\n int ans = INT_MAX;\n\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < m; j++) {\n if (grid[i][j] == 1) {\n grid[i][j] = 0;\n q.push({i, j});\n visited[i][j] = true;\n }\n }\n }\n\n while (!q.empty()) {\n pair<int, int> p = q.front();\n q.pop();\n int r = p.first;\n int c = p.second;\n\n int move_x[] = {-1, 1, 0, 0};\n int move_y[] = {0, 0, -1, 1};\n\n for (int i = 0; i < 4; i++) {\n int row = r + move_x[i];\n int col = c + move_y[i];\n\n if (row >= 0 && row < n && col >= 0 && col < m &&\n !visited[row][col]) {\n grid[row][col] = grid[r][c] + 1;\n q.push({row, col});\n visited[row][col] = true;\n }\n }\n }\n\n // for (int i = 0; i < n; i++) {\n // for (int j = 0; j < m; j++) {\n // cout<<grid[i][j]<<\" \";\n // }\n // cout<<\"\\n\";\n // }\n\n deque<pair<int, pair<int, int>>> pq;\n vector<vector<bool>> visited1(n, vector<bool>(m, false));\n\n pq.push_front({grid[0][0], {0, 0}});\n distance[0][0] = grid[0][0];\n visited1[0][0] = true;\n\n while (!pq.empty()) {\n pair<int, pair<int, int>> p = pq.front();\n pq.pop_front();\n int dis = p.first;\n int row = p.second.first;\n int col = p.second.second;\n\n //cout<<\"grid[row][col]: \"<<grid[row][col]<<\"\\n\";\n\n ans = min(ans, grid[row][col]);\n \n if(row == n-1 && col == m-1){\n //cout<<dis<<\"\\n\";\n //return dis;\n break;\n }\n\n // if (visited1[row][col]) {\n // continue;\n // }\n\n \n //cout<<grid[row][col]<<\" \";\n \n \n\n int move_x[] = {-1, 1, 0, 0};\n int move_y[] = {0, 0, 1, -1};\n\n for (int i = 0; i < 4; i++) {\n int r = row + move_x[i];\n int c = col + move_y[i];\n\n if (r >= 0 && r < n && c >= 0 && c < m &&\n !visited1[r][c]) {\n //distance[r][c] = dis + grid[r][c];\n if(grid[r][c] >= ans){\n pq.push_front({grid[r][c], {r,c}});\n }\n else{\n pq.push_back({dis, {r,c}});\n }\n //pq.push({min(grid[r][c],dis), {r, c}});\n visited1[r][c] = true;\n }\n }\n }\n\n //cout<<\"\\n\"<<distance[n-1][m-1];\n //cout<<\"ans is: \"<<ans<<\"\\n\";\n return ans;\n }\n};",
"memory": "142901"
} |
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 <code>(0, 0)</code>. In one move, you can move to any adjacent cell in the grid, including cells containing thieves.</p>
<p>The <strong>safeness factor</strong> of a path on the grid is defined as the <strong>minimum</strong> manhattan distance from any cell in the path to any thief in the grid.</p>
<p>Return <em>the <strong>maximum safeness factor</strong> of all paths leading to cell </em><code>(n - 1, n - 1)</code><em>.</em></p>
<p>An <strong>adjacent</strong> cell of cell <code>(r, c)</code>, is one of the cells <code>(r, c + 1)</code>, <code>(r, c - 1)</code>, <code>(r + 1, c)</code> and <code>(r - 1, c)</code> if it exists.</p>
<p>The <strong>Manhattan distance</strong> between two cells <code>(a, b)</code> and <code>(x, y)</code> is equal to <code>|a - x| + |b - y|</code>, where <code>|val|</code> denotes the absolute value of val.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example1.png" style="width: 362px; height: 242px;" />
<pre>
<strong>Input:</strong> grid = [[1,0,0],[0,0,0],[0,0,1]]
<strong>Output:</strong> 0
<strong>Explanation:</strong> All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example2.png" style="width: 362px; height: 242px;" />
<pre>
<strong>Input:</strong> grid = [[0,0,1],[0,0,0],[0,0,0]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since:
- The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2.
It can be shown that there are no other paths with a higher safeness factor.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example3.png" style="width: 362px; height: 242px;" />
<pre>
<strong>Input:</strong> grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since:
- The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2.
- The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2.
It can be shown that there are no other paths with a higher safeness factor.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length == n <= 400</code></li>
<li><code>grid[i].length == n</code></li>
<li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li>
<li>There is at least one thief in the <code>grid</code>.</li>
</ul>
| 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(r ,vector<bool>(c ,false));\n queue<pair<int,pair<int ,int>>>q;\n for(int i=0;i<r;i++){\n for(int j=0;j<c;j++){\n if(grid[i][j]==1 &&!vis[i][j] ){\n vis[i][j]=true;\n dis[i][j]=0;\n q.push({0 ,{i ,j}});\n }\n }\n }\n while(!q.empty()){\n auto it=q.front();\n q.pop();\n int cur_dis= it.first;\n int row =it.second.first;\n int col=it.second.second;\n dis[row][col]=cur_dis;\n for(int i=0;i<4;i++){\n int nrow = row+delr[i];\n int ncol = col+delc[i];\n if(nrow>=0 && ncol>=0 && nrow<r && ncol<c && !vis[nrow][ncol] ){\n vis[nrow][ncol]=true;\n q.push({cur_dis+1 ,{nrow , ncol}});\n }\n }\n }\n\n\n\n priority_queue< pair<int,pair<int,int>> >maxHeap;\n vector<vector<bool>>vis2(r ,vector<bool>(c ,false));\n vis[0][0]=true;\n int ans=0;\n maxHeap.push({dis[0][0],{0,0}});\n \n while(!maxHeap.empty()){\n auto it=maxHeap.top();\n maxHeap.pop();\n int d = it.first;\n int row=it.second.first;\n int col =it.second.second;\n if(row==r-1 && col==c-1){\n return d;\n }\n for(int k=0;k<4;k++){\n int nrow = row+delr[k];\n int ncol = col+delc[k];\n if( nrow>=0 && ncol >=0 && nrow<r && ncol <c && !vis2[nrow][ncol] ){\n maxHeap.push({ min(d ,dis[nrow][ncol]) ,{nrow ,ncol}});\n vis2[nrow][ncol]=true;\n }\n }\n }\n\n return ans;\n\n }\n};",
"memory": "142901"
} |
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 <code>(0, 0)</code>. In one move, you can move to any adjacent cell in the grid, including cells containing thieves.</p>
<p>The <strong>safeness factor</strong> of a path on the grid is defined as the <strong>minimum</strong> manhattan distance from any cell in the path to any thief in the grid.</p>
<p>Return <em>the <strong>maximum safeness factor</strong> of all paths leading to cell </em><code>(n - 1, n - 1)</code><em>.</em></p>
<p>An <strong>adjacent</strong> cell of cell <code>(r, c)</code>, is one of the cells <code>(r, c + 1)</code>, <code>(r, c - 1)</code>, <code>(r + 1, c)</code> and <code>(r - 1, c)</code> if it exists.</p>
<p>The <strong>Manhattan distance</strong> between two cells <code>(a, b)</code> and <code>(x, y)</code> is equal to <code>|a - x| + |b - y|</code>, where <code>|val|</code> denotes the absolute value of val.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example1.png" style="width: 362px; height: 242px;" />
<pre>
<strong>Input:</strong> grid = [[1,0,0],[0,0,0],[0,0,1]]
<strong>Output:</strong> 0
<strong>Explanation:</strong> All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example2.png" style="width: 362px; height: 242px;" />
<pre>
<strong>Input:</strong> grid = [[0,0,1],[0,0,0],[0,0,0]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since:
- The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2.
It can be shown that there are no other paths with a higher safeness factor.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example3.png" style="width: 362px; height: 242px;" />
<pre>
<strong>Input:</strong> grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since:
- The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2.
- The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2.
It can be shown that there are no other paths with a higher safeness factor.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length == n <= 400</code></li>
<li><code>grid[i].length == n</code></li>
<li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li>
<li>There is at least one thief in the <code>grid</code>.</li>
</ul>
| 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;i<m;i++)\n for(int j=0;j<n;j++)\n {\n if(arr[i][j] == 1) //If thief, insert into queue\n {\n q.push({0, {i,j}});\n vis[i][j] = 1;\n arr[i][j] = 0; //Dist from itself = 0\n }\n }\n \n while(!q.empty())\n {\n auto cur = q.front();\n q.pop();\n \n int curDist = cur.first;\n int x = cur.second.first;\n int y = cur.second.second;\n \n arr[x][y] = curDist;\n \n for(int i=0;i<4;i++)\n {\n int nx = x + dx[i];\n int ny = y + dy[i];\n \n if(nx>=0 and ny>=0 and nx<m and ny<n and !vis[nx][ny])\n {\n vis[nx][ny] = 1;\n q.push({curDist+1, {nx, ny}});\n }\n }\n }\n }\npublic:\n /*\n Combination of:\n 01 Matrix\n Path with Minimum Effort\n Walls and Gates\n 778. Swim in Rising Water\n */\n \n /*\n Thought Process:\n Dijkstra Modified to find the longest path, so here we will be using a visited array\n to visit each node only once.\n We maximise the min in each path using a MAX HEAP\n */\n \n //NEETCODE\n int maximumSafenessFactor(vector<vector<int>>& arr) {\n int m = arr.size(), n = arr[0].size();\n vector<int> dx{-1,0,1,0};\n vector<int> dy{0,1,0,-1};\n \n //Multisource BFS \n multisourceBFS(arr, dx, dy);\n \n //Modified Dijsktra\n priority_queue<pair<int,pair<int,int>>> pq;//Max heap -> {minManhattanDist in that path, {x,y}}\n vector<vector<int>> vis(m, vector<int>(n,0));\n int minManhattanDist = 0;\n \n pq.push({arr[0][0], {0,0}});\n vis[0][0] = 1;\n \n while(pq.size())\n {\n auto cur = pq.top();\n pq.pop();\n \n int curDist = cur.first;\n int x = cur.second.first;\n int y = cur.second.second;\n \n if(x == m-1 and y==n-1)\n return curDist;\n \n for(int i=0;i<4;i++)\n {\n int nx = x+dx[i];\n int ny = y+dy[i];\n \n if(nx>=0 and ny>=0 and nx<m and ny<n and !vis[nx][ny])\n {\n int newDist = arr[nx][ny];\n minManhattanDist = min(curDist, newDist);\n vis[nx][ny] = 1;\n pq.push({minManhattanDist, {nx, ny}});\n }\n }\n \n }\n \n return -1;\n \n }\n};",
"memory": "147583"
} |
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 <code>(0, 0)</code>. In one move, you can move to any adjacent cell in the grid, including cells containing thieves.</p>
<p>The <strong>safeness factor</strong> of a path on the grid is defined as the <strong>minimum</strong> manhattan distance from any cell in the path to any thief in the grid.</p>
<p>Return <em>the <strong>maximum safeness factor</strong> of all paths leading to cell </em><code>(n - 1, n - 1)</code><em>.</em></p>
<p>An <strong>adjacent</strong> cell of cell <code>(r, c)</code>, is one of the cells <code>(r, c + 1)</code>, <code>(r, c - 1)</code>, <code>(r + 1, c)</code> and <code>(r - 1, c)</code> if it exists.</p>
<p>The <strong>Manhattan distance</strong> between two cells <code>(a, b)</code> and <code>(x, y)</code> is equal to <code>|a - x| + |b - y|</code>, where <code>|val|</code> denotes the absolute value of val.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example1.png" style="width: 362px; height: 242px;" />
<pre>
<strong>Input:</strong> grid = [[1,0,0],[0,0,0],[0,0,1]]
<strong>Output:</strong> 0
<strong>Explanation:</strong> All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example2.png" style="width: 362px; height: 242px;" />
<pre>
<strong>Input:</strong> grid = [[0,0,1],[0,0,0],[0,0,0]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since:
- The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2.
It can be shown that there are no other paths with a higher safeness factor.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example3.png" style="width: 362px; height: 242px;" />
<pre>
<strong>Input:</strong> grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since:
- The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2.
- The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2.
It can be shown that there are no other paths with a higher safeness factor.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length == n <= 400</code></li>
<li><code>grid[i].length == n</code></li>
<li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li>
<li>There is at least one thief in the <code>grid</code>.</li>
</ul>
| 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(), m = grid[0].size();\n queue<pair<int, int>>qu;\n for(int i = 0; i < n; i++){\n for(int j = 0; j < m; j++){\n if(grid[i][j]){\n qu.push({i, j});\n grid[i][j] = 0;\n }\n else{\n grid[i][j] = -1;\n }\n }\n }\n while(qu.size()){\n auto [i, j] = qu.front(); qu.pop();\n for(int k = 0; k < 4; k++){\n int x = i + dx[k]; \n int y = j + dy[k];\n if(valid(x, y, grid) && grid[x][y] == -1){\n grid[x][y] = grid[i][j] + 1;\n qu.push({x, y});\n }\n }\n }\n }\n int id;\n vector<vector<int>>vis;\n bool dfs(int i, int j, int &md, vector<vector<int>>& grid){\n int n = grid.size(), m = grid[0].size();\n if(!valid(i, j, grid) || vis[i][j] == id || grid[i][j] < md)\n return 0;\n if(i == n - 1 && j == m - 1)\n return 1;\n vis[i][j] = id;\n bool ret = 0;\n for(int k = 0; k < 4; k++){\n int x = i + dx[k];\n int y = j + dy[k];\n ret = (ret || dfs(x, y, md, grid));\n }\n return ret;\n }\n int maximumSafenessFactor(vector<vector<int>>& grid) {\n int n = grid.size(), m = grid[0].size();\n bfs(m, grid);\n int l = 0, r = 400;\n vis = vector<vector<int>>(n, vector<int>(m, 0));\n while(l <= r){\n id++;\n int md = (l + r) >> 1;\n if(dfs(0, 0, md, grid))\n l = md + 1;\n else\n r = md - 1; \n }\n return l - 1;\n }\n};",
"memory": "147583"
} |
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 <code>(0, 0)</code>. In one move, you can move to any adjacent cell in the grid, including cells containing thieves.</p>
<p>The <strong>safeness factor</strong> of a path on the grid is defined as the <strong>minimum</strong> manhattan distance from any cell in the path to any thief in the grid.</p>
<p>Return <em>the <strong>maximum safeness factor</strong> of all paths leading to cell </em><code>(n - 1, n - 1)</code><em>.</em></p>
<p>An <strong>adjacent</strong> cell of cell <code>(r, c)</code>, is one of the cells <code>(r, c + 1)</code>, <code>(r, c - 1)</code>, <code>(r + 1, c)</code> and <code>(r - 1, c)</code> if it exists.</p>
<p>The <strong>Manhattan distance</strong> between two cells <code>(a, b)</code> and <code>(x, y)</code> is equal to <code>|a - x| + |b - y|</code>, where <code>|val|</code> denotes the absolute value of val.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example1.png" style="width: 362px; height: 242px;" />
<pre>
<strong>Input:</strong> grid = [[1,0,0],[0,0,0],[0,0,1]]
<strong>Output:</strong> 0
<strong>Explanation:</strong> All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example2.png" style="width: 362px; height: 242px;" />
<pre>
<strong>Input:</strong> grid = [[0,0,1],[0,0,0],[0,0,0]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since:
- The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2.
It can be shown that there are no other paths with a higher safeness factor.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example3.png" style="width: 362px; height: 242px;" />
<pre>
<strong>Input:</strong> grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since:
- The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2.
- The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2.
It can be shown that there are no other paths with a higher safeness factor.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length == n <= 400</code></li>
<li><code>grid[i].length == n</code></li>
<li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li>
<li>There is at least one thief in the <code>grid</code>.</li>
</ul>
| 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>>& distance, vector<vector<bool>>& visited) {\n if (distance[r][c] < d) {\n return false;\n }\n if (r == m - 1 && c == n - 1) {\n return true;\n }\n visited[r][c] = true;\n for (int i = 0; i < 4; ++i) {\n int nr = r + DIR[i], nc = c + DIR[i+1];\n if (nr < 0 || nr == m || nc < 0 || nc == n || visited[nr][nc]) continue;\n if (validPath(d, nr, nc, distance, visited)) {\n return true;\n }\n }\n return false;\n }\npublic:\n int maximumSafenessFactor(vector<vector<int>>& grid) {\n m = grid.size();\n n = grid[0].size();\n if (grid[0][0] == 1 || grid[m - 1][n - 1] == 1) {\n return 0;\n }\n // distance[i][j] is the smallest distance from grid[i][j] to thief\n vector<vector<int>> distance(m, vector<int>(n, -1));\n queue<pair<int, int>> q;\n for (int i = 0; i < m; ++i) {\n for (int j = 0; j < n; ++j) {\n if (grid[i][j] == 1) {\n q.push({i, j});\n distance[i][j] = 0;\n }\n }\n }\n int d = 1;\n while (!q.empty()) {\n int size = q.size();\n for (int i = 0; i < size; ++i) {\n pair<int, int> p = q.front();\n q.pop();\n int r = p.first, c = p.second;\n for (int i = 0; i < 4; ++i) {\n int nr = r + DIR[i], nc = c + DIR[i+1];\n if (nr < 0 || nr == m || nc < 0 || nc == n || distance[nr][nc] != -1) continue;\n distance[nr][nc] = d;\n q.push({nr, nc});\n }\n }\n d++;\n }\n int left = 0, right = d - 1;\n while (left <= right) {\n int mid = left + (right - left) / 2;\n vector<vector<bool>> visited(m, vector<bool>(n, false));\n if (validPath(mid, 0, 0, distance, visited)) {\n left = mid + 1;\n } else {\n cout << endl;\n right = mid - 1;\n }\n }\n return left - 1;\n }\n};",
"memory": "152266"
} |
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 <code>(0, 0)</code>. In one move, you can move to any adjacent cell in the grid, including cells containing thieves.</p>
<p>The <strong>safeness factor</strong> of a path on the grid is defined as the <strong>minimum</strong> manhattan distance from any cell in the path to any thief in the grid.</p>
<p>Return <em>the <strong>maximum safeness factor</strong> of all paths leading to cell </em><code>(n - 1, n - 1)</code><em>.</em></p>
<p>An <strong>adjacent</strong> cell of cell <code>(r, c)</code>, is one of the cells <code>(r, c + 1)</code>, <code>(r, c - 1)</code>, <code>(r + 1, c)</code> and <code>(r - 1, c)</code> if it exists.</p>
<p>The <strong>Manhattan distance</strong> between two cells <code>(a, b)</code> and <code>(x, y)</code> is equal to <code>|a - x| + |b - y|</code>, where <code>|val|</code> denotes the absolute value of val.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example1.png" style="width: 362px; height: 242px;" />
<pre>
<strong>Input:</strong> grid = [[1,0,0],[0,0,0],[0,0,1]]
<strong>Output:</strong> 0
<strong>Explanation:</strong> All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example2.png" style="width: 362px; height: 242px;" />
<pre>
<strong>Input:</strong> grid = [[0,0,1],[0,0,0],[0,0,0]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since:
- The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2.
It can be shown that there are no other paths with a higher safeness factor.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example3.png" style="width: 362px; height: 242px;" />
<pre>
<strong>Input:</strong> grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since:
- The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2.
- The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2.
It can be shown that there are no other paths with a higher safeness factor.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length == n <= 400</code></li>
<li><code>grid[i].length == n</code></li>
<li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li>
<li>There is at least one thief in the <code>grid</code>.</li>
</ul>
| 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>>& distance, vector<vector<bool>>& visited) {\n if (distance[r][c] < d) {\n return false;\n }\n if (r == m - 1 && c == n - 1) {\n return true;\n }\n visited[r][c] = true;\n for (int i = 0; i < 4; ++i) {\n int nr = r + DIR[i], nc = c + DIR[i+1];\n if (nr < 0 || nr == m || nc < 0 || nc == n || visited[nr][nc]) continue;\n if (validPath(d, nr, nc, distance, visited)) {\n return true;\n }\n }\n return false;\n }\npublic:\n int maximumSafenessFactor(vector<vector<int>>& grid) {\n m = grid.size();\n n = grid[0].size();\n if (grid[0][0] == 1 || grid[m - 1][n - 1] == 1) {\n return 0;\n }\n // distance[i][j] is the smallest distance from grid[i][j] to thief\n vector<vector<int>> distance(m, vector<int>(n, -1));\n queue<pair<int, int>> q;\n for (int i = 0; i < m; ++i) {\n for (int j = 0; j < n; ++j) {\n if (grid[i][j] == 1) {\n q.push({i, j});\n distance[i][j] = 0;\n }\n }\n }\n int d = 1;\n while (!q.empty()) {\n int size = q.size();\n for (int i = 0; i < size; ++i) {\n pair<int, int> p = q.front();\n q.pop();\n int r = p.first, c = p.second;\n for (int i = 0; i < 4; ++i) {\n int nr = r + DIR[i], nc = c + DIR[i+1];\n if (nr < 0 || nr == m || nc < 0 || nc == n || distance[nr][nc] != -1) continue;\n distance[nr][nc] = d;\n q.push({nr, nc});\n }\n }\n d++;\n }\n int left = 0, right = d - 1;\n int ans = 0;\n while (left <= right) {\n int mid = left + (right - left) / 2;\n cout << mid;\n vector<vector<bool>> visited(m, vector<bool>(n, false));\n if (validPath(mid, 0, 0, distance, visited)) {\n left = mid + 1;\n ans = mid;\n cout << \" is valid\" << std::endl;\n } else {\n cout << endl;\n right = mid - 1;\n }\n }\n return ans;\n }\n};",
"memory": "152266"
} |
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 <code>(0, 0)</code>. In one move, you can move to any adjacent cell in the grid, including cells containing thieves.</p>
<p>The <strong>safeness factor</strong> of a path on the grid is defined as the <strong>minimum</strong> manhattan distance from any cell in the path to any thief in the grid.</p>
<p>Return <em>the <strong>maximum safeness factor</strong> of all paths leading to cell </em><code>(n - 1, n - 1)</code><em>.</em></p>
<p>An <strong>adjacent</strong> cell of cell <code>(r, c)</code>, is one of the cells <code>(r, c + 1)</code>, <code>(r, c - 1)</code>, <code>(r + 1, c)</code> and <code>(r - 1, c)</code> if it exists.</p>
<p>The <strong>Manhattan distance</strong> between two cells <code>(a, b)</code> and <code>(x, y)</code> is equal to <code>|a - x| + |b - y|</code>, where <code>|val|</code> denotes the absolute value of val.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example1.png" style="width: 362px; height: 242px;" />
<pre>
<strong>Input:</strong> grid = [[1,0,0],[0,0,0],[0,0,1]]
<strong>Output:</strong> 0
<strong>Explanation:</strong> All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example2.png" style="width: 362px; height: 242px;" />
<pre>
<strong>Input:</strong> grid = [[0,0,1],[0,0,0],[0,0,0]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since:
- The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2.
It can be shown that there are no other paths with a higher safeness factor.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example3.png" style="width: 362px; height: 242px;" />
<pre>
<strong>Input:</strong> grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since:
- The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2.
- The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2.
It can be shown that there are no other paths with a higher safeness factor.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length == n <= 400</code></li>
<li><code>grid[i].length == n</code></li>
<li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li>
<li>There is at least one thief in the <code>grid</code>.</li>
</ul>
| 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;\n }\n\n queue<pair<int, int>> traversalQueue;\n\n vector<vector<bool>> visited(n, vector<bool>(n, false));\n\n traversalQueue.push({0, 0});\n\n visited[0][0] = true;\n\n while (!traversalQueue.empty()) {\n auto curr = traversalQueue.front();\n traversalQueue.pop();\n if (curr.first == n - 1 && curr.second == n - 1) {\n return true;\n }\n for (int i = 0; i < 4; i++) {\n int di = curr.first + move[i].first;\n int dj = curr.second + move[i].second;\n if (di >= 0 && dj >= 0 && di < n && dj < n &&\n !visited[di][dj] && grid[di][dj] >= minSafeness) {\n visited[di][dj] = true;\n traversalQueue.push({di, dj});\n }\n }\n }\n return false;\n }\n\n int maximumSafenessFactor(vector<vector<int>>& grid) {\n\n queue<pair<int, int>> q;\n int n = grid.size();\n // vector<vector<int>> dis(n, vector<int>(n, INT_MAX));\n\n // vector<vector<bool>> visited(n, vector<bool>(n, false));\n\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n if (grid[i][j]) {\n q.push({i, j});\n grid[i][j] = 0;\n } else\n grid[i][j] = -1;\n }\n }\n\n while (q.empty() == false) {\n\n int size = q.size();\n while (size-- > 0) {\n auto curr = q.front();\n q.pop();\n // Check neighboring cells\n for (int i = 0; i < 4; i++) {\n int di = curr.first + move[i].first;\n int dj = curr.second + move[i].second;\n int val = grid[curr.first][curr.second];\n\n if (di >= 0 && dj >= 0 && di < n && dj < n && grid[di][dj] == -1) {\n grid[di][dj] = val + 1;\n q.push({di, dj});\n }\n }\n }\n }\n\n int start = 0;\n int end = 0;\n int res = -1;\n\n for (int i = 0; i < n; i++)\n for (int j = 0; j < n; j++) \n end = max(end, grid[i][j]);\n\n \n while (start <= end) {\n int mid = start + (end - start) / 2;\n if (isValidSafeness(grid, mid)) {\n // Store valid safeness and search for larger ones\n res = mid;\n start = mid + 1;\n } else {\n end = mid - 1;\n }\n }\n\n return res;\n }\n};",
"memory": "156948"
} |
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 <code>(0, 0)</code>. In one move, you can move to any adjacent cell in the grid, including cells containing thieves.</p>
<p>The <strong>safeness factor</strong> of a path on the grid is defined as the <strong>minimum</strong> manhattan distance from any cell in the path to any thief in the grid.</p>
<p>Return <em>the <strong>maximum safeness factor</strong> of all paths leading to cell </em><code>(n - 1, n - 1)</code><em>.</em></p>
<p>An <strong>adjacent</strong> cell of cell <code>(r, c)</code>, is one of the cells <code>(r, c + 1)</code>, <code>(r, c - 1)</code>, <code>(r + 1, c)</code> and <code>(r - 1, c)</code> if it exists.</p>
<p>The <strong>Manhattan distance</strong> between two cells <code>(a, b)</code> and <code>(x, y)</code> is equal to <code>|a - x| + |b - y|</code>, where <code>|val|</code> denotes the absolute value of val.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example1.png" style="width: 362px; height: 242px;" />
<pre>
<strong>Input:</strong> grid = [[1,0,0],[0,0,0],[0,0,1]]
<strong>Output:</strong> 0
<strong>Explanation:</strong> All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example2.png" style="width: 362px; height: 242px;" />
<pre>
<strong>Input:</strong> grid = [[0,0,1],[0,0,0],[0,0,0]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since:
- The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2.
It can be shown that there are no other paths with a higher safeness factor.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example3.png" style="width: 362px; height: 242px;" />
<pre>
<strong>Input:</strong> grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since:
- The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2.
- The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2.
It can be shown that there are no other paths with a higher safeness factor.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length == n <= 400</code></li>
<li><code>grid[i].length == n</code></li>
<li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li>
<li>There is at least one thief in the <code>grid</code>.</li>
</ul>
| 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++) {\n\n for (int j = 0; j < n; j++) {\n\n if (grid[i][j] == 1) {\n\n // Push thief coordinates to the queue\n\n multiSourceQueue.push({i, j});\n\n // Mark thief cell with 0\n\n grid[i][j] = 0;\n\n } else {\n\n // Mark empty cell with -1\n\n grid[i][j] = -1;\n\n }\n\n }\n\n }\n\n // Calculate safeness factor for each cell using BFS\n\n while (!multiSourceQueue.empty()) {\n\n int size = multiSourceQueue.size();\n\n while (size-- > 0) {\n\n auto curr = multiSourceQueue.front();\n\n multiSourceQueue.pop();\n\n // Check neighboring cells\n\n for (auto& d : dir) {\n\n int di = curr.first + d[0];\n\n int dj = curr.second + d[1];\n\n int val = grid[curr.first][curr.second];\n\n // Check if the cell is valid and unvisited\n\n if (isValidCell(grid, di, dj) && grid[di][dj] == -1) {\n\n // Update safeness factor and push to the queue\n\n grid[di][dj] = val + 1;\n\n multiSourceQueue.push({di, dj});\n\n }\n\n }\n\n }\n\n }\n\n // Binary search for maximum safeness factor\n\n int start = 0;\n\n int end = 0;\n\n int res = -1;\n\n for (int i = 0; i < n; i++) {\n\n for (int j = 0; j < n; j++) {\n\n // Set end as the maximum safeness factor possible\n\n end = max(end, grid[i][j]);\n\n }\n\n }\n\n while (start <= end) {\n\n int mid = start + (end - start) / 2;\n\n if (isValidSafeness(grid, mid)) {\n\n // Store valid safeness and search for larger ones\n\n res = mid;\n\n start = mid + 1;\n\n } else {\n\n end = mid - 1;\n\n }\n\n }\n\n return res;\n\n }\n\nprivate:\n\n // Directions for moving to neighboring cells: right, left, down, up\n\n vector<vector<int>> dir = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};\n\n // Check if a given cell lies within the grid\n\n bool isValidCell(vector<vector<int>>& grid, int i, int j) {\n\n int n = grid.size();\n\n return i >= 0 && j >= 0 && i < n && j < n;\n\n }\n\n // Check if a path exists with given minimum safeness value\n\n bool isValidSafeness(vector<vector<int>>& grid, int minSafeness) {\n\n int n = grid.size();\n\n // Check if the source and destination cells satisfy minimum safeness\n\n if (grid[0][0] < minSafeness || grid[n - 1][n - 1] < minSafeness) {\n\n return false;\n\n }\n\n queue<pair<int, int>> traversalQueue;\n\n vector<vector<bool>> visited(n, vector<bool>(n, false));\n\n traversalQueue.push({0, 0});\n\n visited[0][0] = true;\n\n // Breadth-first search to find a valid path\n\n while (!traversalQueue.empty()) {\n\n auto curr = traversalQueue.front();\n\n traversalQueue.pop();\n\n if (curr.first == n - 1 && curr.second == n - 1) {\n\n return true; // Valid path found\n\n }\n\n // Check neighboring cells\n\n for (auto& d : dir) {\n\n int di = curr.first + d[0];\n\n int dj = curr.second + d[1];\n\n // Check if the neighboring cell is valid and unvisited\n\n if (isValidCell(grid, di, dj) && !visited[di][dj] &&\n\n grid[di][dj] >= minSafeness) {\n\n visited[di][dj] = true;\n\n traversalQueue.push({di, dj});\n\n }\n\n }\n\n }\n\n return false; // No valid path found\n\n }\n\n};\n\n \n \n \n",
"memory": "156948"
} |
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 <code>(0, 0)</code>. In one move, you can move to any adjacent cell in the grid, including cells containing thieves.</p>
<p>The <strong>safeness factor</strong> of a path on the grid is defined as the <strong>minimum</strong> manhattan distance from any cell in the path to any thief in the grid.</p>
<p>Return <em>the <strong>maximum safeness factor</strong> of all paths leading to cell </em><code>(n - 1, n - 1)</code><em>.</em></p>
<p>An <strong>adjacent</strong> cell of cell <code>(r, c)</code>, is one of the cells <code>(r, c + 1)</code>, <code>(r, c - 1)</code>, <code>(r + 1, c)</code> and <code>(r - 1, c)</code> if it exists.</p>
<p>The <strong>Manhattan distance</strong> between two cells <code>(a, b)</code> and <code>(x, y)</code> is equal to <code>|a - x| + |b - y|</code>, where <code>|val|</code> denotes the absolute value of val.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example1.png" style="width: 362px; height: 242px;" />
<pre>
<strong>Input:</strong> grid = [[1,0,0],[0,0,0],[0,0,1]]
<strong>Output:</strong> 0
<strong>Explanation:</strong> All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example2.png" style="width: 362px; height: 242px;" />
<pre>
<strong>Input:</strong> grid = [[0,0,1],[0,0,0],[0,0,0]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since:
- The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2.
It can be shown that there are no other paths with a higher safeness factor.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example3.png" style="width: 362px; height: 242px;" />
<pre>
<strong>Input:</strong> grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since:
- The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2.
- The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2.
It can be shown that there are no other paths with a higher safeness factor.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length == n <= 400</code></li>
<li><code>grid[i].length == n</code></li>
<li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li>
<li>There is at least one thief in the <code>grid</code>.</li>
</ul>
| 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 Position(int r, int c) : r(r), c(c) {}\n Position operator+(const pair<int,int>& other) const {\n return Position(r + other.first, c + other.second);\n }\n };\n\n int n;\n vector<pair<int,int>> directions = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};\n\n bool isInBound(Position pos){\n return (pos.r >= 0) && (pos.c >= 0) && (pos.r < this->n) && (pos.c < this->n);\n }\n\n vector<vector<int>> getSafenessGrid(vector<vector<int>>& grid) {\n vector<vector<int>> safenessGrid(this->n, vector<int>(this->n, 0));\n queue<Position> q;\n\n // init safenessGrid\n for(int i=0; i<this->n; i++){\n for(int j=0; j<this->n; j++){\n if(grid[i][j]==1) {\n q.push(Position(i,j));\n safenessGrid[i][j] = 0;\n }\n else {\n safenessGrid[i][j] = -1; //unvisited\n }\n }\n }\n\n //update safenessGrid\n while(!q.empty()) {\n Position pos = q.front();\n q.pop();\n\n for(const auto& direction : directions){\n Position nextPos = pos + direction;\n if(isInBound(nextPos) && safenessGrid[nextPos.r][nextPos.c] == -1) {\n safenessGrid[nextPos.r][nextPos.c] = safenessGrid[pos.r][pos.c] + 1;\n q.push(nextPos);\n }\n }\n }\n return safenessGrid;\n }\n\n int findSafestPath(vector<vector<int>>& safenessGrid) {\n auto cmp = [](const std::pair<int, Position>& a, const std::pair<int, Position>& b) {\n return a.first < b.first;\n };\n\n // Since we have a defined start and end point, we can use bidirectional BFS\n // instead of the standard BFS for greater efficiency.\n\n priority_queue<pair<int, Position>, vector<pair<int, Position>>, decltype(cmp)> pqStart(cmp);\n priority_queue<pair<int, Position>, vector<pair<int, Position>>, decltype(cmp)> pqEnd(cmp);\n\n vector<vector<bool>> visitedFromStart(n, vector<bool>(n, false));\n vector<vector<bool>> visitedFromEnd(n, vector<bool>(n, false));\n\n vector<vector<int>> safenessFromStart(n, vector<int>(n, INT_MIN));\n vector<vector<int>> safenessFromEnd(n, vector<int>(n, INT_MIN));\n\n pqStart.push({safenessGrid[0][0], Position(0, 0)});\n pqEnd.push({safenessGrid[n-1][n-1], Position(n-1, n-1)});\n safenessFromStart[0][0] = safenessGrid[0][0];\n safenessFromEnd[n-1][n-1] = safenessGrid[n-1][n-1];\n\n while (!pqStart.empty() && !pqEnd.empty()) {\n // Expand from the start side\n if (!pqStart.empty()) {\n auto [safeness, pos] = pqStart.top();\n pqStart.pop();\n visitedFromStart[pos.r][pos.c] = true;\n\n // Find a path!\n if (visitedFromEnd[pos.r][pos.c]) {\n return min(safeness, safenessFromEnd[pos.r][pos.c]);\n }\n\n for (const auto& direction : directions) {\n Position nextPos = pos + direction;\n if (isInBound(nextPos) && !visitedFromStart[nextPos.r][nextPos.c]) {\n int newSafeness = min(safeness, safenessGrid[nextPos.r][nextPos.c]);\n if (newSafeness > safenessFromStart[nextPos.r][nextPos.c]) {\n safenessFromStart[nextPos.r][nextPos.c] = newSafeness;\n pqStart.push({newSafeness, nextPos});\n }\n }\n }\n }\n\n // Expand from the end side\n if (!pqEnd.empty()) {\n auto [safeness, pos] = pqEnd.top();\n pqEnd.pop();\n visitedFromEnd[pos.r][pos.c] = true;\n\n // Find a path!\n if (visitedFromStart[pos.r][pos.c]) {\n return min(safeness, safenessFromStart[pos.r][pos.c]);\n }\n\n for (const auto& direction : directions) {\n Position nextPos = pos + direction;\n if (isInBound(nextPos) && !visitedFromEnd[nextPos.r][nextPos.c]) {\n int newSafeness = min(safeness, safenessGrid[nextPos.r][nextPos.c]);\n if (newSafeness > safenessFromEnd[nextPos.r][nextPos.c]) {\n safenessFromEnd[nextPos.r][nextPos.c] = newSafeness;\n pqEnd.push({newSafeness, nextPos});\n }\n }\n }\n }\n }\n return -1;\n }\n};",
"memory": "161631"
} |
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 <code>(0, 0)</code>. In one move, you can move to any adjacent cell in the grid, including cells containing thieves.</p>
<p>The <strong>safeness factor</strong> of a path on the grid is defined as the <strong>minimum</strong> manhattan distance from any cell in the path to any thief in the grid.</p>
<p>Return <em>the <strong>maximum safeness factor</strong> of all paths leading to cell </em><code>(n - 1, n - 1)</code><em>.</em></p>
<p>An <strong>adjacent</strong> cell of cell <code>(r, c)</code>, is one of the cells <code>(r, c + 1)</code>, <code>(r, c - 1)</code>, <code>(r + 1, c)</code> and <code>(r - 1, c)</code> if it exists.</p>
<p>The <strong>Manhattan distance</strong> between two cells <code>(a, b)</code> and <code>(x, y)</code> is equal to <code>|a - x| + |b - y|</code>, where <code>|val|</code> denotes the absolute value of val.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example1.png" style="width: 362px; height: 242px;" />
<pre>
<strong>Input:</strong> grid = [[1,0,0],[0,0,0],[0,0,1]]
<strong>Output:</strong> 0
<strong>Explanation:</strong> All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example2.png" style="width: 362px; height: 242px;" />
<pre>
<strong>Input:</strong> grid = [[0,0,1],[0,0,0],[0,0,0]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since:
- The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2.
It can be shown that there are no other paths with a higher safeness factor.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example3.png" style="width: 362px; height: 242px;" />
<pre>
<strong>Input:</strong> grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since:
- The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2.
- The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2.
It can be shown that there are no other paths with a higher safeness factor.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length == n <= 400</code></li>
<li><code>grid[i].length == n</code></li>
<li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li>
<li>There is at least one thief in the <code>grid</code>.</li>
</ul>
| 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 = calculateSafenessFactors(grid);\n // Find the maximum safeness value from the safeness matrix\n int maxSafeness = findMaxSafeness(safeness);\n // Perform binary search to find the maximum safeness factor\n return findMaxSafenessFactor(grid, safeness, maxSafeness);\n }\n\nprivate:\n vector<vector<int>> calculateSafenessFactors(vector<vector<int>>& grid) {\n int n = grid.size();\n vector<vector<int>> safeness(n, vector<int>(n, INT_MAX)); \n queue<pair<int, int>> q;\n\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 safeness[i][j] = 0;\n }\n }\n }\n\n vector<int> dirX = {-1, 1, 0, 0};\n vector<int> dirY = {0, 0, -1, 1};\n\n // Perform BFS to calculate safeness factors\n while (!q.empty()) {\n auto cell = q.front(); q.pop();\n int x = cell.first;\n int y = cell.second;\n\n for (int i = 0; i < 4; i++) {\n int newX = x + dirX[i];\n int newY = y + dirY[i];\n\n if (newX >= 0 && newX < n && newY >= 0 && newY < n && safeness[newX][newY] > safeness[x][y] + 1) {\n safeness[newX][newY] = safeness[x][y] + 1;\n q.push({newX, newY});\n }\n }\n }\n\n return safeness;\n }\n\n int findMaxSafeness(vector<vector<int>>& safeness) {\n int maxSafeness = 0;\n for (auto& row : safeness) {\n for (auto val : row) {\n maxSafeness = max(maxSafeness, val);\n }\n }\n return maxSafeness;\n }\n\n int findMaxSafenessFactor(vector<vector<int>>& grid, vector<vector<int>>& safeness, int maxSafeness) {\n int left = 0, right = maxSafeness, result = 0;\n\n // Binary search to find the maximum safeness factor\n while (left <= right) {\n int mid = left + (right - left) / 2;\n if (canAchieveSafeness(grid, safeness, mid)) {\n result = mid; \n left = mid + 1;\n } else {\n right = mid - 1;\n }\n }\n\n return result;\n }\n\n // Method to check if a path with the given safeness factor exists using BFS\n bool canAchieveSafeness(vector<vector<int>>& grid, vector<vector<int>>& safeness, int threshold) {\n int n = grid.size();\n if (safeness[0][0] < threshold) return false; \n\n queue<pair<int, int>> q;\n vector<vector<bool>> visited(n, vector<bool>(n, false));\n q.push({0, 0});\n visited[0][0] = true;\n\n vector<int> dirX = {-1, 1, 0, 0};\n vector<int> dirY = {0, 0, -1, 1};\n\n while (!q.empty()) {\n auto cell = q.front(); q.pop();\n int x = cell.first;\n int y = cell.second;\n\n if (x == n - 1 && y == n - 1) return true; // If we reach the bottom-right corner\n\n for (int i = 0; i < 4; i++) {\n int newX = x + dirX[i];\n int newY = y + dirY[i];\n\n if (newX >= 0 && newX < n && newY >= 0 && newY < n && !visited[newX][newY] && safeness[newX][newY] >= threshold) {\n q.push({newX, newY});\n visited[newX][newY] = true;\n }\n }\n }\n\n return false;\n }\n};",
"memory": "189726"
} |
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 <code>(0, 0)</code>. In one move, you can move to any adjacent cell in the grid, including cells containing thieves.</p>
<p>The <strong>safeness factor</strong> of a path on the grid is defined as the <strong>minimum</strong> manhattan distance from any cell in the path to any thief in the grid.</p>
<p>Return <em>the <strong>maximum safeness factor</strong> of all paths leading to cell </em><code>(n - 1, n - 1)</code><em>.</em></p>
<p>An <strong>adjacent</strong> cell of cell <code>(r, c)</code>, is one of the cells <code>(r, c + 1)</code>, <code>(r, c - 1)</code>, <code>(r + 1, c)</code> and <code>(r - 1, c)</code> if it exists.</p>
<p>The <strong>Manhattan distance</strong> between two cells <code>(a, b)</code> and <code>(x, y)</code> is equal to <code>|a - x| + |b - y|</code>, where <code>|val|</code> denotes the absolute value of val.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example1.png" style="width: 362px; height: 242px;" />
<pre>
<strong>Input:</strong> grid = [[1,0,0],[0,0,0],[0,0,1]]
<strong>Output:</strong> 0
<strong>Explanation:</strong> All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example2.png" style="width: 362px; height: 242px;" />
<pre>
<strong>Input:</strong> grid = [[0,0,1],[0,0,0],[0,0,0]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since:
- The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2.
It can be shown that there are no other paths with a higher safeness factor.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example3.png" style="width: 362px; height: 242px;" />
<pre>
<strong>Input:</strong> grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since:
- The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2.
- The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2.
It can be shown that there are no other paths with a higher safeness factor.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length == n <= 400</code></li>
<li><code>grid[i].length == n</code></li>
<li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li>
<li>There is at least one thief in the <code>grid</code>.</li>
</ul>
| 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 = calculateSafenessFactors(grid);\n // Find the maximum safeness value from the safeness matrix\n int maxSafeness = findMaxSafeness(safeness);\n // Perform binary search to find the maximum safeness factor\n return findMaxSafenessFactor(grid, safeness, maxSafeness);\n }\n\nprivate:\n vector<vector<int>> calculateSafenessFactors(vector<vector<int>>& grid) {\n int n = grid.size();\n vector<vector<int>> safeness(n, vector<int>(n, INT_MAX)); \n queue<pair<int, int>> q;\n\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 safeness[i][j] = 0;\n }\n }\n }\n\n vector<int> dirX = {-1, 1, 0, 0};\n vector<int> dirY = {0, 0, -1, 1};\n\n // Perform BFS to calculate safeness factors\n while (!q.empty()) {\n auto cell = q.front(); q.pop();\n int x = cell.first;\n int y = cell.second;\n\n for (int i = 0; i < 4; i++) {\n int newX = x + dirX[i];\n int newY = y + dirY[i];\n\n if (newX >= 0 && newX < n && newY >= 0 && newY < n && safeness[newX][newY] > safeness[x][y] + 1) {\n safeness[newX][newY] = safeness[x][y] + 1;\n q.push({newX, newY});\n }\n }\n }\n\n return safeness;\n }\n\n int findMaxSafeness(vector<vector<int>>& safeness) {\n int maxSafeness = 0;\n for (auto& row : safeness) {\n for (auto val : row) {\n maxSafeness = max(maxSafeness, val);\n }\n }\n return maxSafeness;\n }\n\n int findMaxSafenessFactor(vector<vector<int>>& grid, vector<vector<int>>& safeness, int maxSafeness) {\n int left = 0, right = maxSafeness, result = 0;\n\n // Binary search to find the maximum safeness factor\n while (left <= right) {\n int mid = left + (right - left) / 2;\n if (canAchieveSafeness(grid, safeness, mid)) {\n result = mid; \n left = mid + 1;\n } else {\n right = mid - 1;\n }\n }\n\n return result;\n }\n\n // Method to check if a path with the given safeness factor exists using BFS\n bool canAchieveSafeness(vector<vector<int>>& grid, vector<vector<int>>& safeness, int threshold) {\n int n = grid.size();\n if (safeness[0][0] < threshold) return false; \n\n queue<pair<int, int>> q;\n vector<vector<bool>> visited(n, vector<bool>(n, false));\n q.push({0, 0});\n visited[0][0] = true;\n\n vector<int> dirX = {-1, 1, 0, 0};\n vector<int> dirY = {0, 0, -1, 1};\n\n while (!q.empty()) {\n auto cell = q.front(); q.pop();\n int x = cell.first;\n int y = cell.second;\n\n if (x == n - 1 && y == n - 1) return true; // If we reach the bottom-right corner\n\n for (int i = 0; i < 4; i++) {\n int newX = x + dirX[i];\n int newY = y + dirY[i];\n\n if (newX >= 0 && newX < n && newY >= 0 && newY < n && !visited[newX][newY] && safeness[newX][newY] >= threshold) {\n q.push({newX, newY});\n visited[newX][newY] = true;\n }\n }\n }\n\n return false;\n }\n};",
"memory": "189726"
} |
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 <code>(0, 0)</code>. In one move, you can move to any adjacent cell in the grid, including cells containing thieves.</p>
<p>The <strong>safeness factor</strong> of a path on the grid is defined as the <strong>minimum</strong> manhattan distance from any cell in the path to any thief in the grid.</p>
<p>Return <em>the <strong>maximum safeness factor</strong> of all paths leading to cell </em><code>(n - 1, n - 1)</code><em>.</em></p>
<p>An <strong>adjacent</strong> cell of cell <code>(r, c)</code>, is one of the cells <code>(r, c + 1)</code>, <code>(r, c - 1)</code>, <code>(r + 1, c)</code> and <code>(r - 1, c)</code> if it exists.</p>
<p>The <strong>Manhattan distance</strong> between two cells <code>(a, b)</code> and <code>(x, y)</code> is equal to <code>|a - x| + |b - y|</code>, where <code>|val|</code> denotes the absolute value of val.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example1.png" style="width: 362px; height: 242px;" />
<pre>
<strong>Input:</strong> grid = [[1,0,0],[0,0,0],[0,0,1]]
<strong>Output:</strong> 0
<strong>Explanation:</strong> All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example2.png" style="width: 362px; height: 242px;" />
<pre>
<strong>Input:</strong> grid = [[0,0,1],[0,0,0],[0,0,0]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since:
- The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2.
It can be shown that there are no other paths with a higher safeness factor.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example3.png" style="width: 362px; height: 242px;" />
<pre>
<strong>Input:</strong> grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since:
- The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2.
- The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2.
It can be shown that there are no other paths with a higher safeness factor.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length == n <= 400</code></li>
<li><code>grid[i].length == n</code></li>
<li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li>
<li>There is at least one thief in the <code>grid</code>.</li>
</ul>
| 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 grid[i][j]=0;\n }else{\n grid[i][j]=500;\n }\n }\n }\n int dr[]={-1,0,1,0};\n int dc[]={0,-1,0,1};\n while(!q.empty()){\n int x=q.front().first;\n int y=q.front().second;\n q.pop();\n for(int i=0;i<4;i++){\n int r=x+dr[i];\n int c=y+dc[i];\n if(r>=0 && c>=0 && r<n && c<n && grid[r][c]>grid[x][y]+1){\n grid[r][c]=1+grid[x][y];\n q.push({r,c});\n }\n }\n }\n vector<vector<int>> dist(n, vector<int> (n,0));\n dist[0][0]=grid[0][0];\n priority_queue<vector<int>> pq;\n pq.push({dist[0][0],0,0});\n // int dr[]={-1,0,1,0};\n // int dc[]={0,-1,0,1};\n while(!pq.empty()){\n int sf=pq.top()[0];\n int x=pq.top()[1];\n int y=pq.top()[2];\n pq.pop();\n for(int i=0;i<4;i++){\n int r=x+dr[i];\n int c=y+dc[i];\n if(r>=0 && c>=0 && r<n && c<n && min(grid[r][c],sf)>dist[r][c]){\n dist[r][c]=min(grid[r][c],sf);\n pq.push({dist[r][c],r,c});\n }\n }\n }\n return dist[n-1][n-1];\n }\n};",
"memory": "194408"
} |
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 <code>(0, 0)</code>. In one move, you can move to any adjacent cell in the grid, including cells containing thieves.</p>
<p>The <strong>safeness factor</strong> of a path on the grid is defined as the <strong>minimum</strong> manhattan distance from any cell in the path to any thief in the grid.</p>
<p>Return <em>the <strong>maximum safeness factor</strong> of all paths leading to cell </em><code>(n - 1, n - 1)</code><em>.</em></p>
<p>An <strong>adjacent</strong> cell of cell <code>(r, c)</code>, is one of the cells <code>(r, c + 1)</code>, <code>(r, c - 1)</code>, <code>(r + 1, c)</code> and <code>(r - 1, c)</code> if it exists.</p>
<p>The <strong>Manhattan distance</strong> between two cells <code>(a, b)</code> and <code>(x, y)</code> is equal to <code>|a - x| + |b - y|</code>, where <code>|val|</code> denotes the absolute value of val.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example1.png" style="width: 362px; height: 242px;" />
<pre>
<strong>Input:</strong> grid = [[1,0,0],[0,0,0],[0,0,1]]
<strong>Output:</strong> 0
<strong>Explanation:</strong> All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example2.png" style="width: 362px; height: 242px;" />
<pre>
<strong>Input:</strong> grid = [[0,0,1],[0,0,0],[0,0,0]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since:
- The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2.
It can be shown that there are no other paths with a higher safeness factor.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example3.png" style="width: 362px; height: 242px;" />
<pre>
<strong>Input:</strong> grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since:
- The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2.
- The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2.
It can be shown that there are no other paths with a higher safeness factor.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length == n <= 400</code></li>
<li><code>grid[i].length == n</code></li>
<li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li>
<li>There is at least one thief in the <code>grid</code>.</li>
</ul>
| 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 = {0,1,0,-1,0};\n while (!q.empty()) {\n while (!q.empty()) {\n auto [x, y] = q.front();\n q.pop();\n for (int i=0; i<4; i++) {\n int X = x + dirs[i], Y = y + dirs[i+1];\n if (X < 0 or X >= n or Y < 0 or Y >= m) {\n continue;\n }\n if (cost[X][Y] < min(cost[x][y], grid[x][y])) {\n cost[X][Y] = min(cost[x][y], grid[x][y]);\n q1.push({X, Y});\n }\n }\n }\n swap(q, q1);\n }\n\n return min(cost[n-1][m-1], grid[n-1][m-1]);\n }\n\n int maximumSafenessFactor(vector<vector<int>>& grid) {\n int n = grid.size(), m = grid[0].size();\n vector<vector<int>> G(n, vector<int>(m, 1e9));\n vector<vector<int>> vis(n, vector<int>(m, 0));\n queue<pair<int, int>> q,q1;\n\n for (int i=0; i<n; i++) {\n for (int j=0; j<m; j++) {\n if (grid[i][j] == 1) {\n q.push({i, j});\n vis[i][j] = 1;\n G[i][j] = 0;\n }\n }\n }\n\n vector<int> dirs = {0,1,0,-1,0};\n int d = 1;\n while (!q.empty()) {\n while (!q.empty()) {\n auto [x, y] = q.front();\n q.pop();\n for (int i=0; i<4; i++) {\n int X = x + dirs[i], Y = y + dirs[i+1];\n if (X < 0 or X >= n or Y < 0 or Y >= m or vis[X][Y]) {\n continue;\n }\n G[X][Y] = d;\n q1.push({X, Y});\n vis[X][Y] = 1;\n }\n }\n swap(q, q1);\n d++;\n }\n return getCost(G);\n }\n};",
"memory": "194408"
} |
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 <code>(0, 0)</code>. In one move, you can move to any adjacent cell in the grid, including cells containing thieves.</p>
<p>The <strong>safeness factor</strong> of a path on the grid is defined as the <strong>minimum</strong> manhattan distance from any cell in the path to any thief in the grid.</p>
<p>Return <em>the <strong>maximum safeness factor</strong> of all paths leading to cell </em><code>(n - 1, n - 1)</code><em>.</em></p>
<p>An <strong>adjacent</strong> cell of cell <code>(r, c)</code>, is one of the cells <code>(r, c + 1)</code>, <code>(r, c - 1)</code>, <code>(r + 1, c)</code> and <code>(r - 1, c)</code> if it exists.</p>
<p>The <strong>Manhattan distance</strong> between two cells <code>(a, b)</code> and <code>(x, y)</code> is equal to <code>|a - x| + |b - y|</code>, where <code>|val|</code> denotes the absolute value of val.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example1.png" style="width: 362px; height: 242px;" />
<pre>
<strong>Input:</strong> grid = [[1,0,0],[0,0,0],[0,0,1]]
<strong>Output:</strong> 0
<strong>Explanation:</strong> All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example2.png" style="width: 362px; height: 242px;" />
<pre>
<strong>Input:</strong> grid = [[0,0,1],[0,0,0],[0,0,0]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since:
- The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2.
It can be shown that there are no other paths with a higher safeness factor.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example3.png" style="width: 362px; height: 242px;" />
<pre>
<strong>Input:</strong> grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since:
- The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2.
- The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2.
It can be shown that there are no other paths with a higher safeness factor.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length == n <= 400</code></li>
<li><code>grid[i].length == n</code></li>
<li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li>
<li>There is at least one thief in the <code>grid</code>.</li>
</ul>
| 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));\n q.push({0, 0});\n vis[0][0]=true;\n while(!q.empty())\n {\n int x=q.front().first;\n int y=q.front().second;\n q.pop();\n if(x==n-1 && y==n-1) return true;\n for(int k=0; k<4; k++)\n {\n int nx=x+dx[k];\n int ny=y+dy[k];\n if(nx>=0 && nx<n && ny>=0 && ny<n && dist[nx][ny]>=sf && !vis[nx][ny])\n {\n q.push({nx, ny});\n vis[nx][ny]=true;\n }\n }\n }\n return false;\n }\npublic:\n int maximumSafenessFactor(vector<vector<int>>& grid) \n {\n int n=grid.size();\n queue<pair<int, int>> q;\n vector<vector<bool>> vis(n, vector<bool> (n, false));\n vector<vector<int>> dist(n, vector<int> (n, INT_MAX));\n for(int i=0; i<n; i++)\n {\n for(int j=0; j<n; j++) \n {\n if(grid[i][j]==1) \n {\n dist[i][j]=0;\n q.push({i, j});\n vis[i][j]=true;\n }\n }\n }\n while(!q.empty())\n {\n int sz=q.size();\n while(sz--)\n {\n int x=q.front().first, y=q.front().second;\n q.pop();\n for(int k=0; k<4; k++)\n {\n int nx=x+dx[k];\n int ny=y+dy[k];\n if(nx>=0 && nx<n && ny>=0 && ny<n && !vis[nx][ny])\n {\n q.push({nx, ny});\n vis[nx][ny]=true;\n dist[nx][ny]=1+dist[x][y];\n }\n }\n }\n }\n int low=0, high=1e9, ans=0;\n while(low<=high)\n {\n int mid=(low+high)/2;\n if(bfs(dist, mid))\n {\n ans=mid;\n low=mid+1;\n }\n else high=mid-1;\n }\n return ans;\n }\n};",
"memory": "199091"
} |
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 <code>(0, 0)</code>. In one move, you can move to any adjacent cell in the grid, including cells containing thieves.</p>
<p>The <strong>safeness factor</strong> of a path on the grid is defined as the <strong>minimum</strong> manhattan distance from any cell in the path to any thief in the grid.</p>
<p>Return <em>the <strong>maximum safeness factor</strong> of all paths leading to cell </em><code>(n - 1, n - 1)</code><em>.</em></p>
<p>An <strong>adjacent</strong> cell of cell <code>(r, c)</code>, is one of the cells <code>(r, c + 1)</code>, <code>(r, c - 1)</code>, <code>(r + 1, c)</code> and <code>(r - 1, c)</code> if it exists.</p>
<p>The <strong>Manhattan distance</strong> between two cells <code>(a, b)</code> and <code>(x, y)</code> is equal to <code>|a - x| + |b - y|</code>, where <code>|val|</code> denotes the absolute value of val.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example1.png" style="width: 362px; height: 242px;" />
<pre>
<strong>Input:</strong> grid = [[1,0,0],[0,0,0],[0,0,1]]
<strong>Output:</strong> 0
<strong>Explanation:</strong> All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example2.png" style="width: 362px; height: 242px;" />
<pre>
<strong>Input:</strong> grid = [[0,0,1],[0,0,0],[0,0,0]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since:
- The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2.
It can be shown that there are no other paths with a higher safeness factor.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example3.png" style="width: 362px; height: 242px;" />
<pre>
<strong>Input:</strong> grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since:
- The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2.
- The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2.
It can be shown that there are no other paths with a higher safeness factor.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length == n <= 400</code></li>
<li><code>grid[i].length == n</code></li>
<li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li>
<li>There is at least one thief in the <code>grid</code>.</li>
</ul>
| 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));\n q.push({0, 0});\n vis[0][0]=true;\n while(!q.empty())\n {\n int x=q.front().first;\n int y=q.front().second;\n q.pop();\n if(x==n-1 && y==n-1) return true;\n for(int k=0; k<4; k++)\n {\n int nx=x+dx[k];\n int ny=y+dy[k];\n if(nx>=0 && nx<n && ny>=0 && ny<n && dist[nx][ny]>=sf && !vis[nx][ny])\n {\n q.push({nx, ny});\n vis[nx][ny]=true;\n }\n }\n }\n return false;\n }\npublic:\n int maximumSafenessFactor(vector<vector<int>>& grid) \n {\n int n=grid.size();\n queue<pair<int, int>> q;\n vector<vector<bool>> vis(n, vector<bool> (n, false));\n vector<vector<int>> dist(n, vector<int> (n, INT_MAX));\n for(int i=0; i<n; i++)\n {\n for(int j=0; j<n; j++) \n {\n if(grid[i][j]==1) \n {\n dist[i][j]=0;\n q.push({i, j});\n vis[i][j]=true;\n }\n }\n }\n while(!q.empty())\n {\n int sz=q.size();\n while(sz--)\n {\n int x=q.front().first, y=q.front().second;\n q.pop();\n for(int k=0; k<4; k++)\n {\n int nx=x+dx[k];\n int ny=y+dy[k];\n if(nx>=0 && nx<n && ny>=0 && ny<n && !vis[nx][ny])\n {\n q.push({nx, ny});\n vis[nx][ny]=true;\n dist[nx][ny]=1+dist[x][y];\n }\n }\n }\n }\n int low=0, high=1e9, ans=0;\n while(low<=high)\n {\n int mid=(low+high)/2;\n if(bfs(dist, mid))\n {\n ans=mid;\n low=mid+1;\n }\n else high=mid-1;\n }\n return ans;\n }\n};",
"memory": "199091"
} |
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 <code>(0, 0)</code>. In one move, you can move to any adjacent cell in the grid, including cells containing thieves.</p>
<p>The <strong>safeness factor</strong> of a path on the grid is defined as the <strong>minimum</strong> manhattan distance from any cell in the path to any thief in the grid.</p>
<p>Return <em>the <strong>maximum safeness factor</strong> of all paths leading to cell </em><code>(n - 1, n - 1)</code><em>.</em></p>
<p>An <strong>adjacent</strong> cell of cell <code>(r, c)</code>, is one of the cells <code>(r, c + 1)</code>, <code>(r, c - 1)</code>, <code>(r + 1, c)</code> and <code>(r - 1, c)</code> if it exists.</p>
<p>The <strong>Manhattan distance</strong> between two cells <code>(a, b)</code> and <code>(x, y)</code> is equal to <code>|a - x| + |b - y|</code>, where <code>|val|</code> denotes the absolute value of val.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example1.png" style="width: 362px; height: 242px;" />
<pre>
<strong>Input:</strong> grid = [[1,0,0],[0,0,0],[0,0,1]]
<strong>Output:</strong> 0
<strong>Explanation:</strong> All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example2.png" style="width: 362px; height: 242px;" />
<pre>
<strong>Input:</strong> grid = [[0,0,1],[0,0,0],[0,0,0]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since:
- The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2.
It can be shown that there are no other paths with a higher safeness factor.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example3.png" style="width: 362px; height: 242px;" />
<pre>
<strong>Input:</strong> grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since:
- The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2.
- The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2.
It can be shown that there are no other paths with a higher safeness factor.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length == n <= 400</code></li>
<li><code>grid[i].length == n</code></li>
<li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li>
<li>There is at least one thief in the <code>grid</code>.</li>
</ul>
| 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 with all thieves' positions\n for (auto &thief : thieves) {\n q.push(thief);\n safe[thief.first][thief.second] = 0;\n }\n \n int drow[] = {-1, 0, +1, 0};\n int dcol[] = {0, +1, 0, -1};\n \n while (!q.empty()) {\n int row = q.front().first;\n int col = q.front().second;\n int dist = safe[row][col];\n q.pop();\n \n for (int i = 0; i < 4; i++) {\n int nrow = row + drow[i];\n int ncol = col + dcol[i];\n \n if (nrow >= 0 && nrow < n && ncol >= 0 && ncol < n && safe[nrow][ncol] > dist + 1) {\n safe[nrow][ncol] = dist + 1;\n q.push({nrow, ncol});\n }\n }\n }\n \n return safe;\n }\n \n bool check(vector<vector<int>>& grid, int mini, vector<vector<int>>& safe) {\n int n = grid.size();\n queue<pair<int, pair<int, int>>> q;\n vector<vector<bool>> visited(n, vector<bool>(n, false));\n \n if (safe[0][0] < mini) return false;\n q.push({safe[0][0], {0, 0}});\n visited[0][0] = true;\n \n int drow[] = {-1, 0, +1, 0};\n int dcol[] = {0, +1, 0, -1};\n \n while (!q.empty()) {\n int dist = q.front().first;\n int row = q.front().second.first;\n int col = q.front().second.second;\n q.pop();\n \n if (row == n - 1 && col == n - 1 && dist >= mini)\n return true;\n \n for (int i = 0; i < 4; i++) {\n int nrow = row + drow[i];\n int ncol = col + dcol[i];\n \n if (nrow >= 0 && nrow < n && ncol >= 0 && ncol < n && !visited[nrow][ncol] && safe[nrow][ncol] >= mini) {\n visited[nrow][ncol] = true;\n q.push({safe[nrow][ncol], {nrow, ncol}});\n }\n }\n }\n \n return false;\n }\n\n int maximumSafenessFactor(vector<vector<int>>& grid) {\n int n = grid.size();\n vector<pair<int, int>> thieves;\n \n if (grid[0][0] == 1 || grid[n-1][n-1] == 1) return 0;\n \n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n if (grid[i][j] == 1)\n thieves.push_back({i, j});\n }\n }\n \n vector<vector<int>> safe = computeSafeDistances(grid, thieves);\n \n int low = 0, high = n * 2;\n while (low <= high) {\n int mid = (low + high) / 2;\n if (check(grid, mid, safe))\n low = mid + 1;\n else\n high = mid - 1;\n }\n \n return high;\n }\n};\n",
"memory": "203773"
} |
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 <code>(0, 0)</code>. In one move, you can move to any adjacent cell in the grid, including cells containing thieves.</p>
<p>The <strong>safeness factor</strong> of a path on the grid is defined as the <strong>minimum</strong> manhattan distance from any cell in the path to any thief in the grid.</p>
<p>Return <em>the <strong>maximum safeness factor</strong> of all paths leading to cell </em><code>(n - 1, n - 1)</code><em>.</em></p>
<p>An <strong>adjacent</strong> cell of cell <code>(r, c)</code>, is one of the cells <code>(r, c + 1)</code>, <code>(r, c - 1)</code>, <code>(r + 1, c)</code> and <code>(r - 1, c)</code> if it exists.</p>
<p>The <strong>Manhattan distance</strong> between two cells <code>(a, b)</code> and <code>(x, y)</code> is equal to <code>|a - x| + |b - y|</code>, where <code>|val|</code> denotes the absolute value of val.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example1.png" style="width: 362px; height: 242px;" />
<pre>
<strong>Input:</strong> grid = [[1,0,0],[0,0,0],[0,0,1]]
<strong>Output:</strong> 0
<strong>Explanation:</strong> All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example2.png" style="width: 362px; height: 242px;" />
<pre>
<strong>Input:</strong> grid = [[0,0,1],[0,0,0],[0,0,0]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since:
- The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2.
It can be shown that there are no other paths with a higher safeness factor.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example3.png" style="width: 362px; height: 242px;" />
<pre>
<strong>Input:</strong> grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since:
- The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2.
- The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2.
It can be shown that there are no other paths with a higher safeness factor.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length == n <= 400</code></li>
<li><code>grid[i].length == n</code></li>
<li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li>
<li>There is at least one thief in the <code>grid</code>.</li>
</ul>
| 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] = 1;\n\n int n = (int)v.size();\n\n while(!q.empty()){\n pp x = q.front();\n q.pop();\n for(auto k:M){\n int x1 = x.first + k.first;\n int y1 = x.second + k.second;\n\n if(x1>=0 and x1<n and y1>=0 and y1<n){\n if(d[x1][y1] >= t and v[x1][y1]==0){\n q.push({x1, y1});\n v[x1][y1] = 1;\n }\n }\n }\n }\n return v[n-1][n-1]==1;\n }\n\n\n int maximumSafenessFactor(vector<vector<int>>& g) {\n int n = (int)g.size(), i, j;\n\n vector<vector<int>> d(n, vector<int> (n, INT_MAX));\n vector<vector<int>> v(n, vector<int> (n, 0));\n\n\n queue<pp> q;\n\n for(i=0;i<n;i++){\n for(j=0;j<n;j++){\n if(g[i][j]){\n q.push({i, j});\n d[i][j] = 0;\n }\n }\n }\n\n while(!q.empty()){\n pp t = q.front();\n q.pop();\n\n for(auto i:M){\n int x = t.first + i.first, y = t.second + i.second;\n if(x>=0 and x<n and y>=0 and y<n){\n if(d[x][y]==INT_MAX){\n d[x][y] = 1 + d[t.first][t.second];\n q.push({x, y});\n }\n }\n }\n }\n\n i = 0;\n j = n;\n int ans = 0;\n\n while(i<=j){\n fill(v.begin(), v.end(), vector<int> (n, 0));\n int m = i + (j-i)/2;\n if(f(m, d, v)){\n ans = m;\n i = m + 1;\n }else\n j = m - 1;\n }\n \n return ans;\n \n }\n};",
"memory": "203773"
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.