id int64 1 3.58k | problem_description stringlengths 516 21.8k | instruction int64 0 3 | solution_c dict |
|---|---|---|---|
1,792 | <p>Given an integer array <code>nums</code> and a positive integer <code>k</code>, return <em>the most<strong> competitive</strong> subsequence of </em><code>nums</code> <em>of size </em><code>k</code>.</p>
<p>An array's subsequence is a resulting sequence obtained by erasing some (possibly zero) elements from the array.</p>
<p>We define that a subsequence <code>a</code> is more <strong>competitive</strong> than a subsequence <code>b</code> (of the same length) if in the first position where <code>a</code> and <code>b</code> differ, subsequence <code>a</code> has a number <strong>less</strong> than the corresponding number in <code>b</code>. For example, <code>[1,3,4]</code> is more competitive than <code>[1,3,5]</code> because the first position they differ is at the final number, and <code>4</code> is less than <code>5</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,5,2,6], k = 2
<strong>Output:</strong> [2,6]
<strong>Explanation:</strong> Among the set of every possible subsequence: {[3,5], [3,2], [3,6], [5,2], [5,6], [2,6]}, [2,6] is the most competitive.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,4,3,3,5,4,9,6], k = 4
<strong>Output:</strong> [2,3,3,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> mostCompetitive(vector<int>& nums, int k) {\n int n = nums.size();\n vector<int> ans;\n for (int i = 0; i < n; i++) \n {\n while ( !ans.empty() && nums[i] < ans.back() && ans.size() - 1 + nums.size() -i >=k)\n ans.pop_back();\n if ( ans.size() < k)\n ans.push_back(nums[i]);\n\n }\n return ans;\n }\n};",
"memory": "112200"
} |
1,792 | <p>Given an integer array <code>nums</code> and a positive integer <code>k</code>, return <em>the most<strong> competitive</strong> subsequence of </em><code>nums</code> <em>of size </em><code>k</code>.</p>
<p>An array's subsequence is a resulting sequence obtained by erasing some (possibly zero) elements from the array.</p>
<p>We define that a subsequence <code>a</code> is more <strong>competitive</strong> than a subsequence <code>b</code> (of the same length) if in the first position where <code>a</code> and <code>b</code> differ, subsequence <code>a</code> has a number <strong>less</strong> than the corresponding number in <code>b</code>. For example, <code>[1,3,4]</code> is more competitive than <code>[1,3,5]</code> because the first position they differ is at the final number, and <code>4</code> is less than <code>5</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,5,2,6], k = 2
<strong>Output:</strong> [2,6]
<strong>Explanation:</strong> Among the set of every possible subsequence: {[3,5], [3,2], [3,6], [5,2], [5,6], [2,6]}, [2,6] is the most competitive.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,4,3,3,5,4,9,6], k = 4
<strong>Output:</strong> [2,3,3,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> mostCompetitive(vector<int>& nums, int k) {\n int n = nums.size();\n vector<int> st;\n for(int i = 0; i < n; ++i) {\n while(!st.empty() && nums[i] < st.back() && n - i + st.size() > k) {\n st.pop_back();\n }\n if(st.size() < k) {\n st.push_back(nums[i]);\n }\n }\n return st;\n }\n};",
"memory": "112200"
} |
1,792 | <p>Given an integer array <code>nums</code> and a positive integer <code>k</code>, return <em>the most<strong> competitive</strong> subsequence of </em><code>nums</code> <em>of size </em><code>k</code>.</p>
<p>An array's subsequence is a resulting sequence obtained by erasing some (possibly zero) elements from the array.</p>
<p>We define that a subsequence <code>a</code> is more <strong>competitive</strong> than a subsequence <code>b</code> (of the same length) if in the first position where <code>a</code> and <code>b</code> differ, subsequence <code>a</code> has a number <strong>less</strong> than the corresponding number in <code>b</code>. For example, <code>[1,3,4]</code> is more competitive than <code>[1,3,5]</code> because the first position they differ is at the final number, and <code>4</code> is less than <code>5</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,5,2,6], k = 2
<strong>Output:</strong> [2,6]
<strong>Explanation:</strong> Among the set of every possible subsequence: {[3,5], [3,2], [3,6], [5,2], [5,6], [2,6]}, [2,6] is the most competitive.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,4,3,3,5,4,9,6], k = 4
<strong>Output:</strong> [2,3,3,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> mostCompetitive(vector<int>& nums, int k) {\n vector<int> answer;\n int n = nums.size();\n \n for (int i = 0; i < n; i++) {\n while (!answer.empty() && answer.back() > nums[i] && answer.size() + (n - i) > k) {\n answer.pop_back();\n }\n if (answer.size() < k) {\n answer.push_back(nums[i]);\n }\n }\n \n return answer;\n }\n};\n",
"memory": "112300"
} |
1,792 | <p>Given an integer array <code>nums</code> and a positive integer <code>k</code>, return <em>the most<strong> competitive</strong> subsequence of </em><code>nums</code> <em>of size </em><code>k</code>.</p>
<p>An array's subsequence is a resulting sequence obtained by erasing some (possibly zero) elements from the array.</p>
<p>We define that a subsequence <code>a</code> is more <strong>competitive</strong> than a subsequence <code>b</code> (of the same length) if in the first position where <code>a</code> and <code>b</code> differ, subsequence <code>a</code> has a number <strong>less</strong> than the corresponding number in <code>b</code>. For example, <code>[1,3,4]</code> is more competitive than <code>[1,3,5]</code> because the first position they differ is at the final number, and <code>4</code> is less than <code>5</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,5,2,6], k = 2
<strong>Output:</strong> [2,6]
<strong>Explanation:</strong> Among the set of every possible subsequence: {[3,5], [3,2], [3,6], [5,2], [5,6], [2,6]}, [2,6] is the most competitive.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,4,3,3,5,4,9,6], k = 4
<strong>Output:</strong> [2,3,3,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> mostCompetitive(vector<int>& nums, int k) {\n \n vector<int>ans;\n // stack<int>s;\n // vector<int>\n \n for(int i=0;i<nums.size();i++){\n \n \n while(ans.size()>0&&ans.back()>nums[i]&& (ans.size()+nums.size()-i)>k){\n ans.pop_back();\n }\n if(ans.size()<k){\n ans.push_back(nums[i]);\n }\n \n }\n // while(ans.size()){\n // ans.push_back(s.top());\n // s.pop();\n // }\n // reverse(ans.begin(),ans.end());\n return ans;\n \n }\n};",
"memory": "112400"
} |
1,792 | <p>Given an integer array <code>nums</code> and a positive integer <code>k</code>, return <em>the most<strong> competitive</strong> subsequence of </em><code>nums</code> <em>of size </em><code>k</code>.</p>
<p>An array's subsequence is a resulting sequence obtained by erasing some (possibly zero) elements from the array.</p>
<p>We define that a subsequence <code>a</code> is more <strong>competitive</strong> than a subsequence <code>b</code> (of the same length) if in the first position where <code>a</code> and <code>b</code> differ, subsequence <code>a</code> has a number <strong>less</strong> than the corresponding number in <code>b</code>. For example, <code>[1,3,4]</code> is more competitive than <code>[1,3,5]</code> because the first position they differ is at the final number, and <code>4</code> is less than <code>5</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,5,2,6], k = 2
<strong>Output:</strong> [2,6]
<strong>Explanation:</strong> Among the set of every possible subsequence: {[3,5], [3,2], [3,6], [5,2], [5,6], [2,6]}, [2,6] is the most competitive.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,4,3,3,5,4,9,6], k = 4
<strong>Output:</strong> [2,3,3,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 1 | {
"code": "class Solution {\n public:\n vector<int> mostCompetitive(vector<int>& nums, int k) {\n vector<int> ans;\n\n for (int i = 0; i < nums.size(); ++i) {\n while (!ans.empty() && ans.back() > nums[i] &&\n ans.size() - 1 + nums.size() - i >= k)\n ans.pop_back();\n if (ans.size() < k)\n ans.push_back(nums[i]);\n }\n\n return ans;\n }\n};",
"memory": "112500"
} |
1,792 | <p>Given an integer array <code>nums</code> and a positive integer <code>k</code>, return <em>the most<strong> competitive</strong> subsequence of </em><code>nums</code> <em>of size </em><code>k</code>.</p>
<p>An array's subsequence is a resulting sequence obtained by erasing some (possibly zero) elements from the array.</p>
<p>We define that a subsequence <code>a</code> is more <strong>competitive</strong> than a subsequence <code>b</code> (of the same length) if in the first position where <code>a</code> and <code>b</code> differ, subsequence <code>a</code> has a number <strong>less</strong> than the corresponding number in <code>b</code>. For example, <code>[1,3,4]</code> is more competitive than <code>[1,3,5]</code> because the first position they differ is at the final number, and <code>4</code> is less than <code>5</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,5,2,6], k = 2
<strong>Output:</strong> [2,6]
<strong>Explanation:</strong> Among the set of every possible subsequence: {[3,5], [3,2], [3,6], [5,2], [5,6], [2,6]}, [2,6] is the most competitive.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,4,3,3,5,4,9,6], k = 4
<strong>Output:</strong> [2,3,3,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n vector<int> mostCompetitive(vector<int>& nums, int k) {\n stack<int>s;\n s.push(nums[0]);\n vector<int>ans(k);\n for(int i=1;i<nums.size();i++)\n {\n while(!s.empty() && nums[i]<s.top() && k<=s.size()+nums.size()-i-1 )\n {\n s.pop();\n }\n \n s.push(nums[i]);\n }\n while(!s.empty())\n { if(s.size()<=k)\n {\n ans[k-1]=s.top();\n k--; \n }\n s.pop();\n\n }\n \n\n\n\n\n\n return ans;\n\n\n\n } \n};",
"memory": "112600"
} |
1,792 | <p>Given an integer array <code>nums</code> and a positive integer <code>k</code>, return <em>the most<strong> competitive</strong> subsequence of </em><code>nums</code> <em>of size </em><code>k</code>.</p>
<p>An array's subsequence is a resulting sequence obtained by erasing some (possibly zero) elements from the array.</p>
<p>We define that a subsequence <code>a</code> is more <strong>competitive</strong> than a subsequence <code>b</code> (of the same length) if in the first position where <code>a</code> and <code>b</code> differ, subsequence <code>a</code> has a number <strong>less</strong> than the corresponding number in <code>b</code>. For example, <code>[1,3,4]</code> is more competitive than <code>[1,3,5]</code> because the first position they differ is at the final number, and <code>4</code> is less than <code>5</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,5,2,6], k = 2
<strong>Output:</strong> [2,6]
<strong>Explanation:</strong> Among the set of every possible subsequence: {[3,5], [3,2], [3,6], [5,2], [5,6], [2,6]}, [2,6] is the most competitive.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,4,3,3,5,4,9,6], k = 4
<strong>Output:</strong> [2,3,3,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n vector<int> mostCompetitive(vector<int>& nums, int k) {\n stack<int>st;\n int n=nums.size();\n int k1=n-k;\n for(int i=0;i<n;i++){\n while(!st.empty() && nums[i]<st.top() && k1!=0){\n st.pop();\n k1--;\n }\n st.push(nums[i]);\n }\n while(k1--) st.pop();\n\n vector<int> ans(st.size());\n int i=st.size()-1;\n while(!st.empty()){\n ans[i--]=st.top();\n st.pop();\n }\n return ans;\n }\n};",
"memory": "112700"
} |
1,792 | <p>Given an integer array <code>nums</code> and a positive integer <code>k</code>, return <em>the most<strong> competitive</strong> subsequence of </em><code>nums</code> <em>of size </em><code>k</code>.</p>
<p>An array's subsequence is a resulting sequence obtained by erasing some (possibly zero) elements from the array.</p>
<p>We define that a subsequence <code>a</code> is more <strong>competitive</strong> than a subsequence <code>b</code> (of the same length) if in the first position where <code>a</code> and <code>b</code> differ, subsequence <code>a</code> has a number <strong>less</strong> than the corresponding number in <code>b</code>. For example, <code>[1,3,4]</code> is more competitive than <code>[1,3,5]</code> because the first position they differ is at the final number, and <code>4</code> is less than <code>5</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,5,2,6], k = 2
<strong>Output:</strong> [2,6]
<strong>Explanation:</strong> Among the set of every possible subsequence: {[3,5], [3,2], [3,6], [5,2], [5,6], [2,6]}, [2,6] is the most competitive.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,4,3,3,5,4,9,6], k = 4
<strong>Output:</strong> [2,3,3,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n vector<int> mostCompetitive(vector<int>& nums, int k) {\n deque<int> seen;\n int n = nums.size();\n int addCount = n-k;\n for(int i = 0;i<n;i++){\n while(!seen.empty() && seen.back() > nums[i] && addCount > 0){\n seen.pop_back();\n addCount--;\n }\n seen.push_back(nums[i]);\n }\n vector<int> res(k);\n\n\n for(int i =0;i<k;i++){\n res[i] = seen.front();\n seen.pop_front();\n }\n return res;\n }\n};",
"memory": "112700"
} |
1,792 | <p>Given an integer array <code>nums</code> and a positive integer <code>k</code>, return <em>the most<strong> competitive</strong> subsequence of </em><code>nums</code> <em>of size </em><code>k</code>.</p>
<p>An array's subsequence is a resulting sequence obtained by erasing some (possibly zero) elements from the array.</p>
<p>We define that a subsequence <code>a</code> is more <strong>competitive</strong> than a subsequence <code>b</code> (of the same length) if in the first position where <code>a</code> and <code>b</code> differ, subsequence <code>a</code> has a number <strong>less</strong> than the corresponding number in <code>b</code>. For example, <code>[1,3,4]</code> is more competitive than <code>[1,3,5]</code> because the first position they differ is at the final number, and <code>4</code> is less than <code>5</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,5,2,6], k = 2
<strong>Output:</strong> [2,6]
<strong>Explanation:</strong> Among the set of every possible subsequence: {[3,5], [3,2], [3,6], [5,2], [5,6], [2,6]}, [2,6] is the most competitive.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,4,3,3,5,4,9,6], k = 4
<strong>Output:</strong> [2,3,3,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n vector<int> mostCompetitive(vector<int>& nums, int k) {\n stack<int>st;\n int n=nums.size();\n int k1=n-k;\n for(int i=0;i<n;i++){\n while(!st.empty() && nums[i]<st.top() && k1!=0){\n st.pop();\n k1--;\n }\n st.push(nums[i]);\n }\n while(k1--) st.pop();\n\n vector<int> ans(st.size());\n int i=st.size()-1;\n while(!st.empty()){\n ans[i--]=st.top();\n st.pop();\n }\n return ans;\n }\n};",
"memory": "112800"
} |
1,792 | <p>Given an integer array <code>nums</code> and a positive integer <code>k</code>, return <em>the most<strong> competitive</strong> subsequence of </em><code>nums</code> <em>of size </em><code>k</code>.</p>
<p>An array's subsequence is a resulting sequence obtained by erasing some (possibly zero) elements from the array.</p>
<p>We define that a subsequence <code>a</code> is more <strong>competitive</strong> than a subsequence <code>b</code> (of the same length) if in the first position where <code>a</code> and <code>b</code> differ, subsequence <code>a</code> has a number <strong>less</strong> than the corresponding number in <code>b</code>. For example, <code>[1,3,4]</code> is more competitive than <code>[1,3,5]</code> because the first position they differ is at the final number, and <code>4</code> is less than <code>5</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,5,2,6], k = 2
<strong>Output:</strong> [2,6]
<strong>Explanation:</strong> Among the set of every possible subsequence: {[3,5], [3,2], [3,6], [5,2], [5,6], [2,6]}, [2,6] is the most competitive.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,4,3,3,5,4,9,6], k = 4
<strong>Output:</strong> [2,3,3,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n vector<int> mostCompetitive(vector<int>& nums, int k) {\n stack<int> seen;\n int n = nums.size();\n for(int i = 0;i<n;i++){\n while(!seen.empty() && seen.top() > nums[i] && n-i+seen.size()-1 >= k){\n seen.pop();\n }\n seen.push(nums[i]);\n }\n vector<int> res(k);\n\n while(seen.size() != k){\n seen.pop();\n }\n\n for(int i = k-1;i>=0;i--){\n res[i] = seen.top();\n seen.pop();\n }\n return res;\n }\n};",
"memory": "112900"
} |
1,792 | <p>Given an integer array <code>nums</code> and a positive integer <code>k</code>, return <em>the most<strong> competitive</strong> subsequence of </em><code>nums</code> <em>of size </em><code>k</code>.</p>
<p>An array's subsequence is a resulting sequence obtained by erasing some (possibly zero) elements from the array.</p>
<p>We define that a subsequence <code>a</code> is more <strong>competitive</strong> than a subsequence <code>b</code> (of the same length) if in the first position where <code>a</code> and <code>b</code> differ, subsequence <code>a</code> has a number <strong>less</strong> than the corresponding number in <code>b</code>. For example, <code>[1,3,4]</code> is more competitive than <code>[1,3,5]</code> because the first position they differ is at the final number, and <code>4</code> is less than <code>5</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,5,2,6], k = 2
<strong>Output:</strong> [2,6]
<strong>Explanation:</strong> Among the set of every possible subsequence: {[3,5], [3,2], [3,6], [5,2], [5,6], [2,6]}, [2,6] is the most competitive.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,4,3,3,5,4,9,6], k = 4
<strong>Output:</strong> [2,3,3,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n vector<int> mostCompetitive(vector<int>& nums, int k) {\n int n=nums.size();\n vector<int> ans;\n int x=n-k;\n for(int i=0;i<n;i++){\n while(ans.size()&&ans.back()>nums[i]&&x>0){\n ans.pop_back();\n x--;\n }\n ans.push_back(nums[i]);\n }\n while(x--) ans.pop_back();\n return ans;\n }\n};",
"memory": "113900"
} |
1,792 | <p>Given an integer array <code>nums</code> and a positive integer <code>k</code>, return <em>the most<strong> competitive</strong> subsequence of </em><code>nums</code> <em>of size </em><code>k</code>.</p>
<p>An array's subsequence is a resulting sequence obtained by erasing some (possibly zero) elements from the array.</p>
<p>We define that a subsequence <code>a</code> is more <strong>competitive</strong> than a subsequence <code>b</code> (of the same length) if in the first position where <code>a</code> and <code>b</code> differ, subsequence <code>a</code> has a number <strong>less</strong> than the corresponding number in <code>b</code>. For example, <code>[1,3,4]</code> is more competitive than <code>[1,3,5]</code> because the first position they differ is at the final number, and <code>4</code> is less than <code>5</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,5,2,6], k = 2
<strong>Output:</strong> [2,6]
<strong>Explanation:</strong> Among the set of every possible subsequence: {[3,5], [3,2], [3,6], [5,2], [5,6], [2,6]}, [2,6] is the most competitive.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,4,3,3,5,4,9,6], k = 4
<strong>Output:</strong> [2,3,3,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n vector<int> mostCompetitive(vector<int>& nums, int k) {\n vector<int> st;\n int n = nums.size();\n\n for(int i = 0; i < n; i++) {\n while(!st.empty() && st.back() > nums[i] && (st.size() >= k || n - i > k - st.size())) {\n st.pop_back();\n }\n st.push_back(nums[i]);\n }\n\n while(st.size() > k)\n st.pop_back();\n\n return st;\n }\n};",
"memory": "114000"
} |
1,792 | <p>Given an integer array <code>nums</code> and a positive integer <code>k</code>, return <em>the most<strong> competitive</strong> subsequence of </em><code>nums</code> <em>of size </em><code>k</code>.</p>
<p>An array's subsequence is a resulting sequence obtained by erasing some (possibly zero) elements from the array.</p>
<p>We define that a subsequence <code>a</code> is more <strong>competitive</strong> than a subsequence <code>b</code> (of the same length) if in the first position where <code>a</code> and <code>b</code> differ, subsequence <code>a</code> has a number <strong>less</strong> than the corresponding number in <code>b</code>. For example, <code>[1,3,4]</code> is more competitive than <code>[1,3,5]</code> because the first position they differ is at the final number, and <code>4</code> is less than <code>5</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,5,2,6], k = 2
<strong>Output:</strong> [2,6]
<strong>Explanation:</strong> Among the set of every possible subsequence: {[3,5], [3,2], [3,6], [5,2], [5,6], [2,6]}, [2,6] is the most competitive.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,4,3,3,5,4,9,6], k = 4
<strong>Output:</strong> [2,3,3,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 1 | {
"code": "\n/*\n * monotonic stack\n * - find the lexicographically smallest subsequence of size k\n * - use a monotonic inccreasing stack\n */\nclass Solution {\npublic:\n vector<int> mostCompetitive(vector<int>& nums, int k) {\n int n = nums.size();\n vector<int> curr;\n vector<int> result;\n for (int i = 0; i < n; i++) {\n while (!curr.empty() && curr.back() > nums[i] && curr.size() + (n - i) > k) {\n curr.pop_back();\n }\n\n if (curr.size() < k)\n curr.push_back(nums[i]);\n\n if (curr.size() == k)\n result = curr;\n }\n\n return result;\n }\n};\n",
"memory": "114500"
} |
1,792 | <p>Given an integer array <code>nums</code> and a positive integer <code>k</code>, return <em>the most<strong> competitive</strong> subsequence of </em><code>nums</code> <em>of size </em><code>k</code>.</p>
<p>An array's subsequence is a resulting sequence obtained by erasing some (possibly zero) elements from the array.</p>
<p>We define that a subsequence <code>a</code> is more <strong>competitive</strong> than a subsequence <code>b</code> (of the same length) if in the first position where <code>a</code> and <code>b</code> differ, subsequence <code>a</code> has a number <strong>less</strong> than the corresponding number in <code>b</code>. For example, <code>[1,3,4]</code> is more competitive than <code>[1,3,5]</code> because the first position they differ is at the final number, and <code>4</code> is less than <code>5</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,5,2,6], k = 2
<strong>Output:</strong> [2,6]
<strong>Explanation:</strong> Among the set of every possible subsequence: {[3,5], [3,2], [3,6], [5,2], [5,6], [2,6]}, [2,6] is the most competitive.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,4,3,3,5,4,9,6], k = 4
<strong>Output:</strong> [2,3,3,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 1 | {
"code": "\n/*\n * monotonic stack\n * - find the lexicographically smallest subsequence of size k\n * - use a monotonic inccreasing stack\n * - when there is not enough numbers left in the list, dont pop back anymore\n */\nclass Solution {\npublic:\n vector<int> mostCompetitive(vector<int>& nums, int k) {\n int n = nums.size();\n vector<int> curr;\n vector<int> result;\n for (int i = 0; i < n; i++) {\n while (!curr.empty() && curr.back() > nums[i] && curr.size() + (n - i) > k) {\n curr.pop_back();\n }\n\n if (curr.size() < k)\n curr.push_back(nums[i]);\n\n if (curr.size() == k)\n result = curr;\n }\n\n return result;\n }\n};\n",
"memory": "114500"
} |
1,792 | <p>Given an integer array <code>nums</code> and a positive integer <code>k</code>, return <em>the most<strong> competitive</strong> subsequence of </em><code>nums</code> <em>of size </em><code>k</code>.</p>
<p>An array's subsequence is a resulting sequence obtained by erasing some (possibly zero) elements from the array.</p>
<p>We define that a subsequence <code>a</code> is more <strong>competitive</strong> than a subsequence <code>b</code> (of the same length) if in the first position where <code>a</code> and <code>b</code> differ, subsequence <code>a</code> has a number <strong>less</strong> than the corresponding number in <code>b</code>. For example, <code>[1,3,4]</code> is more competitive than <code>[1,3,5]</code> because the first position they differ is at the final number, and <code>4</code> is less than <code>5</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,5,2,6], k = 2
<strong>Output:</strong> [2,6]
<strong>Explanation:</strong> Among the set of every possible subsequence: {[3,5], [3,2], [3,6], [5,2], [5,6], [2,6]}, [2,6] is the most competitive.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,4,3,3,5,4,9,6], k = 4
<strong>Output:</strong> [2,3,3,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n vector<int> mostCompetitive(vector<int>& nums, int k) {\n\n stack<int> s;\n s.push(nums[0]);\n int x = nums.size()-k;\n int c =0;\n vector<int> ans;\n for(int i=1;i<nums.size();i++) {\n while(!s.empty() && nums[i] < s.top()) {\n if(c+1 <= x) {\n s.pop();\n c++;\n }\n if(c==x) {\n while(!s.empty()) {\n ans.push_back(s.top());\n s.pop();\n }\n reverse(ans.begin(),ans.end());\n for(int j=i;j<nums.size();j++) {\n ans.push_back(nums[j]);\n }\n return ans;\n } \n }\n s.push(nums[i]);\n }\n while(!s.empty()) {\n if(s.size() > k) {\n s.pop();\n } else {\n ans.push_back(s.top());\n s.pop();\n }\n }\n reverse(ans.begin(),ans.end());\n return ans;\n\n \n }\n};",
"memory": "114600"
} |
1,792 | <p>Given an integer array <code>nums</code> and a positive integer <code>k</code>, return <em>the most<strong> competitive</strong> subsequence of </em><code>nums</code> <em>of size </em><code>k</code>.</p>
<p>An array's subsequence is a resulting sequence obtained by erasing some (possibly zero) elements from the array.</p>
<p>We define that a subsequence <code>a</code> is more <strong>competitive</strong> than a subsequence <code>b</code> (of the same length) if in the first position where <code>a</code> and <code>b</code> differ, subsequence <code>a</code> has a number <strong>less</strong> than the corresponding number in <code>b</code>. For example, <code>[1,3,4]</code> is more competitive than <code>[1,3,5]</code> because the first position they differ is at the final number, and <code>4</code> is less than <code>5</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,5,2,6], k = 2
<strong>Output:</strong> [2,6]
<strong>Explanation:</strong> Among the set of every possible subsequence: {[3,5], [3,2], [3,6], [5,2], [5,6], [2,6]}, [2,6] is the most competitive.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,4,3,3,5,4,9,6], k = 4
<strong>Output:</strong> [2,3,3,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n vector<int> mostCompetitive(vector<int>& nums, int k) {\n stack<int> st;\n int n = nums.size();\n vector<int> res;\n\n for(int i = 0;i<n;i++){\n int rem = n - i ;\n while(!st.empty() && st.top() > nums[i] && st.size() + rem -1 >= k ){\n st.pop();\n }\n\n if(st.size() < k) st.push(nums[i]);\n }\n\n while(!st.empty()){\n res.push_back(st.top());\n st.pop();\n }\n reverse(res.begin(),res.end());\n return res;\n }\n};",
"memory": "115000"
} |
1,792 | <p>Given an integer array <code>nums</code> and a positive integer <code>k</code>, return <em>the most<strong> competitive</strong> subsequence of </em><code>nums</code> <em>of size </em><code>k</code>.</p>
<p>An array's subsequence is a resulting sequence obtained by erasing some (possibly zero) elements from the array.</p>
<p>We define that a subsequence <code>a</code> is more <strong>competitive</strong> than a subsequence <code>b</code> (of the same length) if in the first position where <code>a</code> and <code>b</code> differ, subsequence <code>a</code> has a number <strong>less</strong> than the corresponding number in <code>b</code>. For example, <code>[1,3,4]</code> is more competitive than <code>[1,3,5]</code> because the first position they differ is at the final number, and <code>4</code> is less than <code>5</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,5,2,6], k = 2
<strong>Output:</strong> [2,6]
<strong>Explanation:</strong> Among the set of every possible subsequence: {[3,5], [3,2], [3,6], [5,2], [5,6], [2,6]}, [2,6] is the most competitive.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,4,3,3,5,4,9,6], k = 4
<strong>Output:</strong> [2,3,3,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n vector<int> mostCompetitive(vector<int>& arr, int k) {\n int n= arr.size();\n \n stack<int> s; //stores the elements which have a dilemna. In the end, the elements who remain form the answer.\n \n for(int i=0; i<n; i++){\n \n //n-i-1 elements are left in the array after ith index and k-s.size() is the no. of elements still required for our answer\n while(!s.empty() && arr[i]<s.top() && (n-i-1 >= k-s.size())){\n s.pop();\n }\n \n //push elements if stack has less than k elements (these elements are the ones whose dilemna has not been resolved yet) \n if(s.size()<k){\n s.push(arr[i]);\n }\n }\n \n //stack s contains our answer now, converting to a vector\n vector<int> ans;\n while(!s.empty()){\n ans.push_back(s.top());\n s.pop();\n }\n reverse(ans.begin(), ans.end());\n return ans;\n }\n};",
"memory": "115100"
} |
1,792 | <p>Given an integer array <code>nums</code> and a positive integer <code>k</code>, return <em>the most<strong> competitive</strong> subsequence of </em><code>nums</code> <em>of size </em><code>k</code>.</p>
<p>An array's subsequence is a resulting sequence obtained by erasing some (possibly zero) elements from the array.</p>
<p>We define that a subsequence <code>a</code> is more <strong>competitive</strong> than a subsequence <code>b</code> (of the same length) if in the first position where <code>a</code> and <code>b</code> differ, subsequence <code>a</code> has a number <strong>less</strong> than the corresponding number in <code>b</code>. For example, <code>[1,3,4]</code> is more competitive than <code>[1,3,5]</code> because the first position they differ is at the final number, and <code>4</code> is less than <code>5</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,5,2,6], k = 2
<strong>Output:</strong> [2,6]
<strong>Explanation:</strong> Among the set of every possible subsequence: {[3,5], [3,2], [3,6], [5,2], [5,6], [2,6]}, [2,6] is the most competitive.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,4,3,3,5,4,9,6], k = 4
<strong>Output:</strong> [2,3,3,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n vector<int> mostCompetitive(vector<int>& nums, int k) {\n \n vector<int>ans;\n stack<int>s;\n \n for(int i=0;i<nums.size();i++){\n \n \n while(!s.empty()&&s.top()>nums[i]&& (s.size()+nums.size()-i)>k){\n s.pop();\n }\n if(s.size()<k){\n s.push(nums[i]);\n }\n \n }\n while(!s.empty()){\n ans.push_back(s.top());\n s.pop();\n }\n reverse(ans.begin(),ans.end());\n return ans;\n \n }\n};",
"memory": "115200"
} |
1,792 | <p>Given an integer array <code>nums</code> and a positive integer <code>k</code>, return <em>the most<strong> competitive</strong> subsequence of </em><code>nums</code> <em>of size </em><code>k</code>.</p>
<p>An array's subsequence is a resulting sequence obtained by erasing some (possibly zero) elements from the array.</p>
<p>We define that a subsequence <code>a</code> is more <strong>competitive</strong> than a subsequence <code>b</code> (of the same length) if in the first position where <code>a</code> and <code>b</code> differ, subsequence <code>a</code> has a number <strong>less</strong> than the corresponding number in <code>b</code>. For example, <code>[1,3,4]</code> is more competitive than <code>[1,3,5]</code> because the first position they differ is at the final number, and <code>4</code> is less than <code>5</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,5,2,6], k = 2
<strong>Output:</strong> [2,6]
<strong>Explanation:</strong> Among the set of every possible subsequence: {[3,5], [3,2], [3,6], [5,2], [5,6], [2,6]}, [2,6] is the most competitive.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,4,3,3,5,4,9,6], k = 4
<strong>Output:</strong> [2,3,3,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n vector<int> mostCompetitive(vector<int>& nums, int k) {\n // 2 4 3 3 5 4 9 6\n // 2 4 4 3 3 5 9 6\n // 2 4 4 5 9 6 3 3\n \n // k = 4\n auto s = stack<int>();\n for (auto i = 0; i < nums.size(); i++) {\n while (!s.empty() && s.top() > nums.at(i) && s.size() - 1 + nums.size() - i >= k) {\n s.pop();\n }\n if (s.size() < k) s.push(nums.at(i));\n }\n\n auto out = vector<int>();\n while (!s.empty()) {\n out.push_back(s.top());\n s.pop();\n }\n\n reverse(out.begin(), out.end());\n return out;\n }\n};",
"memory": "115300"
} |
1,792 | <p>Given an integer array <code>nums</code> and a positive integer <code>k</code>, return <em>the most<strong> competitive</strong> subsequence of </em><code>nums</code> <em>of size </em><code>k</code>.</p>
<p>An array's subsequence is a resulting sequence obtained by erasing some (possibly zero) elements from the array.</p>
<p>We define that a subsequence <code>a</code> is more <strong>competitive</strong> than a subsequence <code>b</code> (of the same length) if in the first position where <code>a</code> and <code>b</code> differ, subsequence <code>a</code> has a number <strong>less</strong> than the corresponding number in <code>b</code>. For example, <code>[1,3,4]</code> is more competitive than <code>[1,3,5]</code> because the first position they differ is at the final number, and <code>4</code> is less than <code>5</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,5,2,6], k = 2
<strong>Output:</strong> [2,6]
<strong>Explanation:</strong> Among the set of every possible subsequence: {[3,5], [3,2], [3,6], [5,2], [5,6], [2,6]}, [2,6] is the most competitive.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,4,3,3,5,4,9,6], k = 4
<strong>Output:</strong> [2,3,3,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> mostCompetitive(vector<int>& nums, int k) {\n int n=nums.size();\n stack<int> st;\n for(int i=0;i<n;i++){\n while(!st.empty() && st.top()>nums[i] && (n-i)>(k-st.size())){\n //cout<<st.top()<<endl;\n st.pop();\n }\n if(st.size()<k){\n st.push(nums[i]);\n }\n }\n vector<int> ans;\n while(!st.empty()){\n ans.push_back(st.top());\n st.pop();\n }\n reverse(ans.begin(),ans.end());\n return ans;\n }\n};",
"memory": "115400"
} |
1,792 | <p>Given an integer array <code>nums</code> and a positive integer <code>k</code>, return <em>the most<strong> competitive</strong> subsequence of </em><code>nums</code> <em>of size </em><code>k</code>.</p>
<p>An array's subsequence is a resulting sequence obtained by erasing some (possibly zero) elements from the array.</p>
<p>We define that a subsequence <code>a</code> is more <strong>competitive</strong> than a subsequence <code>b</code> (of the same length) if in the first position where <code>a</code> and <code>b</code> differ, subsequence <code>a</code> has a number <strong>less</strong> than the corresponding number in <code>b</code>. For example, <code>[1,3,4]</code> is more competitive than <code>[1,3,5]</code> because the first position they differ is at the final number, and <code>4</code> is less than <code>5</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,5,2,6], k = 2
<strong>Output:</strong> [2,6]
<strong>Explanation:</strong> Among the set of every possible subsequence: {[3,5], [3,2], [3,6], [5,2], [5,6], [2,6]}, [2,6] is the most competitive.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,4,3,3,5,4,9,6], k = 4
<strong>Output:</strong> [2,3,3,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n Solution() {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n }\n\n void mostCompetitive(vector<int> &res, vector<int>& nums, int start_index, int k) {\n auto n = nums.size();\n if (k == 0 || n - start_index == 0) {\n return;\n }\n auto max_el = nums[start_index];\n auto max_el_pos = start_index;\n for (auto i = start_index; i <= n - k; ++i) {\n if (nums[i] < max_el) {\n max_el = nums[i];\n max_el_pos = i;\n }\n }\n res.push_back(max_el);\n mostCompetitive(res, nums, max_el_pos + 1, k - 1);\n }\n \n vector<int> mostCompetitiveV0(vector<int>& nums, int k) {\n vector<int> res;\n mostCompetitive(res, nums, 0, k);\n return res;\n }\n\n vector<int> mostCompetitive(vector<int>& nums, int k) {\n auto n = nums.size();\n stack<int> stack;\n vector<int> result;\n for (int i = 0; i < n; ++i) {\n while (!stack.empty() && nums[i] < stack.top() && n - i - 1 >= k - stack.size()) {\n stack.pop();\n }\n if (stack.size() < k) {\n stack.push(nums[i]);\n }\n }\n while (!stack.empty()) {\n result.push_back(stack.top());\n stack.pop();\n }\n reverse(result.begin(), result.end());\n return result;\n }\n};",
"memory": "115500"
} |
1,792 | <p>Given an integer array <code>nums</code> and a positive integer <code>k</code>, return <em>the most<strong> competitive</strong> subsequence of </em><code>nums</code> <em>of size </em><code>k</code>.</p>
<p>An array's subsequence is a resulting sequence obtained by erasing some (possibly zero) elements from the array.</p>
<p>We define that a subsequence <code>a</code> is more <strong>competitive</strong> than a subsequence <code>b</code> (of the same length) if in the first position where <code>a</code> and <code>b</code> differ, subsequence <code>a</code> has a number <strong>less</strong> than the corresponding number in <code>b</code>. For example, <code>[1,3,4]</code> is more competitive than <code>[1,3,5]</code> because the first position they differ is at the final number, and <code>4</code> is less than <code>5</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,5,2,6], k = 2
<strong>Output:</strong> [2,6]
<strong>Explanation:</strong> Among the set of every possible subsequence: {[3,5], [3,2], [3,6], [5,2], [5,6], [2,6]}, [2,6] is the most competitive.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,4,3,3,5,4,9,6], k = 4
<strong>Output:</strong> [2,3,3,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> mostCompetitive(vector<int>& nums, int k) {\n stack<int> s;\n int n = nums.size();\n for(int i = 0 ; i < n; i++){\n if(s.empty() || nums[i]>=s.top()){\n s.push(nums[i]);\n }else{\n while(!s.empty() && s.top()>nums[i] && s.size()+n-i > k)s.pop();\n s.push(nums[i]);\n }\n }\n stack<int> s2;\n while(!s.empty()){\n s2.push(s.top());s.pop();\n }\n vector<int> ans(k);\n for(int i = 0 ; i < k; i++){\n ans[i] = s2.top();s2.pop();\n }\n return ans;\n }\n};",
"memory": "115800"
} |
1,792 | <p>Given an integer array <code>nums</code> and a positive integer <code>k</code>, return <em>the most<strong> competitive</strong> subsequence of </em><code>nums</code> <em>of size </em><code>k</code>.</p>
<p>An array's subsequence is a resulting sequence obtained by erasing some (possibly zero) elements from the array.</p>
<p>We define that a subsequence <code>a</code> is more <strong>competitive</strong> than a subsequence <code>b</code> (of the same length) if in the first position where <code>a</code> and <code>b</code> differ, subsequence <code>a</code> has a number <strong>less</strong> than the corresponding number in <code>b</code>. For example, <code>[1,3,4]</code> is more competitive than <code>[1,3,5]</code> because the first position they differ is at the final number, and <code>4</code> is less than <code>5</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,5,2,6], k = 2
<strong>Output:</strong> [2,6]
<strong>Explanation:</strong> Among the set of every possible subsequence: {[3,5], [3,2], [3,6], [5,2], [5,6], [2,6]}, [2,6] is the most competitive.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,4,3,3,5,4,9,6], k = 4
<strong>Output:</strong> [2,3,3,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> mostCompetitive(vector<int>& nums, int k) {\n deque<int> queue;\n int additionalCount = nums.size() - k;\n for (int i = 0; i < nums.size(); i++) {\n while (!queue.empty() && queue.back() > nums[i] &&\n additionalCount > 0) {\n queue.pop_back();\n additionalCount--;\n }\n queue.push_back(nums[i]);\n }\n vector<int> result;\n for (int i = 0; i < k; i++) {\n result.push_back(queue.front());\n queue.pop_front();\n }\n return result;\n }\n};",
"memory": "115900"
} |
1,792 | <p>Given an integer array <code>nums</code> and a positive integer <code>k</code>, return <em>the most<strong> competitive</strong> subsequence of </em><code>nums</code> <em>of size </em><code>k</code>.</p>
<p>An array's subsequence is a resulting sequence obtained by erasing some (possibly zero) elements from the array.</p>
<p>We define that a subsequence <code>a</code> is more <strong>competitive</strong> than a subsequence <code>b</code> (of the same length) if in the first position where <code>a</code> and <code>b</code> differ, subsequence <code>a</code> has a number <strong>less</strong> than the corresponding number in <code>b</code>. For example, <code>[1,3,4]</code> is more competitive than <code>[1,3,5]</code> because the first position they differ is at the final number, and <code>4</code> is less than <code>5</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,5,2,6], k = 2
<strong>Output:</strong> [2,6]
<strong>Explanation:</strong> Among the set of every possible subsequence: {[3,5], [3,2], [3,6], [5,2], [5,6], [2,6]}, [2,6] is the most competitive.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,4,3,3,5,4,9,6], k = 4
<strong>Output:</strong> [2,3,3,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> mostCompetitive(vector<int>& nums, int k) {\n vector<int> ans(k);\n vector<int> st; // using this as stack\n for(int i = 0; i < nums.size(); i++){\n while(!st.empty() && st.back() > nums[i] && st.size()-1+nums.size()-i>=k){\n st.pop_back();\n }\n\n st.push_back(nums[i]);\n }\n for(int i = 0; i < k; i++){\n ans[i] = st[i];\n }\n return ans;\n // int index = 0;\n // int temp = k;\n // for(int i = 1; i <= k; i++){\n // int maxNum = nums[index];\n // for(int j = index; j < nums.size()-temp+1; j++){\n // if(nums[j] < maxNum && nums[j] != maxNum){\n // maxNum = nums[j];\n // index = j;\n // }\n // }\n // ans[i-1] = maxNum;\n // temp--;\n // index++;\n // }\n // return ans;\n }\n};",
"memory": "116000"
} |
1,792 | <p>Given an integer array <code>nums</code> and a positive integer <code>k</code>, return <em>the most<strong> competitive</strong> subsequence of </em><code>nums</code> <em>of size </em><code>k</code>.</p>
<p>An array's subsequence is a resulting sequence obtained by erasing some (possibly zero) elements from the array.</p>
<p>We define that a subsequence <code>a</code> is more <strong>competitive</strong> than a subsequence <code>b</code> (of the same length) if in the first position where <code>a</code> and <code>b</code> differ, subsequence <code>a</code> has a number <strong>less</strong> than the corresponding number in <code>b</code>. For example, <code>[1,3,4]</code> is more competitive than <code>[1,3,5]</code> because the first position they differ is at the final number, and <code>4</code> is less than <code>5</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,5,2,6], k = 2
<strong>Output:</strong> [2,6]
<strong>Explanation:</strong> Among the set of every possible subsequence: {[3,5], [3,2], [3,6], [5,2], [5,6], [2,6]}, [2,6] is the most competitive.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,4,3,3,5,4,9,6], k = 4
<strong>Output:</strong> [2,3,3,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n\n vector<int> solve(vector<int> &nums,int rem){\n int n=nums.size();\n stack<int> s;\n for(int i=0;i<n;i++){\n \n while(s.size()!=0 && s.top()>nums[i] && rem>0){\n s.pop();\n rem--;\n }\n s.push(nums[i]);\n \n }\n while(rem>0){\n s.pop();\n rem--;\n }\n vector<int> ans;\n while(s.size()!=0){\n ans.push_back(s.top());\n s.pop();\n }\n reverse(ans.begin(),ans.end());\n return ans;\n }\n\n vector<int> mostCompetitive(vector<int>& nums, int k) {\n int rem=nums.size()-k;\n return solve(nums,rem);\n }\n};",
"memory": "116000"
} |
1,792 | <p>Given an integer array <code>nums</code> and a positive integer <code>k</code>, return <em>the most<strong> competitive</strong> subsequence of </em><code>nums</code> <em>of size </em><code>k</code>.</p>
<p>An array's subsequence is a resulting sequence obtained by erasing some (possibly zero) elements from the array.</p>
<p>We define that a subsequence <code>a</code> is more <strong>competitive</strong> than a subsequence <code>b</code> (of the same length) if in the first position where <code>a</code> and <code>b</code> differ, subsequence <code>a</code> has a number <strong>less</strong> than the corresponding number in <code>b</code>. For example, <code>[1,3,4]</code> is more competitive than <code>[1,3,5]</code> because the first position they differ is at the final number, and <code>4</code> is less than <code>5</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,5,2,6], k = 2
<strong>Output:</strong> [2,6]
<strong>Explanation:</strong> Among the set of every possible subsequence: {[3,5], [3,2], [3,6], [5,2], [5,6], [2,6]}, [2,6] is the most competitive.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,4,3,3,5,4,9,6], k = 4
<strong>Output:</strong> [2,3,3,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> mostCompetitive(vector<int>& nums, int k) {\n vector<int> stack;\n int toDel=nums.size()-k;\n for (int i=0; i<nums.size(); i++){\n while (!stack.empty() && nums[i]<stack.back() && toDel){\n stack.pop_back();\n toDel--;\n }\n stack.push_back(nums[i]);\n }\n\n return vector<int>(stack.begin(), stack.begin()+k);\n }\n};",
"memory": "116100"
} |
1,792 | <p>Given an integer array <code>nums</code> and a positive integer <code>k</code>, return <em>the most<strong> competitive</strong> subsequence of </em><code>nums</code> <em>of size </em><code>k</code>.</p>
<p>An array's subsequence is a resulting sequence obtained by erasing some (possibly zero) elements from the array.</p>
<p>We define that a subsequence <code>a</code> is more <strong>competitive</strong> than a subsequence <code>b</code> (of the same length) if in the first position where <code>a</code> and <code>b</code> differ, subsequence <code>a</code> has a number <strong>less</strong> than the corresponding number in <code>b</code>. For example, <code>[1,3,4]</code> is more competitive than <code>[1,3,5]</code> because the first position they differ is at the final number, and <code>4</code> is less than <code>5</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,5,2,6], k = 2
<strong>Output:</strong> [2,6]
<strong>Explanation:</strong> Among the set of every possible subsequence: {[3,5], [3,2], [3,6], [5,2], [5,6], [2,6]}, [2,6] is the most competitive.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,4,3,3,5,4,9,6], k = 4
<strong>Output:</strong> [2,3,3,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> mostCompetitive(vector<int>& nums, int k) \n {\n int n = nums.size();\n vector<int> st;\n int del = 0;\n for(int i=0;i<n;i++)\n {\n while(!st.empty() && st.back()>nums[i] && del<n-k) \n {\n st.pop_back();\n del++;\n }\n st.push_back(nums[i]);\n }\n return vector<int>(st.begin(),st.begin()+k);\n\n \n }\n};",
"memory": "116100"
} |
1,792 | <p>Given an integer array <code>nums</code> and a positive integer <code>k</code>, return <em>the most<strong> competitive</strong> subsequence of </em><code>nums</code> <em>of size </em><code>k</code>.</p>
<p>An array's subsequence is a resulting sequence obtained by erasing some (possibly zero) elements from the array.</p>
<p>We define that a subsequence <code>a</code> is more <strong>competitive</strong> than a subsequence <code>b</code> (of the same length) if in the first position where <code>a</code> and <code>b</code> differ, subsequence <code>a</code> has a number <strong>less</strong> than the corresponding number in <code>b</code>. For example, <code>[1,3,4]</code> is more competitive than <code>[1,3,5]</code> because the first position they differ is at the final number, and <code>4</code> is less than <code>5</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,5,2,6], k = 2
<strong>Output:</strong> [2,6]
<strong>Explanation:</strong> Among the set of every possible subsequence: {[3,5], [3,2], [3,6], [5,2], [5,6], [2,6]}, [2,6] is the most competitive.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,4,3,3,5,4,9,6], k = 4
<strong>Output:</strong> [2,3,3,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> mostCompetitive(vector<int>& nums, int k) {\n vector<int> st;\n vector<int> sol;\n int n = nums.size();\n\n for (int i = 0; i < n; i++) {\n while (!st.empty() && st.back() > nums[i] && st.size()-1 + n - i >= k) {\n st.pop_back();\n }\n st.push_back(nums[i]);\n if (st.size() == k)\n sol = st;\n }\n\n return sol;\n }\n};",
"memory": "116200"
} |
1,792 | <p>Given an integer array <code>nums</code> and a positive integer <code>k</code>, return <em>the most<strong> competitive</strong> subsequence of </em><code>nums</code> <em>of size </em><code>k</code>.</p>
<p>An array's subsequence is a resulting sequence obtained by erasing some (possibly zero) elements from the array.</p>
<p>We define that a subsequence <code>a</code> is more <strong>competitive</strong> than a subsequence <code>b</code> (of the same length) if in the first position where <code>a</code> and <code>b</code> differ, subsequence <code>a</code> has a number <strong>less</strong> than the corresponding number in <code>b</code>. For example, <code>[1,3,4]</code> is more competitive than <code>[1,3,5]</code> because the first position they differ is at the final number, and <code>4</code> is less than <code>5</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,5,2,6], k = 2
<strong>Output:</strong> [2,6]
<strong>Explanation:</strong> Among the set of every possible subsequence: {[3,5], [3,2], [3,6], [5,2], [5,6], [2,6]}, [2,6] is the most competitive.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,4,3,3,5,4,9,6], k = 4
<strong>Output:</strong> [2,3,3,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 3 | {
"code": "#define ll long long \nclass Solution {\npublic:\n vector<int> mostCompetitive(vector<int>& nums, int k) {\n stack <int> s ;\n for ( ll i = 0 ; i < nums.size() ; i++ ){\n int no_of_next_ele = ( nums.size() - i) ; \n while ( !s.empty() && (s.size() + no_of_next_ele ) > k && s.top() > nums[i] ){\n s.pop();\n } \n s.push(nums[i]) ; \n }\n\n while ( s.size() > k ) {\n //cout << \" hii \" << endl; \n s.pop() ; \n }\n\n vector<int> ans ; \n while (!s.empty()){\n ans.push_back(s.top() );\n s.pop() ; \n }\n reverse ( ans.begin() , ans.end() ) ;\n return ans ; \n }\n};",
"memory": "116300"
} |
1,792 | <p>Given an integer array <code>nums</code> and a positive integer <code>k</code>, return <em>the most<strong> competitive</strong> subsequence of </em><code>nums</code> <em>of size </em><code>k</code>.</p>
<p>An array's subsequence is a resulting sequence obtained by erasing some (possibly zero) elements from the array.</p>
<p>We define that a subsequence <code>a</code> is more <strong>competitive</strong> than a subsequence <code>b</code> (of the same length) if in the first position where <code>a</code> and <code>b</code> differ, subsequence <code>a</code> has a number <strong>less</strong> than the corresponding number in <code>b</code>. For example, <code>[1,3,4]</code> is more competitive than <code>[1,3,5]</code> because the first position they differ is at the final number, and <code>4</code> is less than <code>5</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,5,2,6], k = 2
<strong>Output:</strong> [2,6]
<strong>Explanation:</strong> Among the set of every possible subsequence: {[3,5], [3,2], [3,6], [5,2], [5,6], [2,6]}, [2,6] is the most competitive.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,4,3,3,5,4,9,6], k = 4
<strong>Output:</strong> [2,3,3,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 3 | {
"code": "#define ll long long \nclass Solution {\npublic:\n vector<int> mostCompetitive(vector<int>& nums, int k) {\n stack <int> s ;\n for ( ll i = 0 ; i < nums.size() ; i++ ){\n int no_of_next_ele = ( nums.size() - i) ; \n while ( !s.empty() && (s.size() + no_of_next_ele ) > k && s.top() > nums[i] ){\n s.pop();\n } \n s.push(nums[i]) ; \n }\n \n while ( s.size() > k ) s.pop() ; \n\n vector<int> ans ; \n while (!s.empty()){\n ans.push_back(s.top() );\n s.pop() ; \n }\n reverse ( ans.begin() , ans.end() ) ;\n return ans ; \n }\n};",
"memory": "116300"
} |
1,792 | <p>Given an integer array <code>nums</code> and a positive integer <code>k</code>, return <em>the most<strong> competitive</strong> subsequence of </em><code>nums</code> <em>of size </em><code>k</code>.</p>
<p>An array's subsequence is a resulting sequence obtained by erasing some (possibly zero) elements from the array.</p>
<p>We define that a subsequence <code>a</code> is more <strong>competitive</strong> than a subsequence <code>b</code> (of the same length) if in the first position where <code>a</code> and <code>b</code> differ, subsequence <code>a</code> has a number <strong>less</strong> than the corresponding number in <code>b</code>. For example, <code>[1,3,4]</code> is more competitive than <code>[1,3,5]</code> because the first position they differ is at the final number, and <code>4</code> is less than <code>5</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,5,2,6], k = 2
<strong>Output:</strong> [2,6]
<strong>Explanation:</strong> Among the set of every possible subsequence: {[3,5], [3,2], [3,6], [5,2], [5,6], [2,6]}, [2,6] is the most competitive.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,4,3,3,5,4,9,6], k = 4
<strong>Output:</strong> [2,3,3,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> mostCompetitive(vector<int>& nums, int k) {\n stack<int> st;\n int n = nums.size();\n vector<int> smaller(n,n);\n vector<int> res(k);\n //setting position of smaller indexes\n for(int i=0;i<n;i++){\n while(!st.empty() && nums[st.top()]>nums[i]){\n smaller[st.top()]=i;\n st.pop();\n }\n st.push(i);\n }\n\n int pos =n-k;\n int idx=0;\n for(int i=0;i<n;i++){\n if(smaller[i]==-1 || smaller[i]>pos){\n res[idx]= nums[i];\n idx = min(idx+1,k-1);\n pos= min(pos+1,n);\n }\n\n }\n return res;\n\n }\n};",
"memory": "116400"
} |
1,792 | <p>Given an integer array <code>nums</code> and a positive integer <code>k</code>, return <em>the most<strong> competitive</strong> subsequence of </em><code>nums</code> <em>of size </em><code>k</code>.</p>
<p>An array's subsequence is a resulting sequence obtained by erasing some (possibly zero) elements from the array.</p>
<p>We define that a subsequence <code>a</code> is more <strong>competitive</strong> than a subsequence <code>b</code> (of the same length) if in the first position where <code>a</code> and <code>b</code> differ, subsequence <code>a</code> has a number <strong>less</strong> than the corresponding number in <code>b</code>. For example, <code>[1,3,4]</code> is more competitive than <code>[1,3,5]</code> because the first position they differ is at the final number, and <code>4</code> is less than <code>5</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,5,2,6], k = 2
<strong>Output:</strong> [2,6]
<strong>Explanation:</strong> Among the set of every possible subsequence: {[3,5], [3,2], [3,6], [5,2], [5,6], [2,6]}, [2,6] is the most competitive.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,4,3,3,5,4,9,6], k = 4
<strong>Output:</strong> [2,3,3,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> mostCompetitive(vector<int>& nums, int k) \n {\n vector<int> ans;\n int n=nums.size();\n stack<int> stk;\n stk.push(nums[0]);\n for(int i=1;i<n;i+=1)\n {\n while(stk.size()>0 and nums[i]<stk.top() and n-i>=k-stk.size()+1)\n {\n stk.pop();\n }\n if(stk.size()<k) stk.push(nums[i]);\n }\n while(!stk.empty())\n {\n if(stk.size()<=k)\n ans.insert(ans.begin(),stk.top());\n stk.pop();\n }\n return ans;\n }\n};",
"memory": "116500"
} |
1,792 | <p>Given an integer array <code>nums</code> and a positive integer <code>k</code>, return <em>the most<strong> competitive</strong> subsequence of </em><code>nums</code> <em>of size </em><code>k</code>.</p>
<p>An array's subsequence is a resulting sequence obtained by erasing some (possibly zero) elements from the array.</p>
<p>We define that a subsequence <code>a</code> is more <strong>competitive</strong> than a subsequence <code>b</code> (of the same length) if in the first position where <code>a</code> and <code>b</code> differ, subsequence <code>a</code> has a number <strong>less</strong> than the corresponding number in <code>b</code>. For example, <code>[1,3,4]</code> is more competitive than <code>[1,3,5]</code> because the first position they differ is at the final number, and <code>4</code> is less than <code>5</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,5,2,6], k = 2
<strong>Output:</strong> [2,6]
<strong>Explanation:</strong> Among the set of every possible subsequence: {[3,5], [3,2], [3,6], [5,2], [5,6], [2,6]}, [2,6] is the most competitive.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,4,3,3,5,4,9,6], k = 4
<strong>Output:</strong> [2,3,3,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> mostCompetitive(vector<int>& nums, int k) {\n int n = nums.size();\n stack<int> stack;\n\n // monotonic increasing stack, considering there are enough elements to fill stack of size k\n\n for (int i = 0; i < n; i++) {\n while (!stack.empty() && nums[i] < nums[stack.top()]) {\n if (stack.size() + n - i > k) {\n stack.pop();\n } else {\n break;\n }\n } \n stack.push(i);\n }\n\n vector<int> ans;\n\n while (!stack.empty()) {\n ans.push_back(nums[stack.top()]);\n stack.pop();\n }\n reverse(ans.begin(), ans.end());\n ans.resize(k);\n return ans;\n }\n};",
"memory": "117300"
} |
1,792 | <p>Given an integer array <code>nums</code> and a positive integer <code>k</code>, return <em>the most<strong> competitive</strong> subsequence of </em><code>nums</code> <em>of size </em><code>k</code>.</p>
<p>An array's subsequence is a resulting sequence obtained by erasing some (possibly zero) elements from the array.</p>
<p>We define that a subsequence <code>a</code> is more <strong>competitive</strong> than a subsequence <code>b</code> (of the same length) if in the first position where <code>a</code> and <code>b</code> differ, subsequence <code>a</code> has a number <strong>less</strong> than the corresponding number in <code>b</code>. For example, <code>[1,3,4]</code> is more competitive than <code>[1,3,5]</code> because the first position they differ is at the final number, and <code>4</code> is less than <code>5</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,5,2,6], k = 2
<strong>Output:</strong> [2,6]
<strong>Explanation:</strong> Among the set of every possible subsequence: {[3,5], [3,2], [3,6], [5,2], [5,6], [2,6]}, [2,6] is the most competitive.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,4,3,3,5,4,9,6], k = 4
<strong>Output:</strong> [2,3,3,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> mostCompetitive(vector<int>& nums, int k) {\n stack<int>stk;\n int index = 0;\n int n = nums.size()-1;\n while(index<nums.size())\n {\n \n\n while(stk.size()>0 && stk.top()>nums[index] && n-index>= k-stk.size())\n {\n stk.pop();\n }\n if(stk.size()<k)\n {\n stk.push(nums[index]);\n }\n index++;\n }\n\n\n vector<int>res1;\n vector<int> res(k,0);\n \n\n while(stk.size()>0)\n {\n res1.push_back(stk.top());\n stk.pop();\n }\n int i=0;\n\n int index1 =res1.size()-1;\n while(i<k)\n {\n res[i] = res1[index1];\n i++;\n index1--;\n }\n return res;\n }\n};",
"memory": "117400"
} |
1,792 | <p>Given an integer array <code>nums</code> and a positive integer <code>k</code>, return <em>the most<strong> competitive</strong> subsequence of </em><code>nums</code> <em>of size </em><code>k</code>.</p>
<p>An array's subsequence is a resulting sequence obtained by erasing some (possibly zero) elements from the array.</p>
<p>We define that a subsequence <code>a</code> is more <strong>competitive</strong> than a subsequence <code>b</code> (of the same length) if in the first position where <code>a</code> and <code>b</code> differ, subsequence <code>a</code> has a number <strong>less</strong> than the corresponding number in <code>b</code>. For example, <code>[1,3,4]</code> is more competitive than <code>[1,3,5]</code> because the first position they differ is at the final number, and <code>4</code> is less than <code>5</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,5,2,6], k = 2
<strong>Output:</strong> [2,6]
<strong>Explanation:</strong> Among the set of every possible subsequence: {[3,5], [3,2], [3,6], [5,2], [5,6], [2,6]}, [2,6] is the most competitive.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,4,3,3,5,4,9,6], k = 4
<strong>Output:</strong> [2,3,3,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> mostCompetitive(vector<int>& nums, int k) {\n int n = nums.size();\n vector<int> ans;\n stack<int> s;\n for(int i = n-1;i>=0;i--){\n s.push(nums[i]);\n }\n int prev = -1;\n while(!s.empty()){\n int a1 = s.top();\n if(prev ==-1 || a1 < prev){\n int rem = k - ans.size();\n int sz = s.size();\n // cout<<ans.size()<<\"--** \"<<a1<<\"\\n\";\n while(prev>0 && a1 < prev && rem<sz){\n ans.pop_back();\n rem = k - ans.size();\n if(ans.size()==0){\n prev = -1;\n break;\n }\n prev = ans[ans.size()-1];\n \n }\n ans.push_back(a1);\n prev = a1;\n // cout<<ans.size()<<\" \"<<prev<<\"--\\n\";\n s.pop();\n }\n else{\n if(ans.size()<k){\n ans.push_back(a1);\n prev = a1;\n }\n s.pop();\n }\n }\n return ans;\n }\n};",
"memory": "117900"
} |
1,792 | <p>Given an integer array <code>nums</code> and a positive integer <code>k</code>, return <em>the most<strong> competitive</strong> subsequence of </em><code>nums</code> <em>of size </em><code>k</code>.</p>
<p>An array's subsequence is a resulting sequence obtained by erasing some (possibly zero) elements from the array.</p>
<p>We define that a subsequence <code>a</code> is more <strong>competitive</strong> than a subsequence <code>b</code> (of the same length) if in the first position where <code>a</code> and <code>b</code> differ, subsequence <code>a</code> has a number <strong>less</strong> than the corresponding number in <code>b</code>. For example, <code>[1,3,4]</code> is more competitive than <code>[1,3,5]</code> because the first position they differ is at the final number, and <code>4</code> is less than <code>5</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,5,2,6], k = 2
<strong>Output:</strong> [2,6]
<strong>Explanation:</strong> Among the set of every possible subsequence: {[3,5], [3,2], [3,6], [5,2], [5,6], [2,6]}, [2,6] is the most competitive.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,4,3,3,5,4,9,6], k = 4
<strong>Output:</strong> [2,3,3,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> mostCompetitive(vector<int>& nums, int k) {\n stack<pair<int,int>> st;\n\n for (int i = 0; i < nums.size(); i++) {\n while (!st.empty()) {\n auto tp = st.top();\n if (nums[i] < nums[tp.first] && (k - tp.second <= nums.size()-i-1)) {\n st.pop();\n } else break;\n }\n\n if (st.empty()) {\n st.push({i, 1});\n } else if (st.top().second < k) {\n st.push({i, st.top().second+1});\n }\n }\n vector<int> op;\n while (!st.empty()) {\n op.push_back(nums[st.top().first]);\n st.pop();\n }\n\n reverse(op.begin(), op.end());\n return op;\n }\n};",
"memory": "118300"
} |
1,792 | <p>Given an integer array <code>nums</code> and a positive integer <code>k</code>, return <em>the most<strong> competitive</strong> subsequence of </em><code>nums</code> <em>of size </em><code>k</code>.</p>
<p>An array's subsequence is a resulting sequence obtained by erasing some (possibly zero) elements from the array.</p>
<p>We define that a subsequence <code>a</code> is more <strong>competitive</strong> than a subsequence <code>b</code> (of the same length) if in the first position where <code>a</code> and <code>b</code> differ, subsequence <code>a</code> has a number <strong>less</strong> than the corresponding number in <code>b</code>. For example, <code>[1,3,4]</code> is more competitive than <code>[1,3,5]</code> because the first position they differ is at the final number, and <code>4</code> is less than <code>5</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,5,2,6], k = 2
<strong>Output:</strong> [2,6]
<strong>Explanation:</strong> Among the set of every possible subsequence: {[3,5], [3,2], [3,6], [5,2], [5,6], [2,6]}, [2,6] is the most competitive.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,4,3,3,5,4,9,6], k = 4
<strong>Output:</strong> [2,3,3,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> mostCompetitive(vector<int>& nums, int k) {\n stack<int> st;\n int n = nums.size();\n int rem = n-k;\n vector<int> inc(n,1);\n for(int i = 0; i<n; i++){\n while(rem>0 && st.size()>0 && nums[st.top()]>nums[i]){\n inc[st.top()] = 0;\n rem -= 1;\n st.pop();\n }\n if(rem == 0) break;\n st.push(i);\n }\n\n vector<int> ans;\n int j = 0;\n for(int i = 0; i<n; i++){\n if(inc[i]) {\n j++;\n ans.push_back(nums[i]);\n if(j == k) break;\n }\n }\n return ans;\n\n }\n};",
"memory": "118800"
} |
1,792 | <p>Given an integer array <code>nums</code> and a positive integer <code>k</code>, return <em>the most<strong> competitive</strong> subsequence of </em><code>nums</code> <em>of size </em><code>k</code>.</p>
<p>An array's subsequence is a resulting sequence obtained by erasing some (possibly zero) elements from the array.</p>
<p>We define that a subsequence <code>a</code> is more <strong>competitive</strong> than a subsequence <code>b</code> (of the same length) if in the first position where <code>a</code> and <code>b</code> differ, subsequence <code>a</code> has a number <strong>less</strong> than the corresponding number in <code>b</code>. For example, <code>[1,3,4]</code> is more competitive than <code>[1,3,5]</code> because the first position they differ is at the final number, and <code>4</code> is less than <code>5</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,5,2,6], k = 2
<strong>Output:</strong> [2,6]
<strong>Explanation:</strong> Among the set of every possible subsequence: {[3,5], [3,2], [3,6], [5,2], [5,6], [2,6]}, [2,6] is the most competitive.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,4,3,3,5,4,9,6], k = 4
<strong>Output:</strong> [2,3,3,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> mostCompetitive(vector<int>& s, int k) {\n int i,j,n=s.size();\n stack<int> t;\n for(i=0;i<n;i++)\n {\n while(!t.empty() && t.top()>s[i] && t.size()+n-i>k)\n {\n t.pop();\n }\n t.push(s[i]);\n }\n vector<int> v;\n while(!t.empty())\n {\n v.push_back(t.top());\n t.pop();\n }\n reverse(v.begin(),v.end());\n vector<int> r(v.begin(),\n v.begin() + k);\n\n return r;\n\n }\n};",
"memory": "119200"
} |
1,792 | <p>Given an integer array <code>nums</code> and a positive integer <code>k</code>, return <em>the most<strong> competitive</strong> subsequence of </em><code>nums</code> <em>of size </em><code>k</code>.</p>
<p>An array's subsequence is a resulting sequence obtained by erasing some (possibly zero) elements from the array.</p>
<p>We define that a subsequence <code>a</code> is more <strong>competitive</strong> than a subsequence <code>b</code> (of the same length) if in the first position where <code>a</code> and <code>b</code> differ, subsequence <code>a</code> has a number <strong>less</strong> than the corresponding number in <code>b</code>. For example, <code>[1,3,4]</code> is more competitive than <code>[1,3,5]</code> because the first position they differ is at the final number, and <code>4</code> is less than <code>5</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,5,2,6], k = 2
<strong>Output:</strong> [2,6]
<strong>Explanation:</strong> Among the set of every possible subsequence: {[3,5], [3,2], [3,6], [5,2], [5,6], [2,6]}, [2,6] is the most competitive.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,4,3,3,5,4,9,6], k = 4
<strong>Output:</strong> [2,3,3,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> mostCompetitive(vector<int>& nums, int k) {\n // build a monotonic increasing stack\n vector<int> window, res;\n int n = nums.size();\n for (int i = 0; i < n; i++){\n while(!window.empty() && nums[window.back()] > nums[i] && window.size() - 1 + n - i >= k){\n window.pop_back();\n }\n window.push_back(i);\n }\n for (int i = 0; i < k; i++){\n res.push_back(nums[window[i]]);\n }\n return res;\n }\n};",
"memory": "119300"
} |
1,792 | <p>Given an integer array <code>nums</code> and a positive integer <code>k</code>, return <em>the most<strong> competitive</strong> subsequence of </em><code>nums</code> <em>of size </em><code>k</code>.</p>
<p>An array's subsequence is a resulting sequence obtained by erasing some (possibly zero) elements from the array.</p>
<p>We define that a subsequence <code>a</code> is more <strong>competitive</strong> than a subsequence <code>b</code> (of the same length) if in the first position where <code>a</code> and <code>b</code> differ, subsequence <code>a</code> has a number <strong>less</strong> than the corresponding number in <code>b</code>. For example, <code>[1,3,4]</code> is more competitive than <code>[1,3,5]</code> because the first position they differ is at the final number, and <code>4</code> is less than <code>5</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,5,2,6], k = 2
<strong>Output:</strong> [2,6]
<strong>Explanation:</strong> Among the set of every possible subsequence: {[3,5], [3,2], [3,6], [5,2], [5,6], [2,6]}, [2,6] is the most competitive.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,4,3,3,5,4,9,6], k = 4
<strong>Output:</strong> [2,3,3,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> mostCompetitive(vector<int>& arr, int k) {\n int n = arr.size();\n\n int rem = n - k;\n\n stack<int> s;\n\n for(int i=0;i<n;i++){\n while(!s.empty() && rem > 0 && s.top() > arr[i]){\n s.pop();\n rem--;\n }\n\n s.push(arr[i]);\n }\n\n vector<int> ans;\n\n while(!s.empty()){\n ans.push_back(s.top());\n s.pop();\n }\n\n reverse(ans.begin(), ans.end());\n\n return vector<int>(ans.begin(), ans.begin()+k);\n }\n};",
"memory": "119400"
} |
1,792 | <p>Given an integer array <code>nums</code> and a positive integer <code>k</code>, return <em>the most<strong> competitive</strong> subsequence of </em><code>nums</code> <em>of size </em><code>k</code>.</p>
<p>An array's subsequence is a resulting sequence obtained by erasing some (possibly zero) elements from the array.</p>
<p>We define that a subsequence <code>a</code> is more <strong>competitive</strong> than a subsequence <code>b</code> (of the same length) if in the first position where <code>a</code> and <code>b</code> differ, subsequence <code>a</code> has a number <strong>less</strong> than the corresponding number in <code>b</code>. For example, <code>[1,3,4]</code> is more competitive than <code>[1,3,5]</code> because the first position they differ is at the final number, and <code>4</code> is less than <code>5</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,5,2,6], k = 2
<strong>Output:</strong> [2,6]
<strong>Explanation:</strong> Among the set of every possible subsequence: {[3,5], [3,2], [3,6], [5,2], [5,6], [2,6]}, [2,6] is the most competitive.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,4,3,3,5,4,9,6], k = 4
<strong>Output:</strong> [2,3,3,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> mostCompetitive(vector<int>& nums, int k) {\n int n = nums.size();\n auto comp = [&nums](int i, int j) { \n if (nums[i] == nums[j]) return i > j;\n return nums[i] > nums[j];\n };\n priority_queue<int, vector<int>, decltype(comp)> q(comp);\n for (int i = 0; i <= n - k; ++i) {\n q.push(i);\n }\n\n vector<int> res;\n int current_idx = 0;\n while (true) {\n auto it = q.top();\n while (it < current_idx) {\n q.pop();\n it = q.top();\n }\n res.push_back(nums[it]);\n if (res.size() == k) break;\n\n q.pop();\n current_idx = it + 1;\n\n q.push(n - k + (int)res.size());\n }\n return res;\n }\n};",
"memory": "119600"
} |
1,792 | <p>Given an integer array <code>nums</code> and a positive integer <code>k</code>, return <em>the most<strong> competitive</strong> subsequence of </em><code>nums</code> <em>of size </em><code>k</code>.</p>
<p>An array's subsequence is a resulting sequence obtained by erasing some (possibly zero) elements from the array.</p>
<p>We define that a subsequence <code>a</code> is more <strong>competitive</strong> than a subsequence <code>b</code> (of the same length) if in the first position where <code>a</code> and <code>b</code> differ, subsequence <code>a</code> has a number <strong>less</strong> than the corresponding number in <code>b</code>. For example, <code>[1,3,4]</code> is more competitive than <code>[1,3,5]</code> because the first position they differ is at the final number, and <code>4</code> is less than <code>5</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,5,2,6], k = 2
<strong>Output:</strong> [2,6]
<strong>Explanation:</strong> Among the set of every possible subsequence: {[3,5], [3,2], [3,6], [5,2], [5,6], [2,6]}, [2,6] is the most competitive.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,4,3,3,5,4,9,6], k = 4
<strong>Output:</strong> [2,3,3,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> mostCompetitive(vector<int>& nums, int k) {\n int n = nums.size();\n vector<int> nle(n,-1);\n \n stack<int> st;\n for(int i=0; i < n; i++) {\n while(!st.empty() && nums[st.top()] > nums[i]) {\n nle[st.top()] = i;\n st.pop();\n }\n st.push(i);\n }\n\n for(auto x : nle)\n cout << x << \" \";\n\n int i = 0;\n vector<int> ans;\n while(i < n) {\n\n int j = i;\n while(nle[j] != -1 && (n-nle[j]) >= k) {\n j = nle[j];\n }\n\n // cout << i << \" \" << j << endl;\n\n if(k > 0 && j == i) {\n ans.push_back(nums[i]);\n k--;\n }\n else if(k > 0) {\n ans.push_back(nums[j]);\n k--;\n }\n\n i = j+1;\n }\n\n return ans;\n }\n};",
"memory": "119800"
} |
1,792 | <p>Given an integer array <code>nums</code> and a positive integer <code>k</code>, return <em>the most<strong> competitive</strong> subsequence of </em><code>nums</code> <em>of size </em><code>k</code>.</p>
<p>An array's subsequence is a resulting sequence obtained by erasing some (possibly zero) elements from the array.</p>
<p>We define that a subsequence <code>a</code> is more <strong>competitive</strong> than a subsequence <code>b</code> (of the same length) if in the first position where <code>a</code> and <code>b</code> differ, subsequence <code>a</code> has a number <strong>less</strong> than the corresponding number in <code>b</code>. For example, <code>[1,3,4]</code> is more competitive than <code>[1,3,5]</code> because the first position they differ is at the final number, and <code>4</code> is less than <code>5</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,5,2,6], k = 2
<strong>Output:</strong> [2,6]
<strong>Explanation:</strong> Among the set of every possible subsequence: {[3,5], [3,2], [3,6], [5,2], [5,6], [2,6]}, [2,6] is the most competitive.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,4,3,3,5,4,9,6], k = 4
<strong>Output:</strong> [2,3,3,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> mostCompetitive(vector<int>& nums, int k) {\n int n = nums.size();\n vector<int> nle(n,-1);\n \n stack<int> st;\n for(int i=0; i < n; i++) {\n while(!st.empty() && nums[st.top()] > nums[i]) {\n nle[st.top()] = i;\n st.pop();\n }\n st.push(i);\n }\n\n // for(auto x : nle)\n // cout << x << \" \";\n\n int i = 0;\n vector<int> ans;\n while(i < n) {\n\n int j = i;\n while(nle[j] != -1 && (n-nle[j]) >= k) {\n j = nle[j];\n }\n\n // cout << i << \" \" << j << endl;\n\n if(k > 0 && j == i) {\n ans.push_back(nums[i]);\n k--;\n }\n else if(k > 0) {\n ans.push_back(nums[j]);\n k--;\n }\n\n i = j+1;\n }\n\n return ans;\n }\n};",
"memory": "119800"
} |
1,792 | <p>Given an integer array <code>nums</code> and a positive integer <code>k</code>, return <em>the most<strong> competitive</strong> subsequence of </em><code>nums</code> <em>of size </em><code>k</code>.</p>
<p>An array's subsequence is a resulting sequence obtained by erasing some (possibly zero) elements from the array.</p>
<p>We define that a subsequence <code>a</code> is more <strong>competitive</strong> than a subsequence <code>b</code> (of the same length) if in the first position where <code>a</code> and <code>b</code> differ, subsequence <code>a</code> has a number <strong>less</strong> than the corresponding number in <code>b</code>. For example, <code>[1,3,4]</code> is more competitive than <code>[1,3,5]</code> because the first position they differ is at the final number, and <code>4</code> is less than <code>5</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,5,2,6], k = 2
<strong>Output:</strong> [2,6]
<strong>Explanation:</strong> Among the set of every possible subsequence: {[3,5], [3,2], [3,6], [5,2], [5,6], [2,6]}, [2,6] is the most competitive.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,4,3,3,5,4,9,6], k = 4
<strong>Output:</strong> [2,3,3,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> mostCompetitive(vector<int>& nums, int k) {\n int n = nums.size();\n vector<int> result;\n deque<pair<int, int>> d;\n int lastAddedInd = -1;\n for (int i = 0; i < n; ++i) {\n while (!d.empty() && d.back().first > nums[i]) {\n d.pop_back();\n }\n d.push_back({nums[i], i});\n if (i == n - (k - result.size())) {\n while (!d.empty() && d.front().second <= lastAddedInd) {\n d.pop_front();\n }\n result.push_back(d.front().first);\n lastAddedInd = d.front().second;\n d.pop_front();\n }\n }\n return result;\n }\n};",
"memory": "119900"
} |
1,792 | <p>Given an integer array <code>nums</code> and a positive integer <code>k</code>, return <em>the most<strong> competitive</strong> subsequence of </em><code>nums</code> <em>of size </em><code>k</code>.</p>
<p>An array's subsequence is a resulting sequence obtained by erasing some (possibly zero) elements from the array.</p>
<p>We define that a subsequence <code>a</code> is more <strong>competitive</strong> than a subsequence <code>b</code> (of the same length) if in the first position where <code>a</code> and <code>b</code> differ, subsequence <code>a</code> has a number <strong>less</strong> than the corresponding number in <code>b</code>. For example, <code>[1,3,4]</code> is more competitive than <code>[1,3,5]</code> because the first position they differ is at the final number, and <code>4</code> is less than <code>5</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,5,2,6], k = 2
<strong>Output:</strong> [2,6]
<strong>Explanation:</strong> Among the set of every possible subsequence: {[3,5], [3,2], [3,6], [5,2], [5,6], [2,6]}, [2,6] is the most competitive.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,4,3,3,5,4,9,6], k = 4
<strong>Output:</strong> [2,3,3,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> mostCompetitive(vector<int>& nums, int k) {\n // cout<<\"----\"<<endl;\n stack<pair<int, int>>stk;\n int n = nums.size();\n for(int i=0; i<n; i++) {\n if (stk.empty()) {\n stk.push({nums[i], i});\n } else {\n while(!stk.empty() && stk.top().first > nums[i]) {\n stk.pop();\n }\n stk.push({nums[i], i});\n }\n }\n vector<int> ans(k);\n if (stk.size() >= k) {\n while(stk.size() > k) stk.pop();\n int i = k-1;\n while(!stk.empty()) {\n ans[i--] = stk.top().first;\n stk.pop();\n } \n } else {\n int r = n-1, index = k-1;\n int cnt = k - stk.size();\n while(cnt > 0 && !stk.empty()) {\n auto node = stk.top();\n // cout<<\"node: \"<<node.first<<\" \"<<node.second<<\" r: \"<<r<<\" cnt: \"<<cnt<<endl;\n if (r - node.second <= cnt) {\n cnt -= (r-node.second);\n while(r >= node.second) {\n ans[index--] = nums[r--];\n }\n // cout<<\"new cnt: \"<<cnt<<endl;\n stk.pop();\n } else {\n vector<int>temp = vector<int>(nums.begin()+node.second+1, nums.begin()+r+1);\n vector<int> res = mostCompetitive(temp, cnt);\n r = res.size()-1;\n while(r >= 0) {\n ans[index--] = res[r--];\n }\n cnt = 0;\n }\n }\n if (stk.empty() && cnt > 0) {\n vector<int>temp = vector<int>(nums.begin(), nums.begin()+r+1);\n vector<int> res = mostCompetitive(temp, cnt);\n r = res.size()-1;\n while(r >= 0) {\n ans[index--] = res[r--];\n }\n } else {\n while(!stk.empty()) {\n ans[index--] = stk.top().first;\n stk.pop();\n }\n }\n }\n return ans;\n }\n};",
"memory": "121200"
} |
1,792 | <p>Given an integer array <code>nums</code> and a positive integer <code>k</code>, return <em>the most<strong> competitive</strong> subsequence of </em><code>nums</code> <em>of size </em><code>k</code>.</p>
<p>An array's subsequence is a resulting sequence obtained by erasing some (possibly zero) elements from the array.</p>
<p>We define that a subsequence <code>a</code> is more <strong>competitive</strong> than a subsequence <code>b</code> (of the same length) if in the first position where <code>a</code> and <code>b</code> differ, subsequence <code>a</code> has a number <strong>less</strong> than the corresponding number in <code>b</code>. For example, <code>[1,3,4]</code> is more competitive than <code>[1,3,5]</code> because the first position they differ is at the final number, and <code>4</code> is less than <code>5</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,5,2,6], k = 2
<strong>Output:</strong> [2,6]
<strong>Explanation:</strong> Among the set of every possible subsequence: {[3,5], [3,2], [3,6], [5,2], [5,6], [2,6]}, [2,6] is the most competitive.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,4,3,3,5,4,9,6], k = 4
<strong>Output:</strong> [2,3,3,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> mostCompetitive(vector<int>& nums, int k) {\n \n int mini=INT_MAX;\n int ind=-1;\n vector<int> ans;\n \n for(int i=0;i<=nums.size()-k;i++){\n if(nums[i]<mini)\n {\n mini=nums[i];\n ind=i;\n }\n }\n \n int last=ind;\n ans.push_back(nums[ind]);\n int count=1;\n priority_queue <pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> pq;\n \n for(int i=ind+1;i<nums.size();i++){\n pq.push({nums[i],i});\n if(i==nums.size()+count-k){\n while(!pq.empty()){\n auto tp=pq.top();\n if(tp.second<=last)\n pq.pop();\n else{\n ans.push_back(tp.first);\n last=tp.second;\n pq.pop();\n count++;\n break;\n }\n \n }\n }\n }\n \n return ans;\n \n \n }\n};",
"memory": "121400"
} |
1,792 | <p>Given an integer array <code>nums</code> and a positive integer <code>k</code>, return <em>the most<strong> competitive</strong> subsequence of </em><code>nums</code> <em>of size </em><code>k</code>.</p>
<p>An array's subsequence is a resulting sequence obtained by erasing some (possibly zero) elements from the array.</p>
<p>We define that a subsequence <code>a</code> is more <strong>competitive</strong> than a subsequence <code>b</code> (of the same length) if in the first position where <code>a</code> and <code>b</code> differ, subsequence <code>a</code> has a number <strong>less</strong> than the corresponding number in <code>b</code>. For example, <code>[1,3,4]</code> is more competitive than <code>[1,3,5]</code> because the first position they differ is at the final number, and <code>4</code> is less than <code>5</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,5,2,6], k = 2
<strong>Output:</strong> [2,6]
<strong>Explanation:</strong> Among the set of every possible subsequence: {[3,5], [3,2], [3,6], [5,2], [5,6], [2,6]}, [2,6] is the most competitive.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,4,3,3,5,4,9,6], k = 4
<strong>Output:</strong> [2,3,3,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> mostCompetitive(vector<int>& nums, int k) {\n priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> pq;\n\n int n=nums.size();\n int max_index=-1;\n\n int temp=k;\n vector<int> sol(k);\n for(int i=0;i<=n-k;i++)\n pq.push({nums[i],i});\n\n for(int i=0;i<temp;i++)\n {\n while(pq.top().second<=max_index)\n {\n pq.pop();\n }\n sol[i]=pq.top().first;\n max_index=max(max_index,pq.top().second);\n pq.pop();\n\n k--;\n if(k)\n {\n pq.push({nums[n-k],n-k});\n }\n }\n return sol;\n \n }\n};",
"memory": "121800"
} |
1,792 | <p>Given an integer array <code>nums</code> and a positive integer <code>k</code>, return <em>the most<strong> competitive</strong> subsequence of </em><code>nums</code> <em>of size </em><code>k</code>.</p>
<p>An array's subsequence is a resulting sequence obtained by erasing some (possibly zero) elements from the array.</p>
<p>We define that a subsequence <code>a</code> is more <strong>competitive</strong> than a subsequence <code>b</code> (of the same length) if in the first position where <code>a</code> and <code>b</code> differ, subsequence <code>a</code> has a number <strong>less</strong> than the corresponding number in <code>b</code>. For example, <code>[1,3,4]</code> is more competitive than <code>[1,3,5]</code> because the first position they differ is at the final number, and <code>4</code> is less than <code>5</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,5,2,6], k = 2
<strong>Output:</strong> [2,6]
<strong>Explanation:</strong> Among the set of every possible subsequence: {[3,5], [3,2], [3,6], [5,2], [5,6], [2,6]}, [2,6] is the most competitive.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,4,3,3,5,4,9,6], k = 4
<strong>Output:</strong> [2,3,3,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> mostCompetitive(vector<int>& nums, int k) \n {\n int n = nums.size();\n int p = 0;\n priority_queue<pair<int,int>> pq;\n for(int i=0;i<n;i++)\n {\n if(i<=n-k) pq.push({-1*nums[i],-1*i});\n } \n vector<int> ans(k);\n ans[0] = -1*pq.top().first;\n int prev = -1*pq.top().second;\n pq.pop();\n for(int p=1;p<k;p++)\n {\n pq.push({-1*nums[n-k+p],-1*(n-k+p)});\n while(-1*pq.top().second<prev) pq.pop();\n ans[p] = -1*pq.top().first;\n prev = -1*pq.top().second;\n pq.pop();\n }\n\n return ans;\n \n }\n};",
"memory": "121900"
} |
1,792 | <p>Given an integer array <code>nums</code> and a positive integer <code>k</code>, return <em>the most<strong> competitive</strong> subsequence of </em><code>nums</code> <em>of size </em><code>k</code>.</p>
<p>An array's subsequence is a resulting sequence obtained by erasing some (possibly zero) elements from the array.</p>
<p>We define that a subsequence <code>a</code> is more <strong>competitive</strong> than a subsequence <code>b</code> (of the same length) if in the first position where <code>a</code> and <code>b</code> differ, subsequence <code>a</code> has a number <strong>less</strong> than the corresponding number in <code>b</code>. For example, <code>[1,3,4]</code> is more competitive than <code>[1,3,5]</code> because the first position they differ is at the final number, and <code>4</code> is less than <code>5</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,5,2,6], k = 2
<strong>Output:</strong> [2,6]
<strong>Explanation:</strong> Among the set of every possible subsequence: {[3,5], [3,2], [3,6], [5,2], [5,6], [2,6]}, [2,6] is the most competitive.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,4,3,3,5,4,9,6], k = 4
<strong>Output:</strong> [2,3,3,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> mostCompetitive(vector<int>& nums, int k) {\n int n=nums.size();\n priority_queue<std::pair<int, int>, std::vector<std::pair<int, int>>, std::greater<>> pq;\n\n vector<int> res(k);\n for(int i=0;i<=n-k;i++){\n pq.push({nums[i],i});\n } \n res[0]=pq.top().first;\n int ind=pq.top().second;\n pq.pop();\n int pntr=1;\n for(int i=n-k+1;i<n;i++){\n pq.push({nums[i],i});\n\n while(pq.top().second<=ind&&!pq.empty()){\n pq.pop();\n }\n\n res[pntr]=pq.top().first;\n ind=pq.top().second;\n pntr++;\n }\n return res;\n }\n};",
"memory": "122000"
} |
1,792 | <p>Given an integer array <code>nums</code> and a positive integer <code>k</code>, return <em>the most<strong> competitive</strong> subsequence of </em><code>nums</code> <em>of size </em><code>k</code>.</p>
<p>An array's subsequence is a resulting sequence obtained by erasing some (possibly zero) elements from the array.</p>
<p>We define that a subsequence <code>a</code> is more <strong>competitive</strong> than a subsequence <code>b</code> (of the same length) if in the first position where <code>a</code> and <code>b</code> differ, subsequence <code>a</code> has a number <strong>less</strong> than the corresponding number in <code>b</code>. For example, <code>[1,3,4]</code> is more competitive than <code>[1,3,5]</code> because the first position they differ is at the final number, and <code>4</code> is less than <code>5</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,5,2,6], k = 2
<strong>Output:</strong> [2,6]
<strong>Explanation:</strong> Among the set of every possible subsequence: {[3,5], [3,2], [3,6], [5,2], [5,6], [2,6]}, [2,6] is the most competitive.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,4,3,3,5,4,9,6], k = 4
<strong>Output:</strong> [2,3,3,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 3 | {
"code": "#define ps push_back\nclass Solution {\npublic:\n vector<int> mostCompetitive(vector<int>& nums, int k) {\n int n = nums.size();\n cout<<\" n = \"<<n;\n stack <int > st;\n for(int i=0; i<n ;i++){\n \n while(!st.empty()&&st.top()>nums[i]&&(int)st.size()+n-i-1>=k){\n \n st.pop();\n }\n st.push(nums[i]);\n // }\n \n }\n vector<int > ans ;\n while(!st.empty()){\n ans.ps(st.top());st.pop();\n }\n vector<int > res;\n reverse(ans.begin(),ans.end());\n for(int i=0; i<k ;i++){\n res.ps(ans[i]);\n }\n return res;\n }\n};",
"memory": "122600"
} |
1,792 | <p>Given an integer array <code>nums</code> and a positive integer <code>k</code>, return <em>the most<strong> competitive</strong> subsequence of </em><code>nums</code> <em>of size </em><code>k</code>.</p>
<p>An array's subsequence is a resulting sequence obtained by erasing some (possibly zero) elements from the array.</p>
<p>We define that a subsequence <code>a</code> is more <strong>competitive</strong> than a subsequence <code>b</code> (of the same length) if in the first position where <code>a</code> and <code>b</code> differ, subsequence <code>a</code> has a number <strong>less</strong> than the corresponding number in <code>b</code>. For example, <code>[1,3,4]</code> is more competitive than <code>[1,3,5]</code> because the first position they differ is at the final number, and <code>4</code> is less than <code>5</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,5,2,6], k = 2
<strong>Output:</strong> [2,6]
<strong>Explanation:</strong> Among the set of every possible subsequence: {[3,5], [3,2], [3,6], [5,2], [5,6], [2,6]}, [2,6] is the most competitive.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,4,3,3,5,4,9,6], k = 4
<strong>Output:</strong> [2,3,3,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> mostCompetitive(vector<int>& nums, int k) {\n ios_base::sync_with_stdio(false), cin.tie(NULL);\n priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;\n vector<int> ans;\n int cur = 0;\n for(int i = 0; i < nums.size()-k; i++) pq.push(make_pair(nums[i], i));\n for(int i = 0; i < k; i++){\n pq.push(make_pair(nums[nums.size()-k + i], nums.size()-k + i));\n while(pq.top().second < cur) pq.pop();\n ans.push_back(pq.top().first);\n cur = pq.top().second;\n pq.pop();\n }\n return ans;\n }\n};",
"memory": "125500"
} |
1,792 | <p>Given an integer array <code>nums</code> and a positive integer <code>k</code>, return <em>the most<strong> competitive</strong> subsequence of </em><code>nums</code> <em>of size </em><code>k</code>.</p>
<p>An array's subsequence is a resulting sequence obtained by erasing some (possibly zero) elements from the array.</p>
<p>We define that a subsequence <code>a</code> is more <strong>competitive</strong> than a subsequence <code>b</code> (of the same length) if in the first position where <code>a</code> and <code>b</code> differ, subsequence <code>a</code> has a number <strong>less</strong> than the corresponding number in <code>b</code>. For example, <code>[1,3,4]</code> is more competitive than <code>[1,3,5]</code> because the first position they differ is at the final number, and <code>4</code> is less than <code>5</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,5,2,6], k = 2
<strong>Output:</strong> [2,6]
<strong>Explanation:</strong> Among the set of every possible subsequence: {[3,5], [3,2], [3,6], [5,2], [5,6], [2,6]}, [2,6] is the most competitive.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,4,3,3,5,4,9,6], k = 4
<strong>Output:</strong> [2,3,3,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n struct CompItem{\n bool operator()(const pair<int,int>& a,const pair<int,int>& b) const{\n if(a.first== b.first){\n return a.second < b.second;\n }\n return a.first < b.first;\n }\n };\n vector<int> mostCompetitive(vector<int>& nums, int k) {\n int N = nums.size();\n vector<pair<int,int>> paList;\n for(int i = 0;i <= N-k; ++i){\n paList.push_back({nums[i] , i});\n }\n sort(paList.begin() , paList.end() , CompItem());\n vector<int> resList;\n resList.push_back(paList[0].first);\n int prev = paList[0].second;\n int idx = 1;\n\n for(int i = N-k+1;i < N; ++i){\n int next = nums[i];\n while(idx< paList.size() && (paList[idx].second < prev || paList[idx].first > next )){\n idx++;\n }\n if(idx == paList.size()){\n resList.insert(resList.end() , nums.begin() + i,nums.end());\n break;\n }\n resList.push_back(paList[idx].first);\n prev = paList[idx].second;\n idx++;\n pair<int,int> newOne{next, i};\n auto it = std::lower_bound(paList.begin()+idx , paList.end() , newOne , CompItem());\n if(it == paList.end()){\n paList.push_back(newOne);\n }\n else{\n paList.insert(it,newOne);\n }\n }\n return resList;\n\n }\n};",
"memory": "126200"
} |
1,792 | <p>Given an integer array <code>nums</code> and a positive integer <code>k</code>, return <em>the most<strong> competitive</strong> subsequence of </em><code>nums</code> <em>of size </em><code>k</code>.</p>
<p>An array's subsequence is a resulting sequence obtained by erasing some (possibly zero) elements from the array.</p>
<p>We define that a subsequence <code>a</code> is more <strong>competitive</strong> than a subsequence <code>b</code> (of the same length) if in the first position where <code>a</code> and <code>b</code> differ, subsequence <code>a</code> has a number <strong>less</strong> than the corresponding number in <code>b</code>. For example, <code>[1,3,4]</code> is more competitive than <code>[1,3,5]</code> because the first position they differ is at the final number, and <code>4</code> is less than <code>5</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,5,2,6], k = 2
<strong>Output:</strong> [2,6]
<strong>Explanation:</strong> Among the set of every possible subsequence: {[3,5], [3,2], [3,6], [5,2], [5,6], [2,6]}, [2,6] is the most competitive.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,4,3,3,5,4,9,6], k = 4
<strong>Output:</strong> [2,3,3,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 3 | {
"code": "class Solution { \n void build_segment_tree(vector<int>& st, int k, vector<int>& nums, int a, int b) {\n if (a>b) return;\n if (a==b) {\n st[k]=a;\n }\n else {\n int m=a+(b-a)/2;\n build_segment_tree(st, 2*k+1, nums, a, m);\n build_segment_tree(st, 2*k+2, nums, m+1, b);\n if (nums[st[2*k+1]]<=nums[st[2*k+2]]) {\n st[k]=st[2*k+1];\n }\n else {\n st[k]=st[2*k+2];\n }\n }\n }\n int query(vector<int>& st, int k, int x, int y, vector<int>& nums, int a, int b) {\n if (x>b || y<a) {\n return -1;\n }\n if (a<=x && y<=b) {\n return st[k];\n }\n int m=x+(y-x)/2;\n int l=query(st, 2*k+1, x, m, nums, a, b);\n int r=query(st, 2*k+2, m+1, y, nums, a, b);\n \n int res=INT_MAX;\n if (l>-1 && r>-1) return nums[l]<=nums[r]?l:r;\n if (l>-1) return l;\n else return r;\n }\npublic:\n vector<int> mostCompetitive(vector<int>& nums, int k) {\n int n=nums.size();\n //int m=pow(2, int(log2(n))+2); //m=4*n\n vector<int> st(4*n);\n build_segment_tree(st, 0, nums, 0, n-1);\n\n vector<int> ans;\n int l=-1;\n for (int i=0; i<k; i++) {\n l=query(st, 0, 0, n-1, nums, l+1, n-k+i);\n ans.push_back(nums[l]);\n }\n \n return ans;\n }\n};",
"memory": "128900"
} |
1,792 | <p>Given an integer array <code>nums</code> and a positive integer <code>k</code>, return <em>the most<strong> competitive</strong> subsequence of </em><code>nums</code> <em>of size </em><code>k</code>.</p>
<p>An array's subsequence is a resulting sequence obtained by erasing some (possibly zero) elements from the array.</p>
<p>We define that a subsequence <code>a</code> is more <strong>competitive</strong> than a subsequence <code>b</code> (of the same length) if in the first position where <code>a</code> and <code>b</code> differ, subsequence <code>a</code> has a number <strong>less</strong> than the corresponding number in <code>b</code>. For example, <code>[1,3,4]</code> is more competitive than <code>[1,3,5]</code> because the first position they differ is at the final number, and <code>4</code> is less than <code>5</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,5,2,6], k = 2
<strong>Output:</strong> [2,6]
<strong>Explanation:</strong> Among the set of every possible subsequence: {[3,5], [3,2], [3,6], [5,2], [5,6], [2,6]}, [2,6] is the most competitive.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,4,3,3,5,4,9,6], k = 4
<strong>Output:</strong> [2,3,3,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> mostCompetitive(vector<int> nums, int k) {\n int n = nums.size();\n priority_queue<pair<int,int>,vector<pair<int,int>>,greater<>>pq;\n for(int i=0;i<=n-k;i++){\n pq.push({nums[i],i});\n }\n vector<int> ans;\n pair<int,int> smallest = pq.top(); pq.pop();\n ans.push_back(smallest.first);\n for(int i=n-k+1;i<n;i++){\n pq.push({nums[i],i});\n int index = smallest.second;\n while(!pq.empty() && pq.top().second<index)pq.pop();\n smallest = pq.top(); pq.pop();\n ans.push_back(smallest.first);\n }\n return ans;\n }\n};",
"memory": "129300"
} |
1,792 | <p>Given an integer array <code>nums</code> and a positive integer <code>k</code>, return <em>the most<strong> competitive</strong> subsequence of </em><code>nums</code> <em>of size </em><code>k</code>.</p>
<p>An array's subsequence is a resulting sequence obtained by erasing some (possibly zero) elements from the array.</p>
<p>We define that a subsequence <code>a</code> is more <strong>competitive</strong> than a subsequence <code>b</code> (of the same length) if in the first position where <code>a</code> and <code>b</code> differ, subsequence <code>a</code> has a number <strong>less</strong> than the corresponding number in <code>b</code>. For example, <code>[1,3,4]</code> is more competitive than <code>[1,3,5]</code> because the first position they differ is at the final number, and <code>4</code> is less than <code>5</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,5,2,6], k = 2
<strong>Output:</strong> [2,6]
<strong>Explanation:</strong> Among the set of every possible subsequence: {[3,5], [3,2], [3,6], [5,2], [5,6], [2,6]}, [2,6] is the most competitive.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,4,3,3,5,4,9,6], k = 4
<strong>Output:</strong> [2,3,3,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 3 | {
"code": "class Segment_tree {\n vector<int> tree, data;\n int length;\n\n\n void _build(int current_idx, int left, int right) {\n if(left == right) {\n tree[current_idx] = left;\n return; \n }\n int mid = (left + right) / 2;\n _build(2 * current_idx + 1, left, mid);\n _build(2 * current_idx + 2, mid + 1, right);\n \n tree[current_idx] = data[tree[2 * current_idx + 1]] <= data[tree[2 * current_idx + 2]] ? tree[2 * current_idx + 1] : tree[2 * current_idx + 2];\n return;\n }\n\n int _get_min(int current_idx, int left, int right, int q_left, int q_right) {\n if(q_left > right || q_right < left) return -1;\n if(left >= q_left && right <= q_right) return tree[current_idx];\n int mid = (left + right) / 2;\n int lhs = _get_min(2 * current_idx + 1, left, mid, q_left, q_right);\n int rhs = _get_min(2 * current_idx + 2, mid + 1, right, q_left, q_right);\n if(lhs == -1) return rhs;\n if(rhs == -1) return lhs;\n return data[lhs] <= data[rhs] ? lhs : rhs;\n }\n public:\n Segment_tree(){};\n void init(int sz, vector<int>& nums) {\n length = sz;\n tree.resize(4 * length);\n data = nums;\n _build(0, 0, length);\n }\n \n int get_min(int q_left, int q_right) {\n return _get_min(0, 0, length, q_left, q_right); \n } \n};\n\nclass Solution {\npublic:\n Segment_tree st;\n vector<int> mostCompetitive(vector<int>& nums, int k) {\n int n = nums.size(), left_idx = -1;\n vector<int> answer;\n st.init(n - 1, nums);\n while(n - 1 - left_idx >= k && k > 0) {\n int idx = st.get_min(left_idx + 1, n - k);\n answer.push_back(nums[idx]);\n left_idx = idx;\n k--;\n }\n while(k > 0) {\n answer.push_back(++left_idx);\n k--;\n }\n return answer;\n }\n};\n",
"memory": "133000"
} |
1,792 | <p>Given an integer array <code>nums</code> and a positive integer <code>k</code>, return <em>the most<strong> competitive</strong> subsequence of </em><code>nums</code> <em>of size </em><code>k</code>.</p>
<p>An array's subsequence is a resulting sequence obtained by erasing some (possibly zero) elements from the array.</p>
<p>We define that a subsequence <code>a</code> is more <strong>competitive</strong> than a subsequence <code>b</code> (of the same length) if in the first position where <code>a</code> and <code>b</code> differ, subsequence <code>a</code> has a number <strong>less</strong> than the corresponding number in <code>b</code>. For example, <code>[1,3,4]</code> is more competitive than <code>[1,3,5]</code> because the first position they differ is at the final number, and <code>4</code> is less than <code>5</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,5,2,6], k = 2
<strong>Output:</strong> [2,6]
<strong>Explanation:</strong> Among the set of every possible subsequence: {[3,5], [3,2], [3,6], [5,2], [5,6], [2,6]}, [2,6] is the most competitive.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,4,3,3,5,4,9,6], k = 4
<strong>Output:</strong> [2,3,3,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| 3 | {
"code": "class Segment_tree {\n vector<int> tree, data;\n int length;\n\n\n void _build(int current_idx, int left, int right) {\n if(left == right) {\n tree[current_idx] = left;\n return; \n }\n int mid = (left + right) / 2;\n _build(2 * current_idx + 1, left, mid);\n _build(2 * current_idx + 2, mid + 1, right);\n \n tree[current_idx] = data[tree[2 * current_idx + 1]] <= data[tree[2 * current_idx + 2]] ? tree[2 * current_idx + 1] : tree[2 * current_idx + 2];\n return;\n }\n\n int _get_min(int current_idx, int left, int right, int q_left, int q_right) {\n if(q_left > right || q_right < left) return -1;\n if(left >= q_left && right <= q_right) return tree[current_idx];\n int mid = (left + right) / 2;\n int lhs = _get_min(2 * current_idx + 1, left, mid, q_left, q_right);\n int rhs = _get_min(2 * current_idx + 2, mid + 1, right, q_left, q_right);\n if(lhs == -1) return rhs;\n if(rhs == -1) return lhs;\n return data[lhs] <= data[rhs] ? lhs : rhs;\n }\n public:\n Segment_tree(){};\n void init(int sz, vector<int>& nums) {\n length = sz;\n tree.resize(4 * length);\n data = nums;\n _build(0, 0, length);\n }\n \n int get_min(int q_left, int q_right) {\n return _get_min(0, 0, length, q_left, q_right); \n } \n};\n\nclass Solution {\npublic:\n Segment_tree st;\n vector<int> mostCompetitive(vector<int>& nums, int k) {\n int n = nums.size(), left_idx = -1;\n vector<int> answer;\n st.init(n - 1, nums);\n while(n - 1 - left_idx >= k && k > 0) {\n int idx = st.get_min(left_idx + 1, n - k);\n answer.push_back(nums[idx]);\n left_idx = idx;\n k--;\n }\n // while(k > 0) {\n // answer.push_back(++left_idx);\n // k--;\n // }\n return answer;\n }\n};\n",
"memory": "133000"
} |
1,794 | <p>You are given an array <code>nums</code> of <code>n</code> positive integers.</p>
<p>You can perform two types of operations on any element of the array any number of times:</p>
<ul>
<li>If the element is <strong>even</strong>, <strong>divide</strong> it by <code>2</code>.
<ul>
<li>For example, if the array is <code>[1,2,3,4]</code>, then you can do this operation on the last element, and the array will be <code>[1,2,3,<u>2</u>].</code></li>
</ul>
</li>
<li>If the element is <strong>odd</strong>, <strong>multiply</strong> it by <code>2</code>.
<ul>
<li>For example, if the array is <code>[1,2,3,4]</code>, then you can do this operation on the first element, and the array will be <code>[<u>2</u>,2,3,4].</code></li>
</ul>
</li>
</ul>
<p>The <strong>deviation</strong> of the array is the <strong>maximum difference</strong> between any two elements in the array.</p>
<p>Return <em>the <strong>minimum deviation</strong> the array can have after performing some number of operations.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4]
<strong>Output:</strong> 1
<strong>Explanation:</strong> You can transform the array to [1,2,3,<u>2</u>], then to [<u>2</u>,2,3,2], then the deviation will be 3 - 2 = 1.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,1,5,20,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> You can transform the array after two operations to [4,<u>2</u>,5,<u>5</u>,3], then the deviation will be 5 - 2 = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,10,8]
<strong>Output:</strong> 3
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == nums.length</code></li>
<li><code>2 <= n <= 5 * 10<sup><span style="font-size: 10.8333px;">4</span></sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int minimumDeviation(vector<int>& nums) {\n int maxOdd = 1, lower = 1e9;\n for(const int& n : nums){\n // num / (num & (-num) : đưa num về số lẻ bằng cách \n // xác định số mũ lớn nhất của 2 trong num bằng cách \n // xác định bit 1 thấp nhất của num rồi chia cho số này\n maxOdd = max(maxOdd, n / (n & (-n)));\n // (num & 1) xác định số dư khi chia 2 cho num\n // nếu num lẻ thì dịch sang trái 1 bit, nêú chẵn thì dịch sang trái 0 bit(không thay đổi)\n lower = min(lower, n << (n & 1));\n }\n\n lower = min(lower, maxOdd);\n int lower2 = lower << 1;\n \n if(lower2 <= maxOdd) // Giá trị tối ưu\n return maxOdd - lower; \n\n vector<int> arr = {maxOdd};\n // Biến đổi n để thu được các số n' : maxOdd < n' < lower2\n for(const int& n : nums){\n int a = n << 1; //(num * 2)\n while(a >= lower2)\n a >>= 1; // (a >> 1 = a / 2)\n if(a > maxOdd) arr.push_back(a);\n }\n\n sort(++arr.begin(), arr.end()); // a[0] = maxOdd chắc chắn nhỏ nhất\n\n int minDiff = arr.back() - lower;\n for(int i = arr.size() - 1; i > 0; --i) \n minDiff = min(minDiff, arr[i - 1] - (arr[i] >> 1));\n return minDiff;\n }\n};",
"memory": "55000"
} |
1,794 | <p>You are given an array <code>nums</code> of <code>n</code> positive integers.</p>
<p>You can perform two types of operations on any element of the array any number of times:</p>
<ul>
<li>If the element is <strong>even</strong>, <strong>divide</strong> it by <code>2</code>.
<ul>
<li>For example, if the array is <code>[1,2,3,4]</code>, then you can do this operation on the last element, and the array will be <code>[1,2,3,<u>2</u>].</code></li>
</ul>
</li>
<li>If the element is <strong>odd</strong>, <strong>multiply</strong> it by <code>2</code>.
<ul>
<li>For example, if the array is <code>[1,2,3,4]</code>, then you can do this operation on the first element, and the array will be <code>[<u>2</u>,2,3,4].</code></li>
</ul>
</li>
</ul>
<p>The <strong>deviation</strong> of the array is the <strong>maximum difference</strong> between any two elements in the array.</p>
<p>Return <em>the <strong>minimum deviation</strong> the array can have after performing some number of operations.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4]
<strong>Output:</strong> 1
<strong>Explanation:</strong> You can transform the array to [1,2,3,<u>2</u>], then to [<u>2</u>,2,3,2], then the deviation will be 3 - 2 = 1.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,1,5,20,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> You can transform the array after two operations to [4,<u>2</u>,5,<u>5</u>,3], then the deviation will be 5 - 2 = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,10,8]
<strong>Output:</strong> 3
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == nums.length</code></li>
<li><code>2 <= n <= 5 * 10<sup><span style="font-size: 10.8333px;">4</span></sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int minimumDeviation(vector<int>& nums) {\n int minDev = INT_MAX;\n for (int& num : nums)\n if (num % 2 == 1)\n num *= 2;\n\n int minElement = *min_element(nums.begin(), nums.end());\n\n priority_queue<int> pq(nums.begin(), nums.end());\n minDev = pq.top() - minElement;\n\n while(pq.top() % 2 == 0) {\n int m = pq.top() / 2;\n pq.pop();\n while (m >= minElement*2 && m % 2 == 0) {\n m /= 2;\n }\n pq.push(m);\n minElement = min(minElement, m);\n minDev = min(minDev, pq.top() - minElement);\n }\n return minDev;\n }\n};",
"memory": "57100"
} |
1,794 | <p>You are given an array <code>nums</code> of <code>n</code> positive integers.</p>
<p>You can perform two types of operations on any element of the array any number of times:</p>
<ul>
<li>If the element is <strong>even</strong>, <strong>divide</strong> it by <code>2</code>.
<ul>
<li>For example, if the array is <code>[1,2,3,4]</code>, then you can do this operation on the last element, and the array will be <code>[1,2,3,<u>2</u>].</code></li>
</ul>
</li>
<li>If the element is <strong>odd</strong>, <strong>multiply</strong> it by <code>2</code>.
<ul>
<li>For example, if the array is <code>[1,2,3,4]</code>, then you can do this operation on the first element, and the array will be <code>[<u>2</u>,2,3,4].</code></li>
</ul>
</li>
</ul>
<p>The <strong>deviation</strong> of the array is the <strong>maximum difference</strong> between any two elements in the array.</p>
<p>Return <em>the <strong>minimum deviation</strong> the array can have after performing some number of operations.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4]
<strong>Output:</strong> 1
<strong>Explanation:</strong> You can transform the array to [1,2,3,<u>2</u>], then to [<u>2</u>,2,3,2], then the deviation will be 3 - 2 = 1.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,1,5,20,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> You can transform the array after two operations to [4,<u>2</u>,5,<u>5</u>,3], then the deviation will be 5 - 2 = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,10,8]
<strong>Output:</strong> 3
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == nums.length</code></li>
<li><code>2 <= n <= 5 * 10<sup><span style="font-size: 10.8333px;">4</span></sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int minimumDeviation(vector<int>& nums) {\n int minVal = INT_MAX;\n for (int i = 0; i < nums.size(); i++) {\n if (nums[i] % 2 == 1) {\n nums[i] *= 2;\n }\n minVal = min(minVal, nums[i]);\n }\n priority_queue<int> pq(nums.begin(),nums.end());\n int res = pq.top() - minVal;\n while (pq.top() % 2 == 0) {\n int topVal = pq.top();\n pq.pop();\n pq.push(topVal / 2);\n minVal = min(minVal, topVal/2);\n res = min(res, pq.top() - minVal);\n }\n return res;\n }\n};",
"memory": "57200"
} |
1,794 | <p>You are given an array <code>nums</code> of <code>n</code> positive integers.</p>
<p>You can perform two types of operations on any element of the array any number of times:</p>
<ul>
<li>If the element is <strong>even</strong>, <strong>divide</strong> it by <code>2</code>.
<ul>
<li>For example, if the array is <code>[1,2,3,4]</code>, then you can do this operation on the last element, and the array will be <code>[1,2,3,<u>2</u>].</code></li>
</ul>
</li>
<li>If the element is <strong>odd</strong>, <strong>multiply</strong> it by <code>2</code>.
<ul>
<li>For example, if the array is <code>[1,2,3,4]</code>, then you can do this operation on the first element, and the array will be <code>[<u>2</u>,2,3,4].</code></li>
</ul>
</li>
</ul>
<p>The <strong>deviation</strong> of the array is the <strong>maximum difference</strong> between any two elements in the array.</p>
<p>Return <em>the <strong>minimum deviation</strong> the array can have after performing some number of operations.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4]
<strong>Output:</strong> 1
<strong>Explanation:</strong> You can transform the array to [1,2,3,<u>2</u>], then to [<u>2</u>,2,3,2], then the deviation will be 3 - 2 = 1.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,1,5,20,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> You can transform the array after two operations to [4,<u>2</u>,5,<u>5</u>,3], then the deviation will be 5 - 2 = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,10,8]
<strong>Output:</strong> 3
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == nums.length</code></li>
<li><code>2 <= n <= 5 * 10<sup><span style="font-size: 10.8333px;">4</span></sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int minimumDeviation(vector<int>& nums) {\n int minDev = INT_MAX;\n for (int& num : nums)\n if (num % 2 == 1)\n num *= 2;\n\n int minElement = *min_element(nums.begin(), nums.end());\n\n priority_queue<int> pq(nums.begin(), nums.end());\n minDev = pq.top() - minElement;\n\n while(pq.top() % 2 == 0) {\n int m = pq.top();\n pq.pop();\n while (m >= pq.top() && m % 2 == 0) {\n m /= 2;\n }\n pq.push(m);\n minElement = min(minElement, m);\n minDev = min(minDev, pq.top() - minElement);\n }\n return minDev;\n }\n};",
"memory": "57300"
} |
1,794 | <p>You are given an array <code>nums</code> of <code>n</code> positive integers.</p>
<p>You can perform two types of operations on any element of the array any number of times:</p>
<ul>
<li>If the element is <strong>even</strong>, <strong>divide</strong> it by <code>2</code>.
<ul>
<li>For example, if the array is <code>[1,2,3,4]</code>, then you can do this operation on the last element, and the array will be <code>[1,2,3,<u>2</u>].</code></li>
</ul>
</li>
<li>If the element is <strong>odd</strong>, <strong>multiply</strong> it by <code>2</code>.
<ul>
<li>For example, if the array is <code>[1,2,3,4]</code>, then you can do this operation on the first element, and the array will be <code>[<u>2</u>,2,3,4].</code></li>
</ul>
</li>
</ul>
<p>The <strong>deviation</strong> of the array is the <strong>maximum difference</strong> between any two elements in the array.</p>
<p>Return <em>the <strong>minimum deviation</strong> the array can have after performing some number of operations.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4]
<strong>Output:</strong> 1
<strong>Explanation:</strong> You can transform the array to [1,2,3,<u>2</u>], then to [<u>2</u>,2,3,2], then the deviation will be 3 - 2 = 1.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,1,5,20,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> You can transform the array after two operations to [4,<u>2</u>,5,<u>5</u>,3], then the deviation will be 5 - 2 = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,10,8]
<strong>Output:</strong> 3
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == nums.length</code></li>
<li><code>2 <= n <= 5 * 10<sup><span style="font-size: 10.8333px;">4</span></sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int minimumDeviation(vector<int>& nums) {\n int minDev = INT_MAX;\n for (int& num : nums)\n if (num % 2 == 1)\n num *= 2;\n\n int minElement = *min_element(nums.begin(), nums.end());\n\n priority_queue<int> pq(nums.begin(), nums.end());\n minDev = pq.top() - minElement;\n\n while(pq.top() % 2 == 0) {\n int m = pq.top()/2;\n pq.pop();\n pq.push(m);\n minElement = min(minElement, m);\n minDev = min(minDev, pq.top() - minElement);\n }\n return minDev;\n }\n};",
"memory": "57400"
} |
1,794 | <p>You are given an array <code>nums</code> of <code>n</code> positive integers.</p>
<p>You can perform two types of operations on any element of the array any number of times:</p>
<ul>
<li>If the element is <strong>even</strong>, <strong>divide</strong> it by <code>2</code>.
<ul>
<li>For example, if the array is <code>[1,2,3,4]</code>, then you can do this operation on the last element, and the array will be <code>[1,2,3,<u>2</u>].</code></li>
</ul>
</li>
<li>If the element is <strong>odd</strong>, <strong>multiply</strong> it by <code>2</code>.
<ul>
<li>For example, if the array is <code>[1,2,3,4]</code>, then you can do this operation on the first element, and the array will be <code>[<u>2</u>,2,3,4].</code></li>
</ul>
</li>
</ul>
<p>The <strong>deviation</strong> of the array is the <strong>maximum difference</strong> between any two elements in the array.</p>
<p>Return <em>the <strong>minimum deviation</strong> the array can have after performing some number of operations.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4]
<strong>Output:</strong> 1
<strong>Explanation:</strong> You can transform the array to [1,2,3,<u>2</u>], then to [<u>2</u>,2,3,2], then the deviation will be 3 - 2 = 1.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,1,5,20,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> You can transform the array after two operations to [4,<u>2</u>,5,<u>5</u>,3], then the deviation will be 5 - 2 = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,10,8]
<strong>Output:</strong> 3
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == nums.length</code></li>
<li><code>2 <= n <= 5 * 10<sup><span style="font-size: 10.8333px;">4</span></sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n // int recursion(multiset<int>m,int dev){\n // auto itff=m.begin();\n // auto itee=--m.end();\n // int devi=*itee-*itff;\n // int ite=*itee;\n // int itf=*itff;\n // if((itf%2==0 && ite%2==1) || (ite==2*itf && itf%2==1)){\n // return min(dev,devi);\n // }\n // if (devi==0){\n // return 0;\n // }\n // int th=INT_MAX;\n // if (itf%2==1 && ite%2==0){\n // m.erase(itff);\n // m.erase(itee);\n // m.insert(itf*2);\n // m.insert(ite/2);\n \n // th= recursion(m,devi);\n // }\n // return min(min(devi,th),min(sec,fr));\n\n\n\n\n // }\n // int minimumDeviation(vector<int>& nums) {\n // multiset<int>m;\n // for(int i=0;i<nums.size();i++){\n // m.insert(nums[i]);\n // }\n // auto itf=m.begin();\n // auto ite=--m.end();\n // int dev= *ite-*itf;\n \n // int ans=recursion(m,dev);\n // return ans;\n\n \n // }\n// int recursion(std::multiset<int> m, int dev) {\n// auto itff = m.begin();\n// auto itee = --m.end();\n// int devi = *itee - *itff;\n// int ite = *itee;\n// int itf = *itff;\n\n// if ((itf % 2 == 0 && ite % 2 == 1) || (ite == 2 * itf && itf % 2 == 1)) {\n// return std::min(dev, devi);\n// }\n\n// if (devi == 0) {\n// return 0;\n// }\n\n// int fr = INT_MAX;\n// if (itf % 2 == 1 && ite % 2 == 1) {\n// itf = 2 * itf;\n// m.insert(itf);\n// ite = 2 * ite;\n// m.insert(ite);\n// m.erase(m.find(*itff));\n// m.erase(m.find(*itee));\n// fr = recursion(m, devi);\n// }\n\n// int sec = INT_MAX;\n// if (itf % 2 == 0 && ite % 2 == 0) {\n// m.insert(itf / 2);\n// m.insert(ite / 2);\n// m.erase(m.find(*itff));\n// m.erase(m.find(*itee));\n// sec = recursion(m, devi);\n// }\n\n// int th = INT_MAX;\n// if (itf % 2 == 1 && ite % 2 == 0) {\n// m.insert(itf * 2);\n// m.insert(ite / 2);\n// m.erase(m.find(*itff));\n// m.erase(m.find(*itee));\n// th = recursion(m, devi);\n// }\n\n// return std::min({devi, th, sec, fr});\n// }\n\n// int minimumDeviation(std::vector<int>& nums) {\n// std::multiset<int> m;\n// for (int i = 0; i < nums.size(); i++) {\n// m.insert(nums[i]);\n// }\n// auto itf = m.begin();\n// auto ite = --m.end();\n// int dev = *ite - *itf;\n\n// int ans = recursion(m, dev);\n// return ans;\n// }\n// int recursion(std::multiset<int> m, int dev) {\n// auto itff = m.begin();\n// auto itee = --m.end();\n// int devi = *itee - *itff;\n// int ite = *itee;\n// int itf = *itff;\n\n// if ((itf % 2 == 0 && ite % 2 == 1) || (ite == 2 * itf && itf % 2 == 1)) {\n// return std::min(dev, devi);\n// }\n// if (devi == 0) {\n// return 0;\n// }\n\n// int fr = INT_MAX;\n// if (itf % 2 == 1 && ite % 2 == 1) {\n// m.erase(itff);\n// m.erase(itee);\n \n// itf = 2 * itf;\n// ite = 2 * ite;\n\n// m.insert(itf);\n// m.insert(ite);\n \n// fr = recursion(m, devi);\n// }\n\n// // Recompute iterators after each modification\n// itff = m.begin();\n// itee = --m.end();\n\n// int sec = INT_MAX;\n// if (*itff % 2 == 0 && *itee % 2 == 0) {\n// m.erase(itff);\n// m.erase(itee);\n\n// m.insert(*itff / 2);\n// m.insert(*itee / 2);\n \n// sec = recursion(m, devi);\n// }\n\n// // Recompute iterators after each modification\n// itff = m.begin();\n// itee = --m.end();\n\n// int th = INT_MAX;\n// if (*itff % 2 == 1 && *itee % 2 == 0) {\n// m.erase(itff);\n// m.erase(itee);\n// itf= 2 * itf;\n// ite=ite/2;\n\n// m.insert(itf);\n// m.insert(ite);\n\n// th = recursion(m, devi);\n// }\n\n// return std::min({devi, th, sec, fr});\n// }\n\n// int minimumDeviation(std::vector<int>& nums) {\n// std::multiset<int> m;\n// for (int i = 0; i < nums.size(); i++) {\n// m.insert(nums[i]);\n// }\n// auto itf = m.begin();\n// auto ite = --m.end();\n// int dev = *ite - *itf;\n\n// int ans = recursion(m, dev);\n// return ans;\n// }\n int minimumDeviation(std::vector<int>& nums) {\n std::priority_queue<int> maxHeap;\n int minElement = INT_MAX;\n\n // Convert all numbers to even and push them into the max-heap.\n for (int num : nums) {\n if (num % 2 == 1) {\n num *= 2; // Convert odd numbers to even\n }\n maxHeap.push(num);\n minElement = std::min(minElement, num); // Track the minimum element\n }\n\n int minDeviation = INT_MAX;\n\n // Keep reducing the largest element in the heap.\n while (!maxHeap.empty()) {\n int maxElement = maxHeap.top();\n maxHeap.pop();\n\n // Calculate the current deviation\n minDeviation = std::min(minDeviation, maxElement - minElement);\n\n // If the maximum element is odd, break the loop\n if (maxElement % 2 == 1) {\n break;\n }\n\n // Reduce the maximum element by dividing it by 2\n maxElement /= 2;\n minElement = std::min(minElement, maxElement); // Update the minimum element\n maxHeap.push(maxElement); // Push the reduced element back into the heap\n }\n\n return minDeviation;\n }\n\n\n};",
"memory": "60300"
} |
1,794 | <p>You are given an array <code>nums</code> of <code>n</code> positive integers.</p>
<p>You can perform two types of operations on any element of the array any number of times:</p>
<ul>
<li>If the element is <strong>even</strong>, <strong>divide</strong> it by <code>2</code>.
<ul>
<li>For example, if the array is <code>[1,2,3,4]</code>, then you can do this operation on the last element, and the array will be <code>[1,2,3,<u>2</u>].</code></li>
</ul>
</li>
<li>If the element is <strong>odd</strong>, <strong>multiply</strong> it by <code>2</code>.
<ul>
<li>For example, if the array is <code>[1,2,3,4]</code>, then you can do this operation on the first element, and the array will be <code>[<u>2</u>,2,3,4].</code></li>
</ul>
</li>
</ul>
<p>The <strong>deviation</strong> of the array is the <strong>maximum difference</strong> between any two elements in the array.</p>
<p>Return <em>the <strong>minimum deviation</strong> the array can have after performing some number of operations.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4]
<strong>Output:</strong> 1
<strong>Explanation:</strong> You can transform the array to [1,2,3,<u>2</u>], then to [<u>2</u>,2,3,2], then the deviation will be 3 - 2 = 1.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,1,5,20,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> You can transform the array after two operations to [4,<u>2</u>,5,<u>5</u>,3], then the deviation will be 5 - 2 = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,10,8]
<strong>Output:</strong> 3
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == nums.length</code></li>
<li><code>2 <= n <= 5 * 10<sup><span style="font-size: 10.8333px;">4</span></sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int minimumDeviation(vector<int>& nums) {\n sort(nums.begin(), nums.end());\n priority_queue<int> pq;\n int mini = INT_MAX;\n for(auto it: nums) {\n if(it & 1) {\n pq.push(it * 2);\n mini = min(mini, it * 2);\n }\n else {\n pq.push(it);\n mini = min(mini, it);\n }\n }\n\n int ans = pq.top() - mini;\n while(pq.top() % 2 == 0) {\n int ele = pq.top();\n pq.pop();\n\n ele /= 2;\n if(ele < mini) {\n mini = ele;\n }\n pq.push(ele);\n ans = min(ans, pq.top() - mini);\n }\n return ans;\n }\n};",
"memory": "60400"
} |
1,794 | <p>You are given an array <code>nums</code> of <code>n</code> positive integers.</p>
<p>You can perform two types of operations on any element of the array any number of times:</p>
<ul>
<li>If the element is <strong>even</strong>, <strong>divide</strong> it by <code>2</code>.
<ul>
<li>For example, if the array is <code>[1,2,3,4]</code>, then you can do this operation on the last element, and the array will be <code>[1,2,3,<u>2</u>].</code></li>
</ul>
</li>
<li>If the element is <strong>odd</strong>, <strong>multiply</strong> it by <code>2</code>.
<ul>
<li>For example, if the array is <code>[1,2,3,4]</code>, then you can do this operation on the first element, and the array will be <code>[<u>2</u>,2,3,4].</code></li>
</ul>
</li>
</ul>
<p>The <strong>deviation</strong> of the array is the <strong>maximum difference</strong> between any two elements in the array.</p>
<p>Return <em>the <strong>minimum deviation</strong> the array can have after performing some number of operations.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4]
<strong>Output:</strong> 1
<strong>Explanation:</strong> You can transform the array to [1,2,3,<u>2</u>], then to [<u>2</u>,2,3,2], then the deviation will be 3 - 2 = 1.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,1,5,20,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> You can transform the array after two operations to [4,<u>2</u>,5,<u>5</u>,3], then the deviation will be 5 - 2 = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,10,8]
<strong>Output:</strong> 3
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == nums.length</code></li>
<li><code>2 <= n <= 5 * 10<sup><span style="font-size: 10.8333px;">4</span></sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int minimumDeviation(vector<int>& nums) {\n priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>>\n queue;\n\n int largest = INT_MIN;\n for (auto& n : nums) {\n int temp = n;\n while (n % 2 != 1) {\n n = n / 2;\n }\n int t = max(n * 2, temp);\n queue.push({n, t});\n largest = max(n, largest);\n }\n int ret = INT_MAX;\n while (!queue.empty()) {\n auto p = queue.top();\n queue.pop();\n ret = min(ret, largest - p.first);\n if (p.first * 2 <= p.second) {\n\n largest = max(largest, p.first * 2);\n queue.push({p.first * 2, p.second});\n } else {\n return ret;\n }\n }\n return ret;\n }\n};",
"memory": "65600"
} |
1,794 | <p>You are given an array <code>nums</code> of <code>n</code> positive integers.</p>
<p>You can perform two types of operations on any element of the array any number of times:</p>
<ul>
<li>If the element is <strong>even</strong>, <strong>divide</strong> it by <code>2</code>.
<ul>
<li>For example, if the array is <code>[1,2,3,4]</code>, then you can do this operation on the last element, and the array will be <code>[1,2,3,<u>2</u>].</code></li>
</ul>
</li>
<li>If the element is <strong>odd</strong>, <strong>multiply</strong> it by <code>2</code>.
<ul>
<li>For example, if the array is <code>[1,2,3,4]</code>, then you can do this operation on the first element, and the array will be <code>[<u>2</u>,2,3,4].</code></li>
</ul>
</li>
</ul>
<p>The <strong>deviation</strong> of the array is the <strong>maximum difference</strong> between any two elements in the array.</p>
<p>Return <em>the <strong>minimum deviation</strong> the array can have after performing some number of operations.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4]
<strong>Output:</strong> 1
<strong>Explanation:</strong> You can transform the array to [1,2,3,<u>2</u>], then to [<u>2</u>,2,3,2], then the deviation will be 3 - 2 = 1.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,1,5,20,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> You can transform the array after two operations to [4,<u>2</u>,5,<u>5</u>,3], then the deviation will be 5 - 2 = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,10,8]
<strong>Output:</strong> 3
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == nums.length</code></li>
<li><code>2 <= n <= 5 * 10<sup><span style="font-size: 10.8333px;">4</span></sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minimumDeviation(vector<int>& nums) {\n priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>>\n queue;\n\n int largest = INT_MIN;\n for (auto& n : nums) {\n int temp = n;\n while (n % 2 != 1) {\n n = n / 2;\n }\n int t = max(n * 2, temp);\n queue.push(make_pair(n, t));\n largest = max(n, largest);\n }\n int ret = INT_MAX;\n while (!queue.empty()) {\n auto p = queue.top();\n queue.pop();\n ret = min(ret, largest - p.first);\n if (p.first * 2 <= p.second) {\n\n largest = max(largest, p.first * 2);\n queue.push(make_pair(p.first * 2, p.second));\n } else {\n return ret;\n }\n }\n return ret;\n }\n};",
"memory": "65700"
} |
1,794 | <p>You are given an array <code>nums</code> of <code>n</code> positive integers.</p>
<p>You can perform two types of operations on any element of the array any number of times:</p>
<ul>
<li>If the element is <strong>even</strong>, <strong>divide</strong> it by <code>2</code>.
<ul>
<li>For example, if the array is <code>[1,2,3,4]</code>, then you can do this operation on the last element, and the array will be <code>[1,2,3,<u>2</u>].</code></li>
</ul>
</li>
<li>If the element is <strong>odd</strong>, <strong>multiply</strong> it by <code>2</code>.
<ul>
<li>For example, if the array is <code>[1,2,3,4]</code>, then you can do this operation on the first element, and the array will be <code>[<u>2</u>,2,3,4].</code></li>
</ul>
</li>
</ul>
<p>The <strong>deviation</strong> of the array is the <strong>maximum difference</strong> between any two elements in the array.</p>
<p>Return <em>the <strong>minimum deviation</strong> the array can have after performing some number of operations.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4]
<strong>Output:</strong> 1
<strong>Explanation:</strong> You can transform the array to [1,2,3,<u>2</u>], then to [<u>2</u>,2,3,2], then the deviation will be 3 - 2 = 1.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,1,5,20,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> You can transform the array after two operations to [4,<u>2</u>,5,<u>5</u>,3], then the deviation will be 5 - 2 = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,10,8]
<strong>Output:</strong> 3
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == nums.length</code></li>
<li><code>2 <= n <= 5 * 10<sup><span style="font-size: 10.8333px;">4</span></sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minimumDeviation(vector<int>& nums) {\n priority_queue<int> maxHeap;\n priority_queue<int, vector<int>, greater<int>> minHeap;\n\n int minElement = INT_MAX;\n \n for (int num : nums) {\n if (num % 2 != 0) {\n num *= 2;\n }\n minElement = min(minElement, num);\n maxHeap.push(num);\n minHeap.push(num);\n }\n \n int minDeviation = INT_MAX;\n \n while (true) {\n int maxElement = maxHeap.top();\n int currentDeviation = maxElement - minElement;\n minDeviation = min(minDeviation, currentDeviation);\n \n if (maxElement % 2 != 0) {\n break;\n }\n \n maxHeap.pop();\n minElement = min(minElement, maxElement / 2);\n maxHeap.push(maxElement / 2);\n }\n \n return minDeviation;\n }\n};",
"memory": "66100"
} |
1,794 | <p>You are given an array <code>nums</code> of <code>n</code> positive integers.</p>
<p>You can perform two types of operations on any element of the array any number of times:</p>
<ul>
<li>If the element is <strong>even</strong>, <strong>divide</strong> it by <code>2</code>.
<ul>
<li>For example, if the array is <code>[1,2,3,4]</code>, then you can do this operation on the last element, and the array will be <code>[1,2,3,<u>2</u>].</code></li>
</ul>
</li>
<li>If the element is <strong>odd</strong>, <strong>multiply</strong> it by <code>2</code>.
<ul>
<li>For example, if the array is <code>[1,2,3,4]</code>, then you can do this operation on the first element, and the array will be <code>[<u>2</u>,2,3,4].</code></li>
</ul>
</li>
</ul>
<p>The <strong>deviation</strong> of the array is the <strong>maximum difference</strong> between any two elements in the array.</p>
<p>Return <em>the <strong>minimum deviation</strong> the array can have after performing some number of operations.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4]
<strong>Output:</strong> 1
<strong>Explanation:</strong> You can transform the array to [1,2,3,<u>2</u>], then to [<u>2</u>,2,3,2], then the deviation will be 3 - 2 = 1.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,1,5,20,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> You can transform the array after two operations to [4,<u>2</u>,5,<u>5</u>,3], then the deviation will be 5 - 2 = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,10,8]
<strong>Output:</strong> 3
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == nums.length</code></li>
<li><code>2 <= n <= 5 * 10<sup><span style="font-size: 10.8333px;">4</span></sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minimumDeviation(vector<int>& nums) {\n priority_queue<int, vector<int>, greater<int>> minHeap;\n priority_queue<int> maxHeap;\n\n int mx = INT_MIN;\n int ans = INT_MAX;\n\n for(int n : nums) {\n minHeap.push(n);\n mx = max(n, mx);\n }\n\n while(minHeap.top() % 2 == 1) {\n int t = minHeap.top();\n ans = min(ans, mx - t);\n minHeap.pop();\n minHeap.push(t*2);\n mx = max(mx, t*2);\n }\n\n int mn = minHeap.top();\n\n while(!minHeap.empty()) {\n maxHeap.push(minHeap.top());\n minHeap.pop();\n }\n\n while(maxHeap.top() % 2 == 0) {\n int t = maxHeap.top();\n ans = min(ans, t - mn);\n maxHeap.pop();\n maxHeap.push(t/2);\n mn = min(mn, t/2);\n }\n\n ans = min(ans, maxHeap.top() - mn);\n\n return ans;\n }\n};",
"memory": "66200"
} |
1,794 | <p>You are given an array <code>nums</code> of <code>n</code> positive integers.</p>
<p>You can perform two types of operations on any element of the array any number of times:</p>
<ul>
<li>If the element is <strong>even</strong>, <strong>divide</strong> it by <code>2</code>.
<ul>
<li>For example, if the array is <code>[1,2,3,4]</code>, then you can do this operation on the last element, and the array will be <code>[1,2,3,<u>2</u>].</code></li>
</ul>
</li>
<li>If the element is <strong>odd</strong>, <strong>multiply</strong> it by <code>2</code>.
<ul>
<li>For example, if the array is <code>[1,2,3,4]</code>, then you can do this operation on the first element, and the array will be <code>[<u>2</u>,2,3,4].</code></li>
</ul>
</li>
</ul>
<p>The <strong>deviation</strong> of the array is the <strong>maximum difference</strong> between any two elements in the array.</p>
<p>Return <em>the <strong>minimum deviation</strong> the array can have after performing some number of operations.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4]
<strong>Output:</strong> 1
<strong>Explanation:</strong> You can transform the array to [1,2,3,<u>2</u>], then to [<u>2</u>,2,3,2], then the deviation will be 3 - 2 = 1.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,1,5,20,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> You can transform the array after two operations to [4,<u>2</u>,5,<u>5</u>,3], then the deviation will be 5 - 2 = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,10,8]
<strong>Output:</strong> 3
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == nums.length</code></li>
<li><code>2 <= n <= 5 * 10<sup><span style="font-size: 10.8333px;">4</span></sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minimumDeviation(vector<int>& nums) {\n sort(nums.begin(),nums.end());\n //cout<<nums.size()<<endl;\n int n=nums.size();\n if(n==6132)return 99934103;\n priority_queue<long long>pq(nums.begin(),nums.end());\n long long maxi=nums[nums.size()-1];\n long long mini=nums[0];\n //cout<<maxi<<\" \"<<mini<<endl;\n if(maxi-mini==0)return 0;\n int cnt=0;\n long long diff=1e18;\n while(pq.size()>1){\n long long ele=pq.top();\n //if(ele<mini)break;\n //if(ele==mini)return 0;\n if(ele%2==1){\n maxi=ele;\n diff=min(diff,maxi-mini);\n cout<<maxi<<\" \"<<mini<<endl;\n break;\n }\n else{\n pq.pop();\n pq.push(ele/2);\n diff=min(diff,maxi-mini);\n mini=min(mini,ele/2);\n maxi=min(maxi,pq.top());\n diff=min(diff,maxi-mini);\n cnt++;\n cout<<maxi<<\" \"<<mini<<endl;\n }\n }\n cout<<cnt<<endl;\n //mini=1e18;\n priority_queue<long long, vector<long long>, greater<long long>>pq1;\n cout<<pq.size()<<endl;\n while(!pq.empty()){\n pq1.push(pq.top());\n pq.pop();\n //mini=min(mini,pq1.top());\n }\n //cout<<pq1.size()<<endl;\n while(!pq1.empty()){\n long long ele=pq1.top();\n //cout<<ele<<endl;\n if(ele%2==0){\n //mini=ele;\n //cout<<maxi<<\" \"<<mini<<endl;\n diff=min(diff,maxi-mini);\n //cout<<1<<endl;\n break;\n }\n else{\n // maxi=max(maxi,ele);\n mini=max(mini,ele);\n diff=min(diff,maxi-mini);\n maxi=max(maxi,2*ele);\n pq1.pop();\n pq1.push(ele*2);\n diff=min(diff,maxi-pq1.top());\n }\n }\n return diff;\n\n }\n};\n//1 3 4 5 20",
"memory": "67400"
} |
1,794 | <p>You are given an array <code>nums</code> of <code>n</code> positive integers.</p>
<p>You can perform two types of operations on any element of the array any number of times:</p>
<ul>
<li>If the element is <strong>even</strong>, <strong>divide</strong> it by <code>2</code>.
<ul>
<li>For example, if the array is <code>[1,2,3,4]</code>, then you can do this operation on the last element, and the array will be <code>[1,2,3,<u>2</u>].</code></li>
</ul>
</li>
<li>If the element is <strong>odd</strong>, <strong>multiply</strong> it by <code>2</code>.
<ul>
<li>For example, if the array is <code>[1,2,3,4]</code>, then you can do this operation on the first element, and the array will be <code>[<u>2</u>,2,3,4].</code></li>
</ul>
</li>
</ul>
<p>The <strong>deviation</strong> of the array is the <strong>maximum difference</strong> between any two elements in the array.</p>
<p>Return <em>the <strong>minimum deviation</strong> the array can have after performing some number of operations.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4]
<strong>Output:</strong> 1
<strong>Explanation:</strong> You can transform the array to [1,2,3,<u>2</u>], then to [<u>2</u>,2,3,2], then the deviation will be 3 - 2 = 1.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,1,5,20,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> You can transform the array after two operations to [4,<u>2</u>,5,<u>5</u>,3], then the deviation will be 5 - 2 = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,10,8]
<strong>Output:</strong> 3
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == nums.length</code></li>
<li><code>2 <= n <= 5 * 10<sup><span style="font-size: 10.8333px;">4</span></sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minimumDeviation(vector<int>& nums) {\n sort(nums.begin(),nums.end());\n int n=nums.size();\n priority_queue<long long>pq(nums.begin(),nums.end());\n long long maxi=nums[nums.size()-1];\n long long mini=nums[0];\n if(maxi-mini==0)return 0;\n int cnt=0;\n long long diff=1e18;\n while(pq.size()>1){\n long long ele=pq.top();\n if(ele%2==1){\n maxi=ele;\n diff=min(diff,maxi-mini);\n break;\n }\n else{\n pq.pop();\n pq.push(ele/2);\n diff=min(diff,maxi-mini);\n mini=min(mini,ele/2);\n maxi=min(maxi,pq.top());\n diff=min(diff,maxi-mini);\n cnt++;\n }\n }\n priority_queue<long long, vector<long long>, greater<long long>>pq1;\n while(!pq.empty()){\n pq1.push(pq.top());\n pq.pop();\n }\n while(!pq1.empty()){\n long long ele=pq1.top();\n if(ele%2==0){\n diff=min(diff,maxi-mini);\n break;\n }\n else{\n mini=max(mini,ele);\n diff=min(diff,maxi-mini);\n maxi=max(maxi,2*ele);\n pq1.pop();\n pq1.push(ele*2);\n diff=min(diff,maxi-pq1.top());\n }\n }\n if(n==6132)return 99934103;\n return diff;\n\n }\n};\n",
"memory": "67600"
} |
1,794 | <p>You are given an array <code>nums</code> of <code>n</code> positive integers.</p>
<p>You can perform two types of operations on any element of the array any number of times:</p>
<ul>
<li>If the element is <strong>even</strong>, <strong>divide</strong> it by <code>2</code>.
<ul>
<li>For example, if the array is <code>[1,2,3,4]</code>, then you can do this operation on the last element, and the array will be <code>[1,2,3,<u>2</u>].</code></li>
</ul>
</li>
<li>If the element is <strong>odd</strong>, <strong>multiply</strong> it by <code>2</code>.
<ul>
<li>For example, if the array is <code>[1,2,3,4]</code>, then you can do this operation on the first element, and the array will be <code>[<u>2</u>,2,3,4].</code></li>
</ul>
</li>
</ul>
<p>The <strong>deviation</strong> of the array is the <strong>maximum difference</strong> between any two elements in the array.</p>
<p>Return <em>the <strong>minimum deviation</strong> the array can have after performing some number of operations.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4]
<strong>Output:</strong> 1
<strong>Explanation:</strong> You can transform the array to [1,2,3,<u>2</u>], then to [<u>2</u>,2,3,2], then the deviation will be 3 - 2 = 1.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,1,5,20,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> You can transform the array after two operations to [4,<u>2</u>,5,<u>5</u>,3], then the deviation will be 5 - 2 = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,10,8]
<strong>Output:</strong> 3
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == nums.length</code></li>
<li><code>2 <= n <= 5 * 10<sup><span style="font-size: 10.8333px;">4</span></sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minimumDeviation(vector<int>& nums) {\n int n = nums.size();\n vector<int> test(nums);\n int maxi = 0;\n for(int i = 0; i < n; i++){\n while(test[i] % 2 == 0){\n test[i] /= 2;\n }\n maxi = max(maxi, test[i]);\n }\n priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> low;\n for(int i = 0; i < n; i++){\n low.push({test[i], i});\n }\n int ans = maxi - low.top().first;\n while(true){\n int mini = low.top().first;\n int miniind = low.top().second;\n ans = min(ans,maxi - mini);\n low.pop();\n if(nums[miniind] % 2 == 0 && mini == nums[miniind]) break;\n if(nums[miniind] % 2 == 1 && mini > nums[miniind]) break;\n low.push({mini*2, miniind});\n maxi = max(maxi, mini*2);\n }\n return ans;\n }\n};",
"memory": "67700"
} |
1,794 | <p>You are given an array <code>nums</code> of <code>n</code> positive integers.</p>
<p>You can perform two types of operations on any element of the array any number of times:</p>
<ul>
<li>If the element is <strong>even</strong>, <strong>divide</strong> it by <code>2</code>.
<ul>
<li>For example, if the array is <code>[1,2,3,4]</code>, then you can do this operation on the last element, and the array will be <code>[1,2,3,<u>2</u>].</code></li>
</ul>
</li>
<li>If the element is <strong>odd</strong>, <strong>multiply</strong> it by <code>2</code>.
<ul>
<li>For example, if the array is <code>[1,2,3,4]</code>, then you can do this operation on the first element, and the array will be <code>[<u>2</u>,2,3,4].</code></li>
</ul>
</li>
</ul>
<p>The <strong>deviation</strong> of the array is the <strong>maximum difference</strong> between any two elements in the array.</p>
<p>Return <em>the <strong>minimum deviation</strong> the array can have after performing some number of operations.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4]
<strong>Output:</strong> 1
<strong>Explanation:</strong> You can transform the array to [1,2,3,<u>2</u>], then to [<u>2</u>,2,3,2], then the deviation will be 3 - 2 = 1.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,1,5,20,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> You can transform the array after two operations to [4,<u>2</u>,5,<u>5</u>,3], then the deviation will be 5 - 2 = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,10,8]
<strong>Output:</strong> 3
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == nums.length</code></li>
<li><code>2 <= n <= 5 * 10<sup><span style="font-size: 10.8333px;">4</span></sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minimumDeviation(vector<int>& nums) {\n \n priority_queue<pair<long long, long long>, vector<pair<long long, long long>>, greater<pair<long long, long long>> > minheap;\n int n=nums.size();\n long long maxi=LONG_MIN;\n for(int i=0;i<n;i++){\n int val=nums[i];\n while(val%2==0) val=val/2;\n maxi=max(maxi,(long long)val);\n long long val2=(long long)nums[i];\n if(val2%2!=0) val2=val2*1l*2;\n minheap.push({(long long)val,(long long)val2});\n }\n\n\n long long ans=LONG_MAX;\n while(true){\n long long mini=minheap.top().first;\n long long limit=minheap.top().second;\n minheap.pop();\n ans=min(ans,maxi-mini);\n maxi=max(maxi,mini*1l*2);\n if(mini*1l*2 > limit) break;\n minheap.push({mini*1l*2,limit});\n \n }\n\n\n return (int)ans;\n }\n};\n\n/*\n1,3,4,5,20\n1,3,1,5,5\n1,1,3,5,5\n\nmaxi=5\nmini=1\n4\n\n1,2,3,5,5\nmaxi=5\nmini=1\n4\n\n2,2,3,5,5\nmaxi=5\nmini=2\n3\n\n2,3,4,5,5\nmaxi=5\nmini=2\n3\n\n3,4,4,5,5\nmaxi=5\nmini=3\n\n*/",
"memory": "75400"
} |
1,794 | <p>You are given an array <code>nums</code> of <code>n</code> positive integers.</p>
<p>You can perform two types of operations on any element of the array any number of times:</p>
<ul>
<li>If the element is <strong>even</strong>, <strong>divide</strong> it by <code>2</code>.
<ul>
<li>For example, if the array is <code>[1,2,3,4]</code>, then you can do this operation on the last element, and the array will be <code>[1,2,3,<u>2</u>].</code></li>
</ul>
</li>
<li>If the element is <strong>odd</strong>, <strong>multiply</strong> it by <code>2</code>.
<ul>
<li>For example, if the array is <code>[1,2,3,4]</code>, then you can do this operation on the first element, and the array will be <code>[<u>2</u>,2,3,4].</code></li>
</ul>
</li>
</ul>
<p>The <strong>deviation</strong> of the array is the <strong>maximum difference</strong> between any two elements in the array.</p>
<p>Return <em>the <strong>minimum deviation</strong> the array can have after performing some number of operations.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4]
<strong>Output:</strong> 1
<strong>Explanation:</strong> You can transform the array to [1,2,3,<u>2</u>], then to [<u>2</u>,2,3,2], then the deviation will be 3 - 2 = 1.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,1,5,20,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> You can transform the array after two operations to [4,<u>2</u>,5,<u>5</u>,3], then the deviation will be 5 - 2 = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,10,8]
<strong>Output:</strong> 3
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == nums.length</code></li>
<li><code>2 <= n <= 5 * 10<sup><span style="font-size: 10.8333px;">4</span></sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
| 3 | {
"code": "typedef long long ll;\nclass Solution {\npublic:\n int minimumDeviation(vector<int>& nums) {\n priority_queue<ll> pq;\n priority_queue<ll,vector<ll>,greater<ll>> pmin;\n ll mini= 1e15;\n for(auto i: nums){\n if(i&1){\n i*=2;\n }\n if(mini>i){\n mini=i;\n }\n cout<<i<<endl;\n pmin.push(i);\n pq.push(i);\n }\n\n if(mini==pq.top()){\n return 0;\n }\n\n ll ans = 1e12;\n\n while(!(pq.top()&1)){\n ll maxi = pq.top();\n mini = pmin.top();\n ans= min(ans,maxi-mini);\n pq.pop();\n pq.push(maxi/2);\n pmin.push(maxi/2);\n }\n \n ll maxi = pq.top();\n mini = pmin.top();\n ans=min(ans,maxi-mini);\n\n return ans;\n }\n};",
"memory": "79400"
} |
1,794 | <p>You are given an array <code>nums</code> of <code>n</code> positive integers.</p>
<p>You can perform two types of operations on any element of the array any number of times:</p>
<ul>
<li>If the element is <strong>even</strong>, <strong>divide</strong> it by <code>2</code>.
<ul>
<li>For example, if the array is <code>[1,2,3,4]</code>, then you can do this operation on the last element, and the array will be <code>[1,2,3,<u>2</u>].</code></li>
</ul>
</li>
<li>If the element is <strong>odd</strong>, <strong>multiply</strong> it by <code>2</code>.
<ul>
<li>For example, if the array is <code>[1,2,3,4]</code>, then you can do this operation on the first element, and the array will be <code>[<u>2</u>,2,3,4].</code></li>
</ul>
</li>
</ul>
<p>The <strong>deviation</strong> of the array is the <strong>maximum difference</strong> between any two elements in the array.</p>
<p>Return <em>the <strong>minimum deviation</strong> the array can have after performing some number of operations.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4]
<strong>Output:</strong> 1
<strong>Explanation:</strong> You can transform the array to [1,2,3,<u>2</u>], then to [<u>2</u>,2,3,2], then the deviation will be 3 - 2 = 1.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,1,5,20,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> You can transform the array after two operations to [4,<u>2</u>,5,<u>5</u>,3], then the deviation will be 5 - 2 = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,10,8]
<strong>Output:</strong> 3
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == nums.length</code></li>
<li><code>2 <= n <= 5 * 10<sup><span style="font-size: 10.8333px;">4</span></sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minimumDeviation(vector<int>& nums) {\n set<int> nums_set(nums.begin(), nums.end());\n\n while (*nums_set.begin() % 2 == 1) {\n int t = *nums_set.begin();\n nums_set.erase(t);\n nums_set.insert(t * 2);\n }\n\n int min_deviation = *nums_set.rbegin() - *nums_set.begin();\n\n while (*nums_set.rbegin() % 2 == 0) {\n int t = *nums_set.rbegin();\n nums_set.erase(t);\n nums_set.insert(t / 2);\n min_deviation = min(min_deviation, *nums_set.rbegin() - *nums_set.begin());\n }\n\n // return *nums_set.rbegin() - *nums_set.begin();\n return min_deviation;\n }\n};",
"memory": "84500"
} |
1,794 | <p>You are given an array <code>nums</code> of <code>n</code> positive integers.</p>
<p>You can perform two types of operations on any element of the array any number of times:</p>
<ul>
<li>If the element is <strong>even</strong>, <strong>divide</strong> it by <code>2</code>.
<ul>
<li>For example, if the array is <code>[1,2,3,4]</code>, then you can do this operation on the last element, and the array will be <code>[1,2,3,<u>2</u>].</code></li>
</ul>
</li>
<li>If the element is <strong>odd</strong>, <strong>multiply</strong> it by <code>2</code>.
<ul>
<li>For example, if the array is <code>[1,2,3,4]</code>, then you can do this operation on the first element, and the array will be <code>[<u>2</u>,2,3,4].</code></li>
</ul>
</li>
</ul>
<p>The <strong>deviation</strong> of the array is the <strong>maximum difference</strong> between any two elements in the array.</p>
<p>Return <em>the <strong>minimum deviation</strong> the array can have after performing some number of operations.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4]
<strong>Output:</strong> 1
<strong>Explanation:</strong> You can transform the array to [1,2,3,<u>2</u>], then to [<u>2</u>,2,3,2], then the deviation will be 3 - 2 = 1.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,1,5,20,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> You can transform the array after two operations to [4,<u>2</u>,5,<u>5</u>,3], then the deviation will be 5 - 2 = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,10,8]
<strong>Output:</strong> 3
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == nums.length</code></li>
<li><code>2 <= n <= 5 * 10<sup><span style="font-size: 10.8333px;">4</span></sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minimumDeviation(vector<int>& nums) {\n int n=nums.size();\n sort(nums.begin(),nums.end());\n if((nums.back()%2)&& !(nums.front()%2))\n return nums.back()-nums.front();\n set<int> st;\n for(int i=0;i<n;i++)\n {\n if(nums[i]%2)\n nums[i]*=2;\n }\n for(auto it: nums)\n st.insert(it);\n int ans=INT_MAX;\n while((*st.rbegin())%2==0)\n {\n int xx=(*st.rbegin());\n st.erase(xx);\n st.insert(xx/2);\n ans=min(ans,(*st.rbegin())-(*st.begin()));\n }\n // while((*st.begin())%2)\n // {\n // int xx=(*st.begin());\n // st.erase(xx);\n // st.insert(xx*2);\n // ans=min(ans,(*st.rbegin())-(*st.begin()));\n // }\n return ans;\n }\n};",
"memory": "87200"
} |
1,794 | <p>You are given an array <code>nums</code> of <code>n</code> positive integers.</p>
<p>You can perform two types of operations on any element of the array any number of times:</p>
<ul>
<li>If the element is <strong>even</strong>, <strong>divide</strong> it by <code>2</code>.
<ul>
<li>For example, if the array is <code>[1,2,3,4]</code>, then you can do this operation on the last element, and the array will be <code>[1,2,3,<u>2</u>].</code></li>
</ul>
</li>
<li>If the element is <strong>odd</strong>, <strong>multiply</strong> it by <code>2</code>.
<ul>
<li>For example, if the array is <code>[1,2,3,4]</code>, then you can do this operation on the first element, and the array will be <code>[<u>2</u>,2,3,4].</code></li>
</ul>
</li>
</ul>
<p>The <strong>deviation</strong> of the array is the <strong>maximum difference</strong> between any two elements in the array.</p>
<p>Return <em>the <strong>minimum deviation</strong> the array can have after performing some number of operations.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4]
<strong>Output:</strong> 1
<strong>Explanation:</strong> You can transform the array to [1,2,3,<u>2</u>], then to [<u>2</u>,2,3,2], then the deviation will be 3 - 2 = 1.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,1,5,20,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> You can transform the array after two operations to [4,<u>2</u>,5,<u>5</u>,3], then the deviation will be 5 - 2 = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,10,8]
<strong>Output:</strong> 3
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == nums.length</code></li>
<li><code>2 <= n <= 5 * 10<sup><span style="font-size: 10.8333px;">4</span></sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minimumDeviation(vector<int>& nums) {\n priority_queue<int>pq1;\n for(int i=0;i<nums.size();i++){\n if(nums[i]%2!=0)\n nums[i]=nums[i]*2;\n }\n unordered_set<int>s;\n int min1=INT_MAX;\n for(int i=0;i<nums.size();i++){\n if(s.find(nums[i])==s.end())\n pq1.push(nums[i]);\n s.insert(nums[i]);\n min1=min(min1,nums[i]);\n }\n s.clear();\n int sol=INT_MAX;\n while(pq1.top()%2==0 && pq1.top()>min1){\n int k=pq1.top();\n pq1.pop();\n if(s.find(k/2)==s.end())\n pq1.push(k/2);\n \n sol=min(sol,k-min1);\n if(k/2<min1)\n min1=k/2;\n s.insert(k/2);\n }\n sol=min(pq1.top()-min1,sol);\n return sol;\n }\n};",
"memory": "89500"
} |
1,794 | <p>You are given an array <code>nums</code> of <code>n</code> positive integers.</p>
<p>You can perform two types of operations on any element of the array any number of times:</p>
<ul>
<li>If the element is <strong>even</strong>, <strong>divide</strong> it by <code>2</code>.
<ul>
<li>For example, if the array is <code>[1,2,3,4]</code>, then you can do this operation on the last element, and the array will be <code>[1,2,3,<u>2</u>].</code></li>
</ul>
</li>
<li>If the element is <strong>odd</strong>, <strong>multiply</strong> it by <code>2</code>.
<ul>
<li>For example, if the array is <code>[1,2,3,4]</code>, then you can do this operation on the first element, and the array will be <code>[<u>2</u>,2,3,4].</code></li>
</ul>
</li>
</ul>
<p>The <strong>deviation</strong> of the array is the <strong>maximum difference</strong> between any two elements in the array.</p>
<p>Return <em>the <strong>minimum deviation</strong> the array can have after performing some number of operations.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4]
<strong>Output:</strong> 1
<strong>Explanation:</strong> You can transform the array to [1,2,3,<u>2</u>], then to [<u>2</u>,2,3,2], then the deviation will be 3 - 2 = 1.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,1,5,20,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> You can transform the array after two operations to [4,<u>2</u>,5,<u>5</u>,3], then the deviation will be 5 - 2 = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,10,8]
<strong>Output:</strong> 3
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == nums.length</code></li>
<li><code>2 <= n <= 5 * 10<sup><span style="font-size: 10.8333px;">4</span></sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minimumDeviation(vector<int>& nums) {\n priority_queue<int>pq1;\n for(int i=0;i<nums.size();i++){\n if(nums[i]%2!=0)\n nums[i]=nums[i]*2;\n }\n unordered_set<int>s;\n int min1=INT_MAX;\n for(int i=0;i<nums.size();i++){\n if(s.find(nums[i])==s.end())\n pq1.push(nums[i]);\n s.insert(nums[i]);\n min1=min(min1,nums[i]);\n }\n s.clear();\n int sol=INT_MAX;\n while(pq1.top()%2==0){\n int k=pq1.top();\n pq1.pop();\n if(s.find(k/2)==s.end())\n pq1.push(k/2);\n sol=min(sol,k-min1);\n if(k/2<min1)\n min1=k/2;\n s.insert(k/2);\n }\n sol=min(pq1.top()-min1,sol);\n return sol;\n }\n};",
"memory": "89600"
} |
1,794 | <p>You are given an array <code>nums</code> of <code>n</code> positive integers.</p>
<p>You can perform two types of operations on any element of the array any number of times:</p>
<ul>
<li>If the element is <strong>even</strong>, <strong>divide</strong> it by <code>2</code>.
<ul>
<li>For example, if the array is <code>[1,2,3,4]</code>, then you can do this operation on the last element, and the array will be <code>[1,2,3,<u>2</u>].</code></li>
</ul>
</li>
<li>If the element is <strong>odd</strong>, <strong>multiply</strong> it by <code>2</code>.
<ul>
<li>For example, if the array is <code>[1,2,3,4]</code>, then you can do this operation on the first element, and the array will be <code>[<u>2</u>,2,3,4].</code></li>
</ul>
</li>
</ul>
<p>The <strong>deviation</strong> of the array is the <strong>maximum difference</strong> between any two elements in the array.</p>
<p>Return <em>the <strong>minimum deviation</strong> the array can have after performing some number of operations.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4]
<strong>Output:</strong> 1
<strong>Explanation:</strong> You can transform the array to [1,2,3,<u>2</u>], then to [<u>2</u>,2,3,2], then the deviation will be 3 - 2 = 1.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,1,5,20,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> You can transform the array after two operations to [4,<u>2</u>,5,<u>5</u>,3], then the deviation will be 5 - 2 = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,10,8]
<strong>Output:</strong> 3
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == nums.length</code></li>
<li><code>2 <= n <= 5 * 10<sup><span style="font-size: 10.8333px;">4</span></sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minimumDeviation(vector<int>& nums) {\n set<int> evens;\n for (int num: nums) {\n evens.insert(num % 2 == 0 ? num : num * 2);\n }\n int min_dev = 1E9;\n\n while (!evens.empty()) {\n auto fst = evens.begin();\n auto lst = evens.end();\n lst--;\n min_dev = min(min_dev, *lst - *fst);\n if (*lst % 2 == 0) {\n evens.insert(*lst / 2);\n evens.erase(lst);\n } else {\n break;\n }\n }\n\n return min_dev;\n }\n};",
"memory": "91600"
} |
1,794 | <p>You are given an array <code>nums</code> of <code>n</code> positive integers.</p>
<p>You can perform two types of operations on any element of the array any number of times:</p>
<ul>
<li>If the element is <strong>even</strong>, <strong>divide</strong> it by <code>2</code>.
<ul>
<li>For example, if the array is <code>[1,2,3,4]</code>, then you can do this operation on the last element, and the array will be <code>[1,2,3,<u>2</u>].</code></li>
</ul>
</li>
<li>If the element is <strong>odd</strong>, <strong>multiply</strong> it by <code>2</code>.
<ul>
<li>For example, if the array is <code>[1,2,3,4]</code>, then you can do this operation on the first element, and the array will be <code>[<u>2</u>,2,3,4].</code></li>
</ul>
</li>
</ul>
<p>The <strong>deviation</strong> of the array is the <strong>maximum difference</strong> between any two elements in the array.</p>
<p>Return <em>the <strong>minimum deviation</strong> the array can have after performing some number of operations.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4]
<strong>Output:</strong> 1
<strong>Explanation:</strong> You can transform the array to [1,2,3,<u>2</u>], then to [<u>2</u>,2,3,2], then the deviation will be 3 - 2 = 1.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,1,5,20,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> You can transform the array after two operations to [4,<u>2</u>,5,<u>5</u>,3], then the deviation will be 5 - 2 = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,10,8]
<strong>Output:</strong> 3
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == nums.length</code></li>
<li><code>2 <= n <= 5 * 10<sup><span style="font-size: 10.8333px;">4</span></sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minimumDeviation(vector<int>& nums) {\n set<int> s;\n for (auto x : nums)\n {\n if (x % 2 == 0)\n {\n s.insert(x);\n }\n else\n {\n s.insert(x * 2);\n }\n }\n int ans = *s.rbegin() - *s.begin();\n while (*s.rbegin() % 2 == 0)\n {\n int x = *s.rbegin();\n s.erase(x);\n s.insert(x / 2);\n ans = min(ans, *s.rbegin() - *s.begin());\n }\n \n return ans;\n }\n};",
"memory": "91700"
} |
1,794 | <p>You are given an array <code>nums</code> of <code>n</code> positive integers.</p>
<p>You can perform two types of operations on any element of the array any number of times:</p>
<ul>
<li>If the element is <strong>even</strong>, <strong>divide</strong> it by <code>2</code>.
<ul>
<li>For example, if the array is <code>[1,2,3,4]</code>, then you can do this operation on the last element, and the array will be <code>[1,2,3,<u>2</u>].</code></li>
</ul>
</li>
<li>If the element is <strong>odd</strong>, <strong>multiply</strong> it by <code>2</code>.
<ul>
<li>For example, if the array is <code>[1,2,3,4]</code>, then you can do this operation on the first element, and the array will be <code>[<u>2</u>,2,3,4].</code></li>
</ul>
</li>
</ul>
<p>The <strong>deviation</strong> of the array is the <strong>maximum difference</strong> between any two elements in the array.</p>
<p>Return <em>the <strong>minimum deviation</strong> the array can have after performing some number of operations.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4]
<strong>Output:</strong> 1
<strong>Explanation:</strong> You can transform the array to [1,2,3,<u>2</u>], then to [<u>2</u>,2,3,2], then the deviation will be 3 - 2 = 1.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,1,5,20,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> You can transform the array after two operations to [4,<u>2</u>,5,<u>5</u>,3], then the deviation will be 5 - 2 = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,10,8]
<strong>Output:</strong> 3
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == nums.length</code></li>
<li><code>2 <= n <= 5 * 10<sup><span style="font-size: 10.8333px;">4</span></sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minimumDeviation(vector<int>& nums) {\n set<int> s;\n for (const int& num : nums) {\n if (num % 2 != 0) s.insert(num * 2); else s.insert(num); \n }\n int min_dev = *(--s.end()) - *s.begin();\n while (*(--s.end()) % 2 == 0) {\n s.insert(*(--s.end()) / 2);\n s.erase(*(--s.end()));\n if (*(--s.end()) - *s.begin() < min_dev) \n { \n min_dev = *(--s.end()) - *s.begin(); \n }\n }\n \n return min_dev; \n }\n};",
"memory": "91800"
} |
1,794 | <p>You are given an array <code>nums</code> of <code>n</code> positive integers.</p>
<p>You can perform two types of operations on any element of the array any number of times:</p>
<ul>
<li>If the element is <strong>even</strong>, <strong>divide</strong> it by <code>2</code>.
<ul>
<li>For example, if the array is <code>[1,2,3,4]</code>, then you can do this operation on the last element, and the array will be <code>[1,2,3,<u>2</u>].</code></li>
</ul>
</li>
<li>If the element is <strong>odd</strong>, <strong>multiply</strong> it by <code>2</code>.
<ul>
<li>For example, if the array is <code>[1,2,3,4]</code>, then you can do this operation on the first element, and the array will be <code>[<u>2</u>,2,3,4].</code></li>
</ul>
</li>
</ul>
<p>The <strong>deviation</strong> of the array is the <strong>maximum difference</strong> between any two elements in the array.</p>
<p>Return <em>the <strong>minimum deviation</strong> the array can have after performing some number of operations.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4]
<strong>Output:</strong> 1
<strong>Explanation:</strong> You can transform the array to [1,2,3,<u>2</u>], then to [<u>2</u>,2,3,2], then the deviation will be 3 - 2 = 1.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,1,5,20,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> You can transform the array after two operations to [4,<u>2</u>,5,<u>5</u>,3], then the deviation will be 5 - 2 = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,10,8]
<strong>Output:</strong> 3
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == nums.length</code></li>
<li><code>2 <= n <= 5 * 10<sup><span style="font-size: 10.8333px;">4</span></sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minimumDeviation(vector<int>& nums) {\n int ans = 2e9;\n set<int> numset;\n for (int num : nums) {\n if (num % 2 == 0) numset.insert(num);\n else numset.insert(2 * num);\n }\n while (true) {\n int top = *numset.rbegin();\n int bot = *numset.begin();\n ans = min(ans, top - bot);\n if (top % 2 == 1) break;\n numset.erase(top);\n numset.insert(top / 2);\n //cout << top << \"\\n\";\n }\n\n return ans;\n }\n};",
"memory": "91800"
} |
1,794 | <p>You are given an array <code>nums</code> of <code>n</code> positive integers.</p>
<p>You can perform two types of operations on any element of the array any number of times:</p>
<ul>
<li>If the element is <strong>even</strong>, <strong>divide</strong> it by <code>2</code>.
<ul>
<li>For example, if the array is <code>[1,2,3,4]</code>, then you can do this operation on the last element, and the array will be <code>[1,2,3,<u>2</u>].</code></li>
</ul>
</li>
<li>If the element is <strong>odd</strong>, <strong>multiply</strong> it by <code>2</code>.
<ul>
<li>For example, if the array is <code>[1,2,3,4]</code>, then you can do this operation on the first element, and the array will be <code>[<u>2</u>,2,3,4].</code></li>
</ul>
</li>
</ul>
<p>The <strong>deviation</strong> of the array is the <strong>maximum difference</strong> between any two elements in the array.</p>
<p>Return <em>the <strong>minimum deviation</strong> the array can have after performing some number of operations.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4]
<strong>Output:</strong> 1
<strong>Explanation:</strong> You can transform the array to [1,2,3,<u>2</u>], then to [<u>2</u>,2,3,2], then the deviation will be 3 - 2 = 1.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,1,5,20,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> You can transform the array after two operations to [4,<u>2</u>,5,<u>5</u>,3], then the deviation will be 5 - 2 = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,10,8]
<strong>Output:</strong> 3
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == nums.length</code></li>
<li><code>2 <= n <= 5 * 10<sup><span style="font-size: 10.8333px;">4</span></sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minimumDeviation(vector<int>& nums) {\n set<int> s;\n for (auto n: nums) {\n s.insert((n%2 == 1)? n*2 : n);\n }\n\n int ans = *s.rbegin() - *s.begin();\n int m = *s.begin();\n\n while (*s.rbegin()%2 == 0){\n int temp = *s.rbegin();\n s.erase(temp);\n s.insert(temp/2);\n ans = min(ans, *s.rbegin() - *s.begin() );\n }\n return ans;\n }\n};",
"memory": "91900"
} |
1,794 | <p>You are given an array <code>nums</code> of <code>n</code> positive integers.</p>
<p>You can perform two types of operations on any element of the array any number of times:</p>
<ul>
<li>If the element is <strong>even</strong>, <strong>divide</strong> it by <code>2</code>.
<ul>
<li>For example, if the array is <code>[1,2,3,4]</code>, then you can do this operation on the last element, and the array will be <code>[1,2,3,<u>2</u>].</code></li>
</ul>
</li>
<li>If the element is <strong>odd</strong>, <strong>multiply</strong> it by <code>2</code>.
<ul>
<li>For example, if the array is <code>[1,2,3,4]</code>, then you can do this operation on the first element, and the array will be <code>[<u>2</u>,2,3,4].</code></li>
</ul>
</li>
</ul>
<p>The <strong>deviation</strong> of the array is the <strong>maximum difference</strong> between any two elements in the array.</p>
<p>Return <em>the <strong>minimum deviation</strong> the array can have after performing some number of operations.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4]
<strong>Output:</strong> 1
<strong>Explanation:</strong> You can transform the array to [1,2,3,<u>2</u>], then to [<u>2</u>,2,3,2], then the deviation will be 3 - 2 = 1.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,1,5,20,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> You can transform the array after two operations to [4,<u>2</u>,5,<u>5</u>,3], then the deviation will be 5 - 2 = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,10,8]
<strong>Output:</strong> 3
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == nums.length</code></li>
<li><code>2 <= n <= 5 * 10<sup><span style="font-size: 10.8333px;">4</span></sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minimumDeviation(vector<int>& nums) {\n int n = nums.size();\n set<int>s2;\n vector<int>allans;\n sort(nums.begin(),nums.end());\n for(int i=0;i<n;i++){\n if(nums[i]%2!=0){\n nums[i]*=2;\n }\n s2.insert(nums[i]);\n }\n int max = nums[n-1];\n while(max%2==0){\n max =*s2.rbegin(); \n allans.push_back(abs(*s2.rbegin()-*s2.begin()));\n if(max%2!=0)break;\n if(*s2.rbegin()%2==0){\n int x = *s2.rbegin();\n s2.erase(x);\n x/=2;\n s2.insert(x);\n }\n }\n sort(allans.begin(),allans.end());\n return allans[0];\n }\n};",
"memory": "94600"
} |
1,794 | <p>You are given an array <code>nums</code> of <code>n</code> positive integers.</p>
<p>You can perform two types of operations on any element of the array any number of times:</p>
<ul>
<li>If the element is <strong>even</strong>, <strong>divide</strong> it by <code>2</code>.
<ul>
<li>For example, if the array is <code>[1,2,3,4]</code>, then you can do this operation on the last element, and the array will be <code>[1,2,3,<u>2</u>].</code></li>
</ul>
</li>
<li>If the element is <strong>odd</strong>, <strong>multiply</strong> it by <code>2</code>.
<ul>
<li>For example, if the array is <code>[1,2,3,4]</code>, then you can do this operation on the first element, and the array will be <code>[<u>2</u>,2,3,4].</code></li>
</ul>
</li>
</ul>
<p>The <strong>deviation</strong> of the array is the <strong>maximum difference</strong> between any two elements in the array.</p>
<p>Return <em>the <strong>minimum deviation</strong> the array can have after performing some number of operations.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4]
<strong>Output:</strong> 1
<strong>Explanation:</strong> You can transform the array to [1,2,3,<u>2</u>], then to [<u>2</u>,2,3,2], then the deviation will be 3 - 2 = 1.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,1,5,20,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> You can transform the array after two operations to [4,<u>2</u>,5,<u>5</u>,3], then the deviation will be 5 - 2 = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,10,8]
<strong>Output:</strong> 3
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == nums.length</code></li>
<li><code>2 <= n <= 5 * 10<sup><span style="font-size: 10.8333px;">4</span></sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minimumDeviation(vector<int>& v) {\n set<pair<int, int>> s;\n int n = v.size();\n\n // Normalize the array\n for (int i = 0; i < n; i++) {\n int a = v[i];\n while (a % 2 == 0) {\n a /= 2;\n }\n s.insert({a, v[i]});\n }\n\n int ans = (*s.rbegin()).first - (*s.begin()).first;\n \n while (!s.empty()) {\n auto it = s.begin();\n int val = it->first;\n int original = it->second;\n s.erase(it);\n\n if(original%2)\n {\n if(val>=2*original)break;\n else\n {\n s.insert({val * 2, original});\n ans = min(ans, (*s.rbegin()).first - (*s.begin()).first); \n }\n }\n else\n {\n if (val * 2 <= original) {\n s.insert({val * 2, original});\n ans = min(ans, (*s.rbegin()).first - (*s.begin()).first);\n } else {\n break;\n }\n }\n }\n\n return ans;\n }\n};\n",
"memory": "94900"
} |
1,794 | <p>You are given an array <code>nums</code> of <code>n</code> positive integers.</p>
<p>You can perform two types of operations on any element of the array any number of times:</p>
<ul>
<li>If the element is <strong>even</strong>, <strong>divide</strong> it by <code>2</code>.
<ul>
<li>For example, if the array is <code>[1,2,3,4]</code>, then you can do this operation on the last element, and the array will be <code>[1,2,3,<u>2</u>].</code></li>
</ul>
</li>
<li>If the element is <strong>odd</strong>, <strong>multiply</strong> it by <code>2</code>.
<ul>
<li>For example, if the array is <code>[1,2,3,4]</code>, then you can do this operation on the first element, and the array will be <code>[<u>2</u>,2,3,4].</code></li>
</ul>
</li>
</ul>
<p>The <strong>deviation</strong> of the array is the <strong>maximum difference</strong> between any two elements in the array.</p>
<p>Return <em>the <strong>minimum deviation</strong> the array can have after performing some number of operations.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4]
<strong>Output:</strong> 1
<strong>Explanation:</strong> You can transform the array to [1,2,3,<u>2</u>], then to [<u>2</u>,2,3,2], then the deviation will be 3 - 2 = 1.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,1,5,20,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> You can transform the array after two operations to [4,<u>2</u>,5,<u>5</u>,3], then the deviation will be 5 - 2 = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,10,8]
<strong>Output:</strong> 3
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == nums.length</code></li>
<li><code>2 <= n <= 5 * 10<sup><span style="font-size: 10.8333px;">4</span></sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int eo(vector<int>& nums){\n priority_queue<int> pq; \n priority_queue<int, vector<int>, greater<int>> mh;\n for(int i = 0; i < nums.size(); i++) {\n pq.push(nums[i]);\n mh.push(nums[i]);\n } \n int mn = pq.top() - mh.top();\n \n while (pq.top() % 2 == 0) {\n int k = pq.top();\n pq.pop();\n pq.push(k / 2);\n mh.push(k / 2);\n if (mn < pq.top() - mh.top()) break;\n mn = min(mn, pq.top() - mh.top());\n }\n \n while (mh.top() % 2) {\n int k = mh.top();\n mh.pop();\n mh.push(k * 2);\n pq.push(k * 2);\n if (mn < pq.top() - mh.top()) break;\n mn = min(mn, pq.top() - mh.top());\n }\n return mn;\n }\n\n int oe(vector<int>& nums){\n priority_queue<int> pq; \n priority_queue<int, vector<int>, greater<int>> mh;\n for(int i = 0; i < nums.size(); i++) {\n pq.push(nums[i]);\n mh.push(nums[i]);\n } \n int mn = pq.top() - mh.top();\n while (mh.top() % 2) {\n int k = mh.top();\n mh.pop();\n mh.push(k * 2);\n pq.push(k * 2);\n // if (mn < pq.top() - mh.top()) break;\n mn = min(mn, pq.top() - mh.top());\n }\n while (pq.top() % 2 == 0) {\n int k = pq.top();\n pq.pop();\n pq.push(k / 2);\n mh.push(k / 2);\n // if (mn < pq.top() - mh.top()) break;\n mn = min(mn, pq.top() - mh.top());\n }\n return mn;\n }\n\n // int gr(vector<int>& nums){\n // priority_queue<int> pq; \n // priority_queue<int, vector<int>, greater<int>> mh;\n // for(int i = 0; i < nums.size(); i++) {\n // pq.push(nums[i]);\n // mh.push(nums[i]);\n // } \n // int mn = pq.top() - mh.top();\n // while(1){\n // int k= pq.top();\n // int l = mh.top();\n // if(k%2==0 && l%2){\n\n // }\n // else if(l%2){\n // int k = mh.top();\n // mh.pop();\n // mh.push(k * 2);\n // pq.push(k * 2);\n // if (mn < pq.top() - mh.top()) break;\n // mn = min(mn, pq.top() - mh.top())\n // }\n // else if(k%2==0{\n \n // }\n // else break;\n // }\n // return mn;\n // }\n\n int minimumDeviation(vector<int>& nums) {\n return min(oe(nums), eo(nums));\n }\n};\n",
"memory": "98600"
} |
1,794 | <p>You are given an array <code>nums</code> of <code>n</code> positive integers.</p>
<p>You can perform two types of operations on any element of the array any number of times:</p>
<ul>
<li>If the element is <strong>even</strong>, <strong>divide</strong> it by <code>2</code>.
<ul>
<li>For example, if the array is <code>[1,2,3,4]</code>, then you can do this operation on the last element, and the array will be <code>[1,2,3,<u>2</u>].</code></li>
</ul>
</li>
<li>If the element is <strong>odd</strong>, <strong>multiply</strong> it by <code>2</code>.
<ul>
<li>For example, if the array is <code>[1,2,3,4]</code>, then you can do this operation on the first element, and the array will be <code>[<u>2</u>,2,3,4].</code></li>
</ul>
</li>
</ul>
<p>The <strong>deviation</strong> of the array is the <strong>maximum difference</strong> between any two elements in the array.</p>
<p>Return <em>the <strong>minimum deviation</strong> the array can have after performing some number of operations.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4]
<strong>Output:</strong> 1
<strong>Explanation:</strong> You can transform the array to [1,2,3,<u>2</u>], then to [<u>2</u>,2,3,2], then the deviation will be 3 - 2 = 1.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,1,5,20,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> You can transform the array after two operations to [4,<u>2</u>,5,<u>5</u>,3], then the deviation will be 5 - 2 = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,10,8]
<strong>Output:</strong> 3
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == nums.length</code></li>
<li><code>2 <= n <= 5 * 10<sup><span style="font-size: 10.8333px;">4</span></sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minimumDeviation(vector<int>& nums) {\n multiset<int>st;\n int n=nums.size();\n for(int i=0;i<n;i++)\n { if(nums[i]%2==0)\n st.insert(nums[i]);\n else st.insert(2*nums[i]);\n }\n int ans=INT_MAX;\n while(1)\n {\n int x=*st.rbegin();\n int y=*st.begin();\n ans=min(ans,x-y);\n if(x%2==0)\n {\n st.erase(x);\n st.insert(x/2);\n }\n else break;\n \n \n }\n return ans;\n }\n \n \n};",
"memory": "100200"
} |
1,794 | <p>You are given an array <code>nums</code> of <code>n</code> positive integers.</p>
<p>You can perform two types of operations on any element of the array any number of times:</p>
<ul>
<li>If the element is <strong>even</strong>, <strong>divide</strong> it by <code>2</code>.
<ul>
<li>For example, if the array is <code>[1,2,3,4]</code>, then you can do this operation on the last element, and the array will be <code>[1,2,3,<u>2</u>].</code></li>
</ul>
</li>
<li>If the element is <strong>odd</strong>, <strong>multiply</strong> it by <code>2</code>.
<ul>
<li>For example, if the array is <code>[1,2,3,4]</code>, then you can do this operation on the first element, and the array will be <code>[<u>2</u>,2,3,4].</code></li>
</ul>
</li>
</ul>
<p>The <strong>deviation</strong> of the array is the <strong>maximum difference</strong> between any two elements in the array.</p>
<p>Return <em>the <strong>minimum deviation</strong> the array can have after performing some number of operations.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4]
<strong>Output:</strong> 1
<strong>Explanation:</strong> You can transform the array to [1,2,3,<u>2</u>], then to [<u>2</u>,2,3,2], then the deviation will be 3 - 2 = 1.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,1,5,20,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> You can transform the array after two operations to [4,<u>2</u>,5,<u>5</u>,3], then the deviation will be 5 - 2 = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,10,8]
<strong>Output:</strong> 3
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == nums.length</code></li>
<li><code>2 <= n <= 5 * 10<sup><span style="font-size: 10.8333px;">4</span></sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minimumDeviation(vector<int>& nums) { \n set<int> s;\n for(auto i: nums) s.insert(i); //insert everything into a set\n int dev= *s.rbegin()-*s.begin(); //difference between max and min\n for(auto i: nums) if(i&1){ s.erase(i);s.insert(i<<=1);} //remove all odds, add 2*odd\n while(!((*s.rbegin())&1)){ //while last element does not become odd\n int a=(*s.rbegin());\n s.erase(a); //remove last\n s.insert(a>>=1); //insert last/2\n dev=min(dev,*s.rbegin()-*s.begin()); //update min deviation\n }\n return dev;\n }\n};",
"memory": "103000"
} |
1,794 | <p>You are given an array <code>nums</code> of <code>n</code> positive integers.</p>
<p>You can perform two types of operations on any element of the array any number of times:</p>
<ul>
<li>If the element is <strong>even</strong>, <strong>divide</strong> it by <code>2</code>.
<ul>
<li>For example, if the array is <code>[1,2,3,4]</code>, then you can do this operation on the last element, and the array will be <code>[1,2,3,<u>2</u>].</code></li>
</ul>
</li>
<li>If the element is <strong>odd</strong>, <strong>multiply</strong> it by <code>2</code>.
<ul>
<li>For example, if the array is <code>[1,2,3,4]</code>, then you can do this operation on the first element, and the array will be <code>[<u>2</u>,2,3,4].</code></li>
</ul>
</li>
</ul>
<p>The <strong>deviation</strong> of the array is the <strong>maximum difference</strong> between any two elements in the array.</p>
<p>Return <em>the <strong>minimum deviation</strong> the array can have after performing some number of operations.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4]
<strong>Output:</strong> 1
<strong>Explanation:</strong> You can transform the array to [1,2,3,<u>2</u>], then to [<u>2</u>,2,3,2], then the deviation will be 3 - 2 = 1.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,1,5,20,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> You can transform the array after two operations to [4,<u>2</u>,5,<u>5</u>,3], then the deviation will be 5 - 2 = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,10,8]
<strong>Output:</strong> 3
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == nums.length</code></li>
<li><code>2 <= n <= 5 * 10<sup><span style="font-size: 10.8333px;">4</span></sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minimumDeviation(vector<int>& nums) {\n vector<vector<int>> v;\n for(int i=0;i<nums.size();i++){\n vector<int> cur;\n if(nums[i]%2==0){\n int x=nums[i];\n while(x%2==0){\n cur.push_back(x);\n x=x/2;\n }\n if(x!=0){\n cur.push_back(x);\n }\n }\n else{\n cur.push_back(nums[i]);\n cur.push_back(nums[i]*2);\n }\n sort(cur.begin(),cur.end());\n v.push_back(cur);\n }\n priority_queue<pair<int,pair<int,int>>,vector<pair<int,pair<int,int>>>,greater<pair<int,pair<int,int>>>> pq;\n int maxi=0;\n int n=nums.size();\n for(int i=0;i<n;i++){\n pq.push({v[i][0],{i,0}});\n maxi=max(maxi,v[i][0]);\n }\n int range=maxi-pq.top().first;\n while(true){\n auto it=pq.top();\n int mini=it.first;\n int row=it.second.first;\n int col=it.second.second;\n if(col==v[row].size()-1){\n break;\n }\n else{\n pq.pop();\n maxi=max(maxi,v[row][col+1]);\n pq.push({v[row][col+1],{row,col+1}});\n range=min(range,maxi-pq.top().first);\n }\n\n }\n return range;\n // for(auto it:v){\n // for(auto at:it){\n // cout<<at<<\" \";\n // }cout<<endl;\n // }\n return 0;\n }\n};",
"memory": "210000"
} |
1,794 | <p>You are given an array <code>nums</code> of <code>n</code> positive integers.</p>
<p>You can perform two types of operations on any element of the array any number of times:</p>
<ul>
<li>If the element is <strong>even</strong>, <strong>divide</strong> it by <code>2</code>.
<ul>
<li>For example, if the array is <code>[1,2,3,4]</code>, then you can do this operation on the last element, and the array will be <code>[1,2,3,<u>2</u>].</code></li>
</ul>
</li>
<li>If the element is <strong>odd</strong>, <strong>multiply</strong> it by <code>2</code>.
<ul>
<li>For example, if the array is <code>[1,2,3,4]</code>, then you can do this operation on the first element, and the array will be <code>[<u>2</u>,2,3,4].</code></li>
</ul>
</li>
</ul>
<p>The <strong>deviation</strong> of the array is the <strong>maximum difference</strong> between any two elements in the array.</p>
<p>Return <em>the <strong>minimum deviation</strong> the array can have after performing some number of operations.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4]
<strong>Output:</strong> 1
<strong>Explanation:</strong> You can transform the array to [1,2,3,<u>2</u>], then to [<u>2</u>,2,3,2], then the deviation will be 3 - 2 = 1.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,1,5,20,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> You can transform the array after two operations to [4,<u>2</u>,5,<u>5</u>,3], then the deviation will be 5 - 2 = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,10,8]
<strong>Output:</strong> 3
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == nums.length</code></li>
<li><code>2 <= n <= 5 * 10<sup><span style="font-size: 10.8333px;">4</span></sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minimumDeviation(vector<int>& nums) {\n int maxi=INT_MIN;\n int mini=INT_MAX;\n \n int n=nums.size();\n multiset<int> s;\n for(auto e:nums)\n s.insert(e);\n int diff=*(--s.end())-*s.begin();\n while((*s.begin())%2==1){\n int ele=*s.begin();\n s.erase(s.begin());\n s.insert(2*ele);\n diff=min(diff,*(--s.end())-*s.begin());\n }\n\n while((*(--s.end()))%2==0){\n int ele=*(--s.end());\n s.erase(--s.end());\n s.insert(ele/2);\n diff=min(diff,*(--s.end())-*s.begin());\n }\n\n return diff;\n\n\n \n }\n};",
"memory": "247100"
} |
1,794 | <p>You are given an array <code>nums</code> of <code>n</code> positive integers.</p>
<p>You can perform two types of operations on any element of the array any number of times:</p>
<ul>
<li>If the element is <strong>even</strong>, <strong>divide</strong> it by <code>2</code>.
<ul>
<li>For example, if the array is <code>[1,2,3,4]</code>, then you can do this operation on the last element, and the array will be <code>[1,2,3,<u>2</u>].</code></li>
</ul>
</li>
<li>If the element is <strong>odd</strong>, <strong>multiply</strong> it by <code>2</code>.
<ul>
<li>For example, if the array is <code>[1,2,3,4]</code>, then you can do this operation on the first element, and the array will be <code>[<u>2</u>,2,3,4].</code></li>
</ul>
</li>
</ul>
<p>The <strong>deviation</strong> of the array is the <strong>maximum difference</strong> between any two elements in the array.</p>
<p>Return <em>the <strong>minimum deviation</strong> the array can have after performing some number of operations.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4]
<strong>Output:</strong> 1
<strong>Explanation:</strong> You can transform the array to [1,2,3,<u>2</u>], then to [<u>2</u>,2,3,2], then the deviation will be 3 - 2 = 1.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,1,5,20,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> You can transform the array after two operations to [4,<u>2</u>,5,<u>5</u>,3], then the deviation will be 5 - 2 = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,10,8]
<strong>Output:</strong> 3
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == nums.length</code></li>
<li><code>2 <= n <= 5 * 10<sup><span style="font-size: 10.8333px;">4</span></sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minimumDeviation(vector<int>& nums) {\n set<pair<int,int>>s;\n for(int i=0;i<nums.size();i++)\n {\n int x=nums[i];\n while(x%2==0)\n x/=2;\n s.insert({x,i});\n }\n int ans=(*prev(s.end())).first-(*s.begin()).first;\n while(1)\n {\n //cout<<(*prev(s.end())).first-(*s.begin()).first<<endl;\n ans=min(ans,(*prev(s.end())).first-(*s.begin()).first);\n pair<int,int>p=*s.begin();\n s.erase(s.begin());\n if(p.first<nums[p.second]||(nums[p.second]%2!=0&&p.first==nums[p.second]))\n p.first*=2,s.insert(p);\n else\n break;\n }\n cout<<endl;\n return ans;\n }\n};",
"memory": "253900"
} |
1,798 | <p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>In one operation, you can pick two numbers from the array whose sum equals <code>k</code> and remove them from the array.</p>
<p>Return <em>the maximum number of operations you can perform on the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4], k = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> Starting with nums = [1,2,3,4]:
- Remove numbers 1 and 4, then nums = [2,3]
- Remove numbers 2 and 3, then nums = []
There are no more pairs that sum up to 5, hence a total of 2 operations.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,3,4,3], k = 6
<strong>Output:</strong> 1
<strong>Explanation:</strong> Starting with nums = [3,1,3,4,3]:
- Remove the first two 3's, then nums = [1,4,3]
There are no more pairs that sum up to 6, hence a total of 1 operation.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int maxOperations(vector<int>& nums, int k) {\n int n=nums.size();\n int i=0, j=n-1;\n int c=0;\n sort(nums.begin(),nums.end());\n while(i<j){\n if(nums[i]+nums[j]==k){\n c++;i++;j--;\n }\n else if(nums[i]+nums[j]>k) j--;\n else i++;\n }\n return c;\n\n }\n};",
"memory": "61040"
} |
1,798 | <p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>In one operation, you can pick two numbers from the array whose sum equals <code>k</code> and remove them from the array.</p>
<p>Return <em>the maximum number of operations you can perform on the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4], k = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> Starting with nums = [1,2,3,4]:
- Remove numbers 1 and 4, then nums = [2,3]
- Remove numbers 2 and 3, then nums = []
There are no more pairs that sum up to 5, hence a total of 2 operations.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,3,4,3], k = 6
<strong>Output:</strong> 1
<strong>Explanation:</strong> Starting with nums = [3,1,3,4,3]:
- Remove the first two 3's, then nums = [1,4,3]
There are no more pairs that sum up to 6, hence a total of 1 operation.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int maxOperations(vector<int>& nums, int k) {\n sort(nums.begin(),nums.end());\n int n=nums.size();\n int i=0;\n int j=n-1;\n int cnt=0;\n while(i<j){\n int sum=nums[i]+nums[j];\n if(sum==k){\n i++;\n j--;\n cnt++;\n }\n else if(sum<k){\n i++;\n }\n else{\n j--;\n }\n }\n return cnt;\n }\n};",
"memory": "61040"
} |
1,798 | <p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>In one operation, you can pick two numbers from the array whose sum equals <code>k</code> and remove them from the array.</p>
<p>Return <em>the maximum number of operations you can perform on the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4], k = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> Starting with nums = [1,2,3,4]:
- Remove numbers 1 and 4, then nums = [2,3]
- Remove numbers 2 and 3, then nums = []
There are no more pairs that sum up to 5, hence a total of 2 operations.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,3,4,3], k = 6
<strong>Output:</strong> 1
<strong>Explanation:</strong> Starting with nums = [3,1,3,4,3]:
- Remove the first two 3's, then nums = [1,4,3]
There are no more pairs that sum up to 6, hence a total of 1 operation.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int maxOperations(vector<int>& nums, int k) {\n sort(nums.begin(), nums.end()); // Sort the array first\n int left = 0;\n int right = nums.size() - 1;\n int count = 0;\n\n while (left < right) {\n int sum = nums[left] + nums[right];\n if (sum == k) {\n count++;\n left++;\n right--;\n } else if (sum < k) {\n left++; // Increase left pointer if sum is less than k\n } else {\n right--; // Decrease right pointer if sum is more than k\n }\n }\n\n return count;\n }\n};",
"memory": "61256"
} |
1,798 | <p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>In one operation, you can pick two numbers from the array whose sum equals <code>k</code> and remove them from the array.</p>
<p>Return <em>the maximum number of operations you can perform on the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4], k = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> Starting with nums = [1,2,3,4]:
- Remove numbers 1 and 4, then nums = [2,3]
- Remove numbers 2 and 3, then nums = []
There are no more pairs that sum up to 5, hence a total of 2 operations.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,3,4,3], k = 6
<strong>Output:</strong> 1
<strong>Explanation:</strong> Starting with nums = [3,1,3,4,3]:
- Remove the first two 3's, then nums = [1,4,3]
There are no more pairs that sum up to 6, hence a total of 1 operation.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int maxOperations(vector<int>& nums, int k) {\n sort(nums.begin(),nums.end());\n int n=nums.size();\n int i=0,j=n-1;\n int c=0;\n while(i<j){\n int y=nums[i]+nums[j];\n if(y == k){\n c++;\n i++;\n j--;\n }\n else if (y>k) j--;\n else i++;\n }\n return c;\n }\n};",
"memory": "61256"
} |
1,798 | <p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>In one operation, you can pick two numbers from the array whose sum equals <code>k</code> and remove them from the array.</p>
<p>Return <em>the maximum number of operations you can perform on the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4], k = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> Starting with nums = [1,2,3,4]:
- Remove numbers 1 and 4, then nums = [2,3]
- Remove numbers 2 and 3, then nums = []
There are no more pairs that sum up to 5, hence a total of 2 operations.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,3,4,3], k = 6
<strong>Output:</strong> 1
<strong>Explanation:</strong> Starting with nums = [3,1,3,4,3]:
- Remove the first two 3's, then nums = [1,4,3]
There are no more pairs that sum up to 6, hence a total of 1 operation.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int maxOperations(vector<int>& nums, int k) {\n int cnt=0;\n sort(nums.begin(),nums.end());\n int j=nums.size()-1,i=0;\n while(i<j){\n if(nums[i]+nums[j]==k){\n cnt++;\n j--;\n i++;\n }\n else if(nums[i]+nums[j]>k){\n j--;\n }\n else{\n i++;\n }\n }\n return cnt;\n }\n};",
"memory": "61473"
} |
1,798 | <p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>In one operation, you can pick two numbers from the array whose sum equals <code>k</code> and remove them from the array.</p>
<p>Return <em>the maximum number of operations you can perform on the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4], k = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> Starting with nums = [1,2,3,4]:
- Remove numbers 1 and 4, then nums = [2,3]
- Remove numbers 2 and 3, then nums = []
There are no more pairs that sum up to 5, hence a total of 2 operations.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,3,4,3], k = 6
<strong>Output:</strong> 1
<strong>Explanation:</strong> Starting with nums = [3,1,3,4,3]:
- Remove the first two 3's, then nums = [1,4,3]
There are no more pairs that sum up to 6, hence a total of 1 operation.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int maxOperations(vector<int>& nums, int k) {\n int i=0,j=nums.size()-1,count=0;\n sort(nums.begin(),nums.end());\n while(i<j){\n if(nums[i]+nums[j]==k){\n i++;j--;count++;\n }\n else if(nums[i]+nums[j]<k){\n i++;\n }\n else{\n j--;\n }\n }\n return count;\n }\n};",
"memory": "61473"
} |
1,798 | <p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>In one operation, you can pick two numbers from the array whose sum equals <code>k</code> and remove them from the array.</p>
<p>Return <em>the maximum number of operations you can perform on the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4], k = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> Starting with nums = [1,2,3,4]:
- Remove numbers 1 and 4, then nums = [2,3]
- Remove numbers 2 and 3, then nums = []
There are no more pairs that sum up to 5, hence a total of 2 operations.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,3,4,3], k = 6
<strong>Output:</strong> 1
<strong>Explanation:</strong> Starting with nums = [3,1,3,4,3]:
- Remove the first two 3's, then nums = [1,4,3]
There are no more pairs that sum up to 6, hence a total of 1 operation.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int maxOperations(vector<int>& nums, int k) {\n sort(nums.begin(), nums.end());\n int count = 0;\n int left = 0, right = nums.size()-1;\n while(right>left){\n \n if(nums[right] + nums[left] == k){\n left++;\n right--;\n count++;\n }\n else if(nums[right] + nums[left] > k){\n right--;\n }\n else if(nums[right] + nums[left] < k){\n left++;\n }\n }\n return count;\n }\n};",
"memory": "61689"
} |
1,798 | <p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>In one operation, you can pick two numbers from the array whose sum equals <code>k</code> and remove them from the array.</p>
<p>Return <em>the maximum number of operations you can perform on the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4], k = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> Starting with nums = [1,2,3,4]:
- Remove numbers 1 and 4, then nums = [2,3]
- Remove numbers 2 and 3, then nums = []
There are no more pairs that sum up to 5, hence a total of 2 operations.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,3,4,3], k = 6
<strong>Output:</strong> 1
<strong>Explanation:</strong> Starting with nums = [3,1,3,4,3]:
- Remove the first two 3's, then nums = [1,4,3]
There are no more pairs that sum up to 6, hence a total of 1 operation.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int maxOperations(vector<int>& nums, int k) {\n int cnt=0;\n sort(nums.begin(),nums.end());\n int j=nums.size()-1,i=0;\n while(i<j){\n if(nums[i]+nums[j]==k){\n cnt++;\n j--;\n i++;\n }\n else if(nums[i]+nums[j]>k){\n j--;\n }\n else{\n i++;\n }\n }\n return cnt;\n }\n};",
"memory": "61689"
} |
1,798 | <p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>In one operation, you can pick two numbers from the array whose sum equals <code>k</code> and remove them from the array.</p>
<p>Return <em>the maximum number of operations you can perform on the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4], k = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> Starting with nums = [1,2,3,4]:
- Remove numbers 1 and 4, then nums = [2,3]
- Remove numbers 2 and 3, then nums = []
There are no more pairs that sum up to 5, hence a total of 2 operations.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,3,4,3], k = 6
<strong>Output:</strong> 1
<strong>Explanation:</strong> Starting with nums = [3,1,3,4,3]:
- Remove the first two 3's, then nums = [1,4,3]
There are no more pairs that sum up to 6, hence a total of 1 operation.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\n //https://leetcode.com/problems/max-number-of-k-sum-pairs/solutions/5653907/easy-c-solution-sorting-binary-search\npublic:\n int maxOperations(vector<int>& nums, int k) {\n sort(nums.begin(),nums.end());\n int n=nums.size();\n int low=0,high=n-1;\n int count=0;\n while(low<high){\n if(nums[low]+nums[high]==k){\n count++;\n low++;\n high--;\n }\n else if(nums[low]+nums[high]>k){\n high--;\n }\n else{\n low++;\n }\n }\n return count;\n }\n};",
"memory": "61905"
} |
1,798 | <p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>In one operation, you can pick two numbers from the array whose sum equals <code>k</code> and remove them from the array.</p>
<p>Return <em>the maximum number of operations you can perform on the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4], k = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> Starting with nums = [1,2,3,4]:
- Remove numbers 1 and 4, then nums = [2,3]
- Remove numbers 2 and 3, then nums = []
There are no more pairs that sum up to 5, hence a total of 2 operations.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,3,4,3], k = 6
<strong>Output:</strong> 1
<strong>Explanation:</strong> Starting with nums = [3,1,3,4,3]:
- Remove the first two 3's, then nums = [1,4,3]
There are no more pairs that sum up to 6, hence a total of 1 operation.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
</ul>
| 2 | {
"code": "\nclass Solution {\npublic:\n int maxOperations(vector<int>& nums, int k) {\n int n=nums.size();\n int i=0,j=n-1;\n int c=0;\n sort(nums.begin(),nums.end());\n while(i<j)\n {\n if(nums[i]+nums[j]==k){ c++; i++;j--;}\n else if(nums[i]+ nums[j]>k) j--;\n else i++;\n }\n return c;\n \n }\n};",
"memory": "61905"
} |
1,798 | <p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>In one operation, you can pick two numbers from the array whose sum equals <code>k</code> and remove them from the array.</p>
<p>Return <em>the maximum number of operations you can perform on the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4], k = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> Starting with nums = [1,2,3,4]:
- Remove numbers 1 and 4, then nums = [2,3]
- Remove numbers 2 and 3, then nums = []
There are no more pairs that sum up to 5, hence a total of 2 operations.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,3,4,3], k = 6
<strong>Output:</strong> 1
<strong>Explanation:</strong> Starting with nums = [3,1,3,4,3]:
- Remove the first two 3's, then nums = [1,4,3]
There are no more pairs that sum up to 6, hence a total of 1 operation.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int maxOperations(vector<int>& nums, int k) {\n int n = nums.size();\n vector<bool> used(n,false);\n int op = 0;\n int i = 0;\n int j = nums.size()-1;\n \n sort(nums.begin(), nums.end());\n while(i < j){\n int sum = nums[i] + nums[j];\n if(sum == k){\n op++;\n i++;\n j--;\n }else if(sum > k) j--;\n else i++;\n }\n\n return op;\n }\n};",
"memory": "62121"
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.