id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
238
<p>Given an integer array <code>nums</code>, return <em>an array</em> <code>answer</code> <em>such that</em> <code>answer[i]</code> <em>is equal to the product of all the elements of</em> <code>nums</code> <em>except</em> <code>nums[i]</code>.</p> <p>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</p> <p>You must write an algorithm that runs in&nbsp;<code>O(n)</code>&nbsp;time and without using the division operation.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> nums = [1,2,3,4] <strong>Output:</strong> [24,12,8,6] </pre><p><strong class="example">Example 2:</strong></p> <pre><strong>Input:</strong> nums = [-1,1,0,-3,3] <strong>Output:</strong> [0,0,9,0,0] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-30 &lt;= nums[i] &lt;= 30</code></li> <li>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong>&nbsp;Can you solve the problem in <code>O(1)</code>&nbsp;extra&nbsp;space complexity? (The output array <strong>does not</strong> count as extra space for space complexity analysis.)</p>
3
{ "code": "class Solution {\npublic:\n vector<int> productExceptSelf(vector<int>& nums) {\n int n = nums.size();\n vector<int> res;\n int lpref[n];\n int rpref[n];\n\n lpref[0] = nums[0];\n rpref[n-1] = nums[n-1];\n for(int i = 1; i < n ;i++){\n lpref[i] = lpref[i-1]* nums[i];\n }\n\n for(int i = n-2 ; i >= 0 ; i--){\n rpref[i] = rpref[i+1] * nums[i];\n }\n\n int ans = 1;\n for(int i = 0 ; i < n ; i++){\n if(i == 0){\n ans = rpref[1];\n }\n else if(i == n-1){\n ans = lpref[n-2];\n }\n else{\n ans = lpref[i-1] * rpref[i+1];\n }\n\n res.push_back(ans); \n }\n return res; \n }\n};", "memory": "40600" }
238
<p>Given an integer array <code>nums</code>, return <em>an array</em> <code>answer</code> <em>such that</em> <code>answer[i]</code> <em>is equal to the product of all the elements of</em> <code>nums</code> <em>except</em> <code>nums[i]</code>.</p> <p>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</p> <p>You must write an algorithm that runs in&nbsp;<code>O(n)</code>&nbsp;time and without using the division operation.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> nums = [1,2,3,4] <strong>Output:</strong> [24,12,8,6] </pre><p><strong class="example">Example 2:</strong></p> <pre><strong>Input:</strong> nums = [-1,1,0,-3,3] <strong>Output:</strong> [0,0,9,0,0] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-30 &lt;= nums[i] &lt;= 30</code></li> <li>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong>&nbsp;Can you solve the problem in <code>O(1)</code>&nbsp;extra&nbsp;space complexity? (The output array <strong>does not</strong> count as extra space for space complexity analysis.)</p>
3
{ "code": "class Solution {\npublic:\n vector<int> productExceptSelf(vector<int>& nums) {\n int pre[nums.size()];\n int post[nums.size()];\n vector<int> ans;\n pre[0] = nums[0];\n for(int i=1;i<nums.size();i++){\n pre[i] = nums[i]*pre[i-1];\n }\n for(int i=0;i<nums.size();i++){\n cout<<pre[i]<<endl;\n }\n post[nums.size()-1] = nums[nums.size()-1];\n for(int i=nums.size()-2;i>=0;i--){\n post[i] = nums[i]*post[i+1];\n }\n for (int i = 0; i < nums.size(); i++) {\n int prefix = (i > 0) ? pre[i - 1] : 1; // Avoid accessing -1 index\n int postfix = (i < nums.size() - 1) ? post[i + 1] : 1; // Avoid accessing out-of-bounds index\n ans.push_back(prefix * postfix);\n }\n for(int i=0;i<nums.size();i++){\n cout<<pre[i]<<endl;\n }\n return ans;\n }\n};", "memory": "40700" }
238
<p>Given an integer array <code>nums</code>, return <em>an array</em> <code>answer</code> <em>such that</em> <code>answer[i]</code> <em>is equal to the product of all the elements of</em> <code>nums</code> <em>except</em> <code>nums[i]</code>.</p> <p>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</p> <p>You must write an algorithm that runs in&nbsp;<code>O(n)</code>&nbsp;time and without using the division operation.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> nums = [1,2,3,4] <strong>Output:</strong> [24,12,8,6] </pre><p><strong class="example">Example 2:</strong></p> <pre><strong>Input:</strong> nums = [-1,1,0,-3,3] <strong>Output:</strong> [0,0,9,0,0] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-30 &lt;= nums[i] &lt;= 30</code></li> <li>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong>&nbsp;Can you solve the problem in <code>O(1)</code>&nbsp;extra&nbsp;space complexity? (The output array <strong>does not</strong> count as extra space for space complexity analysis.)</p>
3
{ "code": "class Solution {\npublic:\n vector<int> productExceptSelf(vector<int>& nums) {\n int n = nums.size();\n int left[n], right[n];\n left[0] = 1, right[n-1] = 1;\n for(int i = 1; i < n; i++){\n left[i] = left[i-1]*nums[i-1];\n }\n for(int i = n-2; i >= 0; i--)\n right[i] = right[i+1]*nums[i+1];\n \n vector<int> ans;\n for(int i = 0; i < n; i++)\n {\n int val = left[i]*right[i];\n ans.push_back(val);\n }\n return ans;\n }\n};", "memory": "40700" }
238
<p>Given an integer array <code>nums</code>, return <em>an array</em> <code>answer</code> <em>such that</em> <code>answer[i]</code> <em>is equal to the product of all the elements of</em> <code>nums</code> <em>except</em> <code>nums[i]</code>.</p> <p>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</p> <p>You must write an algorithm that runs in&nbsp;<code>O(n)</code>&nbsp;time and without using the division operation.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> nums = [1,2,3,4] <strong>Output:</strong> [24,12,8,6] </pre><p><strong class="example">Example 2:</strong></p> <pre><strong>Input:</strong> nums = [-1,1,0,-3,3] <strong>Output:</strong> [0,0,9,0,0] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-30 &lt;= nums[i] &lt;= 30</code></li> <li>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong>&nbsp;Can you solve the problem in <code>O(1)</code>&nbsp;extra&nbsp;space complexity? (The output array <strong>does not</strong> count as extra space for space complexity analysis.)</p>
3
{ "code": "class Solution {\npublic:\n vector<int> productExceptSelf(vector<int>& nums) {\n vector<int> res;\n int n = nums.size();\n int pf[n];\n int sf[n];\n pf[0] = nums[0];\n sf[n-1] = nums[n-1];\n for(int i=1; i<n; i++){\n pf[i] = pf[i-1]*nums[i];\n }\n for(int i=n-2; i>=0; i--){\n sf[i] = sf[i+1]*nums[i];\n }\n res.push_back(sf[1]);\n for(int i=1; i<n-1; i++){\n res.push_back(sf[i+1] * pf[i-1]);\n }\n res.push_back(pf[n-2]);\n return res;\n }\n};", "memory": "40800" }
238
<p>Given an integer array <code>nums</code>, return <em>an array</em> <code>answer</code> <em>such that</em> <code>answer[i]</code> <em>is equal to the product of all the elements of</em> <code>nums</code> <em>except</em> <code>nums[i]</code>.</p> <p>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</p> <p>You must write an algorithm that runs in&nbsp;<code>O(n)</code>&nbsp;time and without using the division operation.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> nums = [1,2,3,4] <strong>Output:</strong> [24,12,8,6] </pre><p><strong class="example">Example 2:</strong></p> <pre><strong>Input:</strong> nums = [-1,1,0,-3,3] <strong>Output:</strong> [0,0,9,0,0] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-30 &lt;= nums[i] &lt;= 30</code></li> <li>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong>&nbsp;Can you solve the problem in <code>O(1)</code>&nbsp;extra&nbsp;space complexity? (The output array <strong>does not</strong> count as extra space for space complexity analysis.)</p>
3
{ "code": "class Solution {\npublic:\n vector<int> productExceptSelf(vector<int>& nums) {\n int n = nums.size();\n vector<int> answer;\n int prefix[n];\n int suffix[n];\n prefix[0] = 1;\n suffix[n-1] = 1;\n\n for(int i = 1; i < n;i++){\n prefix[i] = prefix[i-1]*nums[i-1];\n }\n for(int i = n-2; i >= 0;i--){\n suffix[i] = suffix[i+1]*nums[i+1];\n }\n for(int i = 0; i < n;++i){\n answer.push_back(suffix[i] * prefix[i]);\n }\n return answer;\n }\n\n};", "memory": "40800" }
238
<p>Given an integer array <code>nums</code>, return <em>an array</em> <code>answer</code> <em>such that</em> <code>answer[i]</code> <em>is equal to the product of all the elements of</em> <code>nums</code> <em>except</em> <code>nums[i]</code>.</p> <p>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</p> <p>You must write an algorithm that runs in&nbsp;<code>O(n)</code>&nbsp;time and without using the division operation.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> nums = [1,2,3,4] <strong>Output:</strong> [24,12,8,6] </pre><p><strong class="example">Example 2:</strong></p> <pre><strong>Input:</strong> nums = [-1,1,0,-3,3] <strong>Output:</strong> [0,0,9,0,0] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-30 &lt;= nums[i] &lt;= 30</code></li> <li>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong>&nbsp;Can you solve the problem in <code>O(1)</code>&nbsp;extra&nbsp;space complexity? (The output array <strong>does not</strong> count as extra space for space complexity analysis.)</p>
3
{ "code": "class Solution {\npublic:\n vector<int> productExceptSelf(vector<int>& nums) {\n vector<int> res;\n int n = nums.size();\n int pf[n];\n int sf[n];\n pf[0] = nums[0];\n sf[n-1] = nums[n-1];\n for(int i=1; i<n; i++){\n pf[i] = pf[i-1]*nums[i];\n }\n for(int i=n-2; i>=0; i--){\n sf[i] = sf[i+1]*nums[i];\n }\n res.push_back(sf[1]);\n for(int i=1; i<n-1; i++){\n res.push_back(sf[i+1] * pf[i-1]);\n }\n res.push_back(pf[n-2]);\n return res;\n }\n};\nauto init=[](){\n ios_base::sync_with_stdio(0);\n cin.tie(0);\n cout.tie(0);\n return 0;\n}();", "memory": "40900" }
238
<p>Given an integer array <code>nums</code>, return <em>an array</em> <code>answer</code> <em>such that</em> <code>answer[i]</code> <em>is equal to the product of all the elements of</em> <code>nums</code> <em>except</em> <code>nums[i]</code>.</p> <p>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</p> <p>You must write an algorithm that runs in&nbsp;<code>O(n)</code>&nbsp;time and without using the division operation.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> nums = [1,2,3,4] <strong>Output:</strong> [24,12,8,6] </pre><p><strong class="example">Example 2:</strong></p> <pre><strong>Input:</strong> nums = [-1,1,0,-3,3] <strong>Output:</strong> [0,0,9,0,0] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-30 &lt;= nums[i] &lt;= 30</code></li> <li>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong>&nbsp;Can you solve the problem in <code>O(1)</code>&nbsp;extra&nbsp;space complexity? (The output array <strong>does not</strong> count as extra space for space complexity analysis.)</p>
3
{ "code": "class Solution {\npublic:\n vector<int> productExceptSelf(vector<int>& nums) {\n int n = nums.size();\n vector<int> answer;\n int prefix[n];\n int suffix[n];\n prefix[0] = 1;\n suffix[n-1] = 1;\n\n for(int i = 1; i < n;i++){\n prefix[i] = prefix[i-1]*nums[i-1];\n }\n for(int i = n-2; i >= 0;i--){\n suffix[i] = suffix[i+1]*nums[i+1];\n }\n for(int i = 0; i < n;++i){\n answer.push_back(suffix[i] * prefix[i]);\n }\n return answer;\n }\n\n};", "memory": "40900" }
238
<p>Given an integer array <code>nums</code>, return <em>an array</em> <code>answer</code> <em>such that</em> <code>answer[i]</code> <em>is equal to the product of all the elements of</em> <code>nums</code> <em>except</em> <code>nums[i]</code>.</p> <p>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</p> <p>You must write an algorithm that runs in&nbsp;<code>O(n)</code>&nbsp;time and without using the division operation.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> nums = [1,2,3,4] <strong>Output:</strong> [24,12,8,6] </pre><p><strong class="example">Example 2:</strong></p> <pre><strong>Input:</strong> nums = [-1,1,0,-3,3] <strong>Output:</strong> [0,0,9,0,0] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-30 &lt;= nums[i] &lt;= 30</code></li> <li>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong>&nbsp;Can you solve the problem in <code>O(1)</code>&nbsp;extra&nbsp;space complexity? (The output array <strong>does not</strong> count as extra space for space complexity analysis.)</p>
3
{ "code": "class Solution {\npublic:\n vector<int> productExceptSelf(vector<int>& nums) {\n vector<int> final_array;\n unordered_map<int, int> count_map;\n int prod = 1;\n for(int i : nums){\n if(i != 0 && count_map[0] <= 1){\n prod*=i;\n }\n count_map[i]++;\n }\n\n if(count_map[0]>=1){\n if(count_map[0]>1){\n final_array.resize(nums.size(), 0);\n }else{\n for(int i : nums){\n if(i!=0){\n final_array.push_back(0);\n }else{\n final_array.push_back(prod);\n }\n }\n }\n \n }\n else{\n for(int i : nums){\n final_array.push_back(prod/i);\n }\n }\n\n return final_array;\n \n }\n};", "memory": "41000" }
238
<p>Given an integer array <code>nums</code>, return <em>an array</em> <code>answer</code> <em>such that</em> <code>answer[i]</code> <em>is equal to the product of all the elements of</em> <code>nums</code> <em>except</em> <code>nums[i]</code>.</p> <p>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</p> <p>You must write an algorithm that runs in&nbsp;<code>O(n)</code>&nbsp;time and without using the division operation.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> nums = [1,2,3,4] <strong>Output:</strong> [24,12,8,6] </pre><p><strong class="example">Example 2:</strong></p> <pre><strong>Input:</strong> nums = [-1,1,0,-3,3] <strong>Output:</strong> [0,0,9,0,0] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-30 &lt;= nums[i] &lt;= 30</code></li> <li>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong>&nbsp;Can you solve the problem in <code>O(1)</code>&nbsp;extra&nbsp;space complexity? (The output array <strong>does not</strong> count as extra space for space complexity analysis.)</p>
3
{ "code": "class Solution {\npublic:\n vector<int> productExceptSelf(vector<int>& nums) {\n vector<int> before;\n vector<int> after(nums.size());\n before.push_back(1);\n after[nums.size() - 1] = 1;\n\n for(int i = 1; i < nums.size(); ++i){\n before.push_back(before[i - 1] * nums[i - 1]);\n }\n\n for(int i = after.size() - 2; i >= 0; --i){\n after[i] = after[i + 1] * nums[i + 1];\n }\n\n for(int i = 0; i < nums.size(); ++i){\n before[i] *= after[i];\n }\n\n return before;\n }\n};", "memory": "41100" }
238
<p>Given an integer array <code>nums</code>, return <em>an array</em> <code>answer</code> <em>such that</em> <code>answer[i]</code> <em>is equal to the product of all the elements of</em> <code>nums</code> <em>except</em> <code>nums[i]</code>.</p> <p>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</p> <p>You must write an algorithm that runs in&nbsp;<code>O(n)</code>&nbsp;time and without using the division operation.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> nums = [1,2,3,4] <strong>Output:</strong> [24,12,8,6] </pre><p><strong class="example">Example 2:</strong></p> <pre><strong>Input:</strong> nums = [-1,1,0,-3,3] <strong>Output:</strong> [0,0,9,0,0] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-30 &lt;= nums[i] &lt;= 30</code></li> <li>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong>&nbsp;Can you solve the problem in <code>O(1)</code>&nbsp;extra&nbsp;space complexity? (The output array <strong>does not</strong> count as extra space for space complexity analysis.)</p>
3
{ "code": "class Solution {\npublic:\n vector<int> productExceptSelf(vector<int>& nums) {\n // int prod = 1;\n // for(int elem:nums) {\n // prod = prod*elem;\n // }\n int end = nums.size();\n\n deque<int> inc;\n stack<int> dec;\n inc.push_front(nums[0]);\n dec.push(nums[end-1]);\n\n for(int i=1;i<end-1;i++) {\n inc.push_back(inc.back()*nums[i]);\n }\n for(int i=end-2;i>0;i--) {\n dec.push(dec.top()*nums[i]);\n }\n\n nums[0] = dec.top();\n dec.pop();\n for(int i=1;i<end-1;i++) {\n nums[i] = inc.front()*dec.top();\n inc.pop_front();\n dec.pop();\n }\n nums[end-1] = inc.back();\n inc.pop_back();\n\n return nums;\n }\n};", "memory": "41200" }
238
<p>Given an integer array <code>nums</code>, return <em>an array</em> <code>answer</code> <em>such that</em> <code>answer[i]</code> <em>is equal to the product of all the elements of</em> <code>nums</code> <em>except</em> <code>nums[i]</code>.</p> <p>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</p> <p>You must write an algorithm that runs in&nbsp;<code>O(n)</code>&nbsp;time and without using the division operation.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> nums = [1,2,3,4] <strong>Output:</strong> [24,12,8,6] </pre><p><strong class="example">Example 2:</strong></p> <pre><strong>Input:</strong> nums = [-1,1,0,-3,3] <strong>Output:</strong> [0,0,9,0,0] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-30 &lt;= nums[i] &lt;= 30</code></li> <li>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong>&nbsp;Can you solve the problem in <code>O(1)</code>&nbsp;extra&nbsp;space complexity? (The output array <strong>does not</strong> count as extra space for space complexity analysis.)</p>
3
{ "code": "class Solution {\npublic:\n vector<int> productExceptSelf(vector<int>& nums) {\n // int prod = 1;\n // for(int elem:nums) {\n // prod = prod*elem;\n // }\n int end = nums.size();\n\n deque<int> inc;\n stack<int> dec;\n inc.push_front(nums[0]);\n dec.push(nums[end-1]);\n\n for(int i=1;i<end-1;i++) {\n inc.push_back(inc.back()*nums[i]);\n }\n for(int i=end-2;i>0;i--) {\n dec.push(dec.top()*nums[i]);\n }\n\n nums[0] = dec.top();\n dec.pop();\n for(int i=1;i<end-1;i++) {\n nums[i] = inc.front()*dec.top();\n inc.pop_front();\n dec.pop();\n }\n nums[end-1] = inc.back();\n inc.pop_back();\n\n return nums;\n }\n};", "memory": "41200" }
238
<p>Given an integer array <code>nums</code>, return <em>an array</em> <code>answer</code> <em>such that</em> <code>answer[i]</code> <em>is equal to the product of all the elements of</em> <code>nums</code> <em>except</em> <code>nums[i]</code>.</p> <p>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</p> <p>You must write an algorithm that runs in&nbsp;<code>O(n)</code>&nbsp;time and without using the division operation.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> nums = [1,2,3,4] <strong>Output:</strong> [24,12,8,6] </pre><p><strong class="example">Example 2:</strong></p> <pre><strong>Input:</strong> nums = [-1,1,0,-3,3] <strong>Output:</strong> [0,0,9,0,0] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-30 &lt;= nums[i] &lt;= 30</code></li> <li>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong>&nbsp;Can you solve the problem in <code>O(1)</code>&nbsp;extra&nbsp;space complexity? (The output array <strong>does not</strong> count as extra space for space complexity analysis.)</p>
3
{ "code": "class Solution {\npublic:\n vector<int> productExceptSelf(vector<int>& nums) {\n int n=nums.size();\n vector<int> prefix(n),suffix(n);\n prefix[0]=nums[0];\n for(int i=1;i<n;i++){\n prefix[i]=prefix[i-1]*nums[i];\n }\n vector<int> nums2(n);\n for(int i=0;i<n;i++){\n nums2[i]=nums[i];\n }\n reverse(nums2.begin(),nums2.end());\n suffix[0]=nums2[0];\n for(int i=1;i<n;i++){\n suffix[i]=suffix[i-1]*nums2[i];\n }\n reverse(suffix.begin(),suffix.end());\n for(int i=0;i<n;i++){\n cout<<prefix[i]<<\" \";\n }\n cout<<endl;\n for(int i=0;i<n;i++){\n cout<<suffix[i]<<\" \";\n }\n vector<int> ans(n);\n for(int i=0;i<n;i++){\n if(i==0){\n ans[i]=suffix[i+1];\n }\n else if(i==n-1){\n ans[i]=prefix[i-1];\n }\n else{\n ans[i]=prefix[i-1]*suffix[i+1];\n }\n }\n return ans;\n }\n};", "memory": "41300" }
238
<p>Given an integer array <code>nums</code>, return <em>an array</em> <code>answer</code> <em>such that</em> <code>answer[i]</code> <em>is equal to the product of all the elements of</em> <code>nums</code> <em>except</em> <code>nums[i]</code>.</p> <p>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</p> <p>You must write an algorithm that runs in&nbsp;<code>O(n)</code>&nbsp;time and without using the division operation.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> nums = [1,2,3,4] <strong>Output:</strong> [24,12,8,6] </pre><p><strong class="example">Example 2:</strong></p> <pre><strong>Input:</strong> nums = [-1,1,0,-3,3] <strong>Output:</strong> [0,0,9,0,0] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-30 &lt;= nums[i] &lt;= 30</code></li> <li>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong>&nbsp;Can you solve the problem in <code>O(1)</code>&nbsp;extra&nbsp;space complexity? (The output array <strong>does not</strong> count as extra space for space complexity analysis.)</p>
3
{ "code": "class Solution {\npublic:\n vector<int> productExceptSelf(vector<int>& nums) {\n vector<int> ret(nums.size(), 1);\n int n=nums.size();\n vector<int> pre(n,1);\n vector<int> suff(n,1);\n\n pre[0]=1;\n suff[n-1]=1;\n for(int i=1; i<n;i++){\n pre[i]=pre[i-1]*nums[i-1];\n }\n for(int i=n-2; i>=0; i--){\n suff[i]=suff[i+1]*nums[i+1];\n }\n vector<int> ans(n,1);\n for(int i=0;i<n;i++){\n ans[i]=pre[i]*suff[i];\n }\n return ans;\n }\n};", "memory": "41400" }
238
<p>Given an integer array <code>nums</code>, return <em>an array</em> <code>answer</code> <em>such that</em> <code>answer[i]</code> <em>is equal to the product of all the elements of</em> <code>nums</code> <em>except</em> <code>nums[i]</code>.</p> <p>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</p> <p>You must write an algorithm that runs in&nbsp;<code>O(n)</code>&nbsp;time and without using the division operation.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> nums = [1,2,3,4] <strong>Output:</strong> [24,12,8,6] </pre><p><strong class="example">Example 2:</strong></p> <pre><strong>Input:</strong> nums = [-1,1,0,-3,3] <strong>Output:</strong> [0,0,9,0,0] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-30 &lt;= nums[i] &lt;= 30</code></li> <li>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong>&nbsp;Can you solve the problem in <code>O(1)</code>&nbsp;extra&nbsp;space complexity? (The output array <strong>does not</strong> count as extra space for space complexity analysis.)</p>
3
{ "code": "class Solution {\npublic:\nvector<int> productExceptSelf(vector<int> nums)\n{\n int n = nums.size();\n vector<int> res(n);\n vector<int> prefix(n);\n int pre = 1;\n for (int i = 0; i < n; i++)\n {\n prefix[i] = pre;\n pre *= nums[i];\n }\n\n int pos = 1;\n vector<int> posfix(n);\n for (int i = n - 1; i >= 0; i--)\n {\n posfix[i] = pos;\n pos *= nums[i];\n }\n for (int i = 0; i < n; i++)\n {\n res[i] = prefix[i] * posfix[i];\n }\n return res;\n}\n};", "memory": "41500" }
238
<p>Given an integer array <code>nums</code>, return <em>an array</em> <code>answer</code> <em>such that</em> <code>answer[i]</code> <em>is equal to the product of all the elements of</em> <code>nums</code> <em>except</em> <code>nums[i]</code>.</p> <p>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</p> <p>You must write an algorithm that runs in&nbsp;<code>O(n)</code>&nbsp;time and without using the division operation.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> nums = [1,2,3,4] <strong>Output:</strong> [24,12,8,6] </pre><p><strong class="example">Example 2:</strong></p> <pre><strong>Input:</strong> nums = [-1,1,0,-3,3] <strong>Output:</strong> [0,0,9,0,0] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-30 &lt;= nums[i] &lt;= 30</code></li> <li>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong>&nbsp;Can you solve the problem in <code>O(1)</code>&nbsp;extra&nbsp;space complexity? (The output array <strong>does not</strong> count as extra space for space complexity analysis.)</p>
3
{ "code": "class Solution {\npublic:\n vector<int> productExceptSelf(vector<int>& nums) {\n vector<int> ret(nums.size(), 1);\n int n=nums.size();\n vector<int> pre(n,1);\n vector<int> suff(n,1);\n\n pre[0]=1;\n suff[n-1]=1;\n for(int i=1; i<n;i++){\n pre[i]=pre[i-1]*nums[i-1];\n }\n for(int i=n-2; i>=0; i--){\n suff[i]=suff[i+1]*nums[i+1];\n }\n vector<int> ans(n,1);\n for(int i=0;i<n;i++){\n ans[i]=pre[i]*suff[i];\n }\n return ans;\n }\n};", "memory": "41500" }
238
<p>Given an integer array <code>nums</code>, return <em>an array</em> <code>answer</code> <em>such that</em> <code>answer[i]</code> <em>is equal to the product of all the elements of</em> <code>nums</code> <em>except</em> <code>nums[i]</code>.</p> <p>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</p> <p>You must write an algorithm that runs in&nbsp;<code>O(n)</code>&nbsp;time and without using the division operation.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> nums = [1,2,3,4] <strong>Output:</strong> [24,12,8,6] </pre><p><strong class="example">Example 2:</strong></p> <pre><strong>Input:</strong> nums = [-1,1,0,-3,3] <strong>Output:</strong> [0,0,9,0,0] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-30 &lt;= nums[i] &lt;= 30</code></li> <li>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong>&nbsp;Can you solve the problem in <code>O(1)</code>&nbsp;extra&nbsp;space complexity? (The output array <strong>does not</strong> count as extra space for space complexity analysis.)</p>
3
{ "code": "class Solution {\npublic:\n vector<int> productExceptSelf(vector<int>& nums) {\n int size = nums.size();\n vector<int> ret_vct(size);\n vector<int> l(size);\n l[0] = 1;\n vector<int> r(size-1);\n r.push_back(1);\n for(int i = 1; i < size; i++) {\n l[i] = l[i-1] * nums[i-1];\n }\n for (int i = size-2; i >= 0; i--){\n r[i] = r[i+1]* nums[i+1];\n }\n for (int i =0; i < size; i++) {\n ret_vct[i] = l[i] * r[i];\n }\n return ret_vct;\n }\n};", "memory": "41600" }
238
<p>Given an integer array <code>nums</code>, return <em>an array</em> <code>answer</code> <em>such that</em> <code>answer[i]</code> <em>is equal to the product of all the elements of</em> <code>nums</code> <em>except</em> <code>nums[i]</code>.</p> <p>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</p> <p>You must write an algorithm that runs in&nbsp;<code>O(n)</code>&nbsp;time and without using the division operation.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> nums = [1,2,3,4] <strong>Output:</strong> [24,12,8,6] </pre><p><strong class="example">Example 2:</strong></p> <pre><strong>Input:</strong> nums = [-1,1,0,-3,3] <strong>Output:</strong> [0,0,9,0,0] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-30 &lt;= nums[i] &lt;= 30</code></li> <li>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong>&nbsp;Can you solve the problem in <code>O(1)</code>&nbsp;extra&nbsp;space complexity? (The output array <strong>does not</strong> count as extra space for space complexity analysis.)</p>
3
{ "code": "class Solution {\npublic:\n vector<int> productExceptSelf(vector<int>& nums) {\n int n = nums.size();\n vector<int> pre(n,1);\n vector<int> pos(n,1);\n vector<int> ans;\n int prod = 1;\n for(int i =0; i < n; i++)\n {\n pre[i] = prod;\n prod *= nums[i];\n }\n prod = 1;\n for(int i = n -1; i >= 0; i--)\n {\n pos[i] = prod;\n prod *= nums[i];\n }\n for(int i =0; i < n; i++)\n {\n ans.push_back(pre[i]*pos[i]);\n }\n\n return ans;\n }\n};", "memory": "41700" }
238
<p>Given an integer array <code>nums</code>, return <em>an array</em> <code>answer</code> <em>such that</em> <code>answer[i]</code> <em>is equal to the product of all the elements of</em> <code>nums</code> <em>except</em> <code>nums[i]</code>.</p> <p>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</p> <p>You must write an algorithm that runs in&nbsp;<code>O(n)</code>&nbsp;time and without using the division operation.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> nums = [1,2,3,4] <strong>Output:</strong> [24,12,8,6] </pre><p><strong class="example">Example 2:</strong></p> <pre><strong>Input:</strong> nums = [-1,1,0,-3,3] <strong>Output:</strong> [0,0,9,0,0] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-30 &lt;= nums[i] &lt;= 30</code></li> <li>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong>&nbsp;Can you solve the problem in <code>O(1)</code>&nbsp;extra&nbsp;space complexity? (The output array <strong>does not</strong> count as extra space for space complexity analysis.)</p>
3
{ "code": "class Solution {\npublic:\n vector<int> productExceptSelf(vector<int>& nums) {\n int n = nums.size();\n\n vector<int>p(n+1, 1), s(n+1, 1), ans;\n\n for(int i=0;i<n;i++){\n p[i+1] = p[i]*nums[i];\n }\n for(int i=n-1;i>=0;i--){\n s[i] = s[i+1]*nums[i];\n }\n\n for(int i=0;i<n;i++){\n ans.push_back( p[i]*s[i+1]);\n }\n\n return ans;\n\n }\n};", "memory": "41800" }
238
<p>Given an integer array <code>nums</code>, return <em>an array</em> <code>answer</code> <em>such that</em> <code>answer[i]</code> <em>is equal to the product of all the elements of</em> <code>nums</code> <em>except</em> <code>nums[i]</code>.</p> <p>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</p> <p>You must write an algorithm that runs in&nbsp;<code>O(n)</code>&nbsp;time and without using the division operation.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> nums = [1,2,3,4] <strong>Output:</strong> [24,12,8,6] </pre><p><strong class="example">Example 2:</strong></p> <pre><strong>Input:</strong> nums = [-1,1,0,-3,3] <strong>Output:</strong> [0,0,9,0,0] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-30 &lt;= nums[i] &lt;= 30</code></li> <li>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong>&nbsp;Can you solve the problem in <code>O(1)</code>&nbsp;extra&nbsp;space complexity? (The output array <strong>does not</strong> count as extra space for space complexity analysis.)</p>
3
{ "code": "class Solution {\npublic:\n vector<int> productExceptSelf(vector<int>& nums) {\n vector<int> ans;\n vector<int> pre = nums;\n vector<int> suf = nums;\n pre[0]=1;\n suf[nums.size()-1]=1;\n for(int i=1 ; i<nums.size(); i++){\n pre[i] = nums[i-1] * pre[i-1];\n }\n for(int i=nums.size()-2; i>=0; i--){\n suf[i] = nums[i+1] * suf[i+1];\n }\n for(int i=0; i<nums.size(); i++){\n ans.push_back(pre[i] * suf[i]);\n }\n return ans;\n }\n};", "memory": "41800" }
238
<p>Given an integer array <code>nums</code>, return <em>an array</em> <code>answer</code> <em>such that</em> <code>answer[i]</code> <em>is equal to the product of all the elements of</em> <code>nums</code> <em>except</em> <code>nums[i]</code>.</p> <p>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</p> <p>You must write an algorithm that runs in&nbsp;<code>O(n)</code>&nbsp;time and without using the division operation.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> nums = [1,2,3,4] <strong>Output:</strong> [24,12,8,6] </pre><p><strong class="example">Example 2:</strong></p> <pre><strong>Input:</strong> nums = [-1,1,0,-3,3] <strong>Output:</strong> [0,0,9,0,0] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-30 &lt;= nums[i] &lt;= 30</code></li> <li>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong>&nbsp;Can you solve the problem in <code>O(1)</code>&nbsp;extra&nbsp;space complexity? (The output array <strong>does not</strong> count as extra space for space complexity analysis.)</p>
3
{ "code": "class Solution {\npublic:\n vector<int> productExceptSelf(vector<int>& nums) {\n int x=nums.size()-1;\n vector<int>left(x+1);\n left[0]=1;\n for(int i=1;i<nums.size();i++){\n left[i]=left[i-1]*nums[i-1];\n }\n vector<int>right(x+1);\n right[x]=1;\n for(int i=x-1;i>=0;i--){\n right[i]=right[i+1]*nums[i+1];\n }\n vector<int>ans;\n ans.push_back(right[0]);\n for(int k=1;k<x;k++){\n ans.push_back(left[k]*right[k]);\n }\n ans.push_back(left[x]);\n return ans;\n }\n};", "memory": "41900" }
238
<p>Given an integer array <code>nums</code>, return <em>an array</em> <code>answer</code> <em>such that</em> <code>answer[i]</code> <em>is equal to the product of all the elements of</em> <code>nums</code> <em>except</em> <code>nums[i]</code>.</p> <p>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</p> <p>You must write an algorithm that runs in&nbsp;<code>O(n)</code>&nbsp;time and without using the division operation.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> nums = [1,2,3,4] <strong>Output:</strong> [24,12,8,6] </pre><p><strong class="example">Example 2:</strong></p> <pre><strong>Input:</strong> nums = [-1,1,0,-3,3] <strong>Output:</strong> [0,0,9,0,0] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-30 &lt;= nums[i] &lt;= 30</code></li> <li>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong>&nbsp;Can you solve the problem in <code>O(1)</code>&nbsp;extra&nbsp;space complexity? (The output array <strong>does not</strong> count as extra space for space complexity analysis.)</p>
3
{ "code": "class Solution {\npublic:\n vector<int> productExceptSelf(vector<int>& nums) {\n int n=nums.size();\n vector<int>pref(n),suff(n);\n vector<int> ans;\n pref[0]=1;\n for(int i=1;i<n;i++){\n pref[i]=pref[i-1]*nums[i-1];\n }\n suff[n-1]=1;\n for(int i=n-2;i>=0;i--){\n suff[i]=suff[i+1]*nums[i+1];\n }\n for(int i=0;i<n;i++){\n ans.push_back(pref[i]*suff[i]);\n }\n return ans;\n }\n\n};", "memory": "41900" }
238
<p>Given an integer array <code>nums</code>, return <em>an array</em> <code>answer</code> <em>such that</em> <code>answer[i]</code> <em>is equal to the product of all the elements of</em> <code>nums</code> <em>except</em> <code>nums[i]</code>.</p> <p>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</p> <p>You must write an algorithm that runs in&nbsp;<code>O(n)</code>&nbsp;time and without using the division operation.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> nums = [1,2,3,4] <strong>Output:</strong> [24,12,8,6] </pre><p><strong class="example">Example 2:</strong></p> <pre><strong>Input:</strong> nums = [-1,1,0,-3,3] <strong>Output:</strong> [0,0,9,0,0] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-30 &lt;= nums[i] &lt;= 30</code></li> <li>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong>&nbsp;Can you solve the problem in <code>O(1)</code>&nbsp;extra&nbsp;space complexity? (The output array <strong>does not</strong> count as extra space for space complexity analysis.)</p>
3
{ "code": "class Solution {\npublic:\n vector<int> productExceptSelf(vector<int>& nums) {\n vector<int> answer = {};\n int i = 0;\n int* pre = new int[nums.size()];\n int* post = new int[nums.size()];\n pre[0]=1; post[nums.size()-1]=1;\n for(int i=1; i<nums.size(); i++) {\n pre[i] = pre[i-1]*nums[i-1];\n }\n for(int i=nums.size()-2; i>=0; i--) {\n post[i] = post[i+1]*nums[i+1];\n }\n for(int i=0; i<nums.size(); i++) {\n answer.push_back(pre[i]*post[i]);\n }\n return answer;\n }\n};", "memory": "42000" }
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the max sliding window</em>.</p> <p>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= nums.length</code></li> </ul>
0
{ "code": "#pragma GCC optimize(\"Ofast\")\n#pragma GCC target(\"avx2,tune=native\")\n\nclass Solution {\npublic:\n vector<int> maxSlidingWindow(vector<int>& nums, int k) {\n ios_base::sync_with_stdio(false);\n cout.tie(0);\n cin.tie(0);\n\n short q[nums.size()];\n unsigned l = 0, r = 0;\n unsigned i = 0;\n unsigned k_i = 0;\n while(i<k-1) {\n while(l<r && q[r-1]<nums[i]) --r;\n q[r++] = nums[i++];\n }\n\n while(i<nums.size()) {\n while(l<r && q[r-1]<nums[i]) --r;\n \n q[r++] = nums[i];\n\n if(q[l] == nums[k_i]) ++l;\n else nums[k_i] = q[l];\n\n ++i;\n ++k_i;\n }\n\n nums.resize(k_i);\n\n return std::move(nums);\n }\n};", "memory": "127646" }
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the max sliding window</em>.</p> <p>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= nums.length</code></li> </ul>
0
{ "code": "// Enable aggressive compiler optimizations\n#pragma GCC optimize(\"Ofast\")\n#pragma GCC target(\"avx2,tune=native\")\n\n// Initialize the environment\nbool init() {\n // Disable synchronization with C streams for faster I/O\n ios_base::sync_with_stdio(false);\n // Untie cout from cin to reduce flushes\n cout.tie(nullptr);\n return true;\n}\n\n// Call init once to set up the environment\nbool _ = init();\n\nclass Solution {\npublic:\n // Function to find the maximum sliding window\n vector<int> maxSlidingWindow(vector<int>& nums, const int k) {\n // Create a fixed-size array to store the queue\n short q[nums.size()];\n // Initialize indices for the queue and result\n unsigned l = 0, r = 0;\n unsigned idx = 0;\n unsigned k_idx = 0;\n\n // Initialize the queue with the first k-1 elements\n while (idx < k - 1) {\n // Remove smaller elements from the end of the queue\n while (l < r && q[r - 1] < nums[idx]) {\n --r;\n }\n // Add the current element to the queue\n q[r++] = nums[idx++];\n }\n\n // Process the rest of the elements\n while (idx < nums.size()) {\n // Remove smaller elements from the end of the queue\n while (l < r && q[r - 1] < nums[idx]) {\n --r;\n }\n // Add the current element to the queue\n q[r++] = nums[idx];\n\n // Update the result\n if (q[l] == nums[k_idx]) {\n // Move to the next element in the queue\n ++l;\n } else {\n // Update the result with the maximum element\n nums[k_idx] = q[l];\n }\n\n // Move to the next element\n ++idx;\n ++k_idx;\n }\n\n // Resize the result vector to the exact size needed\n nums.resize(k_idx);\n\n // Return the result\n return std::move(nums);\n }\n};", "memory": "127646" }
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the max sliding window</em>.</p> <p>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= nums.length</code></li> </ul>
0
{ "code": "#pragma GCC optimize(\"Ofast\")\n#pragma GCC target(\"arch=native\")\nclass Solution {\n using out_t = std::vector<int>;\n\n static bool init() noexcept \n {\n ios::sync_with_stdio(0);\n cin.tie(0);\n return true;\n }\n static inline bool initialized = init();\n\npublic:\n out_t maxSlidingWindow(vector<int>& nums, const int k) const noexcept {\n const auto n = nums.size() + 1 - k;\n std::deque<int> q;\n\n q.push_back(0);\n for (int i = 1; i < k; ++i)\n {\n const auto x = nums[i];\n while (!q.empty() && x >= nums[q.back()]) {\n q.pop_back();\n }\n q.push_back(i);\n }\n nums[0] = nums[q.front()];\n \n for (int i = 1; i < n; ++i)\n {\n if (q.front() < i) {\n q.pop_front();\n }\n\n const int j = i + k - 1;\n const auto x = nums[j];\n while (!q.empty() && x >= nums[q.back()]) {\n q.pop_back();\n }\n q.push_back(j);\n\n nums[i] = nums[q.front()];\n }\n\n nums.resize(n);\n\n return std::move(nums);\n }\n};", "memory": "128738" }
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the max sliding window</em>.</p> <p>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= nums.length</code></li> </ul>
0
{ "code": "#pragma GCC optimize(\"Ofast\")\n#pragma GCC target(\"arch=native\")\nclass Solution {\n using out_t = std::vector<int>;\n\n static bool init() noexcept \n {\n ios::sync_with_stdio(0);\n cin.tie(0);\n return true;\n }\n static inline bool initialized = init();\n\npublic:\n out_t maxSlidingWindow(vector<int>& nums, const int k) const noexcept {\n const auto n = nums.size() + 1 - k;\n std::deque<unsigned> q;\n\n q.push_back(0);\n for (unsigned i = 1; i < k; ++i)\n {\n const auto x = nums[i];\n while (!q.empty() && x >= nums[q.back()]) {\n q.pop_back();\n }\n q.push_back(i);\n }\n nums[0] = nums[q.front()];\n \n for (unsigned i = 1; i < n; ++i)\n {\n if (q.front() < i) {\n q.pop_front();\n }\n\n const int j = i + k - 1;\n const auto x = nums[j];\n while (!q.empty() && x >= nums[q.back()]) {\n q.pop_back();\n }\n q.push_back(j);\n\n nums[i] = nums[q.front()];\n }\n\n nums.resize(n);\n\n return std::move(nums);\n }\n};", "memory": "129831" }
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the max sliding window</em>.</p> <p>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= nums.length</code></li> </ul>
0
{ "code": "#pragma GCC optimize(\"Ofast\")\n#pragma GCC target(\"arch=native\")\nclass Solution {\n using out_t = std::vector<int>;\n\n static bool init() noexcept \n {\n ios::sync_with_stdio(0);\n cin.tie(0);\n return true;\n }\n static inline bool initialized = init();\n\npublic:\n out_t maxSlidingWindow(vector<int>& nums, const int k) const noexcept {\n const auto n = nums.size() + 1 - k;\n std::deque<unsigned> q;\n\n q.push_back(0);\n for (unsigned i = 1; i < k; ++i)\n {\n const auto x = nums[i];\n while (!q.empty() && x >= nums[q.back()]) {\n q.pop_back();\n }\n q.push_back(i);\n }\n nums[0] = nums[q.front()];\n \n for (unsigned i = 1; i < n; ++i)\n {\n if (q.front() < i) {\n q.pop_front();\n }\n\n const unsigned j = i + k - 1;\n const auto x = nums[j];\n while (!q.empty() && x >= nums[q.back()]) {\n q.pop_back();\n }\n q.push_back(j);\n\n nums[i] = nums[q.front()];\n }\n\n nums.resize(n);\n\n return std::move(nums);\n }\n};", "memory": "129831" }
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the max sliding window</em>.</p> <p>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= nums.length</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n vector<int> maxSlidingWindow(vector<int>& nums, int k) {\n int l =0,r =0;\n int st[nums.size()];\n\n for (int i = 0;i<k;i++){\n while (r>l && nums[st[r-1]]< nums[i]){\n r--;\n }\n st[r++] = i;\n }\n\n for (int j =k ; j<nums.size();j++){\n cout<< j-st[l] << \" | \";\n if (j-st[l] > k) l++;\n nums[j-k]=nums[st[l]];\n while (r>l && nums[st[r-1]]<nums[j]) r--;\n\n st[r++] = j;\n }\n if (nums.size()-st[l] > k) l++;\n nums[nums.size()-k]=nums[st[l]];\n\n for (int i = 0; i<k-1;i++){\n nums.pop_back();\n }\n \n return nums;\n }\n};", "memory": "130923" }
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the max sliding window</em>.</p> <p>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= nums.length</code></li> </ul>
0
{ "code": "// class Solution {\n// public:\n// vector<int> maxSlidingWindow(vector<int>& nums, int k) {\n// priority_queue<pair<int, int>> pq;\n \n// vector<int> ans;\n// ans.reserve(nums.size() - k+1);\n// for(int i=0; i<nums.size(); ++i)\n// {\n// pq.push({nums[i], i});\n\n// if(i>=k-1)\n// {\n// while(!pq.empty() && pq.top().second <= (i-k))\n// {\n// pq.pop();\n// }\n// ans.emplace_back(pq.top().first);\n// }\n// }\n\n// return ans;\n// }\n// };\n\nclass Solution {\npublic:\n vector<int> maxSlidingWindow(vector<int>& nums, int k) {\n int q[nums.size()];\n int l = 0, r = 0;\n \n for (int idx = 0; idx < nums.size(); ++idx) {\n while (l < r && q[r - 1] < nums[idx]) {\n --r;\n }\n q[r++] = nums[idx];\n \n if (idx >= k - 1) {\n bool rm = q[l] == nums[idx - k + 1];\n nums[idx - k + 1] = q[l];\n if (rm) {\n ++l;\n }\n }\n }\n \n nums.resize(nums.size() - k + 1);\n\n return nums;\n }\n};", "memory": "130923" }
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the max sliding window</em>.</p> <p>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= nums.length</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n vector<int> maxSlidingWindow(vector<int>& nums, int k) {\n int n=nums.size();\n int next[n];\n stack<int> st;\n st.push(n-1);\n next[n-1]=n;\n for(int i=n-2;i>=0;i--){\n while(st.size()>0 && nums[i]>=nums[st.top()]){\n st.pop();\n }\n if(st.size()==0) next[i]=n;\n else next[i]=st.top();\n\n st.push(i);\n }\n vector<int> ans(n-k+1);\n for(int i=0;i<n-k+1;i++){\n \n // if(next[i]>=i+k) ans[i]=nums[i];\n \n int mx=nums[i];\n\n int j=i;\n while(j<i+k) \n { mx=nums[j];\n j=next[j];\n }\n ans[i]=mx;\n \n \n }\n return ans;\n\n }\n};", "memory": "132016" }
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the max sliding window</em>.</p> <p>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= nums.length</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n vector<int> maxSlidingWindow(vector<int>& nums, int k) {\n int n=nums.size();\n int next[n];\n stack<int> st;\n st.push(n-1);\n next[n-1]=n;\n for(int i=n-2;i>=0;i--){\n while(st.size()>0 && nums[i]>=nums[st.top()]){\n st.pop();\n }\n if(st.size()==0) next[i]=n;\n else next[i]=st.top();\n\n st.push(i);\n }\n vector<int> ans(n-k+1);\n int prev=0,j;\n for(int i=0;i<n-k+1;i++){\n \n // if(next[i]>=i+k) ans[i]=nums[i];\n \n int mx=nums[i];\n \n if(prev>i) j=prev;\n else j=i;\n while(j<i+k) \n { mx=nums[j];\n prev=j;\n j=next[j];\n }\n ans[i]=mx;\n \n \n \n }\n return ans;\n\n }\n};", "memory": "132016" }
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the max sliding window</em>.</p> <p>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= nums.length</code></li> </ul>
0
{ "code": "\n#define pint pair<int, int>\n#define vint vector<int>\n#define vll vector<long long>\n#define vpint vector<pair<int, int>>\n#define vstr vector<string>\n#define uset unordered_set\n#define umap unordered_map\n#define vbool vector<bool>\n#define vvint vector<vector<int>>\n#define vvvint vector<vector<vector<int>>>\n#define ll long long\n#define MOD 1000000007\n\nclass Solution {\npublic:\n vector<int> maxSlidingWindow(vector<int>& nums, int k) {\n int n = nums.size();\n\n deque<int> dq;\n\n for(int i = 0; i < k - 1; i++) {\n if(dq.empty())\n dq.push_back(nums[i]);\n \n else if(dq.back() < nums[i]) {\n while(!dq.empty() && (dq.back() < nums[i]))\n dq.pop_back();\n\n dq.push_back(nums[i]);\n }\n\n else\n dq.push_back(nums[i]);\n }\n\n vint ans(n - k + 1);\n\n for(int i = 0; i <= n - k; i++) {\n if(dq.empty())\n dq.push_back(nums[i + k - 1]);\n \n else if(dq.back() < nums[i + k - 1]) {\n while(!dq.empty() && (dq.back() < nums[i + k - 1]))\n dq.pop_back();\n\n dq.push_back(nums[i + k - 1]);\n }\n\n else\n dq.push_back(nums[i + k - 1]);\n\n ans[i] = dq.front();\n\n if(nums[i] == dq.front())\n dq.pop_front();\n }\n\n return ans;\n }\n};", "memory": "133108" }
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the max sliding window</em>.</p> <p>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= nums.length</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n vector<int> maxSlidingWindow(vector<int>& nums, int k) {\n int n = nums.size();\n vector<int> ans(n-k+1,0);\n stack<int> st; \n for(int i = 0 ;i<n;i++){\n while(!st.empty() && nums[st.top()]<nums[i]){\n int temp = st.top();\n st.pop();\n int prevgreater ; \n if(st.empty())prevgreater=-1;\n else prevgreater = st.top();\n for(int j = max(temp-k+1,prevgreater+1);j<=temp && j+k-1<=i-1;j++){\n ans[j]=nums[temp];\n }\n }\n st.push(i);\n }\n while(!st.empty()){\n int temp = st.top();\n st.pop();\n int prevgreater ; \n if(st.empty())prevgreater=-1;\n else prevgreater = st.top();\n for(int j = max(temp-k+1,prevgreater+1);j<=temp && j+k-1<=n-1;j++){\n ans[j]=nums[temp];\n }\n }\n return ans;\n }\n};", "memory": "133108" }
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the max sliding window</em>.</p> <p>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= nums.length</code></li> </ul>
0
{ "code": "#define pb push_back\nclass Solution {\npublic:\n vector<int> maxSlidingWindow(vector<int>& nums, int k) {\n vector<int> v(nums.size()-k+1);\n deque<int> a;\n a.push_back(10001);\n for(int i = 0 ; i < k ; i++)\n {\n while(*a.rbegin() < nums[i])\n {\n a.pop_back();\n }\n a.push_back(nums[i]);\n }\n v[0] = a[1];\n for(int i = 1 ; i <= nums.size() - k ; i++)\n {\n if(nums[i-1] == a[1])\n {\n a.pop_front();\n a.pop_front();\n a.push_front(10001);\n }\n while(*a.rbegin() < nums[i+k-1]) a.pop_back();\n a.push_back(nums[i+k-1]);\n v[i] = a[1];\n }\n return v;\n }\n};", "memory": "134201" }
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the max sliding window</em>.</p> <p>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= nums.length</code></li> </ul>
0
{ "code": "class Solution\n{\npublic:\n std::vector<int> maxSlidingWindow(const std::vector<int> &nums, int k)\n {\n std::vector<int> result;\n if(nums.size() > k + 1)\n {\n result.reserve(nums.size() - k - 1);\n }\n std::vector<int> window;\n window.reserve(k);\n for (int i = 0; i < nums.size(); i++)\n {\n if (i >= k && window[0] <= i - k)\n {\n window.erase(window.begin());\n }\n while (!window.empty() && nums[window.back()] <= nums[i])\n {\n window.pop_back();\n }\n window.push_back(i);\n if (i >= k - 1)\n {\n result.push_back(nums[window[0]]);\n }\n }\n return result;\n }\n};", "memory": "134201" }
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the max sliding window</em>.</p> <p>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= nums.length</code></li> </ul>
0
{ "code": "class Solution\n{\npublic:\n vector<int> maxSlidingWindow(vector<int>& nums, int k)\n {\n vector<int> result;\n result.reserve(nums.size() - k + 1);\n\n //queue<size_t> qmax;\n deque<size_t> qmax = init_start_window(nums.begin(), nums.begin() + k);\n result.push_back(nums[qmax.front()]);\n\n for (size_t i = 1; i < nums.size() - k + 1; ++i)\n {\n if (qmax.front() == i - 1)\n qmax.pop_front();\n \n while (!qmax.empty() && (nums[qmax.back()] <= nums[i + k - 1]))\n qmax.pop_back();\n\n qmax.push_back(i + k - 1);\n result.push_back(nums[qmax.front()]);\n }\n\n return result;\n }\n\nprivate:\n deque<size_t> init_start_window(std::vector<int>::iterator begin, std::vector<int>::iterator end)\n {\n deque<size_t> q = {0};\n\n for (auto it = begin + 1; it != end; ++it)\n {\n while (!q.empty() && *it >= *(begin + q.back()))\n q.pop_back();\n\n q.push_back(it - begin);\n }\n\n return q;\n }\n};", "memory": "135293" }
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the max sliding window</em>.</p> <p>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= nums.length</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n vector<int> maxSlidingWindow(vector<int>& nums, int k) {\n if(k == 1) return nums;\n int m = 0;\n int l = 0, r = k - 1;\n for(int i = 1; i <= r; i++){\n if(nums[i] >= nums[m]) m = i;\n }\n vector<int> ans;\n while(r < nums.size()){\n ans.push_back(nums[m]);\n r++;\n if(r >= nums.size()) break;\n if(nums[r] >= nums[m]) m = r;\n if(m == l){\n m = l + 1;\n for(int i = l + 2; i <= r; i++){\n if(nums[i] >= nums[m]) m = i;\n }\n }\n l++;\n }\n return ans;\n\n }\n};", "memory": "135293" }
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the max sliding window</em>.</p> <p>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= nums.length</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n vector<int> maxSlidingWindow(vector<int>& nums, int k) {\n int n = nums.size();\n vector <int> res;\n\n if(n == 0)\n return res;\n if(k == 1)\n return nums;\n\n int left[n]; \n int right[n]; \n\n left[0] = nums[0];\n right[n - 1] = nums[n - 1];\n\n //max from left in the windows of k\n for(int i = 0;i < n;i++)\n {\n if(i % k == 0)\n left[i] = nums[i];\n else\n left[i] = std::max(nums[i],left[i - 1]);\n } \n\n // max from right in the windows of k\n for(int i = n - 2;i >= 0;i--)\n {\n if(i % k == 0)\n right[i] = nums[i];\n else\n right[i] = std::max(nums[i],right[i + 1]);\n } \n\n //overall maximum\n for(int i = 0; i <= n - k;i++)\n {\n res.push_back(std::max(right[i],left[i + k - 1]));\n }\n return res;\n }\n};", "memory": "136386" }
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the max sliding window</em>.</p> <p>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= nums.length</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n vector<int> maxSlidingWindow(vector<int>& nums, int k) {\n int n = nums.size();\n vector<int> ans(n-k+1);\n \n stack<int> st;\n vector<int> nextGreaterIndex(n);\n int j = n-1;\n while(j>=0)\n {\n while(st.size()!=0 && nums[st.top()]<nums[j])st.pop();\n (st.size() == 0)? nextGreaterIndex[j] = n : nextGreaterIndex[j] = st.top();\n st.push(j);\n j--;\n }\n\n \n int i = 0;\n j = k-1;\n int x = i;\n while(j<n)\n {\n if(x<i) x = i;\n int p1 = nextGreaterIndex[i], p2 = j;\n while(p1<=p2)\n {\n x = p1;\n p1 = nextGreaterIndex[p1];\n }\n ans[i] = nums[x];\n j++;\n i++;\n }\n\n\n return ans;\n }\n};", "memory": "136386" }
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the max sliding window</em>.</p> <p>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= nums.length</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n vector<int> maxSlidingWindow(vector<int>& nums, int k) {\n //can we do this in O(n) time using something as dequeu,let's try\n //priority_queue ke similar hai,index hi store karwayenge taaki bahar phek de ,but we will also utilize 1 more key point here\n //if a greater element comes and in dequeu smaller elements that exist before me are there then boy fck off\n int n=nums.size();\n vector<int>result;\n deque<int>dq;\n for(int i=0;i<n;i++){\n while(!dq.empty() and dq.front()<=i-k)dq.pop_front();\n while(!dq.empty() and nums[dq.back()]<=nums[i])dq.pop_back();\n dq.push_back(i);\n if(i>=k-1)result.push_back(nums[dq.front()]);\n }\n return result;\n }\n};", "memory": "137478" }
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the max sliding window</em>.</p> <p>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= nums.length</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n vector<int> maxSlidingWindow(vector<int>& nums, int k) {\n int n = nums.size();\n\tdeque<int> dq;\n\tvector<int>ret;\n\tfor(int i=0;i<n;i++){\n\t\twhile(!dq.empty() && dq.front()<=i-k) dq.pop_front();\n\t\twhile(!dq.empty() && nums[dq.back()]<=nums[i]) dq.pop_back();\n\t\tdq.push_back(i);\n\t\tif(i>=k-1) ret.push_back(nums[dq.front()]);\n}\nreturn ret;\n }\n};\n\n\n\n", "memory": "137478" }
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the max sliding window</em>.</p> <p>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= nums.length</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> maxSlidingWindow(vector<int>& nums, int k) {\n deque<int> dq;\n int i, j, n = nums.size();\n vector<int> ans;\n i = j = 0;\n // storing the first window\n while(j < k){\n if(dq.empty()){\n dq.push_back(nums[j]);\n }\n \n else{\n while(!dq.empty() &&dq.back() < nums[j]){\n dq.pop_back();\n }\n dq.push_back(nums[j]);\n }\n j++;\n }\n ans.push_back(dq.front());\n \n while(j < n){\n if(dq.front() == nums[i]) dq.pop_front();\n i++;\n if(nums[j] > dq.front()){\n while(!dq.empty()){\n dq.pop_front();\n }\n dq.push_back(nums[j]);\n }\n else{\n while(dq.back() < nums[j]){\n dq.pop_back();\n }\n dq.push_back(nums[j]);\n }\n j++;\n ans.push_back(dq.front());\n \n }\n \n return ans;\n \n \n \n }\n};", "memory": "138571" }
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the max sliding window</em>.</p> <p>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= nums.length</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> maxSlidingWindow(vector<int>& nums, int k) {\n deque<int>maxi;\n deque<int>mini;\n vector<int>ans1;\n for(int i=0;i<k;i++){\n while(!maxi.empty() && nums[maxi.back()]<=nums[i]){\n maxi.pop_back();\n }\n while(!mini.empty() && nums[mini.back()]>=nums[i]){\n mini.pop_back();\n }\n maxi.push_back(i);\n mini.push_back(i);\n }\n \n int ans=0;\n for(int i=k;i<nums.size();i++){\n ans+=nums[maxi.front()]+nums[mini.front()];\n ans1.push_back(nums[maxi.front()]);\n while(!maxi.empty() && i-maxi.front()>=k){\n maxi.pop_front();\n }\n while(!mini.empty() && i-mini.front()>=k){\n mini.pop_front();\n }\n\n while(!maxi.empty() && nums[maxi.back()]<=nums[i]){\n maxi.pop_back();\n }\n while(!mini.empty() && nums[mini.back()]>=nums[i]){\n mini.pop_back();\n }\n maxi.push_back(i);\n mini.push_back(i);\n }\n ans1.push_back(nums[maxi.front()]);\n return ans1;\n }\n};", "memory": "138571" }
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the max sliding window</em>.</p> <p>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= nums.length</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> maxSlidingWindow(vector<int> &nums, int k) {\n if (k == 1 || nums.size() <= 1) return nums;\n\n vector<int> ans;\n deque<int> D;\n\n int start = 0;\n for (int i = 0; i < nums.size(); i++) {\n while (!D.empty() && D.front() < start) D.pop_front();\n\n if (D.empty() || (nums[D.front()] > nums[i])) {\n while (!D.empty() && nums[D.back()] < nums[i]) D.pop_back();\n D.push_back(i);\n } else {\n while (!D.empty() && nums[D.front()] <= nums[i]) D.pop_front();\n D.push_front(i);\n }\n\n if (i + 1 >= k) {\n ans.push_back(nums[D.front()]);\n ++start;\n }\n\n// for (auto x: D) cout << x << \" \"; cout << endl;\n }\n\n return ans;\n }\n};\n", "memory": "139663" }
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the max sliding window</em>.</p> <p>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= nums.length</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> maxSlidingWindow(vector<int>& nums, int k) {\n vector<int> sol;\n deque<pair<int, int>> max;\n for(int i = 0; i < k; ++i){\n removeLowerVals(max, nums[i], i);\n }\n sol.push_back(max.front().first);\n int l = 0;\n\n for(int i = k; i < nums.size(); ++i) {\n ++l;\n if(max.front().second < l) max.pop_front();\n removeLowerVals(max, nums[i], i);\n sol.push_back(max.front().first);\n }\n return sol;\n }\n\n void removeLowerVals(deque<pair<int, int>>& max, int n, int i) {\n while(!max.empty() && max.back().first < n){\n max.pop_back();\n }\n max.push_back(make_pair(n, i));\n }\n};", "memory": "139663" }
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the max sliding window</em>.</p> <p>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= nums.length</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n // Solution 2-Monotonous queue: O(n) and O(n)\n vector<int> maxSlidingWindow(vector<int>& nums, int k) {\n vector<int> ret;\n for(int i = 0; i < nums.size(); i++){\n pushIntoMonotoneQ(nums[i]);\n if(i >= k-1) {\n ret.push_back(monoQ.front().first);\n popFromMonotoneQ();\n }\n }\n return ret;\n }\nprivate:\n deque<pair<int, int>> monoQ;// pair.first = actual value\n // pair.second = count of the number of elements popped before placing a number into the back of the deque.\n void pushIntoMonotoneQ(int val){\n int count = 0;\n while(!monoQ.empty() and val > monoQ.back().first){\n count += monoQ.back().second + 1;\n monoQ.pop_back();\n }\n monoQ.emplace_back(val, count);\n \n }\n void popFromMonotoneQ(){\n if(monoQ.front().second > 0){\n monoQ.front().second--;\n return;\n }\n monoQ.pop_front();\n }\n};\n\n/*\nclass Solution {\npublic:\n // Solution 1- Brute force: O(nk) and O(1) | TLE\n vector<int> maxSlidingWindow(vector<int>& nums, int k) {\n vector<int> ret;\n for(int i = 0; i < nums.size() - k + 1; i++){\n int maxx = INT_MIN;\n for(int j = i; j < i + k; j++){\n maxx = max(maxx, nums[j]);\n }\n ret.push_back(maxx);\n }\n \n return ret;\n }\n};\n*/", "memory": "140756" }
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the max sliding window</em>.</p> <p>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= nums.length</code></li> </ul>
2
{ "code": "class Solution {\nprivate:\n deque<int> dic;\n vector<int> output;\npublic:\n vector<int> maxSlidingWindow(vector<int>& nums, int k) {\n for(int i = 0; i < k; ++i)\n {\n while(!dic.empty() && nums[i] > nums[dic.back()])\n {\n dic.pop_back();\n }\n\n dic.push_back(i);\n }\n\n for(int i = k; i < nums.size(); ++i)\n {\n output.push_back(nums[dic.front()]);\n \n if(dic.front() == i - k) dic.pop_front();\n while(!dic.empty() && nums[i] > nums[dic.back()])\n {\n dic.pop_back();\n }\n\n dic.push_back(i);\n }\n\n output.push_back(nums[dic.front()]);\n\n return output;\n }\n};", "memory": "140756" }
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the max sliding window</em>.</p> <p>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= nums.length</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> maxSlidingWindow(vector<int> &nums, int k){\n vector<int> pge(nums.size(), -1), nge(nums.size(), nums.size()); //store prev >= & next > elements\n stack<int> st; //monotonically dec. stack\n for (int i = 0; i < nums.size(); ++i){\n while (!st.empty() && nums[st.top()] < nums[i]){\n nge[st.top()] = i;\n st.pop();\n }\n\n if (!st.empty()){ pge[i] = st.top(); }\n\n st.push(i);\n }\n\n vector<int> ans(nums.size() - k + 1); //stores ans for windows\n\n for (int i = 0; i < nums.size(); ++i){\n //finding all windows where nums[i] is max \n int currWindow = max(pge[i] + 1, i - k + 1); //stores curr window\n while (currWindow + k - 1 < min(nge[i], i + k)){ \n ans[currWindow] = nums[i]; \n ++currWindow;\n }\n }\n\n return ans;\n }\n};", "memory": "141848" }
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the max sliding window</em>.</p> <p>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= nums.length</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> maxSlidingWindow(vector<int>& nums, int k) {\n vector<int> res;\n if(nums.empty() || k == 0) {\n return res;\n }\n \n deque<int> work;\n \n auto push_num = [&work, nums](int i) {\n while(work.size() && (nums[i] > nums[work.back()])) {\n //cout << \"pop: \" << work.back() << endl;\n work.pop_back();\n }\n work.push_back( i );\n //cout << \"push: \" << work.back() << endl;\n };\n \n //build the first window\n int r = 0;\n for(; (r < nums.size()) && (r < k); ++r) {\n push_num( r );\n }\n // r may be out of range if k == size\n // r is out of window, we have not pushed\n // nums[r] into work\n \n // record the first windows\n assert( work.size() );\n res.push_back( nums[work.front()] );\n \n int l = 0;\n // move window and record max\n while(r < nums.size()) {\n // start with r pointing at next element out of the window\n ++l; // [l:r] is now the new legal window\n //cout << \"l: \" << l << \" r: \" << r << endl;\n // add new value\n push_num( r );\n // find the (potentially new) max\n // discard all that is left of l\n // note that may be l == r\n while(work.size() && (work.front() < l)) {\n //cout << \"pop \" << work.front() << \"<\" << l << endl;\n work.pop_front();\n }\n //cout << \"work.size: \" << work.size() << endl;\n // new max is leftmost in work\n assert( work.size() );\n res.push_back( nums[work.front()] );\n ++r;\n }\n // r is out of range of nums, no more windows\n \n return res;\n }\n};", "memory": "141848" }
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the max sliding window</em>.</p> <p>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= nums.length</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> maxSlidingWindow(vector<int>& nums, int k) {\n\n for (int idx = 0; idx < nums.size(); ++idx) {\n update_window(nums[idx], idx, window_left_idx);\n \n if (idx - window_left_idx + 1 == k) {\n auto max = window.front();\n output.push_back(max.first);\n window_left_idx += 1;\n }\n }\n \n return output;\n }\n\n auto update_window(int n, int idx, int window_left_idx) -> void {\n if (window.empty()) {\n // do nothing\n } else {\n if (window_left_idx > window.front().second) window.pop_front();\n\n while (!window.empty() && window.back().first < n) {\n window.pop_back();\n }\n }\n window.push_back({n, idx});\n }\nprivate:\n vector<int> output = {};\n deque<pair<int, int>> window = {}; // first = value, second = idx\n int window_left_idx = 0;\n};", "memory": "142941" }
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the max sliding window</em>.</p> <p>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= nums.length</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n struct InWindow {\n int value;\n int vanish_index;\n\n bool operator<(const InWindow& b) const {\n return value < b.value;\n }\n };\n\n vector<int> maxSlidingWindow(vector<int>& nums, int k) {\n std::vector<InWindow> in_window_storage(nums.size());\n size_t in_window_begin = 0;\n size_t in_window_end = 0;\n\n int i = 0;\n for(; i < k; i++) {\n while(in_window_begin != in_window_end &&\n in_window_storage[in_window_end - 1].value <= nums[i]\n ) {\n in_window_end--;\n }\n\n in_window_storage[in_window_end++] = InWindow{nums[i], i + k};\n }\n\n std::vector<int> result;\n result.reserve(nums.size() - k);\n \n result.push_back(in_window_storage[in_window_begin].value);\n \n for(; i < nums.size(); i++) {\n while(in_window_begin != in_window_end &&\n in_window_storage[in_window_begin].vanish_index <= i\n ) {\n in_window_begin++;\n }\n\n while(in_window_begin != in_window_end &&\n in_window_storage[in_window_end - 1].value <= nums[i]\n ) {\n in_window_end--;\n }\n\n in_window_storage[in_window_end++] = InWindow{nums[i], i + k};\n\n result.push_back(in_window_storage[in_window_begin].value);\n }\n\n return result;\n }\n};", "memory": "142941" }
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the max sliding window</em>.</p> <p>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= nums.length</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> maxSlidingWindow(vector<int>& nums, int k) {\n deque<int> deq;\n vector<int> maxofWindow;\n\n if (nums.size() == 0) {\n return {};\n }\n for (int end = 0; end < nums.size(); end++) {\n if (!deq.empty() && deq.front() < end - k + 1) {\n deq.pop_front();\n }\n\n while (!deq.empty() && nums[deq.back()] < nums[end]) {\n deq.pop_back();\n }\n deq.push_back(end);\n\n maxofWindow.push_back(nums[deq.front()]);\n }\n vector<int> answer(maxofWindow.begin() + (k - 1), maxofWindow.end());\n return answer;\n }\n};", "memory": "144033" }
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the max sliding window</em>.</p> <p>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= nums.length</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n // Using PQ TC->(NlogK)\n vector<int> maxSlidingWindow(vector<int>& nums, int k) {\n int n=nums.size();\n vector<int>ans;\n priority_queue<pair<int,int>>pq;\n for(int i=0;i<n;i++){\n while(!pq.empty() && pq.top().first<=nums[i]){\n pq.pop();\n }\n pq.push({nums[i],i});\n if(i+1-k>=0){\n while(pq.top().second<i+1-k){\n pq.pop();\n }\n ans.push_back(pq.top().first);\n }\n }\n return ans;\n }\n\n\n // Using DeQue TC->O(n)\n // vector<int> maxSlidingWindow(vector<int>& nums, int k) {\n // int n=nums.size();\n // vector<int>ans;\n // deque<int>dq;\n // for(int i=0;i<n;i++){\n // while(!dq.empty() && nums[i]>nums[dq.back()]){\n // dq.pop_back();\n // }\n // dq.push_back(i);\n // if(i+1-k>=0){\n // while(dq.front()<i+1-k){\n // dq.pop_front();\n // }\n // ans.push_back(nums[dq.front()]);\n // }\n // }\n // return ans;\n // }\n};", "memory": "144033" }
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the max sliding window</em>.</p> <p>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= nums.length</code></li> </ul>
2
{ "code": "#include <queue>\n#include <unordered_map>\n#include <climits>\n#include <functional>\n\nusing namespace std;\n\nclass Solution {\npublic:\n vector<int> maxSlidingWindow(vector<int>& nums, int k) {\n priority_queue<int, vector<int>, less<int>> pq;\n // unordered_map<int, int> rm;\n int rm[2*10000+1] = {0};\n int off = 10000;\n vector<int> maxes;\n \n int maxVal = INT_MIN;\n int l = 0, r = k-1;\n for (int i = 0; i < k; i++) {\n if (nums[i] > maxVal) {\n maxVal = nums[i];\n }\n pq.push(nums[i]);\n }\n maxes.push_back(maxVal);\n while (r < nums.size() - 1) {\n rm[nums[l] + off] += 1;\n l++;\n while (rm[pq.top() + off] > 0) {\n rm[pq.top() + off] -= 1;\n pq.pop();\n }\n pq.push(nums[++r]);\n maxes.push_back(pq.top());\n // while (pqc.size() > 0) {\n // cout << pqc.top() << \" \";\n // pqc.pop();\n // }\n // cout << endl;\n // for (auto i : rm) {\n // cout << \"(\" << i.first << \" \" << i.second << \"), \";\n // }\n // cout << endl;\n }\n return maxes;\n }\n};", "memory": "145126" }
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the max sliding window</em>.</p> <p>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= nums.length</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> maxSlidingWindow(vector<int>& nums, int k) {\n priority_queue<int, vector<int>, function<bool(int, int)>> pq([&nums](int l, int r){return nums[l]<nums[r];});\n vector<bool> visible(nums.size(), false);\n vector<int> ans;\n for (int i = 0; i < nums.size(); ++i) {\n visible[i] = true;\n pq.push(i);\n if (i+1 < k) continue;\n while (!visible[pq.top()]) pq.pop();\n ans.push_back(nums[pq.top()]);\n visible[i+1-k] = false;\n }\n return ans;\n }\n};", "memory": "145126" }
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the max sliding window</em>.</p> <p>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= nums.length</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n\n // 1 3 -1 -3 5 3 6 7\n // k = 3\n // 3 3 -1\n // p1-> 0 (ith) p2 -> 2(ith + k -1)\n // p1-> 1 index p2 -> 3 index\n // 3 -3 = 3\n // p1-> 2 p2-> 4\n // -1 5 = > 5\n // ith +k + k- 1\n // for finding k max values i am k opeartions\n\n vector<int> maxSlidingWindow(vector<int>& nums, int k) {\n \n\n int n = nums.size();\n\n vector<int>ans;\n\n for(int i=0;i<n;i+=k){\n\n vector<int>suff;\n\n for(int j=i;j<=min(n-1,i+k-1);j++){\n suff.push_back(nums[j]);\n }\n\n for(int j=suff.size()-2;j>=0;j--){\n suff[j] =max(suff[j+1],suff[j]);\n }\n\n int p1 = i;\n int p2 = i+k-1;\n\n int p2_max = nums[p2];\n\n while(p1<i+k && p2<n){\n int val1 = suff[p1-i];\n int val2 = max(nums[p2],p2_max);\n\n p2_max = val2;\n\n ans.push_back(max(val1,val2));\n\n p1++;\n p2++;\n }\n if (p2==n)break;\n }\n\n return ans;\n }\n};", "memory": "146218" }
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the max sliding window</em>.</p> <p>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= nums.length</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> maxSlidingWindow(vector<int>& nums, int k) {\n priority_queue< pair < int , int> > q;\n vector<int> ans;\n int n = nums.size();\n for(int i=0 ; i<k ; i++){\n if(!q.empty() and q.top().first > nums[i]){\n q.push({nums[i] , i});\n }\n else{\n while(!q.empty() and q.top().first <= nums[i]){\n q.pop();\n }\n q.push({nums[i] , i});\n }\n }\n ans.push_back( q.top().first );\n\n \n int x = 1;\n for(int i=k ; i<n ; i++){\n while(!q.empty() and q.top().second < x){\n q.pop();\n }\n x++;\n if(!q.empty() and q.top().first > nums[i]){\n q.push({nums[i] , i});\n }\n else{\n while(!q.empty() and q.top().first < nums[i]){\n q.pop();\n }\n q.push({nums[i] , i});\n }\n ans.push_back( q.top().first );\n }\n\n return ans;\n }\n};", "memory": "146218" }
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the max sliding window</em>.</p> <p>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= nums.length</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> maxSlidingWindow(vector<int>& nums, int k) {\n deque<pair<int, int>> queue;\n vector<int> ans;\n int maxi = -1999999;\n int pos = -1;\n for(int i=0;i<k;i++) {\n if(nums[i]>=maxi){\n maxi = nums[i];\n pos = i;\n }\n }\n queue.push_front({maxi, pos});\n ans.push_back(maxi);\n for(int i=0;i<nums.size();i++) {\n pair<int, int> next = {nums[i], i};\n while(!queue.empty()) {\n pair<int, int> latest = queue.back();\n if(latest.first > next.first) {\n break;\n }\n queue.pop_back();\n }\n queue.push_back(next);\n while(!queue.empty() && i >=k) {\n pair<int, int> earliest = queue.front();\n if(earliest.second > (i-k)){\n ans.push_back(earliest.first);\n break;\n }\n queue.pop_front();\n }\n }\n return ans;\n }\n};", "memory": "147311" }
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the max sliding window</em>.</p> <p>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= nums.length</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> maxSlidingWindow(vector<int>& nums, int k) {\n std::ios_base::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::cout.tie(nullptr);\n\n if(k == 1)\n return nums;\n\n vector<int> res;\n priority_queue<pair<int, int>> pq;\n int left = 1, right = k;\n\n pq.push({nums[0], 0});\n for(int i = 1; i < k; i++){\n while(!pq.empty() && pq.top().first < nums[i])\n pq.pop();\n pq.push({nums[i], i});\n }\n res.push_back(pq.top().first);\n\n while(right < nums.size() && left < right) {\n while(!pq.empty() && pq.top().second < left)\n pq.pop();\n pq.push({nums[right],right});\n res.push_back(pq.top().first);\n left++;\n right++;\n }\n\n return res;\n }\n};", "memory": "151681" }
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the max sliding window</em>.</p> <p>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= nums.length</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> maxSlidingWindow(vector<int>& nums, int k) {\n std::ios_base::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::cout.tie(nullptr);\n\n if(k == 1)\n return nums;\n\n vector<int> res;\n priority_queue<pair<int, int>> pq;\n int left = 1, right = k;\n\n pq.push({nums[0], 0});\n for(int i = 1; i < k; i++){\n while(!pq.empty() && pq.top().first < nums[i])\n pq.pop();\n pq.push({nums[i], i});\n }\n res.push_back(pq.top().first);\n\n while(right < nums.size() && left < right) {\n while(!pq.empty() && pq.top().second < left)\n pq.pop();\n pq.push({nums[right],right});\n res.push_back(pq.top().first);\n left++;\n right++;\n }\n\n return res;\n }\n};", "memory": "151681" }
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the max sliding window</em>.</p> <p>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= 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<pair<int,int>>p;\n \n for(int i=0;i<nums.size();i++){\n p.push({nums[i],i});\n if(i>=k-1){\n int d=i-k;\n while(p.top().second<=d) p.pop();\n\n ans.push_back(p.top().first);\n\n\n\n }\n }\n return ans;\n\n }\n};", "memory": "152773" }
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the max sliding window</em>.</p> <p>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= 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<pair<int,int>>pq;\n for(int i=0; i<k;i++)pq.push({nums[i], i});\n ans.push_back(pq.top().first);\n for(int i=k; i<nums.size();i++){\n pq.push({nums[i],i});\n while(pq.top().second<=i-k)pq.pop();\n ans.push_back(pq.top().first);\n }\n return ans;\n }\n};", "memory": "152773" }
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the max sliding window</em>.</p> <p>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= nums.length</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> maxSlidingWindow(vector<int>& nums, int k) {\n \n int pos = 0;\n int maxi = 0;\n priority_queue<pair<int,int>>q;\n for(int i=0;i<k;i++){\n if(nums[i]>maxi){\n maxi = nums[i];\n pos = i;\n }\n q.push({nums[i],i});\n }\n int i = 1;\n int j = k;\n vector<int>ans;\n ans.push_back(maxi);\n while(j<nums.size()){\n auto p = q.top();\n // cout<<p.first<<\" \"<<p.second;\n if(i<=p.second && p.first>=nums[j]) ans.push_back(p.first);\n else if(i<=p.second && p.first<nums[j]){\n ans.push_back(nums[j]);\n q.pop();\n // q.push({nums[j],j});\n }\n else if(i>p.second){\n q.push({nums[j],j});\n \n while(1){\n auto x = q.top();\n if(x.second>=i) break;\n q.pop();\n }\n\n ans.push_back(q.top().first);\n }\n if(i==p.second) q.pop();\n q.push({nums[j],j});\n j++;\n i++;\n }\n return ans;\n }\n};", "memory": "153866" }
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the max sliding window</em>.</p> <p>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= nums.length</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> maxSlidingWindow(vector<int>& nums, int k) {\n deque<pair<int, int>> dq; // decreasing best \n\t\tvector<int> res;\n\t\tint n = nums.size();\n\t\tfor (int i = 0; i < n; i++) {\n\t\t\twhile (!dq.empty() && dq.back().second <= i - k) {\n\t\t\t\tdq.pop_back();\n\t\t\t}\n\t\t\twhile (!dq.empty() && dq.front().first <= nums[i]) {\n\t\t\t\tdq.pop_front();\n\t\t\t}\n\t\t\tdq.push_front({nums[i], i});\n\t\t\tif (i + 1 >= k) {\n\t\t\t\tres.push_back(dq.back().first);\n\t\t\t}\n\t\t}\n\t\treturn res;\n }\n};\n\n", "memory": "153866" }
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the max sliding window</em>.</p> <p>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= nums.length</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int t[400005];\n void build(vector <int>& t, vector <int>& nums, int pos, int l, int r){\n if(l == r){\n t[pos] = nums[l - 1];\n return;\n }\n int mid = (l + r) >> 1;\n build(t, nums, pos << 1, l, mid);\n build(t, nums, pos << 1 | 1, mid + 1, r);\n t[pos] = max(t[pos << 1], t[pos << 1 | 1]);\n }\n\n int query(vector <int>& t, int pos, int l, int r, int u, int v){\n if(l > v || r < u)\n return -1e9;\n if (u <= l && v >= r)\n return t[pos];\n int mid = (l + r) >> 1;\n int leaf1 = query(t, pos << 1, l, mid, u, v);\n int leaf2 = query(t, pos << 1 | 1, mid + 1, r, u , v);\n return max(leaf1, leaf2);\n }\n \n vector<int> maxSlidingWindow(vector<int>& nums, int k) {\n int n = nums.size();\n vector <int> t(4 * n, 0);\n build(t, nums, 1, 1, n);\n vector <int> res;\n for (int i = 1; i + k - 1 <= n; ++i){\n int cur = query(t, 1, 1, n, i, i + k - 1);\n res.push_back(cur);\n }\n return res;\n }\n};", "memory": "154958" }
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the max sliding window</em>.</p> <p>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= nums.length</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> maxSlidingWindow(vector<int>& nums, int k) {\n vector<int> ans;\n stack<int> s1, s2, mx1, mx2;\n for (int i = 0; i < k; i++){\n s2.push(nums[i]);\n if (mx2.empty()){\n mx2.push(nums[i]);\n }\n else {\n mx2.push(max(mx2.top(), nums[i]));\n }\n }\n ans.push_back(mx2.top());\n for (int i = k, n = nums.size(); i < n; i++){\n if (s1.empty()){\n for (int i = 0; i < k; i++){\n int ph = s2.top();\n s2.pop();\n mx2.pop();\n s1.push(ph);\n mx1.push((mx1.empty() ? ph: max(ph, mx1.top())));\n }\n }\n s1.pop();\n mx1.pop();\n s2.push(nums[i]);\n mx2.push((mx2.empty() ? nums[i]: max(nums[i], mx2.top())));\n int mx;\n if (mx2.empty()) mx = mx1.top();\n else if (mx1.empty()) mx = mx2.top();\n else mx = max(mx1.top(), mx2.top());\n ans.push_back(mx);\n }\n return ans;\n }\n};", "memory": "154958" }
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the max sliding window</em>.</p> <p>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= nums.length</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n #define pp pair<int,int> \n // Push into back stack while maintaining the max in the back stack\n void enq(stack<pp> &front, stack<pp> &back, int value, int &currmax) {\n if(back.empty()){\n back.push({value, value});\n currmax = value;\n return;\n }\n\n currmax = back.top().second;\n \n back.push({value, max(currmax, value)});\n currmax = max(currmax, value);\n }\n\n // Pop from front stack. If front is empty, move elements from back to front\n void deq(stack<pp> &front, stack<pp> &back, int &currmax) {\n if (front.empty()) {\n int maxVal = INT_MIN;\n while (!back.empty()) {\n auto ele = back.top();\n back.pop();\n // Recalculate max while pushing into the front stack\n front.push({ele.first, max(maxVal, ele.first)});\n maxVal = max(maxVal, ele.first);\n }\n }\n front.pop(); // Remove the front element\n currmax = getmax(front, back);\n }\n\n // Get the maximum by comparing the top max values from both stacks\n int getmax(stack<pp> &a, stack<pp> &b) {\n int maxVal = INT_MIN;\n if (!a.empty()) maxVal = max(maxVal, a.top().second);\n if (!b.empty()) maxVal = max(maxVal, b.top().second);\n return maxVal;\n }\n\n vector<int> maxSlidingWindow(vector<int>& nums, int k) {\n if (k == 1) return nums; // If window size is 1, every element is its own max\n\n int n = nums.size();\n stack<pp> back, front;\n int currmax = INT_MIN;\n\n // Initialize the first window\n for (int i = 0; i < k; i++) {\n enq(front, back, nums[i], currmax);\n }\n\n vector<int> ans;\n ans.push_back(getmax(front, back)); // First window's max\n int backmax;\n\n\n // Slide the window through the rest of the array\n for (int i = k; i < n; i++) {\n deq(front, back, currmax); // Remove the element that's sliding out of the window\n enq(front, back, nums[i], currmax); // Add the new element\n ans.push_back(getmax(front, back)); // Get the max of the current window\n }\n\n return ans;\n }\n};\n", "memory": "156051" }
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the max sliding window</em>.</p> <p>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= nums.length</code></li> </ul>
3
{ "code": "class MaxQueue {\npublic:\n MaxQueue() {\n\n }\n \n int max_v() {\n if (!push_stack.empty() && !pop_stack.empty()) {\n return max(push_stack.top().second, pop_stack.top().second);\n } else if (!push_stack.empty()) {\n return push_stack.top().second;\n } else if (!pop_stack.empty()) {\n return pop_stack.top().second;\n }\n\n return 0;\n }\n\n void push(int val) {\n int max_value = val;\n if (!push_stack.empty()) max_value = max(max_value, push_stack.top().second);\n push_stack.push({val, max_value});\n }\n\n void pop() {\n int max_value = std::numeric_limits<int>::min();\n if (pop_stack.empty()) {\n while(!push_stack.empty()) {\n pair<int, int> top = push_stack.top();\n max_value = max(top.first, max_value);\n pop_stack.push({top.first, max_value});\n push_stack.pop();\n }\n }\n pop_stack.pop();\n }\nprivate:\n stack<pair<int, int>> push_stack;\n stack<pair<int, int>> pop_stack;\n};\n\nclass Solution {\npublic:\n vector<int> maxSlidingWindow(vector<int>& nums, int k) {\n vector<int> ans;\n MaxQueue q;\n for (int i = 0; i < nums.size(); ++i) {\n q.push(nums[i]);\n if (i>=k) q.pop();\n if (i >= k-1) ans.push_back(q.max_v());\n }\n return ans;\n \n }\n};", "memory": "156051" }
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the max sliding window</em>.</p> <p>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= nums.length</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> maxSlidingWindow(vector<int>& nums, int k) {\n if(k == 1) return nums;\n deque<int> dq;\n for(int i = k -1; i >=0 ; i--){\n if(dq.empty() || nums[dq.front()] < nums[i] ) {\n dq.push_front(i);\n }\n }\n\n vector<int> ans; \n ans.push_back(nums[dq.front()]);\n for(int i =k; i < nums.size(); i ++){\n\n if(dq.front() < i - k +1 ) dq.pop_front();\n while(!dq.empty() && nums[dq.back()] <= nums[i] ) dq.pop_back();\n dq.push_back(i);\n ans.push_back(nums[dq.front()]);\n }\n return ans;\n }\n};", "memory": "157143" }
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the max sliding window</em>.</p> <p>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= nums.length</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> maxSlidingWindow(vector<int>& nums, int k) {\n std::priority_queue<int> maxHeap;\n int l = 0;\n vector<int> res;\n unordered_map<int,int> lazyDelete;\n for (int r = 0; r < nums.size(); r++) {\n if (r - l + 1 > k) {\n lazyDelete[nums[l]]++;\n l++;\n }\n maxHeap.push(nums[r]);\n if (r - l + 1 == k) {\n int maxElem = maxHeap.top();\n while (lazyDelete[maxElem] > 0) {\n maxHeap.pop();\n lazyDelete[maxElem] -= 1;\n maxElem = maxHeap.top();\n }\n res.push_back(maxElem);\n }\n }\n return res;\n }\n};", "memory": "157143" }
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the max sliding window</em>.</p> <p>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= nums.length</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> maxSlidingWindow(vector<int>& nums, int k) {\n deque<int> dq;\n vector<int> ans;\n\n dq.push_front(nums[k - 1]);\n \n for (int i = k - 2; i >= 0; i--) {\n if (dq.size() > 0 and dq.front() <= nums[i]) dq.push_front(nums[i]);\n }\n\n ans.push_back(dq.front());\n\n for (int i = k; i < nums.size(); i++) {\n if (nums[i - k] == dq.front()) dq.pop_front();\n while (dq.size() > 0 and dq.back() < nums[i]) {\n dq.pop_back();\n }\n dq.push_back(nums[i]);\n ans.push_back(dq.front());\n }\n return ans;\n \n\n\n \n }\n};", "memory": "158236" }
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the max sliding window</em>.</p> <p>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= nums.length</code></li> </ul>
3
{ "code": "class Compare {\npublic:\n bool operator()(pair<int,int>a,pair<int,int>b)\n {\n if ( a.second< b.second) \n {\n return true;\n }\n return false;\n }\n};\n\nclass Solution {\npublic:\n \n \n vector<int> maxSlidingWindow(vector<int>& nums, int k) {\n priority_queue<pair<int,int>, vector<pair<int,int>>, Compare> q;\n vector<int>ans;\n deque<int>dq;\n\n for(int i=0;i<k;i++)\n {\n dq.push_back(i);\n q.push({i,nums[i]});\n }\n ans.push_back(q.top().second);\n for(int i=k;i<nums.size();i++)\n {\n dq.push_back(nums[i]);\n q.push({i,nums[i]});\n dq.pop_front();\n int isin=q.top().first;\n int j=i-k+1;\n while(isin<j)\n {\n q.pop();\n isin=q.top().first;\n }\n ans.push_back(q.top().second);\n \n }\n return ans;\n }\n};", "memory": "158236" }
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the max sliding window</em>.</p> <p>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= nums.length</code></li> </ul>
3
{ "code": "\nclass Solution\n{\npublic:\n vector<int> maxSlidingWindow(vector<int> &nums, int k)\n {\n int n = nums.size();\n int s = 0, e = 0;\n priority_queue<int> pq; \n unordered_map<int, int> freq_map; \n vector<int> ans;\n\n while (e < n)\n {\n \n pq.push(nums[e]);\n freq_map[nums[e]]++;\n\n \n if (e - s + 1 == k)\n {\n \n while (freq_map[pq.top()] == 0)\n {\n pq.pop();\n }\n\n \n ans.push_back(pq.top());\n\n \n freq_map[nums[s]]--;\n\n \n s++;\n }\n\n \n e++;\n }\n\n return ans;\n }\n};\n", "memory": "159328" }
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the max sliding window</em>.</p> <p>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= nums.length</code></li> </ul>
3
{ "code": "class Solution\n{\npublic:\n vector<int> maxSlidingWindow(vector<int> &nums, int k)\n {\n int n = nums.size();\n int s = 0, e = 0;\n priority_queue<int> pq;\n unordered_map<int, int> m;\n vector<int> ans;\n\n while (s <= e && e < n)\n {\n\n pq.push(nums[e]);\n m[nums[e]]++;\n\n if (e - s + 1 == k)\n {\n while (m[pq.top()] == 0)\n {\n pq.pop();\n }\n ans.push_back(pq.top());\n m[nums[s]]--;\n s++;\n }\n e++;\n }\n return ans;\n }\n};", "memory": "159328" }
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the max sliding window</em>.</p> <p>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= nums.length</code></li> </ul>
3
{ "code": "class Solution{\npublic:\n vector<int>maxSlidingWindow(vector<int>&arr, int k){\n vector<int>ans;\n deque<int>dq;\n for(int i=0; i<arr.size(); i++){\n if(!dq.empty() and dq.front()<=i-k)dq.pop_front();\n while(!dq.empty() and arr[dq.back()]<=arr[i])dq.pop_back();\n dq.insert(dq.end(), i);\n if(i>=k-1)ans.push_back(arr[dq.front()]);\n }\n return ans;\n }\n};", "memory": "160421" }
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the max sliding window</em>.</p> <p>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= nums.length</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> maxSlidingWindow(vector<int>& nums, int k) {\n \n vector<int> ans;\n \n deque<int> q;\n \n for(int i=0;i<k-1;i++)\n {\n if(q.empty())\n {\n q.push_front(i);\n }\n else{\n if(nums[i]>=nums[q.front()])\n {\n q.push_front(i);\n }\n else if(nums[q.back()]>=nums[i])\n {\n q.push_back(i);\n }\n else{\n \n while(!q.empty() && nums[q.back()]<nums[i])\n {\n q.pop_back();\n }\n q.push_back(i);\n \n }\n }\n }\n \n for(int i=k-1;i<nums.size();i++)\n {\n while(!q.empty() && q.front()<(i+1-k))\n {\n q.pop_front();\n }\n \n if(q.empty())\n {\n q.push_front(i);\n }\n else{\n if(nums[i]>=nums[q.front()])\n {\n q.push_front(i);\n }\n else if(nums[q.back()]>=nums[i])\n {\n q.push_back(i);\n }\n else{\n \n while(!q.empty() && nums[q.back()]<nums[i])\n {\n q.pop_back();\n }\n q.push_back(i);\n \n }\n }\n ans.push_back(nums[q.front()]);\n }\n \n return ans;\n }\n};", "memory": "160421" }
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the max sliding window</em>.</p> <p>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= nums.length</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> maxSlidingWindow(vector<int>& nums, int k) {\n \n map<int,int>mpp;\n\n priority_queue<int> q;\n\n vector<int>ans;\n\n for(int i=0;i<k;i++){\n mpp[nums[i]]++;\n\n q.push(nums[i]);\n }\n\n ans.push_back(q.top());\n\n int s = 0;\n\n for(int i = k; i<nums.size(); i++){\n mpp[nums[s++]]--;\n\n q.push(nums[i]);\n mpp[nums[i]]++;\n\n while(mpp[q.top()] == 0){\n q.pop();\n }\n\n ans.push_back(q.top());\n }\n\n return ans;\n\n }\n};", "memory": "161513" }
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the max sliding window</em>.</p> <p>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= nums.length</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n// this solution is written and created by me all of it no help\n void print(priority_queue<int>q)\n {\n while(!q.empty())\n {\n cout<<q.top()<<\" \";\n q.pop();\n }\n cout<<endl;\n }\n vector<int> maxSlidingWindow(vector<int>& nums, int k) {\n vector<int>ans;\n priority_queue<int>q;\n map<int,int>m;\n for(int i=0;i<k;i++)\n {\n q.push(nums[i]);\n m[nums[i]]++;\n }\n //print(q);\n ans.push_back(q.top());\n for(int i=1,j=k;j<nums.size();i++,j++)\n {\n int old=nums[i-1];\n m[old]--;\n int neww=nums[j];\n q.push(neww);\n m[neww]++;\n // print(q);\n while(m[q.top()]<=0)\n {\n q.pop();\n }\n ans.push_back(q.top());\n }\n return ans;\n \n }\n};", "memory": "161513" }
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the max sliding window</em>.</p> <p>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= 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 int ans = INT_MIN;\n vector<int> res;\n priority_queue<int> pq;\n map<int,int> count;\n for(int i=0;i<k;i++){\n count[nums[i]]++;\n pq.push(nums[i]);\n }\n res.push_back(pq.top());\n for(int i=k;i<n;i++){\n count[nums[i]]++;\n count[nums[i-k]]--;\n if(count[pq.top()] <=0){\n while(!pq.empty() && count[pq.top()] <=0){\n pq.pop();\n }\n }\n pq.push(nums[i]);\n res.push_back(pq.top());\n\n }\n return res;\n }\n};", "memory": "162606" }
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the max sliding window</em>.</p> <p>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= nums.length</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> maxSlidingWindow(vector<int>& nums, int k) {\n vector<int> ans;\n int maxi = INT_MIN;\n int end = 0;\n unordered_map<int, int> mp;\n\n while (end < k) {\n maxi = max(maxi, nums[end]);\n mp[nums[end]]++;\n end++;\n }\n\n int start = 0;\n ans.push_back(maxi);\n while (end < nums.size()) {\n int previous = nums[start];\n int nextNum = nums[end];\n mp[previous]--;\n start++;\n if (mp[previous] <= 0) mp.erase(previous);\n mp[nextNum]++;\n if (previous == nextNum) maxi = maxi;\n else if (mp.contains(maxi)) {\n maxi = max(maxi, nextNum);\n } else {\n int i = start;\n maxi = INT_MIN;\n while (i <= end) maxi = max(maxi, nums[i++]);\n }\n ans.push_back(maxi);\n end++;\n }\n\n return ans;\n }\n};", "memory": "162606" }
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the max sliding window</em>.</p> <p>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= nums.length</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> maxSlidingWindow(vector<int>& nums, int k) {\n unordered_map<int,int> counts;\n vector<int> output;\n int maxx=INT_MIN,n=nums.size();\n for(int i=0;i<k;i++)\n {\n maxx=max(maxx,nums[i]);\n counts[nums[i]]++;\n }\n output.push_back(maxx);\n for(int i=0;i<n-k;i++)\n {\n if(maxx<nums[i+k])\n {\n maxx=nums[i+k];\n }\n counts[nums[i+k]]++;\n if(counts[nums[i]]==1)\n {\n counts.erase(nums[i]);\n if(nums[i]==maxx)\n {\n maxx=INT_MIN;\n for(pair<int,int> it:counts)\n {\n maxx=max(maxx,it.first);\n }\n }\n }\n else\n {\n counts[nums[i]]--;\n } \n output.push_back(maxx);\n }\n return output;\n }\n};\n", "memory": "163698" }
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the max sliding window</em>.</p> <p>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= nums.length</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> maxSlidingWindow(vector<int>& nums, int k) {\n deque<pair<int,int>> q;\n vector<int> ans;\n for(int i=0;i<nums.size();i++){\n while(!q.empty() && q.front().second+k <= i ) q.pop_front();\n if(!q.empty()){\n if(q.front().first<=nums[i]) q.push_front({nums[i],i});\n else{\n while(q.back().first<nums[i]) q.pop_back();\n q.push_back({nums[i],i});\n }\n }\n else{\n q.push_front({nums[i],i});\n }\n if(i-k+1>=0) ans.push_back(q.front().first);\n }\n return ans;\n }\n};", "memory": "163698" }
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the max sliding window</em>.</p> <p>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= nums.length</code></li> </ul>
3
{ "code": "class Solution {\n void insertEle(deque<pair<int,int>> & dq, vector<int> &nums, int i){\n if(!dq.size() || dq.front().first <= nums[i] ){\n dq.clear();\n dq.push_front({nums[i],i});\n }\n else{\n while(dq.size() && dq.back().first<=nums[i]){\n dq.pop_back();\n }\n dq.push_back({nums[i],i});\n }\n }\npublic:\n vector<int> maxSlidingWindow(vector<int>& nums, int k) {\n // priority_queue<pair<int,int>> s;\n deque<pair<int,int>> dq;\n // O klogk\n for(int i=0;i<k;i++){\n insertEle(dq,nums,i);\n }\n \n vector<int> ans;\n // auto it = s.begin();\n ans.push_back(dq.front().first);\n \n for(int i=k;i<nums.size();i++){\n insertEle(dq,nums,i);\n while(dq.front().second <= i-k){\n dq.pop_front();\n }\n auto [topEle, ind] = dq.front();\n ans.push_back(topEle);\n }\n return ans;\n }\n};", "memory": "164791" }
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the max sliding window</em>.</p> <p>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= nums.length</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> maxSlidingWindow(vector<int>& nums, int k) {\n // priority_queue<pair<int,int>> s;\n deque<pair<int,int>> dq;\n auto insertEle = [&](int i){\n if(!dq.size() || dq.front().first <= nums[i] ){\n dq.clear();\n dq.push_front({nums[i],i});\n }\n else{\n while(dq.size() && dq.back().first<=nums[i]){\n dq.pop_back();\n }\n dq.push_back({nums[i],i});\n }\n };\n // O klogk\n for(int i=0;i<k;i++){\n insertEle(i);\n }\n \n vector<int> ans;\n // auto it = s.begin();\n ans.push_back(dq.front().first);\n \n for(int i=k;i<nums.size();i++){\n insertEle(i); \n while(dq.front().second <= i-k){\n dq.pop_front();\n }\n auto [topEle, ind] = dq.front();\n ans.push_back(topEle);\n }\n return ans;\n }\n};", "memory": "164791" }
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the max sliding window</em>.</p> <p>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= 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 vector<int> ans;\n int i = 0, j = 0;\n priority_queue<int> pq;\n unordered_map<int, int> mp;\n\n while (j < n) {\n if ((j - i) < k) {\n pq.push(arr[j]);\n j++;\n } else {\n int top = pq.top();\n while (!pq.empty() && mp.count(top)) {\n mp[top]--;\n if (mp[top] == 0)\n mp.erase(top);\n pq.pop();\n top = pq.top();\n }\n ans.push_back(top);\n mp[arr[i]]++;\n i++;\n }\n }\n\n int top = pq.top();\n while (!pq.empty() && mp.count(top)) {\n mp[top]--;\n if (mp[top] == 0)\n mp.erase(top);\n pq.pop();\n top = pq.top();\n }\n ans.push_back(top); \n\n return ans;\n }\n};", "memory": "165883" }
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the max sliding window</em>.</p> <p>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= nums.length</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> maxSlidingWindow(vector<int>& nums, int k) {\n //can I use nge concept here?\n deque <int> q;\n for(int i=k-1;i>=0;i--){\n if(q.empty()){\n q.push_back(i);\n }else if(nums[i]>=nums[q.back()]){\n q.push_back(i);\n }\n }\n int i=0;\n int j=i+k;\n vector<int> v;\n while(j<nums.size()){\n v.push_back(nums[q.back()]);\n if(i==q.back()){\n q.pop_back();\n }\n i++;\n while(!q.empty() && nums[q.front()]<nums[j]){\n q.pop_front();\n }\n q.push_front(j);\n j++;\n }\n v.push_back(nums[q.back()]);\n std::cout<<nums[q.back()]<<std::endl;\n return v;\n }\n};", "memory": "165883" }
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the max sliding window</em>.</p> <p>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= 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 vector<int> ans;\n \n map<int, int, greater<int>> freqMap;\n int i=0, j=0;\n while(j-i<k){\n freqMap[nums[j]]++;\n j++;\n }\n ans.push_back((*freqMap.begin()).first);\n \n while(j<n){\n freqMap[nums[i]]--;\n freqMap[nums[j]]++;\n i++;\n j++;\n while((*freqMap.begin()).second == 0)\n freqMap.erase(freqMap.begin());\n ans.push_back((*freqMap.begin()).first);\n }\n\n return ans;\n }\n};", "memory": "166976" }
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the max sliding window</em>.</p> <p>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= 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 vector<int>ans;\n for(int i=0;i<k-1;i++)\n {\n mp[nums[i]]=i;\n }\n for(int i=k-1;i<nums.size();i++)\n {\n mp[nums[i]]=i;\n \n while(true)\n {\n auto x=*(--mp.end());\n if(x.second < i-k+1) \n {\n mp.erase(x.first);\n continue;\n }\n else\n {\n ans.push_back(x.first);\n break;\n }\n }\n }\n return ans;\n }\n};", "memory": "166976" }
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the max sliding window</em>.</p> <p>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= nums.length</code></li> </ul>
3
{ "code": "#include <span>\n#include <format>\n\nclass Compare {\npublic:\n bool operator()(const pair<int, size_t> &a, const pair<int, size_t> &b) {\n return a.first < b.first;\n }\n};\n\nclass Solution {\npublic:\n vector<int> maxSlidingWindow(vector<int>& nums, int k) {\n auto N = nums.size();\n auto nums_span = span(nums);\n auto initial = nums_span.subspan(0, k);\n auto remaining = nums_span.subspan(k, N-k);\n\n priority_queue<std::pair<int, size_t>, vector<std::pair<int, size_t>>, Compare> data;\n vector<int> result;\n\n // Fill first result\n size_t index = 0;\n for (const auto &num : initial) {\n // cout << std::format(\"Inserting: {}, Index: {}\", num, index) << endl;\n data.push({num, index++});\n }\n\n result.push_back(data.top().first);\n\n // Remaining\n for (const auto &num : remaining) {\n // cout << std::format(\"Inserting: {}, Index: {}\", num, index) << endl;\n data.push({num, index});\n while (true) {\n auto top = data.top();\n // cout << std::format(\"Top: {}, Index: {}, ind: {}\", top.first, top.second, index) << endl;\n if (top.second <= (index - k)) {\n // cout << std::format(\"Erasing {}\", top.first);\n data.pop();\n } else {\n result.push_back(top.first);\n break;\n }\n }\n index++;\n }\n\n return result;\n }\n};", "memory": "168068" }
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the max sliding window</em>.</p> <p>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= nums.length</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> maxSlidingWindow(vector<int>& nums, int k) {\n priority_queue<pair<int, int>> maxPq;\n priority_queue<pair<int, int>, vector<pair<int, int>>,\n greater<pair<int, int>>>\n minPq;\n int n = nums.size();\n vector<int> res(n - k + 1), minRes(n - k + 1);\n\n for (int i = 0; i < k; i++) {\n maxPq.push({nums[i], i});\n minPq.push({nums[i], i});\n }\n res[0] = maxPq.top().first;\n minRes[0] = minPq.top().first;\n for (int i = k; i < n; i++) {\n while (!maxPq.empty() && maxPq.top().second <= i - k) {\n maxPq.pop();\n }\n while (!minPq.empty() && minPq.top().second <= i - k) {\n minPq.pop();\n }\n maxPq.push({nums[i], i});\n minPq.push({nums[i], i});\n\n res[i - k + 1] = (maxPq.top().first);\n minRes[i - k + 1] = (minPq.top().first);\n }\n\n for(auto it:minRes){\n cout<<it<<\" \";\n }\n return res;\n }\n};", "memory": "168068" }
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the max sliding window</em>.</p> <p>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= nums.length</code></li> </ul>
3
{ "code": "#include <algorithm>\n#include <queue>\n#include <unordered_map>\n\nclass MapHeap {\nprivate:\n std::priority_queue<int> maxHeap; // Max heap to store values\n std::unordered_map<int, int> countMap; // Map to store counts of each value\n\npublic:\n // Add a value to the heap\n void add(int value) {\n maxHeap.push(value);\n countMap[value]++;\n }\n\n // Delete a value from the heap\n void deleteValue(int value) {\n if (countMap.find(value) != countMap.end()) {\n countMap[value]--;\n if (countMap[value] == 0) {\n countMap.erase(value);\n }\n }\n }\n\n // Peek the top value in the heap\n int peek() {\n // Clean up the top of the heap if it's not in the map\n while (!maxHeap.empty() && countMap[maxHeap.top()] == 0) {\n maxHeap.pop();\n }\n if (maxHeap.empty()) {\n throw std::runtime_error(\"Heap is empty.\");\n }\n return maxHeap.top();\n }\n};\n\nclass Solution {\npublic:\n vector<int> maxSlidingWindow(vector<int>& nums, int k) {\n std::vector<int> toReturn;\n if (nums.size() == 0) {\n return toReturn;\n }\n\n int numToReturn = nums.size() - k + 1;\n toReturn.reserve(numToReturn);\n\n MapHeap heap;\n int start_idx = 0;\n int end_idx = k - 1;\n for (int idx = start_idx; idx <= end_idx; ++idx) {\n heap.add(nums[idx]);\n }\n\n for (int retIdx = 0; retIdx < numToReturn; ++retIdx) {\n toReturn.push_back(heap.peek());\n if (retIdx < numToReturn - 1) {\n heap.deleteValue(nums[start_idx]);\n start_idx += 1;\n end_idx += 1;\n heap.add(nums[end_idx]);\n }\n }\n\n return toReturn;\n }\n};", "memory": "169161" }
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the max sliding window</em>.</p> <p>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= nums.length</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> maxSlidingWindow(vector<int>& nums, int k) {\n unordered_map<int, int> map;\n int current_max = INT_MIN;\n std::vector<int> result;\n\n for (int i = 0; i < k; ++i) {\n map[nums[i]]++;\n current_max = max(nums[i], current_max);\n }\n\n result.push_back(current_max);\n\n for (int i = k; i < nums.size(); ++i) {\n map[nums[i - k]]--;\n map[nums[i]]++;\n\n if (nums[i] > current_max) {\n current_max = nums[i];\n } \n if (map[current_max] == 0) {\n current_max = INT_MIN;\n std::vector<int> keys_to_erase;\n for (auto pair : map) {\n if (pair.second > 0) {\n current_max = max(current_max, pair.first);\n } else {\n keys_to_erase.push_back(pair.first);\n }\n }\n\n for (auto key : keys_to_erase) {\n map.erase(key);\n }\n }\n\n result.push_back(current_max);\n }\n\n return result;\n }\n};\n", "memory": "169161" }
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the max sliding window</em>.</p> <p>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= nums.length</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> maxSlidingWindow(vector<int>& arr, int k) {\n if(k == 1) return arr;\n int n = arr.size();\n deque<int> q;\n for(int i = 0 ; i < k-1 ; ++i)\n {\n if(q.empty()) \n {\n q.push_front(0);\n continue;\n }\n if(arr[i] >= arr[q.front()]) q.push_front(i);\n else\n {\n while(arr[i] >= arr[q.back()]) q.pop_back();\n q.push_back(i);\n }\n }\n \n vector<int> ans;\n for(int i = k-1 ; i < n ; ++i)\n {\n if(arr[i] >= arr[q.front()]) q.push_front(i);\n else\n {\n while(arr[i] >= arr[q.back()]) q.pop_back();\n q.push_back(i);\n }\n while(q.front() < (i-k+1)) q.pop_front();\n ans.push_back(arr[q.front()]); \n }\n return ans;\n\n }\n};", "memory": "170253" }
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the max sliding window</em>.</p> <p>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= nums.length</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> st;\n vector<int> lazy;\n void propagate(int i,int l,int r){\n if(lazy[i] != 0){\n st[i] = max(lazy[i],st[i]);\n if(l != r){\n lazy[2*i+1] = max(lazy[2*i+1],lazy[i]);\n lazy[2*i+2] = max(lazy[2*i+2],lazy[i]);\n }\n lazy[i] = 0;\n }\n }\n void rangeUpdate(int i,int l,int r,int strt,int end,int val){\n propagate(i,l,r);\n if(l > r || r < strt || l > end) return ;\n if(l >= strt && r <= end){\n st[i] = max(st[i],val);\n if(l != r){\n lazy[2*i+1] = max(lazy[2*i+1],val);\n lazy[2*i+2] = max(lazy[2*i+2],val);\n }\n return ;\n }\n int mid = (r-l)/2 + l;\n rangeUpdate(2*i+1,l,mid,strt,end,val);\n rangeUpdate(2*i+2,mid + 1,r,strt,end,val);\n st[i] = max(st[2*i+1],st[2*i+2]);\n }\n int query(int i,int l,int r,int strt,int end){\n propagate(i,l,r);\n if(l > r || r < strt || l > end) return -1e9;\n if(l >= strt && r <= end){\n return st[i];\n }\n int mid = (r-l)/2 + l;\n return max(query(2*i+1,l,mid,strt,end),query(2*i+2,mid+1,r,strt,end));\n \n }\n vector<int> maxSlidingWindow(vector<int>& nums, int k) {\n int n = nums.size();\n st.resize(4*n,-1e9);\n lazy.resize(4*n,-1e9);\n for(int i = 0;i<n;i++){\n rangeUpdate(0,0,n-1,i,i,nums[i]);\n }\n vector<int> ans;\n for(int i = 0;i+k<=n;i++){\n int j = i+k-1;\n \n ans.push_back(query(0,0,n-1,i,j));\n }\n return ans;\n }\n};", "memory": "170253" }
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the max sliding window</em>.</p> <p>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= nums.length</code></li> </ul>
3
{ "code": "#include <algorithm>\n#include <queue>\n#include <unordered_map>\n\nclass MapHeap {\nprivate:\n std::priority_queue<int> maxHeap; // Max heap to store values\n std::unordered_map<int, int> countMap; // Map to store counts of each value\n\npublic:\n // Add a value to the heap\n void add(int value) {\n if (countMap[value] == 0) {\n maxHeap.push(value);\n }\n countMap[value] += 1;\n }\n\n // Delete a value from the heap\n void deleteValue(int value) {\n countMap[value] -= 1;\n if (countMap[value] <= 0) {\n countMap.erase(value);\n }\n }\n\n // Peek the top value in the heap\n int peek() {\n while (!maxHeap.empty() && countMap[maxHeap.top()] == 0) {\n countMap.erase(maxHeap.top());\n maxHeap.pop();\n }\n if (maxHeap.empty()) throw std::runtime_error(\"Heap is empty.\");\n return maxHeap.top();\n }\n};\n\nclass Solution {\npublic:\n vector<int> maxSlidingWindow(vector<int>& nums, int k) {\n std::vector<int> toReturn;\n if (nums.size() == 0) {\n return toReturn;\n }\n\n int numToReturn = nums.size() - k + 1;\n toReturn.reserve(numToReturn);\n\n MapHeap heap;\n int start_idx = 0;\n int end_idx = k - 1;\n for (int idx = start_idx; idx <= end_idx; ++idx) {\n heap.add(nums[idx]);\n }\n\n for (int retIdx = 0; retIdx < numToReturn; ++retIdx) {\n toReturn.push_back(heap.peek());\n if (retIdx < numToReturn - 1) {\n heap.deleteValue(nums[start_idx]);\n start_idx += 1;\n end_idx += 1;\n heap.add(nums[end_idx]);\n }\n }\n\n return toReturn;\n }\n};", "memory": "171346" }
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the max sliding window</em>.</p> <p>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= nums.length</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> maxSlidingWindow(vector<int>& nums, int k) {\n //priority queue to keep track of the largest elements in k-subset\n //what happens when the sliding window has to remove that largest element?\n //we need a second priority queue that keeps track of \n //also have to keep in ming about duplicates \n\n int start = 0;\n unordered_map<int, int>mp;\n unordered_map<int, int>passed;\n priority_queue<int>pq;\n vector<int>ans;\n for(int i = 0; i < k; i ++){\n pq.push(nums[i]);\n mp[nums[i]]++;\n }\n ans.push_back(pq.top());\n for(int i = k; i < nums.size(); i ++){\n passed[nums[start]] ++;\n mp[nums[i]] ++;\n pq.push(nums[i]);\n\n \n \n if(nums[start] == pq.top()){\n //cout << mp[nums[start]] << \" \" << pq.top() << \" \" << passed[nums[start]] << endl;\n if(mp[nums[start]] > passed[nums[start]]){\n ans.push_back(pq.top());\n } else {\n int head = pq.top();\n while(passed[head] >= mp[head]){\n pq.pop();\n head = pq.top();\n }\n ans.push_back(head);\n }\n } else {\n ans.push_back(pq.top());\n }\n\n start ++;\n }\n\n return ans;\n\n }\n};", "memory": "171346" }
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the max sliding window</em>.</p> <p>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= nums.length</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> maxSlidingWindow(vector<int>& nums, int k) {\n deque<pair<int, int>>q;\n vector<int>res;\n int n = nums.size();\n\n for(int i = 0; i < k; i++)\n {\n if(q.size() == 0)q.push_front({nums[i], i});\n else\n {\n if(nums[i] >= q.front().first)\n {\n q.push_front({nums[i], i});\n }\n else\n {\n while(!q.empty() && q.back().first <= nums[i])\n {\n q.pop_back();\n }\n q.push_back({nums[i], i});\n }\n }\n }\n res.push_back(q.front().first);\n for(int i = k; i < n; i++)\n {\n if(nums[i] >= q.front().first)\n {\n q.push_front({nums[i], i});\n }\n else\n {\n while(!q.empty() && q.back().first <= nums[i])\n {\n q.pop_back();\n }\n q.push_back({nums[i], i});\n }\n while(q.front().second < i-k+1)\n {\n q.pop_front();\n }\n res.push_back(q.front().first);\n }\n return res;\n\n }\n};", "memory": "172438" }
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the max sliding window</em>.</p> <p>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= nums.length</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> maxSlidingWindow(vector<int>& nums, int k) {\n std::deque<std::pair<int, int>> rollingMaxes;\n std::vector<int> result;\n\n for (int i = 0; i < nums.size(); ++i) {\n while (!rollingMaxes.empty() && rollingMaxes.back().second <= i-k) {\n rollingMaxes.pop_back();\n }\n\n if (rollingMaxes.empty() || nums[i] >= rollingMaxes.back().first) {\n rollingMaxes.push_back({nums[i], i});\n } else {\n while (!rollingMaxes.empty() && nums[i] >= rollingMaxes.front().first) {\n rollingMaxes.pop_front();\n }\n rollingMaxes.push_front({nums[i], i});\n }\n\n if (i >= k-1) {\n result.push_back(rollingMaxes.back().first);\n }\n }\n\n return result;\n }\n};", "memory": "172438" }
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the max sliding window</em>.</p> <p>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= 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 deque<pair<int,int>> dq;\n vector<int> ans; \n for(int i = 0; i<n; i++){\n while(!dq.empty() && dq.front().second<=i-k){\n dq.pop_front();\n }\n if(!dq.empty() && dq.front().first < nums[i]){\n dq.push_front({nums[i],i});\n } else {\n while(!dq.empty() && dq.back().first<=nums[i]){\n dq.pop_back();\n }\n dq.push_back({nums[i],i});\n }\n \n if(i>=k-1){\n ans.push_back(dq.front().first);\n }\n\n }\n\n return ans;\n }\n};", "memory": "173531" }
239
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the max sliding window</em>.</p> <p>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= nums.length</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> maxSlidingWindow(vector<int>& nums, int k) {\n std::deque<std::pair<int, int>> rollingMaxes;\n // for (int i = 0; i < k-1; ++i) {\n // if (rollingMaxes.empty() || nums[i] >= rollingMaxes.back().first) {\n // rollingMaxes.push_back({nums[i], i});\n // } else {\n // while (!rollingMaxes.empty() && nums[i] <= rollingMaxes.front().first) {\n // rollingMaxes.pop_front();\n // }\n // rollingMaxes.push_front({nums[i], i});\n // }\n // }\n\n std::vector<int> result;\n for (int i = 0; i < nums.size(); ++i) {\n while (!rollingMaxes.empty() && rollingMaxes.back().second <= i-k) {\n rollingMaxes.pop_back();\n }\n if (rollingMaxes.empty() || nums[i] >= rollingMaxes.back().first) {\n rollingMaxes.push_back({nums[i], i});\n } else {\n while (!rollingMaxes.empty() && nums[i] >= rollingMaxes.front().first) {\n rollingMaxes.pop_front();\n }\n rollingMaxes.push_front({nums[i], i});\n }\n\n if (i >= k-1) {\n result.push_back(rollingMaxes.back().first);\n }\n }\n\n return result;\n\n\n // std::priority_queue<std::pair<int, int>> maxValuesAndIndices;\n // for (int i = 0; i < k-1; ++i) {\n // maxValuesAndIndices.push({nums[i], i});\n // }\n // std::vector<int> result;\n // for (int i = k-1; i < nums.size(); ++i) {\n // maxValuesAndIndices.push({nums[i], i});\n // while (maxValuesAndIndices.top().second < i-k) {\n // maxValuesAndIndices.pop();\n // }\n // }\n }\n};", "memory": "173531" }