id int64 1 3.58k | problem_description stringlengths 516 21.8k | instruction int64 0 3 | solution_c dict |
|---|---|---|---|
2,119 | <p>You are given an integer array <code>nums</code>. In one operation, you can replace <strong>any</strong> element in <code>nums</code> with <strong>any</strong> integer.</p>
<p><code>nums</code> is considered <strong>continuous</strong> if both of the following conditions are fulfilled:</p>
<ul>
<li>All elements in <code>nums</code> are <strong>unique</strong>.</li>
<li>The difference between the <strong>maximum</strong> element and the <strong>minimum</strong> element in <code>nums</code> equals <code>nums.length - 1</code>.</li>
</ul>
<p>For example, <code>nums = [4, 2, 5, 3]</code> is <strong>continuous</strong>, but <code>nums = [1, 2, 3, 5, 6]</code> is <strong>not continuous</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of operations to make </em><code>nums</code><em> </em><strong><em>continuous</em></strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,2,5,3]
<strong>Output:</strong> 0
<strong>Explanation:</strong> nums is already continuous.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,5,6]
<strong>Output:</strong> 1
<strong>Explanation:</strong> One possible solution is to change the last element to 4.
The resulting array is [1,2,3,5,4], which is continuous.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,10,100,1000]
<strong>Output:</strong> 3
<strong>Explanation:</strong> One possible solution is to:
- Change the second element to 2.
- Change the third element to 3.
- Change the fourth element to 4.
The resulting array is [1,2,3,4], which is continuous.
</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>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int minOperations(vector<int>& nums) {\n std::sort(nums.begin(), nums.end());\n\n // remove duplicates\n auto new_end = std::unique(nums.begin(), nums.end());\n int dup_count = nums.end() - new_end;\n nums.erase(new_end, nums.end());\n\n int n = nums.size(); \n int max_window_size = 0;\n\n auto left = nums.begin();\n auto right = nums.begin();\n\n while(right != nums.end()) {\n\n \n while (*right - *left > (n+dup_count)-1) {\n left++; // impossible for left to overtake right, since sorted\n }\n\n max_window_size = std::max(max_window_size, static_cast<int>(right - left + 1));\n right++;\n }\n\n return (n+dup_count) - max_window_size;\n }\n};\n\n// 4, 5, 8, 9",
"memory": "68200"
} |
2,119 | <p>You are given an integer array <code>nums</code>. In one operation, you can replace <strong>any</strong> element in <code>nums</code> with <strong>any</strong> integer.</p>
<p><code>nums</code> is considered <strong>continuous</strong> if both of the following conditions are fulfilled:</p>
<ul>
<li>All elements in <code>nums</code> are <strong>unique</strong>.</li>
<li>The difference between the <strong>maximum</strong> element and the <strong>minimum</strong> element in <code>nums</code> equals <code>nums.length - 1</code>.</li>
</ul>
<p>For example, <code>nums = [4, 2, 5, 3]</code> is <strong>continuous</strong>, but <code>nums = [1, 2, 3, 5, 6]</code> is <strong>not continuous</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of operations to make </em><code>nums</code><em> </em><strong><em>continuous</em></strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,2,5,3]
<strong>Output:</strong> 0
<strong>Explanation:</strong> nums is already continuous.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,5,6]
<strong>Output:</strong> 1
<strong>Explanation:</strong> One possible solution is to change the last element to 4.
The resulting array is [1,2,3,5,4], which is continuous.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,10,100,1000]
<strong>Output:</strong> 3
<strong>Explanation:</strong> One possible solution is to:
- Change the second element to 2.
- Change the third element to 3.
- Change the fourth element to 4.
The resulting array is [1,2,3,4], which is continuous.
</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>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int minOperations(vector<int>& nums) {\n std::sort(nums.begin(), nums.end(), [](int a, int b) {return a>b;});\n std::vector<int> h;\n // std::unordered_set<int> c;\n int max_continues = 0;\n int last_number=0;\n for (int i : nums) {\n while(h.size() > 0 && h[0]-i>nums.size()-1) {\n std::pop_heap(h.begin(), h.end());\n h.pop_back();\n }\n if(h.size() == 0 || last_number != i){\n h.push_back(i);\n std::push_heap(h.begin(), h.end());\n last_number = i;\n }\n if (h.size() > max_continues) {\n max_continues = h.size();\n }\n }\n\n return nums.size()-max_continues;\n }\n};",
"memory": "70500"
} |
2,119 | <p>You are given an integer array <code>nums</code>. In one operation, you can replace <strong>any</strong> element in <code>nums</code> with <strong>any</strong> integer.</p>
<p><code>nums</code> is considered <strong>continuous</strong> if both of the following conditions are fulfilled:</p>
<ul>
<li>All elements in <code>nums</code> are <strong>unique</strong>.</li>
<li>The difference between the <strong>maximum</strong> element and the <strong>minimum</strong> element in <code>nums</code> equals <code>nums.length - 1</code>.</li>
</ul>
<p>For example, <code>nums = [4, 2, 5, 3]</code> is <strong>continuous</strong>, but <code>nums = [1, 2, 3, 5, 6]</code> is <strong>not continuous</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of operations to make </em><code>nums</code><em> </em><strong><em>continuous</em></strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,2,5,3]
<strong>Output:</strong> 0
<strong>Explanation:</strong> nums is already continuous.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,5,6]
<strong>Output:</strong> 1
<strong>Explanation:</strong> One possible solution is to change the last element to 4.
The resulting array is [1,2,3,5,4], which is continuous.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,10,100,1000]
<strong>Output:</strong> 3
<strong>Explanation:</strong> One possible solution is to:
- Change the second element to 2.
- Change the third element to 3.
- Change the fourth element to 4.
The resulting array is [1,2,3,4], which is continuous.
</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>
</ul>
| 1 | {
"code": "static const int speedup = []{ios::sync_with_stdio(0); cin.tie(0); return 0;}();\n\nclass Solution {\npublic:\n int minOperations(std::vector<int>& nums) {\n std::sort(nums.begin(), nums.end());\n std::vector<int> dup = {0};\n dup.reserve(nums.size());\n for (int i = 1; i < nums.size(); ++i) {\n int equal = nums[i] == nums[i - 1] ? 1 : 0;\n dup.push_back(dup.back() + equal);\n }\n\n int ans = static_cast<int>(nums.size());\n for (int i = 0; i < nums.size(); ++i) {\n int min = nums[i];\n int max = static_cast<int>(min + nums.size() - 1);\n int right = static_cast<int>(std::upper_bound(nums.begin(), nums.end(), max) - nums.begin());\n int equal = dup[right - 1] - dup[i];\n int count = static_cast<int>(equal + i + nums.size() - right);\n ans = std::min(ans, count);\n }\n return ans;\n }\n};",
"memory": "70600"
} |
2,119 | <p>You are given an integer array <code>nums</code>. In one operation, you can replace <strong>any</strong> element in <code>nums</code> with <strong>any</strong> integer.</p>
<p><code>nums</code> is considered <strong>continuous</strong> if both of the following conditions are fulfilled:</p>
<ul>
<li>All elements in <code>nums</code> are <strong>unique</strong>.</li>
<li>The difference between the <strong>maximum</strong> element and the <strong>minimum</strong> element in <code>nums</code> equals <code>nums.length - 1</code>.</li>
</ul>
<p>For example, <code>nums = [4, 2, 5, 3]</code> is <strong>continuous</strong>, but <code>nums = [1, 2, 3, 5, 6]</code> is <strong>not continuous</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of operations to make </em><code>nums</code><em> </em><strong><em>continuous</em></strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,2,5,3]
<strong>Output:</strong> 0
<strong>Explanation:</strong> nums is already continuous.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,5,6]
<strong>Output:</strong> 1
<strong>Explanation:</strong> One possible solution is to change the last element to 4.
The resulting array is [1,2,3,5,4], which is continuous.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,10,100,1000]
<strong>Output:</strong> 3
<strong>Explanation:</strong> One possible solution is to:
- Change the second element to 2.
- Change the third element to 3.
- Change the fourth element to 4.
The resulting array is [1,2,3,4], which is continuous.
</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>
</ul>
| 1 | {
"code": "static const int speedup = []{ios::sync_with_stdio(0); cin.tie(0); return 0;}();\n\nclass Solution {\npublic:\n int minOperations(std::vector<int>& nums) {\n std::sort(nums.begin(), nums.end());\n std::vector<int> dup = {0};\n dup.reserve(nums.size());\n for (int i = 1; i < nums.size(); ++i) {\n int equal = nums[i] == nums[i - 1] ? 1 : 0;\n dup.push_back(dup.back() + equal);\n }\n\n int ans = static_cast<int>(nums.size());\n for (int i = 0; i < nums.size(); ++i) {\n int min = nums[i];\n int max = static_cast<int>(min + nums.size() - 1);\n int right = static_cast<int>(std::upper_bound(nums.begin(), nums.end(), max) - nums.begin());\n int equal = dup[right - 1] - dup[i];\n int count = static_cast<int>(equal + i + nums.size() - right);\n ans = std::min(ans, count);\n }\n return ans;\n }\n};",
"memory": "70600"
} |
2,119 | <p>You are given an integer array <code>nums</code>. In one operation, you can replace <strong>any</strong> element in <code>nums</code> with <strong>any</strong> integer.</p>
<p><code>nums</code> is considered <strong>continuous</strong> if both of the following conditions are fulfilled:</p>
<ul>
<li>All elements in <code>nums</code> are <strong>unique</strong>.</li>
<li>The difference between the <strong>maximum</strong> element and the <strong>minimum</strong> element in <code>nums</code> equals <code>nums.length - 1</code>.</li>
</ul>
<p>For example, <code>nums = [4, 2, 5, 3]</code> is <strong>continuous</strong>, but <code>nums = [1, 2, 3, 5, 6]</code> is <strong>not continuous</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of operations to make </em><code>nums</code><em> </em><strong><em>continuous</em></strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,2,5,3]
<strong>Output:</strong> 0
<strong>Explanation:</strong> nums is already continuous.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,5,6]
<strong>Output:</strong> 1
<strong>Explanation:</strong> One possible solution is to change the last element to 4.
The resulting array is [1,2,3,5,4], which is continuous.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,10,100,1000]
<strong>Output:</strong> 3
<strong>Explanation:</strong> One possible solution is to:
- Change the second element to 2.
- Change the third element to 3.
- Change the fourth element to 4.
The resulting array is [1,2,3,4], which is continuous.
</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>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int minOperations(vector<int>& nums) {\n int n = nums.size();\n vector<int> v = nums;\n sort(v.begin(), v.end());\n //remove duplicates from the sorted array\n v.erase(unique(v.begin(), v.end()), v.end());\n int m = v.size(), max_len = 0, j = 0;\n // Sliding window to find the longest continuous subarray\n for (int i = 0; i < m; ++i) {\n while (j < m && v[j] < v[i] + n) {\n ++j;\n }\n max_len = max(max_len, j - i);\n }\n\n return n - max_len;\n }\n};\n",
"memory": "70700"
} |
2,119 | <p>You are given an integer array <code>nums</code>. In one operation, you can replace <strong>any</strong> element in <code>nums</code> with <strong>any</strong> integer.</p>
<p><code>nums</code> is considered <strong>continuous</strong> if both of the following conditions are fulfilled:</p>
<ul>
<li>All elements in <code>nums</code> are <strong>unique</strong>.</li>
<li>The difference between the <strong>maximum</strong> element and the <strong>minimum</strong> element in <code>nums</code> equals <code>nums.length - 1</code>.</li>
</ul>
<p>For example, <code>nums = [4, 2, 5, 3]</code> is <strong>continuous</strong>, but <code>nums = [1, 2, 3, 5, 6]</code> is <strong>not continuous</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of operations to make </em><code>nums</code><em> </em><strong><em>continuous</em></strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,2,5,3]
<strong>Output:</strong> 0
<strong>Explanation:</strong> nums is already continuous.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,5,6]
<strong>Output:</strong> 1
<strong>Explanation:</strong> One possible solution is to change the last element to 4.
The resulting array is [1,2,3,5,4], which is continuous.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,10,100,1000]
<strong>Output:</strong> 3
<strong>Explanation:</strong> One possible solution is to:
- Change the second element to 2.
- Change the third element to 3.
- Change the fourth element to 4.
The resulting array is [1,2,3,4], which is continuous.
</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>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int minOperations(vector<int>& nums) {\n sort(begin(nums), end(nums));\n int n = nums.size();\n vector<int> dup(n);\n int mx = 1;\n for(int i=0; i<n; ++i) {\n if(i>0)\n {\n dup[i] = dup[i-1];\n if(nums[i] == nums[i-1])\n dup[i]++;\n }\n int low = 0, high = i-1;\n int num = nums[i]-(n-1);\n while(low<=high) {\n int mid = (low+high)/2;\n if(nums[mid]>=num)\n high = mid-1;\n else low = mid+1;\n }\n // cout << i << \" \" << low << \" \" << dup[i] << \" \" << num << \" \";\n // cout << dup[low] << \" \";\n mx = max(mx, i-low+1 - (dup[i]-dup[low]));\n // cout << mx << \"\\n\";\n }\n\n return n-mx;\n }\n};",
"memory": "70800"
} |
2,119 | <p>You are given an integer array <code>nums</code>. In one operation, you can replace <strong>any</strong> element in <code>nums</code> with <strong>any</strong> integer.</p>
<p><code>nums</code> is considered <strong>continuous</strong> if both of the following conditions are fulfilled:</p>
<ul>
<li>All elements in <code>nums</code> are <strong>unique</strong>.</li>
<li>The difference between the <strong>maximum</strong> element and the <strong>minimum</strong> element in <code>nums</code> equals <code>nums.length - 1</code>.</li>
</ul>
<p>For example, <code>nums = [4, 2, 5, 3]</code> is <strong>continuous</strong>, but <code>nums = [1, 2, 3, 5, 6]</code> is <strong>not continuous</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of operations to make </em><code>nums</code><em> </em><strong><em>continuous</em></strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,2,5,3]
<strong>Output:</strong> 0
<strong>Explanation:</strong> nums is already continuous.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,5,6]
<strong>Output:</strong> 1
<strong>Explanation:</strong> One possible solution is to change the last element to 4.
The resulting array is [1,2,3,5,4], which is continuous.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,10,100,1000]
<strong>Output:</strong> 3
<strong>Explanation:</strong> One possible solution is to:
- Change the second element to 2.
- Change the third element to 3.
- Change the fourth element to 4.
The resulting array is [1,2,3,4], which is continuous.
</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>
</ul>
| 1 | {
"code": "#define A(x) (x).begin(), (x).end()\n#define sz(x) ((int)(x).size())\n\nclass Solution {\n int solve(vector<int> &v, int LEN) {\n int n = v.size();\n queue<int> q;\n int best = LEN;\n for(int i = n - 1; i >= 0; --i) {\n int start = v[i];\n int end = v[i] + LEN - 1;\n q.push(start);\n while(!q.empty() && q.front() > end) {\n q.pop();\n }\n int now = LEN - q.size();\n best = min(best, now);\n }\n\n return best;\n }\n \npublic:\n int minOperations(vector<int>&ve) {\n int LEN = sz(ve);\n sort(A(ve));\n ve.erase(unique(A(ve)), ve.end());\n int best = solve(ve, LEN);\n return best;\n }\n};",
"memory": "71100"
} |
2,119 | <p>You are given an integer array <code>nums</code>. In one operation, you can replace <strong>any</strong> element in <code>nums</code> with <strong>any</strong> integer.</p>
<p><code>nums</code> is considered <strong>continuous</strong> if both of the following conditions are fulfilled:</p>
<ul>
<li>All elements in <code>nums</code> are <strong>unique</strong>.</li>
<li>The difference between the <strong>maximum</strong> element and the <strong>minimum</strong> element in <code>nums</code> equals <code>nums.length - 1</code>.</li>
</ul>
<p>For example, <code>nums = [4, 2, 5, 3]</code> is <strong>continuous</strong>, but <code>nums = [1, 2, 3, 5, 6]</code> is <strong>not continuous</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of operations to make </em><code>nums</code><em> </em><strong><em>continuous</em></strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,2,5,3]
<strong>Output:</strong> 0
<strong>Explanation:</strong> nums is already continuous.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,5,6]
<strong>Output:</strong> 1
<strong>Explanation:</strong> One possible solution is to change the last element to 4.
The resulting array is [1,2,3,5,4], which is continuous.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,10,100,1000]
<strong>Output:</strong> 3
<strong>Explanation:</strong> One possible solution is to:
- Change the second element to 2.
- Change the third element to 3.
- Change the fourth element to 4.
The resulting array is [1,2,3,4], which is continuous.
</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>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int minOperations(vector<int>& nums) {\n sort(nums.begin(),nums.end());\n int n=nums.size();\n vector<int> temp;\n temp.push_back(nums[0]);\n for(int i=1;i<n;i++)\n {if(nums[i]!=nums[i-1])\n temp.push_back(nums[i]);\n cout<<nums[i]<<\" \";\n }\n\n int ans=n;\n int left=0;\n int right=0;\n while(left<temp.size())\n { \n while(right<temp.size() && temp[right]<=temp[left]+n-1)\n { \n right++;\n }\n ans=min(ans,n-(right-left));\n left++;\n \n }\n\n return ans;\n }\n};",
"memory": "73300"
} |
2,119 | <p>You are given an integer array <code>nums</code>. In one operation, you can replace <strong>any</strong> element in <code>nums</code> with <strong>any</strong> integer.</p>
<p><code>nums</code> is considered <strong>continuous</strong> if both of the following conditions are fulfilled:</p>
<ul>
<li>All elements in <code>nums</code> are <strong>unique</strong>.</li>
<li>The difference between the <strong>maximum</strong> element and the <strong>minimum</strong> element in <code>nums</code> equals <code>nums.length - 1</code>.</li>
</ul>
<p>For example, <code>nums = [4, 2, 5, 3]</code> is <strong>continuous</strong>, but <code>nums = [1, 2, 3, 5, 6]</code> is <strong>not continuous</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of operations to make </em><code>nums</code><em> </em><strong><em>continuous</em></strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,2,5,3]
<strong>Output:</strong> 0
<strong>Explanation:</strong> nums is already continuous.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,5,6]
<strong>Output:</strong> 1
<strong>Explanation:</strong> One possible solution is to change the last element to 4.
The resulting array is [1,2,3,5,4], which is continuous.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,10,100,1000]
<strong>Output:</strong> 3
<strong>Explanation:</strong> One possible solution is to:
- Change the second element to 2.
- Change the third element to 3.
- Change the fourth element to 4.
The resulting array is [1,2,3,4], which is continuous.
</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>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int minOperations(vector<int>& nums) {\n int n = nums.size();\n sort(nums.begin(), nums.end());\n vector<int> uni;\n uni.push_back(nums[0]);\n for(int i = 1; i<n; i++){\n if(nums[i] != uni[uni.size() - 1]){\n uni.push_back(nums[i]);\n }\n }\n int ans = 1e9;\n int nn = uni.size();\n for(int i = 0; i<nn; i++){\n int ind = upper_bound(uni.begin(), uni.end(), uni[i]+n-1) - uni.begin();\n int nochange = ind - i;\n ans = min(ans, n - nochange);\n }\n return ans;\n \n }\n};",
"memory": "73400"
} |
2,119 | <p>You are given an integer array <code>nums</code>. In one operation, you can replace <strong>any</strong> element in <code>nums</code> with <strong>any</strong> integer.</p>
<p><code>nums</code> is considered <strong>continuous</strong> if both of the following conditions are fulfilled:</p>
<ul>
<li>All elements in <code>nums</code> are <strong>unique</strong>.</li>
<li>The difference between the <strong>maximum</strong> element and the <strong>minimum</strong> element in <code>nums</code> equals <code>nums.length - 1</code>.</li>
</ul>
<p>For example, <code>nums = [4, 2, 5, 3]</code> is <strong>continuous</strong>, but <code>nums = [1, 2, 3, 5, 6]</code> is <strong>not continuous</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of operations to make </em><code>nums</code><em> </em><strong><em>continuous</em></strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,2,5,3]
<strong>Output:</strong> 0
<strong>Explanation:</strong> nums is already continuous.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,5,6]
<strong>Output:</strong> 1
<strong>Explanation:</strong> One possible solution is to change the last element to 4.
The resulting array is [1,2,3,5,4], which is continuous.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,10,100,1000]
<strong>Output:</strong> 3
<strong>Explanation:</strong> One possible solution is to:
- Change the second element to 2.
- Change the third element to 3.
- Change the fourth element to 4.
The resulting array is [1,2,3,4], which is continuous.
</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>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n vector<int> unique(vector<int> &a){\n int n = a.size();\n vector<int> ans;\n for(int i = 1 ; i < n ; i++){\n if(a[i] != a[i - 1]) ans.push_back(a[i - 1]); \n }\n ans.push_back(a[n - 1]);\n return ans;\n }\n int minOperations(vector<int>& a) {\n sort(a.begin(),a.end());\n int ans = INT_MAX;\n int n = a.size();\n a = unique(a);\n int m = a.size();\n int end = a[0] + n - 1;\n for(int i = 0 , j = 0 ; i < m ; ){\n if(j < m && a[j] <= end){\n j++;\n }\n else{\n ans = min(ans,i + n - j);\n i++;\n if(i < m) end = a[i] + n - 1;\n }\n }\n return ans;\n }\n};\n\n// 4 5 8 9",
"memory": "73500"
} |
2,119 | <p>You are given an integer array <code>nums</code>. In one operation, you can replace <strong>any</strong> element in <code>nums</code> with <strong>any</strong> integer.</p>
<p><code>nums</code> is considered <strong>continuous</strong> if both of the following conditions are fulfilled:</p>
<ul>
<li>All elements in <code>nums</code> are <strong>unique</strong>.</li>
<li>The difference between the <strong>maximum</strong> element and the <strong>minimum</strong> element in <code>nums</code> equals <code>nums.length - 1</code>.</li>
</ul>
<p>For example, <code>nums = [4, 2, 5, 3]</code> is <strong>continuous</strong>, but <code>nums = [1, 2, 3, 5, 6]</code> is <strong>not continuous</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of operations to make </em><code>nums</code><em> </em><strong><em>continuous</em></strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,2,5,3]
<strong>Output:</strong> 0
<strong>Explanation:</strong> nums is already continuous.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,5,6]
<strong>Output:</strong> 1
<strong>Explanation:</strong> One possible solution is to change the last element to 4.
The resulting array is [1,2,3,5,4], which is continuous.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,10,100,1000]
<strong>Output:</strong> 3
<strong>Explanation:</strong> One possible solution is to:
- Change the second element to 2.
- Change the third element to 3.
- Change the fourth element to 4.
The resulting array is [1,2,3,4], which is continuous.
</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>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int minOperations(vector<int>& nums) {\n sort(nums.begin(),nums.end());\n vector<int> temp;\n for(int i=0;i<nums.size();){\n int j=i;\n while(j<nums.size() && nums[i]==nums[j]){\n j++;\n }\n temp.push_back(nums[i]);\n i=j;\n }\n int n=nums.size();\n int m=temp.size();\n int ans=INT_MAX;\n for(int i=0;i<m;i++){\n int j=-1;\n int low=i;\n int high=m-1;\n while(low<=high){\n int mid=low+(high-low)/2;\n if(temp[mid]<=(temp[i]+n-1)){\n j=mid;\n low=mid+1;\n }\n else high=mid-1;\n }\n //cout<<(j-i+1)<<\" \";\n ans=min(ans,n-(j-i+1));\n }\n return ans;\n }\n};",
"memory": "73600"
} |
2,119 | <p>You are given an integer array <code>nums</code>. In one operation, you can replace <strong>any</strong> element in <code>nums</code> with <strong>any</strong> integer.</p>
<p><code>nums</code> is considered <strong>continuous</strong> if both of the following conditions are fulfilled:</p>
<ul>
<li>All elements in <code>nums</code> are <strong>unique</strong>.</li>
<li>The difference between the <strong>maximum</strong> element and the <strong>minimum</strong> element in <code>nums</code> equals <code>nums.length - 1</code>.</li>
</ul>
<p>For example, <code>nums = [4, 2, 5, 3]</code> is <strong>continuous</strong>, but <code>nums = [1, 2, 3, 5, 6]</code> is <strong>not continuous</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of operations to make </em><code>nums</code><em> </em><strong><em>continuous</em></strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,2,5,3]
<strong>Output:</strong> 0
<strong>Explanation:</strong> nums is already continuous.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,5,6]
<strong>Output:</strong> 1
<strong>Explanation:</strong> One possible solution is to change the last element to 4.
The resulting array is [1,2,3,5,4], which is continuous.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,10,100,1000]
<strong>Output:</strong> 3
<strong>Explanation:</strong> One possible solution is to:
- Change the second element to 2.
- Change the third element to 3.
- Change the fourth element to 4.
The resulting array is [1,2,3,4], which is continuous.
</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>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int minOperations(vector<int>& nums) {\n vector<int>s;\n sort(nums.begin(),nums.end());\n for(int i=0;i<nums.size();i++){\n if(s.empty()||s[s.size()-1]!=nums[i]){\n s.push_back(nums[i]);\n }\n }\n int ans=1e9;\n for(int i=0;i<s.size();i++){\n int last = s[i]+nums.size()-1;\n auto it = upper_bound(s.begin(),s.end(),last);\n int x = it-s.begin();\n int req = nums.size() - (x - i);\n ans = min(ans,req);\n }\n return ans;\n }\n};",
"memory": "73600"
} |
2,119 | <p>You are given an integer array <code>nums</code>. In one operation, you can replace <strong>any</strong> element in <code>nums</code> with <strong>any</strong> integer.</p>
<p><code>nums</code> is considered <strong>continuous</strong> if both of the following conditions are fulfilled:</p>
<ul>
<li>All elements in <code>nums</code> are <strong>unique</strong>.</li>
<li>The difference between the <strong>maximum</strong> element and the <strong>minimum</strong> element in <code>nums</code> equals <code>nums.length - 1</code>.</li>
</ul>
<p>For example, <code>nums = [4, 2, 5, 3]</code> is <strong>continuous</strong>, but <code>nums = [1, 2, 3, 5, 6]</code> is <strong>not continuous</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of operations to make </em><code>nums</code><em> </em><strong><em>continuous</em></strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,2,5,3]
<strong>Output:</strong> 0
<strong>Explanation:</strong> nums is already continuous.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,5,6]
<strong>Output:</strong> 1
<strong>Explanation:</strong> One possible solution is to change the last element to 4.
The resulting array is [1,2,3,5,4], which is continuous.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,10,100,1000]
<strong>Output:</strong> 3
<strong>Explanation:</strong> One possible solution is to:
- Change the second element to 2.
- Change the third element to 3.
- Change the fourth element to 4.
The resulting array is [1,2,3,4], which is continuous.
</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>
</ul>
| 1 | {
"code": "\nvoid print(auto v)\n{\n // cout << (#v) << \": \";\n for (auto ele : v)\n cout << ele << \" \";\n cout << endl;\n}\n\nclass Solution {\npublic:\n int minOperations(vector<int>& v) {\n int n = v.size();\n sort(v.begin(), v.end());\n vector<int> dist_cnt(n);\n int ans = n;\n\n int prev = -1;\n int counter = 0;\n\n for (int i=0; i<n; i++)\n {\n if (v[i] != prev)\n counter++;\n dist_cnt[i] = counter;\n prev = v[i];\n }\n\n print(v);\n print(dist_cnt);\n\n for (int i=0; i<n; i++)\n {\n int start = v[i];\n int end = start + n - 1;\n int ind = i;\n int l = i, r = n - 1;\n while (l <= r)\n {\n int mid = (l + r) / 2;\n if (v[mid] <= end)\n {\n ind = mid;\n l = mid + 1;\n }\n else \n r = mid - 1;\n }\n\n // 0 1 2 3 4\n int sub_arr_sz = ind - i + 1;\n int dup_cnt = sub_arr_sz - (dist_cnt[ind] - dist_cnt[i] + 1);\n int rem = n - sub_arr_sz + dup_cnt; \n ans = min(ans, rem);\n }\n\n return ans;\n }\n};\n\n// 4 5 8 8 9 9 ",
"memory": "75600"
} |
2,119 | <p>You are given an integer array <code>nums</code>. In one operation, you can replace <strong>any</strong> element in <code>nums</code> with <strong>any</strong> integer.</p>
<p><code>nums</code> is considered <strong>continuous</strong> if both of the following conditions are fulfilled:</p>
<ul>
<li>All elements in <code>nums</code> are <strong>unique</strong>.</li>
<li>The difference between the <strong>maximum</strong> element and the <strong>minimum</strong> element in <code>nums</code> equals <code>nums.length - 1</code>.</li>
</ul>
<p>For example, <code>nums = [4, 2, 5, 3]</code> is <strong>continuous</strong>, but <code>nums = [1, 2, 3, 5, 6]</code> is <strong>not continuous</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of operations to make </em><code>nums</code><em> </em><strong><em>continuous</em></strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,2,5,3]
<strong>Output:</strong> 0
<strong>Explanation:</strong> nums is already continuous.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,5,6]
<strong>Output:</strong> 1
<strong>Explanation:</strong> One possible solution is to change the last element to 4.
The resulting array is [1,2,3,5,4], which is continuous.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,10,100,1000]
<strong>Output:</strong> 3
<strong>Explanation:</strong> One possible solution is to:
- Change the second element to 2.
- Change the third element to 3.
- Change the fourth element to 4.
The resulting array is [1,2,3,4], which is continuous.
</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>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int minOperations(vector<int>& nums) {\n int n= nums.size();\n sort(nums.begin(),nums.end());\n vector<int> a;\n a.push_back(nums[0]);\n \n for(int i=1; i<n; i++){\n if(nums[i-1]!=nums[i]){\n a.push_back(nums[i]);\n }\n }\n\n vector<int> noe(a.size(),0);\n\n for(int i=0; i<a.size(); i++){\n int x= a[i]+n-1;\n\n int ind= upper_bound(a.begin(),a.end(),a[i]+n-1) - a.begin();\n ind--;\n noe[i]=ind-i+1;\n }\n\n int maxi = *max_element(noe.begin(),noe.end());\n\n return n-maxi;\n }\n};",
"memory": "76000"
} |
2,119 | <p>You are given an integer array <code>nums</code>. In one operation, you can replace <strong>any</strong> element in <code>nums</code> with <strong>any</strong> integer.</p>
<p><code>nums</code> is considered <strong>continuous</strong> if both of the following conditions are fulfilled:</p>
<ul>
<li>All elements in <code>nums</code> are <strong>unique</strong>.</li>
<li>The difference between the <strong>maximum</strong> element and the <strong>minimum</strong> element in <code>nums</code> equals <code>nums.length - 1</code>.</li>
</ul>
<p>For example, <code>nums = [4, 2, 5, 3]</code> is <strong>continuous</strong>, but <code>nums = [1, 2, 3, 5, 6]</code> is <strong>not continuous</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of operations to make </em><code>nums</code><em> </em><strong><em>continuous</em></strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,2,5,3]
<strong>Output:</strong> 0
<strong>Explanation:</strong> nums is already continuous.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,5,6]
<strong>Output:</strong> 1
<strong>Explanation:</strong> One possible solution is to change the last element to 4.
The resulting array is [1,2,3,5,4], which is continuous.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,10,100,1000]
<strong>Output:</strong> 3
<strong>Explanation:</strong> One possible solution is to:
- Change the second element to 2.
- Change the third element to 3.
- Change the fourth element to 4.
The resulting array is [1,2,3,4], which is continuous.
</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>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int minOperations(vector<int>& nums) {\n int n=nums.size();\n vector<long long>v;\n sort(nums.begin(),nums.end());\n v.push_back(nums[0]);\n for(int i=1;i<n;++i){\n int pos=v.size()-1;\n if(nums[i]!=v[pos]) v.push_back(nums[i]);\n }\n int ans=1e6;\n for(int i=0;i<v.size();++i){\n int pos=upper_bound(v.begin(),v.end(),v[i]+n-1)-v.begin();\n ans=min(ans,n-pos+i);\n }\n return ans;\n }\n};",
"memory": "78900"
} |
2,119 | <p>You are given an integer array <code>nums</code>. In one operation, you can replace <strong>any</strong> element in <code>nums</code> with <strong>any</strong> integer.</p>
<p><code>nums</code> is considered <strong>continuous</strong> if both of the following conditions are fulfilled:</p>
<ul>
<li>All elements in <code>nums</code> are <strong>unique</strong>.</li>
<li>The difference between the <strong>maximum</strong> element and the <strong>minimum</strong> element in <code>nums</code> equals <code>nums.length - 1</code>.</li>
</ul>
<p>For example, <code>nums = [4, 2, 5, 3]</code> is <strong>continuous</strong>, but <code>nums = [1, 2, 3, 5, 6]</code> is <strong>not continuous</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of operations to make </em><code>nums</code><em> </em><strong><em>continuous</em></strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,2,5,3]
<strong>Output:</strong> 0
<strong>Explanation:</strong> nums is already continuous.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,5,6]
<strong>Output:</strong> 1
<strong>Explanation:</strong> One possible solution is to change the last element to 4.
The resulting array is [1,2,3,5,4], which is continuous.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,10,100,1000]
<strong>Output:</strong> 3
<strong>Explanation:</strong> One possible solution is to:
- Change the second element to 2.
- Change the third element to 3.
- Change the fourth element to 4.
The resulting array is [1,2,3,4], which is continuous.
</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>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int minOperations(vector<int>& nums) {\n sort(nums.begin(), nums.end());\n unordered_map<int,int> values;\n\n int start= 0, end = 0, result = nums.size() - 1;\n while (end < nums.size()) {\n values[nums[end]] += 1;\n\n while (start < end && (nums[end] - nums[start]) > nums.size() - 1) {\n values[nums[start]]--;\n if (values[nums[start]] == 0) {\n values.erase(nums[start]);\n }\n start++;\n }\n\n int windowSize = (end - start) + 1;\n int amtToChange = (windowSize - values.size()) + (nums.size() - windowSize);\n result = min(result, amtToChange);\n end++;\n }\n\n return result;\n /*\n [1,10,100,1000]\n sort the array\n sliding window\n hash table vals\n \n iterate through array w/ window\n window only hold values nums[start] - (nums[start] + (nums.size - 1))\n insert uniqeVales totable\n amtToChange = (window size - values.size) + (nums.size - window size)\n --- Find min amtToChange --\n\n int start = 0, end = 0, result = nums.size;\n \n while (end < nums.size)\n vals[nums[end]] += 1;\n\n while (start < end && nums[end] - num[start]) {\n vals[startVal]--\n remove startVal from vals if count == 0\n start++\n }\n\n int windowSize = (end - start) + 1;\n amtToChange = (windowSize - values.size) + (nums.size - windowSize)\n result = min(result, amtToChange)\n \n return amtToChange\n */\n }\n};",
"memory": "96500"
} |
2,119 | <p>You are given an integer array <code>nums</code>. In one operation, you can replace <strong>any</strong> element in <code>nums</code> with <strong>any</strong> integer.</p>
<p><code>nums</code> is considered <strong>continuous</strong> if both of the following conditions are fulfilled:</p>
<ul>
<li>All elements in <code>nums</code> are <strong>unique</strong>.</li>
<li>The difference between the <strong>maximum</strong> element and the <strong>minimum</strong> element in <code>nums</code> equals <code>nums.length - 1</code>.</li>
</ul>
<p>For example, <code>nums = [4, 2, 5, 3]</code> is <strong>continuous</strong>, but <code>nums = [1, 2, 3, 5, 6]</code> is <strong>not continuous</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of operations to make </em><code>nums</code><em> </em><strong><em>continuous</em></strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,2,5,3]
<strong>Output:</strong> 0
<strong>Explanation:</strong> nums is already continuous.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,5,6]
<strong>Output:</strong> 1
<strong>Explanation:</strong> One possible solution is to change the last element to 4.
The resulting array is [1,2,3,5,4], which is continuous.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,10,100,1000]
<strong>Output:</strong> 3
<strong>Explanation:</strong> One possible solution is to:
- Change the second element to 2.
- Change the third element to 3.
- Change the fourth element to 4.
The resulting array is [1,2,3,4], which is continuous.
</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>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int minOperations(vector<int>& nums) {\n // [4,5,8,8,9,9]\n sort(nums.begin(), nums.end());\n unordered_map<int,int> m;\n int n = nums.size();\n int res = n; \n int l = 0;\n for(int i = 0; i < n; i++){\n while(nums[i] - nums[l] >= n) {\n m[nums[l]]--;\n if(m[nums[l]] == 0) m.erase(nums[l]);\n l++;\n }\n m[nums[i]]++;\n if (m.size() != i - l + 1) {\n res = min(res, n - (int)m.size());\n }else {\n res = min(res, n - (i - l + 1));\n }\n }\n return res;\n }\n};",
"memory": "96600"
} |
2,119 | <p>You are given an integer array <code>nums</code>. In one operation, you can replace <strong>any</strong> element in <code>nums</code> with <strong>any</strong> integer.</p>
<p><code>nums</code> is considered <strong>continuous</strong> if both of the following conditions are fulfilled:</p>
<ul>
<li>All elements in <code>nums</code> are <strong>unique</strong>.</li>
<li>The difference between the <strong>maximum</strong> element and the <strong>minimum</strong> element in <code>nums</code> equals <code>nums.length - 1</code>.</li>
</ul>
<p>For example, <code>nums = [4, 2, 5, 3]</code> is <strong>continuous</strong>, but <code>nums = [1, 2, 3, 5, 6]</code> is <strong>not continuous</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of operations to make </em><code>nums</code><em> </em><strong><em>continuous</em></strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,2,5,3]
<strong>Output:</strong> 0
<strong>Explanation:</strong> nums is already continuous.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,5,6]
<strong>Output:</strong> 1
<strong>Explanation:</strong> One possible solution is to change the last element to 4.
The resulting array is [1,2,3,5,4], which is continuous.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,10,100,1000]
<strong>Output:</strong> 3
<strong>Explanation:</strong> One possible solution is to:
- Change the second element to 2.
- Change the third element to 3.
- Change the fourth element to 4.
The resulting array is [1,2,3,4], which is continuous.
</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>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int minOperations(vector<int>& nums) {\n std::sort(nums.begin(), nums.end(), [](int a, int b) {return a>b;});\n std::vector<int> h;\n std::unordered_set<int> c;\n int max_continues = 0;\n for (int i : nums) {\n while(h.size() > 0 && h[0]-i>nums.size()-1) {\n std::pop_heap(h.begin(), h.end());\n c.erase(h.back());\n h.pop_back();\n }\n auto b = c.emplace(i);\n if(b.second){\n h.push_back(i);\n std::push_heap(h.begin(), h.end());\n }\n if (h.size() > max_continues) {\n max_continues = c.size();\n }\n }\n\n return nums.size()-max_continues;\n }\n};",
"memory": "101100"
} |
2,119 | <p>You are given an integer array <code>nums</code>. In one operation, you can replace <strong>any</strong> element in <code>nums</code> with <strong>any</strong> integer.</p>
<p><code>nums</code> is considered <strong>continuous</strong> if both of the following conditions are fulfilled:</p>
<ul>
<li>All elements in <code>nums</code> are <strong>unique</strong>.</li>
<li>The difference between the <strong>maximum</strong> element and the <strong>minimum</strong> element in <code>nums</code> equals <code>nums.length - 1</code>.</li>
</ul>
<p>For example, <code>nums = [4, 2, 5, 3]</code> is <strong>continuous</strong>, but <code>nums = [1, 2, 3, 5, 6]</code> is <strong>not continuous</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of operations to make </em><code>nums</code><em> </em><strong><em>continuous</em></strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,2,5,3]
<strong>Output:</strong> 0
<strong>Explanation:</strong> nums is already continuous.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,5,6]
<strong>Output:</strong> 1
<strong>Explanation:</strong> One possible solution is to change the last element to 4.
The resulting array is [1,2,3,5,4], which is continuous.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,10,100,1000]
<strong>Output:</strong> 3
<strong>Explanation:</strong> One possible solution is to:
- Change the second element to 2.
- Change the third element to 3.
- Change the fourth element to 4.
The resulting array is [1,2,3,4], which is continuous.
</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>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int minOperations(vector<int>& nums) {\n std::sort(nums.begin(), nums.end(), [](int a, int b) {return a>b;});\n std::vector<int> h;\n std::unordered_set<int> c;\n int max_continues = 0;\n for (int i : nums) {\n while(h.size() > 0 && h[0]-i>nums.size()-1) {\n std::pop_heap(h.begin(), h.end());\n c.erase(h.back());\n h.pop_back();\n }\n auto b = c.emplace(i);\n if(b.second){\n h.push_back(i);\n std::push_heap(h.begin(), h.end());\n }\n if (h.size() > max_continues) {\n max_continues = c.size();\n }\n }\n\n return nums.size()-max_continues;\n }\n};",
"memory": "101200"
} |
2,119 | <p>You are given an integer array <code>nums</code>. In one operation, you can replace <strong>any</strong> element in <code>nums</code> with <strong>any</strong> integer.</p>
<p><code>nums</code> is considered <strong>continuous</strong> if both of the following conditions are fulfilled:</p>
<ul>
<li>All elements in <code>nums</code> are <strong>unique</strong>.</li>
<li>The difference between the <strong>maximum</strong> element and the <strong>minimum</strong> element in <code>nums</code> equals <code>nums.length - 1</code>.</li>
</ul>
<p>For example, <code>nums = [4, 2, 5, 3]</code> is <strong>continuous</strong>, but <code>nums = [1, 2, 3, 5, 6]</code> is <strong>not continuous</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of operations to make </em><code>nums</code><em> </em><strong><em>continuous</em></strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,2,5,3]
<strong>Output:</strong> 0
<strong>Explanation:</strong> nums is already continuous.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,5,6]
<strong>Output:</strong> 1
<strong>Explanation:</strong> One possible solution is to change the last element to 4.
The resulting array is [1,2,3,5,4], which is continuous.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,10,100,1000]
<strong>Output:</strong> 3
<strong>Explanation:</strong> One possible solution is to:
- Change the second element to 2.
- Change the third element to 3.
- Change the fourth element to 4.
The resulting array is [1,2,3,4], which is continuous.
</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>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int minOperations(vector<int>& nums) {\n std::sort(nums.begin(), nums.end(), [](int a, int b) {return a>b;});\n std::vector<int> h;\n std::unordered_set<int> c;\n int max_continues = 0;\n for (int i : nums) {\n while(h.size() > 0 && h[0]-i>nums.size()-1) {\n std::pop_heap(h.begin(), h.end());\n c.erase(h.back());\n h.pop_back();\n }\n auto b = c.emplace(i);\n if(b.second){\n h.push_back(i);\n std::push_heap(h.begin(), h.end());\n }\n if (h.size() > max_continues) {\n max_continues = c.size();\n }\n }\n\n return nums.size()-max_continues;\n }\n};",
"memory": "101300"
} |
2,119 | <p>You are given an integer array <code>nums</code>. In one operation, you can replace <strong>any</strong> element in <code>nums</code> with <strong>any</strong> integer.</p>
<p><code>nums</code> is considered <strong>continuous</strong> if both of the following conditions are fulfilled:</p>
<ul>
<li>All elements in <code>nums</code> are <strong>unique</strong>.</li>
<li>The difference between the <strong>maximum</strong> element and the <strong>minimum</strong> element in <code>nums</code> equals <code>nums.length - 1</code>.</li>
</ul>
<p>For example, <code>nums = [4, 2, 5, 3]</code> is <strong>continuous</strong>, but <code>nums = [1, 2, 3, 5, 6]</code> is <strong>not continuous</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of operations to make </em><code>nums</code><em> </em><strong><em>continuous</em></strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,2,5,3]
<strong>Output:</strong> 0
<strong>Explanation:</strong> nums is already continuous.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,5,6]
<strong>Output:</strong> 1
<strong>Explanation:</strong> One possible solution is to change the last element to 4.
The resulting array is [1,2,3,5,4], which is continuous.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,10,100,1000]
<strong>Output:</strong> 3
<strong>Explanation:</strong> One possible solution is to:
- Change the second element to 2.
- Change the third element to 3.
- Change the fourth element to 4.
The resulting array is [1,2,3,4], which is continuous.
</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>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int minOperations(vector<int>& nums) {\n sort(nums.begin(),nums.end());\n int i=0;\n int ans = 0,curr = 0;\n int n = nums.size();\n unordered_map<int,int> m;\n for(int j=0;j<n;j++){\n // cout<<nums[j]<<\" \";\n m[nums[j]]++;\n while(nums[j]>nums[i]+n-1){\n m[nums[i]]--;\n if(m[nums[i]]==0){\n curr--;\n }\n i++;\n }\n if(nums[j]<=nums[i]+n-1 && m[nums[j]]==1) curr++;\n // cout<<i<<\" \"<<j<<\" \"<<curr<<endl;\n ans = max(ans,curr);\n }\n return n-ans;\n }\n};",
"memory": "104800"
} |
2,119 | <p>You are given an integer array <code>nums</code>. In one operation, you can replace <strong>any</strong> element in <code>nums</code> with <strong>any</strong> integer.</p>
<p><code>nums</code> is considered <strong>continuous</strong> if both of the following conditions are fulfilled:</p>
<ul>
<li>All elements in <code>nums</code> are <strong>unique</strong>.</li>
<li>The difference between the <strong>maximum</strong> element and the <strong>minimum</strong> element in <code>nums</code> equals <code>nums.length - 1</code>.</li>
</ul>
<p>For example, <code>nums = [4, 2, 5, 3]</code> is <strong>continuous</strong>, but <code>nums = [1, 2, 3, 5, 6]</code> is <strong>not continuous</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of operations to make </em><code>nums</code><em> </em><strong><em>continuous</em></strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,2,5,3]
<strong>Output:</strong> 0
<strong>Explanation:</strong> nums is already continuous.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,5,6]
<strong>Output:</strong> 1
<strong>Explanation:</strong> One possible solution is to change the last element to 4.
The resulting array is [1,2,3,5,4], which is continuous.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,10,100,1000]
<strong>Output:</strong> 3
<strong>Explanation:</strong> One possible solution is to:
- Change the second element to 2.
- Change the third element to 3.
- Change the fourth element to 4.
The resulting array is [1,2,3,4], which is continuous.
</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>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int minOperations(vector<int>& nums) {\n int n = nums.size();\n sort(nums.begin(), nums.end());\n unordered_set<int> a(nums.begin(), nums.end());\n int res = n, r=0;\n nums.erase(unique(begin(nums), end(nums)), end(nums));\n int m = nums.size();\n for(int l=0;l<m;l++){\n while(r<m && nums[r] < nums[l] + n) ++r;\n res = min(res, n-r+l);\n }\n return res;\n }\n};",
"memory": "104900"
} |
2,119 | <p>You are given an integer array <code>nums</code>. In one operation, you can replace <strong>any</strong> element in <code>nums</code> with <strong>any</strong> integer.</p>
<p><code>nums</code> is considered <strong>continuous</strong> if both of the following conditions are fulfilled:</p>
<ul>
<li>All elements in <code>nums</code> are <strong>unique</strong>.</li>
<li>The difference between the <strong>maximum</strong> element and the <strong>minimum</strong> element in <code>nums</code> equals <code>nums.length - 1</code>.</li>
</ul>
<p>For example, <code>nums = [4, 2, 5, 3]</code> is <strong>continuous</strong>, but <code>nums = [1, 2, 3, 5, 6]</code> is <strong>not continuous</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of operations to make </em><code>nums</code><em> </em><strong><em>continuous</em></strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,2,5,3]
<strong>Output:</strong> 0
<strong>Explanation:</strong> nums is already continuous.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,5,6]
<strong>Output:</strong> 1
<strong>Explanation:</strong> One possible solution is to change the last element to 4.
The resulting array is [1,2,3,5,4], which is continuous.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,10,100,1000]
<strong>Output:</strong> 3
<strong>Explanation:</strong> One possible solution is to:
- Change the second element to 2.
- Change the third element to 3.
- Change the fourth element to 4.
The resulting array is [1,2,3,4], which is continuous.
</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>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int minOperations(vector<int>& nums) {\n unordered_map<int ,int>mp;\n\n int n = nums.size();\n for(int i = 0; i<n ;i++){\n\n if(mp[nums[i]] == 1){\n nums[i] = INT_MAX;\n }\n else {\n mp[nums[i]] = 1;\n }\n }\n sort(nums.begin(), nums.end());\n\n int operation = 0;\n\n for(int i = 0; i<n ;i++){\n\n if(nums[i] != INT_MAX){\n\n int start = nums[i];\n int end = start + n-1;\n\n int indxOfEnd = upper_bound(nums.begin(), nums.end(), end) - nums.begin();\n indxOfEnd--;\n\n int presentEle = indxOfEnd - i + 1;\n operation = max(operation, presentEle);\n }\n }\n return n - operation;\n }\n};",
"memory": "105000"
} |
2,119 | <p>You are given an integer array <code>nums</code>. In one operation, you can replace <strong>any</strong> element in <code>nums</code> with <strong>any</strong> integer.</p>
<p><code>nums</code> is considered <strong>continuous</strong> if both of the following conditions are fulfilled:</p>
<ul>
<li>All elements in <code>nums</code> are <strong>unique</strong>.</li>
<li>The difference between the <strong>maximum</strong> element and the <strong>minimum</strong> element in <code>nums</code> equals <code>nums.length - 1</code>.</li>
</ul>
<p>For example, <code>nums = [4, 2, 5, 3]</code> is <strong>continuous</strong>, but <code>nums = [1, 2, 3, 5, 6]</code> is <strong>not continuous</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of operations to make </em><code>nums</code><em> </em><strong><em>continuous</em></strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,2,5,3]
<strong>Output:</strong> 0
<strong>Explanation:</strong> nums is already continuous.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,5,6]
<strong>Output:</strong> 1
<strong>Explanation:</strong> One possible solution is to change the last element to 4.
The resulting array is [1,2,3,5,4], which is continuous.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,10,100,1000]
<strong>Output:</strong> 3
<strong>Explanation:</strong> One possible solution is to:
- Change the second element to 2.
- Change the third element to 3.
- Change the fourth element to 4.
The resulting array is [1,2,3,4], which is continuous.
</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>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int minOperations(vector<int>& nums) {\n int n = nums.size();\n sort(nums.begin(), nums.end());\n int i = 0, j = 0; \n int result = 0;\n int repeated = 0;\n unordered_map<int, int> mp;\n while (i < n) {\n while (j < n and nums[j] - nums[i] <= n - 1) {\n mp[nums[j]]++;\n if (mp[nums[j]] > 1)\n repeated++;\n ++j;\n }\n result = max(result, j - i - repeated);\n mp[nums[i]]--;\n if (mp[nums[i]] != 0)\n repeated--;\n ++i;\n }\n return n - result;\n }\n};",
"memory": "105100"
} |
2,119 | <p>You are given an integer array <code>nums</code>. In one operation, you can replace <strong>any</strong> element in <code>nums</code> with <strong>any</strong> integer.</p>
<p><code>nums</code> is considered <strong>continuous</strong> if both of the following conditions are fulfilled:</p>
<ul>
<li>All elements in <code>nums</code> are <strong>unique</strong>.</li>
<li>The difference between the <strong>maximum</strong> element and the <strong>minimum</strong> element in <code>nums</code> equals <code>nums.length - 1</code>.</li>
</ul>
<p>For example, <code>nums = [4, 2, 5, 3]</code> is <strong>continuous</strong>, but <code>nums = [1, 2, 3, 5, 6]</code> is <strong>not continuous</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of operations to make </em><code>nums</code><em> </em><strong><em>continuous</em></strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,2,5,3]
<strong>Output:</strong> 0
<strong>Explanation:</strong> nums is already continuous.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,5,6]
<strong>Output:</strong> 1
<strong>Explanation:</strong> One possible solution is to change the last element to 4.
The resulting array is [1,2,3,5,4], which is continuous.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,10,100,1000]
<strong>Output:</strong> 3
<strong>Explanation:</strong> One possible solution is to:
- Change the second element to 2.
- Change the third element to 3.
- Change the fourth element to 4.
The resulting array is [1,2,3,4], which is continuous.
</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>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n // int binary_search(vector<int>&nums, int idx, int val){\n // return x - idx;\n // }\n int minOperations(vector<int>& nums) {\n int n = nums.size(), res = INT_MAX;\n set<int> s(nums.begin(), nums.end());\n nums.clear();\n nums.assign(s.begin(), s.end());\n\n int m = nums.size();\n\n for(int i = 0; i < m; i++){\n int temp = upper_bound(nums.begin(), nums.end(), nums[i] + n - 1) - nums.begin();\n // int temp = binary_search(nums, i, nums[i] + n - 1);\n res = min(res, n - temp + i);\n }\n\n return res;\n }\n};",
"memory": "106600"
} |
2,119 | <p>You are given an integer array <code>nums</code>. In one operation, you can replace <strong>any</strong> element in <code>nums</code> with <strong>any</strong> integer.</p>
<p><code>nums</code> is considered <strong>continuous</strong> if both of the following conditions are fulfilled:</p>
<ul>
<li>All elements in <code>nums</code> are <strong>unique</strong>.</li>
<li>The difference between the <strong>maximum</strong> element and the <strong>minimum</strong> element in <code>nums</code> equals <code>nums.length - 1</code>.</li>
</ul>
<p>For example, <code>nums = [4, 2, 5, 3]</code> is <strong>continuous</strong>, but <code>nums = [1, 2, 3, 5, 6]</code> is <strong>not continuous</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of operations to make </em><code>nums</code><em> </em><strong><em>continuous</em></strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,2,5,3]
<strong>Output:</strong> 0
<strong>Explanation:</strong> nums is already continuous.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,5,6]
<strong>Output:</strong> 1
<strong>Explanation:</strong> One possible solution is to change the last element to 4.
The resulting array is [1,2,3,5,4], which is continuous.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,10,100,1000]
<strong>Output:</strong> 3
<strong>Explanation:</strong> One possible solution is to:
- Change the second element to 2.
- Change the third element to 3.
- Change the fourth element to 4.
The resulting array is [1,2,3,4], which is continuous.
</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>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int minOperations(vector<int>& nums) {\n int n=nums.size();\n set<int>st(nums.begin(),nums.end());\n nums.assign(st.begin(),st.end());\n int ans=INT_MAX;\n for(int i=0;i<nums.size();i++){\n int x=nums[i];\n int y=x+n-1;\n int it=upper_bound(nums.begin(),nums.end(),y)-nums.begin();\n ans=min(ans,n-(it-i));\n }\n return ans;\n }\n};",
"memory": "106700"
} |
2,119 | <p>You are given an integer array <code>nums</code>. In one operation, you can replace <strong>any</strong> element in <code>nums</code> with <strong>any</strong> integer.</p>
<p><code>nums</code> is considered <strong>continuous</strong> if both of the following conditions are fulfilled:</p>
<ul>
<li>All elements in <code>nums</code> are <strong>unique</strong>.</li>
<li>The difference between the <strong>maximum</strong> element and the <strong>minimum</strong> element in <code>nums</code> equals <code>nums.length - 1</code>.</li>
</ul>
<p>For example, <code>nums = [4, 2, 5, 3]</code> is <strong>continuous</strong>, but <code>nums = [1, 2, 3, 5, 6]</code> is <strong>not continuous</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of operations to make </em><code>nums</code><em> </em><strong><em>continuous</em></strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,2,5,3]
<strong>Output:</strong> 0
<strong>Explanation:</strong> nums is already continuous.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,5,6]
<strong>Output:</strong> 1
<strong>Explanation:</strong> One possible solution is to change the last element to 4.
The resulting array is [1,2,3,5,4], which is continuous.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,10,100,1000]
<strong>Output:</strong> 3
<strong>Explanation:</strong> One possible solution is to:
- Change the second element to 2.
- Change the third element to 3.
- Change the fourth element to 4.
The resulting array is [1,2,3,4], which is continuous.
</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>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int minOperations(vector<int>& nums) {\n int n=nums.size();\n set<int> st;\n for(auto it:nums)st.insert(it);\n nums.resize(0);\n for(auto it:st)nums.push_back(it);\n int ans=n;\n for(int i=0;i<nums.size();i++){\n int first=nums[i];\n int last=first+n-1;\n vector<int>::iterator upper=upper_bound(nums.begin(),nums.end(),last);\n int len=upper-nums.begin();\n ans=min(ans,n-(len-i));\n }\n return ans;\n }\n};",
"memory": "106800"
} |
2,119 | <p>You are given an integer array <code>nums</code>. In one operation, you can replace <strong>any</strong> element in <code>nums</code> with <strong>any</strong> integer.</p>
<p><code>nums</code> is considered <strong>continuous</strong> if both of the following conditions are fulfilled:</p>
<ul>
<li>All elements in <code>nums</code> are <strong>unique</strong>.</li>
<li>The difference between the <strong>maximum</strong> element and the <strong>minimum</strong> element in <code>nums</code> equals <code>nums.length - 1</code>.</li>
</ul>
<p>For example, <code>nums = [4, 2, 5, 3]</code> is <strong>continuous</strong>, but <code>nums = [1, 2, 3, 5, 6]</code> is <strong>not continuous</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of operations to make </em><code>nums</code><em> </em><strong><em>continuous</em></strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,2,5,3]
<strong>Output:</strong> 0
<strong>Explanation:</strong> nums is already continuous.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,5,6]
<strong>Output:</strong> 1
<strong>Explanation:</strong> One possible solution is to change the last element to 4.
The resulting array is [1,2,3,5,4], which is continuous.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,10,100,1000]
<strong>Output:</strong> 3
<strong>Explanation:</strong> One possible solution is to:
- Change the second element to 2.
- Change the third element to 3.
- Change the fourth element to 4.
The resulting array is [1,2,3,4], which is continuous.
</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>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int minOperations(vector<int>& nums) {\n int n=nums.size();\n set<int>st(nums.begin(),nums.end());\n nums.assign(st.begin(),st.end());\n int maxUnique = 0,left = 0;\n for (int right = 0; right < nums.size(); right++) {\n while (nums[right] - nums[left] >= n) left++;\n maxUnique = max(maxUnique, right - left + 1);\n }\n return n - maxUnique;\n }\n};",
"memory": "106900"
} |
2,119 | <p>You are given an integer array <code>nums</code>. In one operation, you can replace <strong>any</strong> element in <code>nums</code> with <strong>any</strong> integer.</p>
<p><code>nums</code> is considered <strong>continuous</strong> if both of the following conditions are fulfilled:</p>
<ul>
<li>All elements in <code>nums</code> are <strong>unique</strong>.</li>
<li>The difference between the <strong>maximum</strong> element and the <strong>minimum</strong> element in <code>nums</code> equals <code>nums.length - 1</code>.</li>
</ul>
<p>For example, <code>nums = [4, 2, 5, 3]</code> is <strong>continuous</strong>, but <code>nums = [1, 2, 3, 5, 6]</code> is <strong>not continuous</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of operations to make </em><code>nums</code><em> </em><strong><em>continuous</em></strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,2,5,3]
<strong>Output:</strong> 0
<strong>Explanation:</strong> nums is already continuous.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,5,6]
<strong>Output:</strong> 1
<strong>Explanation:</strong> One possible solution is to change the last element to 4.
The resulting array is [1,2,3,5,4], which is continuous.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,10,100,1000]
<strong>Output:</strong> 3
<strong>Explanation:</strong> One possible solution is to:
- Change the second element to 2.
- Change the third element to 3.
- Change the fourth element to 4.
The resulting array is [1,2,3,4], which is continuous.
</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>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int minOperations(vector<int>& nums) {\n int n=nums.size();\n set<int>st(nums.begin(),nums.end());\n nums.assign(st.begin(),st.end());\n int ans=INT_MAX;\n for(int i=0;i<nums.size();i++){\n int x=nums[i],y=x+n-1;\n int it=upper_bound(nums.begin(),nums.end(),y)-nums.begin();\n ans=min(ans,n-(it-i));\n }\n return ans;\n }\n};",
"memory": "106900"
} |
2,119 | <p>You are given an integer array <code>nums</code>. In one operation, you can replace <strong>any</strong> element in <code>nums</code> with <strong>any</strong> integer.</p>
<p><code>nums</code> is considered <strong>continuous</strong> if both of the following conditions are fulfilled:</p>
<ul>
<li>All elements in <code>nums</code> are <strong>unique</strong>.</li>
<li>The difference between the <strong>maximum</strong> element and the <strong>minimum</strong> element in <code>nums</code> equals <code>nums.length - 1</code>.</li>
</ul>
<p>For example, <code>nums = [4, 2, 5, 3]</code> is <strong>continuous</strong>, but <code>nums = [1, 2, 3, 5, 6]</code> is <strong>not continuous</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of operations to make </em><code>nums</code><em> </em><strong><em>continuous</em></strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,2,5,3]
<strong>Output:</strong> 0
<strong>Explanation:</strong> nums is already continuous.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,5,6]
<strong>Output:</strong> 1
<strong>Explanation:</strong> One possible solution is to change the last element to 4.
The resulting array is [1,2,3,5,4], which is continuous.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,10,100,1000]
<strong>Output:</strong> 3
<strong>Explanation:</strong> One possible solution is to:
- Change the second element to 2.
- Change the third element to 3.
- Change the fourth element to 4.
The resulting array is [1,2,3,4], which is continuous.
</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>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int minOperations(vector<int>& nums) \n {\n unordered_set<int> unique_nums(nums.begin(), nums.end());\n vector<int> sorted_nums(unique_nums.begin(), unique_nums.end());\n\n // Step 2: Sort the array to find the largest continuous subset\n sort(sorted_nums.begin(), sorted_nums.end());\n \n int n = nums.size();\n int maxContinuousLength = 0;\n int left = 0;\n\n // Step 3: Use a sliding window to find the largest continuous subarray\n for (int right = 0; right < sorted_nums.size(); ++right) {\n // While the window is not continuous (difference between max and min is too large)\n while (sorted_nums[right] - sorted_nums[left] >= n) {\n left++; // Shrink the window from the left\n }\n // Calculate the length of the current continuous window\n maxContinuousLength = max(maxContinuousLength, right - left + 1);\n }\n\n // Step 4: Return the minimum operations required\n // Total elements - longest continuous subarray gives the number of operations\n return n - maxContinuousLength;\n \n }\n};",
"memory": "107300"
} |
2,119 | <p>You are given an integer array <code>nums</code>. In one operation, you can replace <strong>any</strong> element in <code>nums</code> with <strong>any</strong> integer.</p>
<p><code>nums</code> is considered <strong>continuous</strong> if both of the following conditions are fulfilled:</p>
<ul>
<li>All elements in <code>nums</code> are <strong>unique</strong>.</li>
<li>The difference between the <strong>maximum</strong> element and the <strong>minimum</strong> element in <code>nums</code> equals <code>nums.length - 1</code>.</li>
</ul>
<p>For example, <code>nums = [4, 2, 5, 3]</code> is <strong>continuous</strong>, but <code>nums = [1, 2, 3, 5, 6]</code> is <strong>not continuous</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of operations to make </em><code>nums</code><em> </em><strong><em>continuous</em></strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,2,5,3]
<strong>Output:</strong> 0
<strong>Explanation:</strong> nums is already continuous.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,5,6]
<strong>Output:</strong> 1
<strong>Explanation:</strong> One possible solution is to change the last element to 4.
The resulting array is [1,2,3,5,4], which is continuous.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,10,100,1000]
<strong>Output:</strong> 3
<strong>Explanation:</strong> One possible solution is to:
- Change the second element to 2.
- Change the third element to 3.
- Change the fourth element to 4.
The resulting array is [1,2,3,4], which is continuous.
</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>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int minOperations(vector<int>& nums) {\n int n = nums.size();\n unordered_set<int>S(nums.begin(),nums.end());\n vector<int>temp(S.begin(),S.end());\n int minOpe = INT_MAX;\n sort(temp.begin(),temp.end());\n for(int i=0;i<temp.size();i++){ \n int startPoint = temp[i],endPoint = temp[i] + n-1;\n int inside_range = upper_bound(temp.begin(),temp.end(),endPoint) - (temp.begin()+i);\n int outside_range = n - inside_range;\n minOpe = min(minOpe,outside_range);\n }\n return minOpe;\n }\n};",
"memory": "107400"
} |
2,119 | <p>You are given an integer array <code>nums</code>. In one operation, you can replace <strong>any</strong> element in <code>nums</code> with <strong>any</strong> integer.</p>
<p><code>nums</code> is considered <strong>continuous</strong> if both of the following conditions are fulfilled:</p>
<ul>
<li>All elements in <code>nums</code> are <strong>unique</strong>.</li>
<li>The difference between the <strong>maximum</strong> element and the <strong>minimum</strong> element in <code>nums</code> equals <code>nums.length - 1</code>.</li>
</ul>
<p>For example, <code>nums = [4, 2, 5, 3]</code> is <strong>continuous</strong>, but <code>nums = [1, 2, 3, 5, 6]</code> is <strong>not continuous</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of operations to make </em><code>nums</code><em> </em><strong><em>continuous</em></strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,2,5,3]
<strong>Output:</strong> 0
<strong>Explanation:</strong> nums is already continuous.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,5,6]
<strong>Output:</strong> 1
<strong>Explanation:</strong> One possible solution is to change the last element to 4.
The resulting array is [1,2,3,5,4], which is continuous.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,10,100,1000]
<strong>Output:</strong> 3
<strong>Explanation:</strong> One possible solution is to:
- Change the second element to 2.
- Change the third element to 3.
- Change the fourth element to 4.
The resulting array is [1,2,3,4], which is continuous.
</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>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int minOperations(vector<int>& nums) {\n int count = 0;\n int n = nums.size();\n unordered_set<int> mp(nums.begin(), nums.end());\n vector<int> arr(mp.begin(), mp.end());\n sort(arr.begin(), arr.end());\n\n int window_size = 0;\n int j = 0;\n\n for(int i=0; i<arr.size(); i++){\n while(j < arr.size() && arr[j] <= arr[i] + n - 1){\n j++;\n }\n window_size = max(window_size, j-i);\n }\n return n - window_size;\n }\n};",
"memory": "107500"
} |
2,119 | <p>You are given an integer array <code>nums</code>. In one operation, you can replace <strong>any</strong> element in <code>nums</code> with <strong>any</strong> integer.</p>
<p><code>nums</code> is considered <strong>continuous</strong> if both of the following conditions are fulfilled:</p>
<ul>
<li>All elements in <code>nums</code> are <strong>unique</strong>.</li>
<li>The difference between the <strong>maximum</strong> element and the <strong>minimum</strong> element in <code>nums</code> equals <code>nums.length - 1</code>.</li>
</ul>
<p>For example, <code>nums = [4, 2, 5, 3]</code> is <strong>continuous</strong>, but <code>nums = [1, 2, 3, 5, 6]</code> is <strong>not continuous</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of operations to make </em><code>nums</code><em> </em><strong><em>continuous</em></strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,2,5,3]
<strong>Output:</strong> 0
<strong>Explanation:</strong> nums is already continuous.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,5,6]
<strong>Output:</strong> 1
<strong>Explanation:</strong> One possible solution is to change the last element to 4.
The resulting array is [1,2,3,5,4], which is continuous.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,10,100,1000]
<strong>Output:</strong> 3
<strong>Explanation:</strong> One possible solution is to:
- Change the second element to 2.
- Change the third element to 3.
- Change the fourth element to 4.
The resulting array is [1,2,3,4], which is continuous.
</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>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int minOperations(vector<int>& nums) {\n int n=nums.size();\n unordered_map<int,int>mp;\n vector<int>pref(n+1,0);\n sort(nums.begin(),nums.end());\n for(int i=0;i<n;i++){\n int it=nums[i];\n mp[it]++;\n if(mp[it]>1)pref[i+1]=1+pref[i];\n else pref[i+1]=pref[i];\n }\n \n int ans=INT_MAX;\n for(int i=0;i<n;i++){\n int x=nums[i]; \n int ind=lower_bound(nums.begin(),nums.end(),x+n)-nums.begin();\n ind--;\n int rep=pref[ind+1]-pref[i];\n ans=min(ans,n-(ind-i+1)+rep);\n }\n return ans;\n }\n};",
"memory": "107700"
} |
2,119 | <p>You are given an integer array <code>nums</code>. In one operation, you can replace <strong>any</strong> element in <code>nums</code> with <strong>any</strong> integer.</p>
<p><code>nums</code> is considered <strong>continuous</strong> if both of the following conditions are fulfilled:</p>
<ul>
<li>All elements in <code>nums</code> are <strong>unique</strong>.</li>
<li>The difference between the <strong>maximum</strong> element and the <strong>minimum</strong> element in <code>nums</code> equals <code>nums.length - 1</code>.</li>
</ul>
<p>For example, <code>nums = [4, 2, 5, 3]</code> is <strong>continuous</strong>, but <code>nums = [1, 2, 3, 5, 6]</code> is <strong>not continuous</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of operations to make </em><code>nums</code><em> </em><strong><em>continuous</em></strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,2,5,3]
<strong>Output:</strong> 0
<strong>Explanation:</strong> nums is already continuous.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,5,6]
<strong>Output:</strong> 1
<strong>Explanation:</strong> One possible solution is to change the last element to 4.
The resulting array is [1,2,3,5,4], which is continuous.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,10,100,1000]
<strong>Output:</strong> 3
<strong>Explanation:</strong> One possible solution is to:
- Change the second element to 2.
- Change the third element to 3.
- Change the fourth element to 4.
The resulting array is [1,2,3,4], which is continuous.
</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>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int minOperations(vector<int>& nums) {\n int n=nums.size();\n unordered_map<int,int>mp;\n vector<int>pref(n+1,0);\n sort(nums.begin(),nums.end());\n for(int i=0;i<n;i++){\n int it=nums[i];\n mp[it]++;\n if(mp[it]>1)pref[i+1]=1+pref[i];\n else pref[i+1]=pref[i];\n }\n \n int ans=INT_MAX;\n for(int i=0;i<n;i++){\n int x=nums[i]; \n int ind=lower_bound(nums.begin(),nums.end(),x+n)-nums.begin();\n ind--;\n int rep=pref[ind+1]-pref[i];\n ans=min(ans,n-(ind-i+1)+rep);\n }\n return ans;\n }\n};",
"memory": "107700"
} |
2,119 | <p>You are given an integer array <code>nums</code>. In one operation, you can replace <strong>any</strong> element in <code>nums</code> with <strong>any</strong> integer.</p>
<p><code>nums</code> is considered <strong>continuous</strong> if both of the following conditions are fulfilled:</p>
<ul>
<li>All elements in <code>nums</code> are <strong>unique</strong>.</li>
<li>The difference between the <strong>maximum</strong> element and the <strong>minimum</strong> element in <code>nums</code> equals <code>nums.length - 1</code>.</li>
</ul>
<p>For example, <code>nums = [4, 2, 5, 3]</code> is <strong>continuous</strong>, but <code>nums = [1, 2, 3, 5, 6]</code> is <strong>not continuous</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of operations to make </em><code>nums</code><em> </em><strong><em>continuous</em></strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,2,5,3]
<strong>Output:</strong> 0
<strong>Explanation:</strong> nums is already continuous.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,5,6]
<strong>Output:</strong> 1
<strong>Explanation:</strong> One possible solution is to change the last element to 4.
The resulting array is [1,2,3,5,4], which is continuous.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,10,100,1000]
<strong>Output:</strong> 3
<strong>Explanation:</strong> One possible solution is to:
- Change the second element to 2.
- Change the third element to 3.
- Change the fourth element to 4.
The resulting array is [1,2,3,4], which is continuous.
</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>
</ul>
| 1 | {
"code": "class Solution\n{\npublic:\n int minOperations(vector<int> &nums)\n {\n int n = nums.size();\n int i = 0, j = 0;\n set<int> hm;\n sort(nums.begin(), nums.end());\n\n int count = n;\n int c = 0;\n int curr = nums[i];\n int presentCount = 0;\n while (i < n)\n {\n curr = nums[i];\n while (j < n && nums[j] <= curr + n - 1)\n {\n if (hm.find(nums[j]) == hm.end())\n {\n hm.insert(nums[j]);\n presentCount += 1;\n }\n j++;\n }\n /*\n for (int l = 0; l < n; l++)\n if (hm.find(curr + l) == hm.end())\n c += 1;\n count = min(count, c);\n c = 0;\n */\n count = min(count, n - presentCount);\n while (i < n && nums[i] == curr)\n i++;\n\n if (hm.find(curr) != hm.end())\n {\n hm.erase(curr);\n presentCount -= 1;\n }\n }\n return count;\n }\n};",
"memory": "107800"
} |
2,119 | <p>You are given an integer array <code>nums</code>. In one operation, you can replace <strong>any</strong> element in <code>nums</code> with <strong>any</strong> integer.</p>
<p><code>nums</code> is considered <strong>continuous</strong> if both of the following conditions are fulfilled:</p>
<ul>
<li>All elements in <code>nums</code> are <strong>unique</strong>.</li>
<li>The difference between the <strong>maximum</strong> element and the <strong>minimum</strong> element in <code>nums</code> equals <code>nums.length - 1</code>.</li>
</ul>
<p>For example, <code>nums = [4, 2, 5, 3]</code> is <strong>continuous</strong>, but <code>nums = [1, 2, 3, 5, 6]</code> is <strong>not continuous</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of operations to make </em><code>nums</code><em> </em><strong><em>continuous</em></strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,2,5,3]
<strong>Output:</strong> 0
<strong>Explanation:</strong> nums is already continuous.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,5,6]
<strong>Output:</strong> 1
<strong>Explanation:</strong> One possible solution is to change the last element to 4.
The resulting array is [1,2,3,5,4], which is continuous.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,10,100,1000]
<strong>Output:</strong> 3
<strong>Explanation:</strong> One possible solution is to:
- Change the second element to 2.
- Change the third element to 3.
- Change the fourth element to 4.
The resulting array is [1,2,3,4], which is continuous.
</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>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int minOperations(vector<int>& nums) {\n\n sort(nums.begin(), nums.end());\n int n = nums.size();\n\n set<int> st(nums.begin(), nums.end());\n nums.assign(st.begin(), st.end());\n\n int r = 0;\n int ans = nums.back();\n for(int l=0;l<nums.size();l++)\n {\n while(r<nums.size() && nums[r]<nums[l]+n)\n r++;\n int window = r-l;\n ans = min(ans,n-window);\n }\n return ans;\n \n }\n};",
"memory": "107900"
} |
2,119 | <p>You are given an integer array <code>nums</code>. In one operation, you can replace <strong>any</strong> element in <code>nums</code> with <strong>any</strong> integer.</p>
<p><code>nums</code> is considered <strong>continuous</strong> if both of the following conditions are fulfilled:</p>
<ul>
<li>All elements in <code>nums</code> are <strong>unique</strong>.</li>
<li>The difference between the <strong>maximum</strong> element and the <strong>minimum</strong> element in <code>nums</code> equals <code>nums.length - 1</code>.</li>
</ul>
<p>For example, <code>nums = [4, 2, 5, 3]</code> is <strong>continuous</strong>, but <code>nums = [1, 2, 3, 5, 6]</code> is <strong>not continuous</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of operations to make </em><code>nums</code><em> </em><strong><em>continuous</em></strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,2,5,3]
<strong>Output:</strong> 0
<strong>Explanation:</strong> nums is already continuous.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,5,6]
<strong>Output:</strong> 1
<strong>Explanation:</strong> One possible solution is to change the last element to 4.
The resulting array is [1,2,3,5,4], which is continuous.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,10,100,1000]
<strong>Output:</strong> 3
<strong>Explanation:</strong> One possible solution is to:
- Change the second element to 2.
- Change the third element to 3.
- Change the fourth element to 4.
The resulting array is [1,2,3,4], which is continuous.
</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>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int minOperations(vector<int>& nums) {\n // 1 2 4 5 7\n int n = nums.size();\n sort(nums.begin(),nums.end());\n if(n==1)return 0;\n int ans = INT_MAX;\n int low = 0;\n int high = 0;\n int sz = 0;\n map<int,int> mp;\n while(high<n){\n if(nums[high]-nums[low]<=n-1){\n if(mp.find(nums[high])==mp.end())sz++;\n mp[nums[high]]++;\n high++;\n }\n else{\n int cnt = sz;\n ans = min(ans,n-cnt);\n mp[nums[low]]--;\n if(mp[nums[low]]==0)sz--;\n low++;\n }\n }\n int cnt = sz;\n ans = min(ans,n-cnt);\n return ans;\n }\n};",
"memory": "108000"
} |
2,119 | <p>You are given an integer array <code>nums</code>. In one operation, you can replace <strong>any</strong> element in <code>nums</code> with <strong>any</strong> integer.</p>
<p><code>nums</code> is considered <strong>continuous</strong> if both of the following conditions are fulfilled:</p>
<ul>
<li>All elements in <code>nums</code> are <strong>unique</strong>.</li>
<li>The difference between the <strong>maximum</strong> element and the <strong>minimum</strong> element in <code>nums</code> equals <code>nums.length - 1</code>.</li>
</ul>
<p>For example, <code>nums = [4, 2, 5, 3]</code> is <strong>continuous</strong>, but <code>nums = [1, 2, 3, 5, 6]</code> is <strong>not continuous</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of operations to make </em><code>nums</code><em> </em><strong><em>continuous</em></strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,2,5,3]
<strong>Output:</strong> 0
<strong>Explanation:</strong> nums is already continuous.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,5,6]
<strong>Output:</strong> 1
<strong>Explanation:</strong> One possible solution is to change the last element to 4.
The resulting array is [1,2,3,5,4], which is continuous.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,10,100,1000]
<strong>Output:</strong> 3
<strong>Explanation:</strong> One possible solution is to:
- Change the second element to 2.
- Change the third element to 3.
- Change the fourth element to 4.
The resulting array is [1,2,3,4], which is continuous.
</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>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int minOperations(vector<int>& nums) {\n sort(nums.begin(), nums.end());\n int n = nums.size();\n\n int s=0,e=0, ans=n;\n int start = nums[0];\n set<int> st;\n\n while(e < n) {\n if(start+n-1 >= nums[e]) {\n st.insert(nums[e]);\n e++;\n }\n else {\n int ops = (n - (e-s)) + ((e-s) - st.size());\n ans = min(ans, ops);\n st.erase(nums[s]);\n s++;\n start = nums[s];\n }\n }\n\n int ops = (n - (e-s)) + ((e-s) - st.size());\n ans = min(ans, ops);\n\n return ans;\n }\n};",
"memory": "108100"
} |
2,119 | <p>You are given an integer array <code>nums</code>. In one operation, you can replace <strong>any</strong> element in <code>nums</code> with <strong>any</strong> integer.</p>
<p><code>nums</code> is considered <strong>continuous</strong> if both of the following conditions are fulfilled:</p>
<ul>
<li>All elements in <code>nums</code> are <strong>unique</strong>.</li>
<li>The difference between the <strong>maximum</strong> element and the <strong>minimum</strong> element in <code>nums</code> equals <code>nums.length - 1</code>.</li>
</ul>
<p>For example, <code>nums = [4, 2, 5, 3]</code> is <strong>continuous</strong>, but <code>nums = [1, 2, 3, 5, 6]</code> is <strong>not continuous</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of operations to make </em><code>nums</code><em> </em><strong><em>continuous</em></strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,2,5,3]
<strong>Output:</strong> 0
<strong>Explanation:</strong> nums is already continuous.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,5,6]
<strong>Output:</strong> 1
<strong>Explanation:</strong> One possible solution is to change the last element to 4.
The resulting array is [1,2,3,5,4], which is continuous.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,10,100,1000]
<strong>Output:</strong> 3
<strong>Explanation:</strong> One possible solution is to:
- Change the second element to 2.
- Change the third element to 3.
- Change the fourth element to 4.
The resulting array is [1,2,3,4], which is continuous.
</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>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int minOperations(vector<int>& nums) {\n //max subarray such that diff(max-min)<=n and all numbers are unique\n int n =nums.size();\n int end =0,begin=0,maxi = 0;\n sort(nums.begin(),nums.end());\n unordered_map<int,int> mp;\n //monotonic queue?\n deque<int> q;//minq will store elements in ascending order,maxq stores elements in descendind order\n int dup=0;\n while(end<n){\n if(q.empty() || q.back()<=nums[end]) q.push_back(nums[end]);\n else if(q.front()>=nums[end]) q.push_front(nums[end]);\n if(mp[nums[end]]++>0) dup++;\n while(q.back()-q.front()>=n){\n if(q.back()==nums[begin]) q.pop_back();\n if(q.front()==nums[begin]) q.pop_front();\n if(--mp[nums[begin++]]>0) dup--;\n }\n maxi = max(maxi,end-begin+1-dup);\n end++;\n }\n return n-maxi;\n }\n};",
"memory": "108400"
} |
2,119 | <p>You are given an integer array <code>nums</code>. In one operation, you can replace <strong>any</strong> element in <code>nums</code> with <strong>any</strong> integer.</p>
<p><code>nums</code> is considered <strong>continuous</strong> if both of the following conditions are fulfilled:</p>
<ul>
<li>All elements in <code>nums</code> are <strong>unique</strong>.</li>
<li>The difference between the <strong>maximum</strong> element and the <strong>minimum</strong> element in <code>nums</code> equals <code>nums.length - 1</code>.</li>
</ul>
<p>For example, <code>nums = [4, 2, 5, 3]</code> is <strong>continuous</strong>, but <code>nums = [1, 2, 3, 5, 6]</code> is <strong>not continuous</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of operations to make </em><code>nums</code><em> </em><strong><em>continuous</em></strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,2,5,3]
<strong>Output:</strong> 0
<strong>Explanation:</strong> nums is already continuous.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,5,6]
<strong>Output:</strong> 1
<strong>Explanation:</strong> One possible solution is to change the last element to 4.
The resulting array is [1,2,3,5,4], which is continuous.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,10,100,1000]
<strong>Output:</strong> 3
<strong>Explanation:</strong> One possible solution is to:
- Change the second element to 2.
- Change the third element to 3.
- Change the fourth element to 4.
The resulting array is [1,2,3,4], which is continuous.
</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>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int minOperations(vector<int>& nums) {\n sort(nums.begin(), nums.end());\n int n = nums.size(), minOp= n-1;\n\n int count = 0; // for distinct num\n unordered_map<int, int> map;\n map[-1] = 0;\n for (int i = 0; i < n; i++) {\n if (i == 0 || nums[i] != nums[i-1]) count++;\n map[i] = count;\n }\n\n for (int i = 0; i < n; i++) {\n if (i > 0 && nums[i] == nums[i-1]) continue;\n\n int endNum = nums[i]+n-1;\n int idx = upper_bound(nums.begin()+i+1, nums.end(), endNum)-nums.begin()-1;\n int contain = map[idx]-map[i-1];\n minOp = min(minOp, n-contain);\n }\n return minOp;\n }\n};",
"memory": "108700"
} |
2,119 | <p>You are given an integer array <code>nums</code>. In one operation, you can replace <strong>any</strong> element in <code>nums</code> with <strong>any</strong> integer.</p>
<p><code>nums</code> is considered <strong>continuous</strong> if both of the following conditions are fulfilled:</p>
<ul>
<li>All elements in <code>nums</code> are <strong>unique</strong>.</li>
<li>The difference between the <strong>maximum</strong> element and the <strong>minimum</strong> element in <code>nums</code> equals <code>nums.length - 1</code>.</li>
</ul>
<p>For example, <code>nums = [4, 2, 5, 3]</code> is <strong>continuous</strong>, but <code>nums = [1, 2, 3, 5, 6]</code> is <strong>not continuous</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of operations to make </em><code>nums</code><em> </em><strong><em>continuous</em></strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,2,5,3]
<strong>Output:</strong> 0
<strong>Explanation:</strong> nums is already continuous.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,5,6]
<strong>Output:</strong> 1
<strong>Explanation:</strong> One possible solution is to change the last element to 4.
The resulting array is [1,2,3,5,4], which is continuous.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,10,100,1000]
<strong>Output:</strong> 3
<strong>Explanation:</strong> One possible solution is to:
- Change the second element to 2.
- Change the third element to 3.
- Change the fourth element to 4.
The resulting array is [1,2,3,4], which is continuous.
</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>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n// TC O(NlogN), SC O(N)\n int minOperations(vector<int>& nums) {\n int n = nums.size();\n int res = n;\n \n set<int> unique(nums.begin(), nums.end());\n vector<int> newNums(unique.begin(), unique.end());\n\n for(int i = 0; i < newNums.size(); ++i) {\n int left = newNums[i];\n int right = left + n - 1;\n int j = upper_bound(newNums.begin(), newNums.end(), right) - newNums.begin();\n int count = (j - i+1)-1; // num[j], if any, is not counted as the good ones\n res = min(res, n - count); // n - count is the missing count\n }\n \n return res;\n }\n};",
"memory": "109100"
} |
2,119 | <p>You are given an integer array <code>nums</code>. In one operation, you can replace <strong>any</strong> element in <code>nums</code> with <strong>any</strong> integer.</p>
<p><code>nums</code> is considered <strong>continuous</strong> if both of the following conditions are fulfilled:</p>
<ul>
<li>All elements in <code>nums</code> are <strong>unique</strong>.</li>
<li>The difference between the <strong>maximum</strong> element and the <strong>minimum</strong> element in <code>nums</code> equals <code>nums.length - 1</code>.</li>
</ul>
<p>For example, <code>nums = [4, 2, 5, 3]</code> is <strong>continuous</strong>, but <code>nums = [1, 2, 3, 5, 6]</code> is <strong>not continuous</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of operations to make </em><code>nums</code><em> </em><strong><em>continuous</em></strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,2,5,3]
<strong>Output:</strong> 0
<strong>Explanation:</strong> nums is already continuous.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,5,6]
<strong>Output:</strong> 1
<strong>Explanation:</strong> One possible solution is to change the last element to 4.
The resulting array is [1,2,3,5,4], which is continuous.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,10,100,1000]
<strong>Output:</strong> 3
<strong>Explanation:</strong> One possible solution is to:
- Change the second element to 2.
- Change the third element to 3.
- Change the fourth element to 4.
The resulting array is [1,2,3,4], which is continuous.
</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>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int minOperations(vector<int>& nums) {\n set<int> s(nums.begin(), nums.end());\n vector<int> newNums;\n newNums.reserve(s.size());\n std::copy(s.begin(), s.end(), std::back_inserter(newNums));\n\n int n = nums.size();\n size_t ops = nums.size();\n for(int i = 0; i < newNums.size(); i++) {\n int left = newNums[i];\n int right = left + n - 1;\n size_t rIdx = distance(newNums.begin(), upper_bound(newNums.begin()+i+1, newNums.end(), right));\n ops = min(ops, n - (rIdx - i));\n }\n return (int)ops;\n }\n};\n\n/*\n[1,10,100,1000]\ngapsize = 10 - 1 - 1 = 8 (we need to insert 8 elements at most)\n*/",
"memory": "109200"
} |
2,119 | <p>You are given an integer array <code>nums</code>. In one operation, you can replace <strong>any</strong> element in <code>nums</code> with <strong>any</strong> integer.</p>
<p><code>nums</code> is considered <strong>continuous</strong> if both of the following conditions are fulfilled:</p>
<ul>
<li>All elements in <code>nums</code> are <strong>unique</strong>.</li>
<li>The difference between the <strong>maximum</strong> element and the <strong>minimum</strong> element in <code>nums</code> equals <code>nums.length - 1</code>.</li>
</ul>
<p>For example, <code>nums = [4, 2, 5, 3]</code> is <strong>continuous</strong>, but <code>nums = [1, 2, 3, 5, 6]</code> is <strong>not continuous</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of operations to make </em><code>nums</code><em> </em><strong><em>continuous</em></strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,2,5,3]
<strong>Output:</strong> 0
<strong>Explanation:</strong> nums is already continuous.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,5,6]
<strong>Output:</strong> 1
<strong>Explanation:</strong> One possible solution is to change the last element to 4.
The resulting array is [1,2,3,5,4], which is continuous.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,10,100,1000]
<strong>Output:</strong> 3
<strong>Explanation:</strong> One possible solution is to:
- Change the second element to 2.
- Change the third element to 3.
- Change the fourth element to 4.
The resulting array is [1,2,3,4], which is continuous.
</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>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int minOperations(vector<int>& nums) {\n int n = nums.size();\n // Use a set to store only unique elements and sort them automatically\n set<int> s(nums.begin(), nums.end());\n vector<int> v(s.begin(), s.end());\n int m = v.size(), max_len = 0, j = 0;\n // Sliding window to find the longest continuous subarray\n for (int i = 0; i < m; ++i) {\n while (j < m && v[j] < v[i] + n) {\n ++j;\n }\n max_len = max(max_len, j - i);\n }\n return n - max_len;\n }\n};\n",
"memory": "109300"
} |
2,119 | <p>You are given an integer array <code>nums</code>. In one operation, you can replace <strong>any</strong> element in <code>nums</code> with <strong>any</strong> integer.</p>
<p><code>nums</code> is considered <strong>continuous</strong> if both of the following conditions are fulfilled:</p>
<ul>
<li>All elements in <code>nums</code> are <strong>unique</strong>.</li>
<li>The difference between the <strong>maximum</strong> element and the <strong>minimum</strong> element in <code>nums</code> equals <code>nums.length - 1</code>.</li>
</ul>
<p>For example, <code>nums = [4, 2, 5, 3]</code> is <strong>continuous</strong>, but <code>nums = [1, 2, 3, 5, 6]</code> is <strong>not continuous</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of operations to make </em><code>nums</code><em> </em><strong><em>continuous</em></strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,2,5,3]
<strong>Output:</strong> 0
<strong>Explanation:</strong> nums is already continuous.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,5,6]
<strong>Output:</strong> 1
<strong>Explanation:</strong> One possible solution is to change the last element to 4.
The resulting array is [1,2,3,5,4], which is continuous.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,10,100,1000]
<strong>Output:</strong> 3
<strong>Explanation:</strong> One possible solution is to:
- Change the second element to 2.
- Change the third element to 3.
- Change the fourth element to 4.
The resulting array is [1,2,3,4], which is continuous.
</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>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int minOperations(vector<int>& nums) {\n set<int> s(nums.begin(), nums.end());\n vector<int> newNums;\n newNums.reserve(s.size());\n std::copy(s.begin(), s.end(), std::back_inserter(newNums));\n\n int n = nums.size();\n size_t ops = nums.size();\n for(int i = 0; i < newNums.size(); i++) {\n int left = newNums[i];\n int right = left + n - 1;\n size_t rIdx = distance(newNums.begin(), upper_bound(newNums.begin(), newNums.end(), right));\n ops = min(ops, n - (rIdx - i));\n }\n return (int)ops;\n }\n};\n\n/*\n[1,10,100,1000]\ngapsize = 10 - 1 - 1 = 8 (we need to insert 8 elements at most)\n*/",
"memory": "109400"
} |
2,119 | <p>You are given an integer array <code>nums</code>. In one operation, you can replace <strong>any</strong> element in <code>nums</code> with <strong>any</strong> integer.</p>
<p><code>nums</code> is considered <strong>continuous</strong> if both of the following conditions are fulfilled:</p>
<ul>
<li>All elements in <code>nums</code> are <strong>unique</strong>.</li>
<li>The difference between the <strong>maximum</strong> element and the <strong>minimum</strong> element in <code>nums</code> equals <code>nums.length - 1</code>.</li>
</ul>
<p>For example, <code>nums = [4, 2, 5, 3]</code> is <strong>continuous</strong>, but <code>nums = [1, 2, 3, 5, 6]</code> is <strong>not continuous</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of operations to make </em><code>nums</code><em> </em><strong><em>continuous</em></strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,2,5,3]
<strong>Output:</strong> 0
<strong>Explanation:</strong> nums is already continuous.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,5,6]
<strong>Output:</strong> 1
<strong>Explanation:</strong> One possible solution is to change the last element to 4.
The resulting array is [1,2,3,5,4], which is continuous.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,10,100,1000]
<strong>Output:</strong> 3
<strong>Explanation:</strong> One possible solution is to:
- Change the second element to 2.
- Change the third element to 3.
- Change the fourth element to 4.
The resulting array is [1,2,3,4], which is continuous.
</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>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int minOperations(vector<int>& nums) {\n int n=nums.size();\n set<int>st(nums.begin(),nums.end());\n vector<int>temp(st.begin(),st.end());\n int ans=INT_MAX;\n for(int i=0;i<temp.size();i++){\n int higherBound=temp[i]+(n-1);\n int j=upper_bound(temp.begin(),temp.end(),higherBound)-temp.begin();\n int inRange=(j-i);\n ans=min(ans,(n-inRange));\n }\n return ans;\n }\n};",
"memory": "109500"
} |
2,119 | <p>You are given an integer array <code>nums</code>. In one operation, you can replace <strong>any</strong> element in <code>nums</code> with <strong>any</strong> integer.</p>
<p><code>nums</code> is considered <strong>continuous</strong> if both of the following conditions are fulfilled:</p>
<ul>
<li>All elements in <code>nums</code> are <strong>unique</strong>.</li>
<li>The difference between the <strong>maximum</strong> element and the <strong>minimum</strong> element in <code>nums</code> equals <code>nums.length - 1</code>.</li>
</ul>
<p>For example, <code>nums = [4, 2, 5, 3]</code> is <strong>continuous</strong>, but <code>nums = [1, 2, 3, 5, 6]</code> is <strong>not continuous</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of operations to make </em><code>nums</code><em> </em><strong><em>continuous</em></strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,2,5,3]
<strong>Output:</strong> 0
<strong>Explanation:</strong> nums is already continuous.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,5,6]
<strong>Output:</strong> 1
<strong>Explanation:</strong> One possible solution is to change the last element to 4.
The resulting array is [1,2,3,5,4], which is continuous.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,10,100,1000]
<strong>Output:</strong> 3
<strong>Explanation:</strong> One possible solution is to:
- Change the second element to 2.
- Change the third element to 3.
- Change the fourth element to 4.
The resulting array is [1,2,3,4], which is continuous.
</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>
</ul>
| 2 | {
"code": "class Solution {\n public:\n int minOperations(vector<int> &nums) {\n std::ranges::sort(nums);\n int res = nums.size(), sz = nums.size();\n\n // Count the cumulative duplicates\n set<int> s;\n vector<int> dups(sz, 0);\n for (int i = 0; i < sz; ++i) {\n auto [it, inserted] = s.insert(nums[i]);\n if (i > 0)\n dups[i] = dups[i - 1] + (inserted == false);\n }\n\n for (int i = 0; i < sz; ++i) {\n\n // For each nums[i], find the numbers that are outside of the\n // range [nums[i], nums[i] + sz - 1]. Those numbers need to change\n // and we also need to add the duplicates in the range [nums[i],\n // nums[i] + sz - 1].\n\n int n = nums[i] + sz - 1;\n auto it = std::lower_bound(nums.begin(), nums.end(), n);\n if (it == nums.end()) {\n int count = sz - i;\n int changes =\n sz - count + dups[sz - 1] - (i > 0 ? dups[i - 1] : 0);\n res = min(res, changes);\n } else {\n if (*it != n)\n --it;\n int j = it - nums.begin();\n int count = j - i + 1;\n int changes = sz - count + dups[j] - (i > 0 ? dups[i - 1] : 0);\n res = min(res, changes);\n }\n }\n\n return res;\n }\n};\n",
"memory": "109600"
} |
2,119 | <p>You are given an integer array <code>nums</code>. In one operation, you can replace <strong>any</strong> element in <code>nums</code> with <strong>any</strong> integer.</p>
<p><code>nums</code> is considered <strong>continuous</strong> if both of the following conditions are fulfilled:</p>
<ul>
<li>All elements in <code>nums</code> are <strong>unique</strong>.</li>
<li>The difference between the <strong>maximum</strong> element and the <strong>minimum</strong> element in <code>nums</code> equals <code>nums.length - 1</code>.</li>
</ul>
<p>For example, <code>nums = [4, 2, 5, 3]</code> is <strong>continuous</strong>, but <code>nums = [1, 2, 3, 5, 6]</code> is <strong>not continuous</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of operations to make </em><code>nums</code><em> </em><strong><em>continuous</em></strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,2,5,3]
<strong>Output:</strong> 0
<strong>Explanation:</strong> nums is already continuous.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,5,6]
<strong>Output:</strong> 1
<strong>Explanation:</strong> One possible solution is to change the last element to 4.
The resulting array is [1,2,3,5,4], which is continuous.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,10,100,1000]
<strong>Output:</strong> 3
<strong>Explanation:</strong> One possible solution is to:
- Change the second element to 2.
- Change the third element to 3.
- Change the fourth element to 4.
The resulting array is [1,2,3,4], which is continuous.
</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>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int minOperations(vector<int>& nums) {\n std::ranges::sort(nums);\n int res = nums.size(), sz = nums.size();\n set<int> s;\n vector<int> dups(sz, 0);\n for (int i = 0; i < sz; ++i) {\n auto [it, inserted] = s.insert(nums[i]);\n if (i > 0)\n dups[i] = dups[i-1] + (inserted == false);\n }\n\n for (int i = 0; i < sz; ++i) {\n int n = nums[i] + sz - 1;\n auto it = std::lower_bound(nums.begin(), nums.end(), n);\n if (it == nums.end()) {\n int count = sz - i;\n int changes = sz - count + dups[sz-1] - (i > 0 ? dups[i-1] : 0);\n res = min(res, changes);\n } else {\n if (*it != n)\n --it;\n int j = it - nums.begin();\n int count = j - i + 1;\n int changes = sz - count + dups[j] - (i > 0 ? dups[i-1] : 0);\n res = min(res, changes);\n }\n }\n\n return res;\n }\n};\n",
"memory": "109700"
} |
2,119 | <p>You are given an integer array <code>nums</code>. In one operation, you can replace <strong>any</strong> element in <code>nums</code> with <strong>any</strong> integer.</p>
<p><code>nums</code> is considered <strong>continuous</strong> if both of the following conditions are fulfilled:</p>
<ul>
<li>All elements in <code>nums</code> are <strong>unique</strong>.</li>
<li>The difference between the <strong>maximum</strong> element and the <strong>minimum</strong> element in <code>nums</code> equals <code>nums.length - 1</code>.</li>
</ul>
<p>For example, <code>nums = [4, 2, 5, 3]</code> is <strong>continuous</strong>, but <code>nums = [1, 2, 3, 5, 6]</code> is <strong>not continuous</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of operations to make </em><code>nums</code><em> </em><strong><em>continuous</em></strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,2,5,3]
<strong>Output:</strong> 0
<strong>Explanation:</strong> nums is already continuous.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,5,6]
<strong>Output:</strong> 1
<strong>Explanation:</strong> One possible solution is to change the last element to 4.
The resulting array is [1,2,3,5,4], which is continuous.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,10,100,1000]
<strong>Output:</strong> 3
<strong>Explanation:</strong> One possible solution is to:
- Change the second element to 2.
- Change the third element to 3.
- Change the fourth element to 4.
The resulting array is [1,2,3,4], which is continuous.
</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>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int minOperations(vector<int>& nums) {\n\n set<int> st(nums.begin(),nums.end());\n int ans = nums.size();\n sort(nums.begin(),nums.end());\n vector<int> temp(st.begin(),st.end());\n for(int i = 0;i<nums.size();i++){\n int left = nums[i];\n int right = nums[i] + nums.size()-1;\n int r = upper_bound(temp.begin(),temp.end(),right)-temp.begin();\n int l = lower_bound(temp.begin(),temp.end(),left)-temp.begin();\n r--;\n \n int elementsInRange = r-l+1;\n \n \n int missingElements = nums.size() - elementsInRange;\n \n \n ans = std::min(ans, missingElements);\n\n\n }\n\n return ans;\n \n }\n};",
"memory": "110300"
} |
2,119 | <p>You are given an integer array <code>nums</code>. In one operation, you can replace <strong>any</strong> element in <code>nums</code> with <strong>any</strong> integer.</p>
<p><code>nums</code> is considered <strong>continuous</strong> if both of the following conditions are fulfilled:</p>
<ul>
<li>All elements in <code>nums</code> are <strong>unique</strong>.</li>
<li>The difference between the <strong>maximum</strong> element and the <strong>minimum</strong> element in <code>nums</code> equals <code>nums.length - 1</code>.</li>
</ul>
<p>For example, <code>nums = [4, 2, 5, 3]</code> is <strong>continuous</strong>, but <code>nums = [1, 2, 3, 5, 6]</code> is <strong>not continuous</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of operations to make </em><code>nums</code><em> </em><strong><em>continuous</em></strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,2,5,3]
<strong>Output:</strong> 0
<strong>Explanation:</strong> nums is already continuous.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,5,6]
<strong>Output:</strong> 1
<strong>Explanation:</strong> One possible solution is to change the last element to 4.
The resulting array is [1,2,3,5,4], which is continuous.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,10,100,1000]
<strong>Output:</strong> 3
<strong>Explanation:</strong> One possible solution is to:
- Change the second element to 2.
- Change the third element to 3.
- Change the fourth element to 4.
The resulting array is [1,2,3,4], which is continuous.
</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>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findIndex(vector<int>& nums, int lim, int index) {\n int res = nums.size();\n int l = index, r = nums.size() - 1;\n while(l <= r) {\n int mid = l + (r - l) / 2;\n if(nums[mid] > lim) {\n res = mid;\n r = mid - 1;\n } else {\n l = mid + 1;\n }\n }\n return res;\n }\n int minOperations(vector<int>& nums) {\n sort(nums.begin(), nums.end());\n set<int> uniq (nums.begin(), nums.end());\n vector<int> uniq_nums (uniq.begin(), uniq.end());\n int dup = nums.size() - uniq.size();\n int res = 1e9, j = 1;\n for(int i=0; i<uniq_nums.size(); i++) {\n int low = uniq_nums[i];\n int high = low + nums.size() - 1;\n while(j < uniq_nums.size() && high >= uniq_nums[j]) j++;\n // int index = findIndex(uniq_nums, high, i + 1);\n res = min(res, (int)uniq_nums.size() - j + i + dup);\n }\n return res;\n }\n};\n\n/*\n\n[41, 33, 29, 33, 35, 26, 47, 24, 18, 28]\n[18, 24, 26, 28, 29, 33, 33, 35, 41, 47]\n[18, 24, 26, 28, 29, 33, 35, 41, 47]\n \nminOp = \n\nvis = [2, 3, 4, 5, 6, 8, 10]\n\n*/",
"memory": "110400"
} |
2,119 | <p>You are given an integer array <code>nums</code>. In one operation, you can replace <strong>any</strong> element in <code>nums</code> with <strong>any</strong> integer.</p>
<p><code>nums</code> is considered <strong>continuous</strong> if both of the following conditions are fulfilled:</p>
<ul>
<li>All elements in <code>nums</code> are <strong>unique</strong>.</li>
<li>The difference between the <strong>maximum</strong> element and the <strong>minimum</strong> element in <code>nums</code> equals <code>nums.length - 1</code>.</li>
</ul>
<p>For example, <code>nums = [4, 2, 5, 3]</code> is <strong>continuous</strong>, but <code>nums = [1, 2, 3, 5, 6]</code> is <strong>not continuous</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of operations to make </em><code>nums</code><em> </em><strong><em>continuous</em></strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,2,5,3]
<strong>Output:</strong> 0
<strong>Explanation:</strong> nums is already continuous.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,5,6]
<strong>Output:</strong> 1
<strong>Explanation:</strong> One possible solution is to change the last element to 4.
The resulting array is [1,2,3,5,4], which is continuous.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,10,100,1000]
<strong>Output:</strong> 3
<strong>Explanation:</strong> One possible solution is to:
- Change the second element to 2.
- Change the third element to 3.
- Change the fourth element to 4.
The resulting array is [1,2,3,4], which is continuous.
</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>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int minOperations(vector<int>& nums) {\n set<int> st(nums.begin(), nums.end());\n vector<int> v(st.begin(), st.end());\n\n sort(nums.begin(), nums.end());\n\n // for (int i: v)\n // cout<<i<<\" \";\n // cout<<endl;\n \n int ans = INT_MAX, n = nums.size();\n for (int i=0; i<v.size(); i++) {\n // cout<<v[i]+n-i-1<<\" \";\n int index = lb(v[i]+n-1, v);\n // cout<<index<<endl;\n ans = min(ans, n - (index-i+1));\n }\n\n return ans;\n }\n\n int lb(int target, vector<int>& nums) {\n int low = 0, high = nums.size()-1;\n while (low < high) {\n int mid = low + (high-low+1)/2;\n if (nums[mid] > target)\n high = mid-1;\n else\n low = mid;\n }\n\n return low;\n }\n};",
"memory": "110400"
} |
2,119 | <p>You are given an integer array <code>nums</code>. In one operation, you can replace <strong>any</strong> element in <code>nums</code> with <strong>any</strong> integer.</p>
<p><code>nums</code> is considered <strong>continuous</strong> if both of the following conditions are fulfilled:</p>
<ul>
<li>All elements in <code>nums</code> are <strong>unique</strong>.</li>
<li>The difference between the <strong>maximum</strong> element and the <strong>minimum</strong> element in <code>nums</code> equals <code>nums.length - 1</code>.</li>
</ul>
<p>For example, <code>nums = [4, 2, 5, 3]</code> is <strong>continuous</strong>, but <code>nums = [1, 2, 3, 5, 6]</code> is <strong>not continuous</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of operations to make </em><code>nums</code><em> </em><strong><em>continuous</em></strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,2,5,3]
<strong>Output:</strong> 0
<strong>Explanation:</strong> nums is already continuous.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,5,6]
<strong>Output:</strong> 1
<strong>Explanation:</strong> One possible solution is to change the last element to 4.
The resulting array is [1,2,3,5,4], which is continuous.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,10,100,1000]
<strong>Output:</strong> 3
<strong>Explanation:</strong> One possible solution is to:
- Change the second element to 2.
- Change the third element to 3.
- Change the fourth element to 4.
The resulting array is [1,2,3,4], which is continuous.
</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>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int minOperations(vector<int>& nums) {\n set<int> st(nums.begin(), nums.end());\n vector<int> v(st.begin(), st.end());\n\n sort(nums.begin(), nums.end());\n\n for (int i: v)\n cout<<i<<\" \";\n cout<<endl;\n \n int ans = INT_MAX, n = nums.size();\n for (int i=0; i<v.size(); i++) {\n cout<<v[i]+n-i-1<<\" \";\n int index = lb(v[i]+n-1, v);\n cout<<index<<endl;\n ans = min(ans, n - (index-i+1));\n }\n\n return ans;\n }\n\n int lb(int target, vector<int>& nums) {\n int low = 0, high = nums.size()-1;\n while (low < high) {\n int mid = low + (high-low+1)/2;\n if (nums[mid] > target)\n high = mid-1;\n else\n low = mid;\n }\n\n return low;\n }\n};",
"memory": "110500"
} |
2,119 | <p>You are given an integer array <code>nums</code>. In one operation, you can replace <strong>any</strong> element in <code>nums</code> with <strong>any</strong> integer.</p>
<p><code>nums</code> is considered <strong>continuous</strong> if both of the following conditions are fulfilled:</p>
<ul>
<li>All elements in <code>nums</code> are <strong>unique</strong>.</li>
<li>The difference between the <strong>maximum</strong> element and the <strong>minimum</strong> element in <code>nums</code> equals <code>nums.length - 1</code>.</li>
</ul>
<p>For example, <code>nums = [4, 2, 5, 3]</code> is <strong>continuous</strong>, but <code>nums = [1, 2, 3, 5, 6]</code> is <strong>not continuous</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of operations to make </em><code>nums</code><em> </em><strong><em>continuous</em></strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,2,5,3]
<strong>Output:</strong> 0
<strong>Explanation:</strong> nums is already continuous.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,5,6]
<strong>Output:</strong> 1
<strong>Explanation:</strong> One possible solution is to change the last element to 4.
The resulting array is [1,2,3,5,4], which is continuous.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,10,100,1000]
<strong>Output:</strong> 3
<strong>Explanation:</strong> One possible solution is to:
- Change the second element to 2.
- Change the third element to 3.
- Change the fourth element to 4.
The resulting array is [1,2,3,4], which is continuous.
</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>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int minOperations(vector<int>& nums) {\n set<int> st(nums.begin(), nums.end());\n vector<int> v(st.begin(), st.end());\n\n sort(nums.begin(), nums.end());\n\n // for (int i: v)\n // cout<<i<<\" \";\n // cout<<endl;\n \n int ans = INT_MAX, n = nums.size();\n for (int i=0; i<v.size(); i++) {\n // cout<<v[i]+n-i-1<<\" \";\n int index = lb(v[i]+n-1, v);\n // cout<<index<<endl;\n ans = min(ans, n - (index-i+1));\n }\n\n return ans;\n }\n\n int lb(int target, vector<int>& nums) {\n int low = 0, high = nums.size()-1;\n while (low < high) {\n int mid = low + (high-low+1)/2;\n if (nums[mid] > target)\n high = mid-1;\n else\n low = mid;\n }\n\n return low;\n }\n};",
"memory": "110500"
} |
2,119 | <p>You are given an integer array <code>nums</code>. In one operation, you can replace <strong>any</strong> element in <code>nums</code> with <strong>any</strong> integer.</p>
<p><code>nums</code> is considered <strong>continuous</strong> if both of the following conditions are fulfilled:</p>
<ul>
<li>All elements in <code>nums</code> are <strong>unique</strong>.</li>
<li>The difference between the <strong>maximum</strong> element and the <strong>minimum</strong> element in <code>nums</code> equals <code>nums.length - 1</code>.</li>
</ul>
<p>For example, <code>nums = [4, 2, 5, 3]</code> is <strong>continuous</strong>, but <code>nums = [1, 2, 3, 5, 6]</code> is <strong>not continuous</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of operations to make </em><code>nums</code><em> </em><strong><em>continuous</em></strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,2,5,3]
<strong>Output:</strong> 0
<strong>Explanation:</strong> nums is already continuous.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,5,6]
<strong>Output:</strong> 1
<strong>Explanation:</strong> One possible solution is to change the last element to 4.
The resulting array is [1,2,3,5,4], which is continuous.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,10,100,1000]
<strong>Output:</strong> 3
<strong>Explanation:</strong> One possible solution is to:
- Change the second element to 2.
- Change the third element to 3.
- Change the fourth element to 4.
The resulting array is [1,2,3,4], which is continuous.
</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>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int minOperations(vector<int>& nums) {\n \n unordered_set<int> unqEles;\n\n vector<int> unqs;\n for(auto num : nums){\n if(unqEles.find(num) == unqEles.end()){\n unqs.push_back(num);\n unqEles.insert(num);\n }\n }\n sort(unqs.begin(), unqs.end());\n int endIndx = 0; \n\n int sz = unqs.size();\n int minOps = INT_MAX;\n\n \n for(int startIndx = 0; startIndx < sz; startIndx++){\n while(endIndx < sz && unqs[endIndx] - unqs[startIndx] < nums.size())endIndx++;\n int opReq = nums.size() - (endIndx - startIndx);\n minOps = min(minOps, opReq);\n }\n return minOps;\n }\n};",
"memory": "110600"
} |
2,119 | <p>You are given an integer array <code>nums</code>. In one operation, you can replace <strong>any</strong> element in <code>nums</code> with <strong>any</strong> integer.</p>
<p><code>nums</code> is considered <strong>continuous</strong> if both of the following conditions are fulfilled:</p>
<ul>
<li>All elements in <code>nums</code> are <strong>unique</strong>.</li>
<li>The difference between the <strong>maximum</strong> element and the <strong>minimum</strong> element in <code>nums</code> equals <code>nums.length - 1</code>.</li>
</ul>
<p>For example, <code>nums = [4, 2, 5, 3]</code> is <strong>continuous</strong>, but <code>nums = [1, 2, 3, 5, 6]</code> is <strong>not continuous</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of operations to make </em><code>nums</code><em> </em><strong><em>continuous</em></strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,2,5,3]
<strong>Output:</strong> 0
<strong>Explanation:</strong> nums is already continuous.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,5,6]
<strong>Output:</strong> 1
<strong>Explanation:</strong> One possible solution is to change the last element to 4.
The resulting array is [1,2,3,5,4], which is continuous.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,10,100,1000]
<strong>Output:</strong> 3
<strong>Explanation:</strong> One possible solution is to:
- Change the second element to 2.
- Change the third element to 3.
- Change the fourth element to 4.
The resulting array is [1,2,3,4], which is continuous.
</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>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findIndex(vector<int>& nums, int lim, int index) {\n int res = nums.size();\n int l = index, r = nums.size() - 1;\n while(l <= r) {\n int mid = l + (r - l) / 2;\n if(nums[mid] > lim) {\n res = mid;\n r = mid - 1;\n } else {\n l = mid + 1;\n }\n }\n return res;\n }\n int minOperations(vector<int>& nums) {\n sort(nums.begin(), nums.end());\n set<int> uniq (nums.begin(), nums.end());\n vector<int> uniq_nums (uniq.begin(), uniq.end());\n int dup = nums.size() - uniq.size();\n int res = 1e9;\n for(int i=0; i<uniq_nums.size(); i++) {\n int low = uniq_nums[i];\n int high = low + nums.size() - 1;\n int index = findIndex(uniq_nums, high, i + 1);\n res = min(res, (int)uniq_nums.size() - index + i + dup);\n }\n return res;\n }\n};\n\n/*\n\n[41, 33, 29, 33, 35, 26, 47, 24, 18, 28]\n[18, 24, 26, 28, 29, 33, 33, 35, 41, 47]\n[18, 24, 26, 28, 29, 33, 35, 41, 47]\n \nminOp = \n\nvis = [2, 3, 4, 5, 6, 8, 10]\n\n*/",
"memory": "110600"
} |
2,119 | <p>You are given an integer array <code>nums</code>. In one operation, you can replace <strong>any</strong> element in <code>nums</code> with <strong>any</strong> integer.</p>
<p><code>nums</code> is considered <strong>continuous</strong> if both of the following conditions are fulfilled:</p>
<ul>
<li>All elements in <code>nums</code> are <strong>unique</strong>.</li>
<li>The difference between the <strong>maximum</strong> element and the <strong>minimum</strong> element in <code>nums</code> equals <code>nums.length - 1</code>.</li>
</ul>
<p>For example, <code>nums = [4, 2, 5, 3]</code> is <strong>continuous</strong>, but <code>nums = [1, 2, 3, 5, 6]</code> is <strong>not continuous</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of operations to make </em><code>nums</code><em> </em><strong><em>continuous</em></strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,2,5,3]
<strong>Output:</strong> 0
<strong>Explanation:</strong> nums is already continuous.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,5,6]
<strong>Output:</strong> 1
<strong>Explanation:</strong> One possible solution is to change the last element to 4.
The resulting array is [1,2,3,5,4], which is continuous.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,10,100,1000]
<strong>Output:</strong> 3
<strong>Explanation:</strong> One possible solution is to:
- Change the second element to 2.
- Change the third element to 3.
- Change the fourth element to 4.
The resulting array is [1,2,3,4], which is continuous.
</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>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minOperations(vector<int>& nums) {\n int ans = INT_MAX; \n int n = nums.size();\n sort(nums.begin() , nums.end());\n set<int> st(nums.begin() , nums.end());\n vector<int> num(st.begin() , st.end());\n\n for(int i = 0; i<num.size(); ++i){\n int l = num[i];\n int r = num[i]+n-1;\n\n auto it = upper_bound(num.begin()+i , num.end() , r)-num.begin();\n cout<<it<<\" \";\n int k = i+n-it;\n ans = min(ans , k);\n }\n return ans;\n }\n};",
"memory": "110700"
} |
2,119 | <p>You are given an integer array <code>nums</code>. In one operation, you can replace <strong>any</strong> element in <code>nums</code> with <strong>any</strong> integer.</p>
<p><code>nums</code> is considered <strong>continuous</strong> if both of the following conditions are fulfilled:</p>
<ul>
<li>All elements in <code>nums</code> are <strong>unique</strong>.</li>
<li>The difference between the <strong>maximum</strong> element and the <strong>minimum</strong> element in <code>nums</code> equals <code>nums.length - 1</code>.</li>
</ul>
<p>For example, <code>nums = [4, 2, 5, 3]</code> is <strong>continuous</strong>, but <code>nums = [1, 2, 3, 5, 6]</code> is <strong>not continuous</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of operations to make </em><code>nums</code><em> </em><strong><em>continuous</em></strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,2,5,3]
<strong>Output:</strong> 0
<strong>Explanation:</strong> nums is already continuous.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,5,6]
<strong>Output:</strong> 1
<strong>Explanation:</strong> One possible solution is to change the last element to 4.
The resulting array is [1,2,3,5,4], which is continuous.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,10,100,1000]
<strong>Output:</strong> 3
<strong>Explanation:</strong> One possible solution is to:
- Change the second element to 2.
- Change the third element to 3.
- Change the fourth element to 4.
The resulting array is [1,2,3,4], which is continuous.
</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>
</ul>
| 3 | {
"code": "/* Watch Editorial */\n\nclass Solution {\npublic:\n int minOperations(vector<int>& nums) {\n int n = nums.size();\n int ans = n;\n \n // std::set automatically sorts\n set<int> unique(nums.begin(), nums.end());\n vector<int> newNums;\n for (int num : unique) {\n newNums.push_back(num);\n }\n \n int j = 0;\n for (int i = 0; i < newNums.size(); i++) {\n while (j < newNums.size() && newNums[j] < newNums[i] + n) {\n j++;\n }\n \n int count = j - i;\n ans = min(ans, n - count);\n }\n \n return ans;\n }\n};",
"memory": "112000"
} |
2,119 | <p>You are given an integer array <code>nums</code>. In one operation, you can replace <strong>any</strong> element in <code>nums</code> with <strong>any</strong> integer.</p>
<p><code>nums</code> is considered <strong>continuous</strong> if both of the following conditions are fulfilled:</p>
<ul>
<li>All elements in <code>nums</code> are <strong>unique</strong>.</li>
<li>The difference between the <strong>maximum</strong> element and the <strong>minimum</strong> element in <code>nums</code> equals <code>nums.length - 1</code>.</li>
</ul>
<p>For example, <code>nums = [4, 2, 5, 3]</code> is <strong>continuous</strong>, but <code>nums = [1, 2, 3, 5, 6]</code> is <strong>not continuous</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of operations to make </em><code>nums</code><em> </em><strong><em>continuous</em></strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,2,5,3]
<strong>Output:</strong> 0
<strong>Explanation:</strong> nums is already continuous.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,5,6]
<strong>Output:</strong> 1
<strong>Explanation:</strong> One possible solution is to change the last element to 4.
The resulting array is [1,2,3,5,4], which is continuous.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,10,100,1000]
<strong>Output:</strong> 3
<strong>Explanation:</strong> One possible solution is to:
- Change the second element to 2.
- Change the third element to 3.
- Change the fourth element to 4.
The resulting array is [1,2,3,4], which is continuous.
</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>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minOperations(vector<int>& nums) {\n int n = nums.size();\n set <int> s(nums.begin(), nums.end());\n vector <int> arr;\n for(int num : s)arr.push_back(num);\n //iterate and ending at current, how many we need, and how many we have (with bs)\n //say current = 6, we need 4, so search for 3 (lb)\n int res = n;\n for(int i = 0; i<arr.size(); i++){\n int it = upper_bound(arr.begin(), arr.end(), arr[i] - n) - arr.begin();\n res = min(res, n - (i - it + 1));\n }\n return res;\n }\n};",
"memory": "112100"
} |
2,119 | <p>You are given an integer array <code>nums</code>. In one operation, you can replace <strong>any</strong> element in <code>nums</code> with <strong>any</strong> integer.</p>
<p><code>nums</code> is considered <strong>continuous</strong> if both of the following conditions are fulfilled:</p>
<ul>
<li>All elements in <code>nums</code> are <strong>unique</strong>.</li>
<li>The difference between the <strong>maximum</strong> element and the <strong>minimum</strong> element in <code>nums</code> equals <code>nums.length - 1</code>.</li>
</ul>
<p>For example, <code>nums = [4, 2, 5, 3]</code> is <strong>continuous</strong>, but <code>nums = [1, 2, 3, 5, 6]</code> is <strong>not continuous</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of operations to make </em><code>nums</code><em> </em><strong><em>continuous</em></strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,2,5,3]
<strong>Output:</strong> 0
<strong>Explanation:</strong> nums is already continuous.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,5,6]
<strong>Output:</strong> 1
<strong>Explanation:</strong> One possible solution is to change the last element to 4.
The resulting array is [1,2,3,5,4], which is continuous.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,10,100,1000]
<strong>Output:</strong> 3
<strong>Explanation:</strong> One possible solution is to:
- Change the second element to 2.
- Change the third element to 3.
- Change the fourth element to 4.
The resulting array is [1,2,3,4], which is continuous.
</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>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minOperations(vector<int>& nums) {\n\n set<int>st(nums.begin(), nums.end());\n\n vector<int>v;\n\n for(auto it : st)\n {\n v.push_back(it);\n }\n\n int ans=nums.size()-1;\n\n for(int i=0;i<v.size();i++)\n {\n int target = v[i] + nums.size()-1;\n auto idx = upper_bound(v.begin(), v.end() , target) - (v.begin()+i);\n int k = nums.size() - idx;\n // cout<<idx<<\". \"<<k<<endl;\n ans=min(ans, k);\n }\n\n return ans;\n \n }\n};",
"memory": "112100"
} |
2,119 | <p>You are given an integer array <code>nums</code>. In one operation, you can replace <strong>any</strong> element in <code>nums</code> with <strong>any</strong> integer.</p>
<p><code>nums</code> is considered <strong>continuous</strong> if both of the following conditions are fulfilled:</p>
<ul>
<li>All elements in <code>nums</code> are <strong>unique</strong>.</li>
<li>The difference between the <strong>maximum</strong> element and the <strong>minimum</strong> element in <code>nums</code> equals <code>nums.length - 1</code>.</li>
</ul>
<p>For example, <code>nums = [4, 2, 5, 3]</code> is <strong>continuous</strong>, but <code>nums = [1, 2, 3, 5, 6]</code> is <strong>not continuous</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of operations to make </em><code>nums</code><em> </em><strong><em>continuous</em></strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,2,5,3]
<strong>Output:</strong> 0
<strong>Explanation:</strong> nums is already continuous.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,5,6]
<strong>Output:</strong> 1
<strong>Explanation:</strong> One possible solution is to change the last element to 4.
The resulting array is [1,2,3,5,4], which is continuous.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,10,100,1000]
<strong>Output:</strong> 3
<strong>Explanation:</strong> One possible solution is to:
- Change the second element to 2.
- Change the third element to 3.
- Change the fourth element to 4.
The resulting array is [1,2,3,4], which is continuous.
</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>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minOperations(vector<int>& nums) {\n set<int> s(nums.begin(), nums.end());\n int ans = INT_MAX;\n vector<int> temp;\n for (auto x : s)\n temp.push_back(x);\n int n = nums.size();\n for (int i = 0; i < temp.size(); i++) {\n int k = n - 1 + temp[i];\n int j = upper_bound(temp.begin(), temp.end(), k) - temp.begin();\n ans = min(ans, n - j + i);\n }\n return ans;\n }\n};",
"memory": "112200"
} |
2,119 | <p>You are given an integer array <code>nums</code>. In one operation, you can replace <strong>any</strong> element in <code>nums</code> with <strong>any</strong> integer.</p>
<p><code>nums</code> is considered <strong>continuous</strong> if both of the following conditions are fulfilled:</p>
<ul>
<li>All elements in <code>nums</code> are <strong>unique</strong>.</li>
<li>The difference between the <strong>maximum</strong> element and the <strong>minimum</strong> element in <code>nums</code> equals <code>nums.length - 1</code>.</li>
</ul>
<p>For example, <code>nums = [4, 2, 5, 3]</code> is <strong>continuous</strong>, but <code>nums = [1, 2, 3, 5, 6]</code> is <strong>not continuous</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of operations to make </em><code>nums</code><em> </em><strong><em>continuous</em></strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,2,5,3]
<strong>Output:</strong> 0
<strong>Explanation:</strong> nums is already continuous.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,5,6]
<strong>Output:</strong> 1
<strong>Explanation:</strong> One possible solution is to change the last element to 4.
The resulting array is [1,2,3,5,4], which is continuous.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,10,100,1000]
<strong>Output:</strong> 3
<strong>Explanation:</strong> One possible solution is to:
- Change the second element to 2.
- Change the third element to 3.
- Change the fourth element to 4.
The resulting array is [1,2,3,4], which is continuous.
</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>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minOperations(vector<int>& nums) {\n \n int n = nums.size();\n int ans = n;\n\n set<int> unique(begin(nums), end(nums));\n vector<int> newNums;\n for (int num: unique){\n newNums.push_back(num);\n }\n\n int j = 0;\n for (int i = 0; i < newNums.size(); ++i){\n while (j < newNums.size() && newNums[j] < newNums[i] + n){\n ++j;\n }\n int window = j - i;\n ans = min(ans, n - window);\n }\n\n return ans;\n\n }\n};",
"memory": "112300"
} |
2,119 | <p>You are given an integer array <code>nums</code>. In one operation, you can replace <strong>any</strong> element in <code>nums</code> with <strong>any</strong> integer.</p>
<p><code>nums</code> is considered <strong>continuous</strong> if both of the following conditions are fulfilled:</p>
<ul>
<li>All elements in <code>nums</code> are <strong>unique</strong>.</li>
<li>The difference between the <strong>maximum</strong> element and the <strong>minimum</strong> element in <code>nums</code> equals <code>nums.length - 1</code>.</li>
</ul>
<p>For example, <code>nums = [4, 2, 5, 3]</code> is <strong>continuous</strong>, but <code>nums = [1, 2, 3, 5, 6]</code> is <strong>not continuous</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of operations to make </em><code>nums</code><em> </em><strong><em>continuous</em></strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,2,5,3]
<strong>Output:</strong> 0
<strong>Explanation:</strong> nums is already continuous.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,5,6]
<strong>Output:</strong> 1
<strong>Explanation:</strong> One possible solution is to change the last element to 4.
The resulting array is [1,2,3,5,4], which is continuous.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,10,100,1000]
<strong>Output:</strong> 3
<strong>Explanation:</strong> One possible solution is to:
- Change the second element to 2.
- Change the third element to 3.
- Change the fourth element to 4.
The resulting array is [1,2,3,4], which is continuous.
</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>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n\n int minOperations(vector<int>& nums) \n {\n int n = nums.size();\n int i;\n set<int> s;\n for(i=0;i<n;i++)\n {\n s.insert(nums[i]);\n }\n vector<int> v;\n for(auto itr : s)\n {\n v.push_back(itr);\n }\n int k = v.size();\n int ct = INT_MAX;\n for(i=0;i<k;i++)\n {\n int mr = v[i]+n-1;\n int j = upper_bound(v.begin(),v.end(),mr)-v.begin();\n int orr = n - (j-i);\n ct = min(ct,orr);\n }\n return ct;\n }\n};",
"memory": "112400"
} |
2,119 | <p>You are given an integer array <code>nums</code>. In one operation, you can replace <strong>any</strong> element in <code>nums</code> with <strong>any</strong> integer.</p>
<p><code>nums</code> is considered <strong>continuous</strong> if both of the following conditions are fulfilled:</p>
<ul>
<li>All elements in <code>nums</code> are <strong>unique</strong>.</li>
<li>The difference between the <strong>maximum</strong> element and the <strong>minimum</strong> element in <code>nums</code> equals <code>nums.length - 1</code>.</li>
</ul>
<p>For example, <code>nums = [4, 2, 5, 3]</code> is <strong>continuous</strong>, but <code>nums = [1, 2, 3, 5, 6]</code> is <strong>not continuous</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of operations to make </em><code>nums</code><em> </em><strong><em>continuous</em></strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,2,5,3]
<strong>Output:</strong> 0
<strong>Explanation:</strong> nums is already continuous.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,5,6]
<strong>Output:</strong> 1
<strong>Explanation:</strong> One possible solution is to change the last element to 4.
The resulting array is [1,2,3,5,4], which is continuous.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,10,100,1000]
<strong>Output:</strong> 3
<strong>Explanation:</strong> One possible solution is to:
- Change the second element to 2.
- Change the third element to 3.
- Change the fourth element to 4.
The resulting array is [1,2,3,4], which is continuous.
</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>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minOperations(vector<int>& nums) {\n int n= nums.size();\n int ans=n;\n set<int> st(nums.begin(),nums.end());\n vector<int> nw;\n for(int num:st)\n nw.push_back(num);\n for(int i=0;i<nw.size();i++){\n int lt=nw[i];\n int rt=nw[i]+n-1;\n int j=upper_bound(nw.begin(),nw.end(),rt)-nw.begin();\n ans = min(ans,n-j+i);\n printf(\"%d\\n\",j-i);\n }\n return ans;\n }\n};",
"memory": "112500"
} |
2,119 | <p>You are given an integer array <code>nums</code>. In one operation, you can replace <strong>any</strong> element in <code>nums</code> with <strong>any</strong> integer.</p>
<p><code>nums</code> is considered <strong>continuous</strong> if both of the following conditions are fulfilled:</p>
<ul>
<li>All elements in <code>nums</code> are <strong>unique</strong>.</li>
<li>The difference between the <strong>maximum</strong> element and the <strong>minimum</strong> element in <code>nums</code> equals <code>nums.length - 1</code>.</li>
</ul>
<p>For example, <code>nums = [4, 2, 5, 3]</code> is <strong>continuous</strong>, but <code>nums = [1, 2, 3, 5, 6]</code> is <strong>not continuous</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of operations to make </em><code>nums</code><em> </em><strong><em>continuous</em></strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,2,5,3]
<strong>Output:</strong> 0
<strong>Explanation:</strong> nums is already continuous.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,5,6]
<strong>Output:</strong> 1
<strong>Explanation:</strong> One possible solution is to change the last element to 4.
The resulting array is [1,2,3,5,4], which is continuous.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,10,100,1000]
<strong>Output:</strong> 3
<strong>Explanation:</strong> One possible solution is to:
- Change the second element to 2.
- Change the third element to 3.
- Change the fourth element to 4.
The resulting array is [1,2,3,4], which is continuous.
</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>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minOperations(vector<int>& nums) {\n int n = nums.size();\n map<int,int> freq;\n for(int &x : nums){\n freq[x]++;\n }\n\n vector<pair<int,int>> arr(freq.begin(),freq.end());\n\n int m = arr.size();\n int ans = 1e9;\n for(int i=0;i<m;i++){\n int first = arr[i].first, last = arr[i].first+n-1;\n\n int lo = i, hi = m-1;\n int idx = m;\n while(lo<=hi){\n int mid = lo + (hi-lo)/2;\n if(arr[mid].first > last){\n idx = mid;\n hi = mid - 1;\n }\n else{\n lo = mid + 1;\n }\n }\n\n ans = min(ans,n-(idx-i));\n }\n return ans;\n }\n};",
"memory": "112700"
} |
2,119 | <p>You are given an integer array <code>nums</code>. In one operation, you can replace <strong>any</strong> element in <code>nums</code> with <strong>any</strong> integer.</p>
<p><code>nums</code> is considered <strong>continuous</strong> if both of the following conditions are fulfilled:</p>
<ul>
<li>All elements in <code>nums</code> are <strong>unique</strong>.</li>
<li>The difference between the <strong>maximum</strong> element and the <strong>minimum</strong> element in <code>nums</code> equals <code>nums.length - 1</code>.</li>
</ul>
<p>For example, <code>nums = [4, 2, 5, 3]</code> is <strong>continuous</strong>, but <code>nums = [1, 2, 3, 5, 6]</code> is <strong>not continuous</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of operations to make </em><code>nums</code><em> </em><strong><em>continuous</em></strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,2,5,3]
<strong>Output:</strong> 0
<strong>Explanation:</strong> nums is already continuous.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,5,6]
<strong>Output:</strong> 1
<strong>Explanation:</strong> One possible solution is to change the last element to 4.
The resulting array is [1,2,3,5,4], which is continuous.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,10,100,1000]
<strong>Output:</strong> 3
<strong>Explanation:</strong> One possible solution is to:
- Change the second element to 2.
- Change the third element to 3.
- Change the fourth element to 4.
The resulting array is [1,2,3,4], which is continuous.
</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>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n // fm-vo\n int minOperations(vector<int>& nums) {\n int n = nums.size();\n map<int, int> mp;\n vector<int> arr;\n for(auto i: nums) {\n if(mp.find(i) == mp.end()) {\n arr.push_back(i);\n }\n mp[i] = 1;\n }\n\n sort(arr.begin(), arr.end());\n int ans = n;\n for(int i=0; i<arr.size(); i++) {\n int j = lower_bound(arr.begin() + i, arr.end(), arr[i] + n) - arr.begin();\n ans = min(ans, n-(j-i));\n }\n\n return ans;\n\n\n }\n};",
"memory": "113300"
} |
2,119 | <p>You are given an integer array <code>nums</code>. In one operation, you can replace <strong>any</strong> element in <code>nums</code> with <strong>any</strong> integer.</p>
<p><code>nums</code> is considered <strong>continuous</strong> if both of the following conditions are fulfilled:</p>
<ul>
<li>All elements in <code>nums</code> are <strong>unique</strong>.</li>
<li>The difference between the <strong>maximum</strong> element and the <strong>minimum</strong> element in <code>nums</code> equals <code>nums.length - 1</code>.</li>
</ul>
<p>For example, <code>nums = [4, 2, 5, 3]</code> is <strong>continuous</strong>, but <code>nums = [1, 2, 3, 5, 6]</code> is <strong>not continuous</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of operations to make </em><code>nums</code><em> </em><strong><em>continuous</em></strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,2,5,3]
<strong>Output:</strong> 0
<strong>Explanation:</strong> nums is already continuous.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,5,6]
<strong>Output:</strong> 1
<strong>Explanation:</strong> One possible solution is to change the last element to 4.
The resulting array is [1,2,3,5,4], which is continuous.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,10,100,1000]
<strong>Output:</strong> 3
<strong>Explanation:</strong> One possible solution is to:
- Change the second element to 2.
- Change the third element to 3.
- Change the fourth element to 4.
The resulting array is [1,2,3,4], which is continuous.
</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>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minOperations(vector<int>& nums) {\n int n= nums.size();\n \n set<int>st;\n for(int i=0; i<n; i++){\n st.insert(nums[i]);\n }\n vector<int>v;\n for(auto it:st){\n v.push_back(it);\n }\n sort(v.begin(), v.end());\n int ans=INT_MAX;\n for(int i=0; i<v.size(); i++){\n int l= v[i];\n int r= v[i]+n-1;\n int u= upper_bound(v.begin(), v.end(),r)-v.begin();\n ans= min(ans,n-(u-i));\n }\n\n return ans;\n }\n};",
"memory": "113400"
} |
101 | <p>Given the <code>root</code> of a binary tree, <em>check whether it is a mirror of itself</em> (i.e., symmetric around its center).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/symtree1.jpg" style="width: 354px; height: 291px;" />
<pre>
<strong>Input:</strong> root = [1,2,2,3,4,4,3]
<strong>Output:</strong> true
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/symtree2.jpg" style="width: 308px; height: 258px;" />
<pre>
<strong>Input:</strong> root = [1,2,2,null,3,null,3]
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 1000]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you solve it both recursively and iteratively? | 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n bool func(TreeNode* root1, TreeNode* root2){\n if(!root1 && !root2) return true;\n if((!root1 && root2) || (root1 && !root2)) return false;\n if(root1->val!=root2->val) return false;\n bool n1 = func(root1->left, root2->right);\n bool n2 = func(root1->right, root2->left);\n return n1&&n2;\n }\n bool isSymmetric(TreeNode* root) {\n bool flag = func(root->left, root->right);\n root->left = root->right = NULL;\n root=NULL;\n free(root);\n return flag;\n }\n};",
"memory": "13700"
} |
101 | <p>Given the <code>root</code> of a binary tree, <em>check whether it is a mirror of itself</em> (i.e., symmetric around its center).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/symtree1.jpg" style="width: 354px; height: 291px;" />
<pre>
<strong>Input:</strong> root = [1,2,2,3,4,4,3]
<strong>Output:</strong> true
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/symtree2.jpg" style="width: 308px; height: 258px;" />
<pre>
<strong>Input:</strong> root = [1,2,2,null,3,null,3]
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 1000]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you solve it both recursively and iteratively? | 0 | {
"code": "class Solution {\npublic:\n bool func(TreeNode* root1, TreeNode* root2){\n if(!root1 && !root2) return true;\n if((!root1 && root2) || (root1 && !root2)) return false;\n if(root1->val!=root2->val) return false;\n bool n1 = func(root1->left, root2->right);\n bool n2 = func(root1->right, root2->left);\n return n1&&n2;\n }\n bool isSymmetric(TreeNode* root) {\n bool flag = func(root->left, root->right);\n root->left = root->right = NULL;\n root=NULL;\n free(root);\n return flag;\n }\n};",
"memory": "13800"
} |
101 | <p>Given the <code>root</code> of a binary tree, <em>check whether it is a mirror of itself</em> (i.e., symmetric around its center).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/symtree1.jpg" style="width: 354px; height: 291px;" />
<pre>
<strong>Input:</strong> root = [1,2,2,3,4,4,3]
<strong>Output:</strong> true
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/symtree2.jpg" style="width: 308px; height: 258px;" />
<pre>
<strong>Input:</strong> root = [1,2,2,null,3,null,3]
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 1000]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you solve it both recursively and iteratively? | 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\n\nclass Solution {\npublic:\n\n bool isMirror(TreeNode* tree1, TreeNode* tree2){\n if(!tree1 && !tree2){\n return true;\n }\n\n if(!tree1 || !tree2){\n return false;\n }\n\n return (tree1->val == tree2->val) && isMirror(tree1->left, tree2->right) && isMirror(tree1->right, tree2->left);\n }\n\n bool isSymmetric(TreeNode* root) {\n if(!root) return true;\n bool flag = isMirror(root->left, root->right);\n root->left = root->right = NULL;\n root=NULL;\n free(root);\n return flag;\n }\n};",
"memory": "13900"
} |
101 | <p>Given the <code>root</code> of a binary tree, <em>check whether it is a mirror of itself</em> (i.e., symmetric around its center).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/symtree1.jpg" style="width: 354px; height: 291px;" />
<pre>
<strong>Input:</strong> root = [1,2,2,3,4,4,3]
<strong>Output:</strong> true
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/symtree2.jpg" style="width: 308px; height: 258px;" />
<pre>
<strong>Input:</strong> root = [1,2,2,null,3,null,3]
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 1000]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you solve it both recursively and iteratively? | 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n bool func(TreeNode* root1, TreeNode* root2){\n if(!root1 && !root2) return true;\n if((!root1 && root2) || (root1 && !root2)) return false;\n if(root1->val!=root2->val) return false;\n bool n1 = func(root1->left, root2->right);\n bool n2 = func(root1->right, root2->left);\n root2->left = root2->right = root1->left = root1->right = NULL;\n root2=root1=NULL;\n free(root1); free(root2);\n return n1&&n2;\n }\n bool isSymmetric(TreeNode* root) {\n bool flag = func(root->left, root->right);\n return flag;\n }\n};",
"memory": "14300"
} |
101 | <p>Given the <code>root</code> of a binary tree, <em>check whether it is a mirror of itself</em> (i.e., symmetric around its center).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/symtree1.jpg" style="width: 354px; height: 291px;" />
<pre>
<strong>Input:</strong> root = [1,2,2,3,4,4,3]
<strong>Output:</strong> true
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/symtree2.jpg" style="width: 308px; height: 258px;" />
<pre>
<strong>Input:</strong> root = [1,2,2,null,3,null,3]
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 1000]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you solve it both recursively and iteratively? | 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n bool func(TreeNode* root1, TreeNode* root2){\n if(!root1 && !root2) return true;\n if((!root1 && root2) || (root1 && !root2)) return false;\n if(root1->val!=root2->val) return false;\n bool n1 = func(root1->left, root2->right);\n bool n2 = func(root1->right, root2->left);\n root2->left = root2->right = root1->left = root1->right = NULL;\n root2=root1=NULL;\n free(root1); free(root2);\n return n1&&n2;\n }\n bool isSymmetric(TreeNode* root) {\n bool flag = func(root->left, root->right);\n return flag;\n }\n};",
"memory": "14300"
} |
101 | <p>Given the <code>root</code> of a binary tree, <em>check whether it is a mirror of itself</em> (i.e., symmetric around its center).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/symtree1.jpg" style="width: 354px; height: 291px;" />
<pre>
<strong>Input:</strong> root = [1,2,2,3,4,4,3]
<strong>Output:</strong> true
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/symtree2.jpg" style="width: 308px; height: 258px;" />
<pre>
<strong>Input:</strong> root = [1,2,2,null,3,null,3]
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 1000]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you solve it both recursively and iteratively? | 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n bool func(TreeNode* root1, TreeNode* root2){\n if(!root1 && !root2) return true;\n if((!root1 && root2) || (root1 && !root2)) return false;\n if(root1->val!=root2->val) return false;\n bool n1 = func(root1->left, root2->right);\n bool n2 = func(root1->right, root2->left);\n root1->left = root1->right = NULL;\n root1=NULL;\n free(root1);\n return n1&&n2;\n }\n bool isSymmetric(TreeNode* root) {\n bool flag = func(root->left, root->right);\n return flag;\n }\n};",
"memory": "16800"
} |
101 | <p>Given the <code>root</code> of a binary tree, <em>check whether it is a mirror of itself</em> (i.e., symmetric around its center).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/symtree1.jpg" style="width: 354px; height: 291px;" />
<pre>
<strong>Input:</strong> root = [1,2,2,3,4,4,3]
<strong>Output:</strong> true
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/symtree2.jpg" style="width: 308px; height: 258px;" />
<pre>
<strong>Input:</strong> root = [1,2,2,null,3,null,3]
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 1000]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you solve it both recursively and iteratively? | 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n\n bool solve(TreeNode *p , TreeNode *q) {\n if(p == NULL && q == NULL) {\n return true;\n }\n\n if(p != NULL && q == NULL || p == NULL && q != NULL || p->val != q->val) {\n return false;\n }\n\n bool left;\n bool right;\n\n if(p->val == q->val) {\n left = solve(p->left , q->right);\n right = solve(p->right , q->left);\n }\n\n return left && right;\n }\n\n bool isSymmetric(TreeNode* root) {\n if(root == NULL) {\n return true;\n }\n\n TreeNode *p = root->left;\n TreeNode *q = root->right;\n\n return solve(p , q);\n }\n};",
"memory": "17200"
} |
101 | <p>Given the <code>root</code> of a binary tree, <em>check whether it is a mirror of itself</em> (i.e., symmetric around its center).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/symtree1.jpg" style="width: 354px; height: 291px;" />
<pre>
<strong>Input:</strong> root = [1,2,2,3,4,4,3]
<strong>Output:</strong> true
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/symtree2.jpg" style="width: 308px; height: 258px;" />
<pre>
<strong>Input:</strong> root = [1,2,2,null,3,null,3]
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 1000]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you solve it both recursively and iteratively? | 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n bool check(TreeNode * l,TreeNode* r){\n if(l==NULL && r==NULL){\n return true;\n }\n if(l==NULL || r==NULL || l->val!=r->val) return false;\n bool le=check(l->left,r->right);\n bool ri=check(l->right,r->left);\n return le&&ri;\n }\n bool isSymmetric(TreeNode* root) {\n return check(root->left,root->right);\n }\n};",
"memory": "17300"
} |
101 | <p>Given the <code>root</code> of a binary tree, <em>check whether it is a mirror of itself</em> (i.e., symmetric around its center).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/symtree1.jpg" style="width: 354px; height: 291px;" />
<pre>
<strong>Input:</strong> root = [1,2,2,3,4,4,3]
<strong>Output:</strong> true
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/symtree2.jpg" style="width: 308px; height: 258px;" />
<pre>
<strong>Input:</strong> root = [1,2,2,null,3,null,3]
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 1000]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you solve it both recursively and iteratively? | 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n//SOLUTION USING QUEUE AND THIS SOLUTION SOO TRUMENDUS \nbool ismirror(TreeNode* root1,TreeNode* root2){\n if(root1==NULL && root2==NULL){\n return true;\n }\n if (root1 && root2 && root1->val == root2->val)\n return ismirror(root1->left, root2->right)\n && ismirror(root1->right, root2->left);\n\n // if none of above conditions is true then root1\n // and root2 are not mirror images\n return false; \n}\n\nbool check(TreeNode* root){\n return ismirror(root,root);\n}\n bool isSymmetric(TreeNode* root) {\n\n return check(root);\n // if(root == NULL) return true; // An empty tree is symmetric\n // queue<TreeNode*> q;\n // q.push(root->left);\n // q.push(root->right);\n // while (!q.empty()){\n // TreeNode* left = q.front(); q.pop();\n // TreeNode* right = q.front(); q.pop();\n // if(left == NULL && right == NULL) continue; // Both are NULL, symmetric at this level\n // if(left ==NULL or right==NULL) return false; // One is NULL and the other is not, not symmetric\n // if(left->val != right->val) return false; // Values differ, not symmetric\n // // Enqueue children in the order to compare them as mirror images\n // q.push(left->left);\n // q.push(right->right);\n // q.push(left->right);\n // q.push(right->left);\n // }\n // return true; // tree is symmetric\n }\n};",
"memory": "17300"
} |
101 | <p>Given the <code>root</code> of a binary tree, <em>check whether it is a mirror of itself</em> (i.e., symmetric around its center).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/symtree1.jpg" style="width: 354px; height: 291px;" />
<pre>
<strong>Input:</strong> root = [1,2,2,3,4,4,3]
<strong>Output:</strong> true
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/symtree2.jpg" style="width: 308px; height: 258px;" />
<pre>
<strong>Input:</strong> root = [1,2,2,null,3,null,3]
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 1000]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you solve it both recursively and iteratively? | 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n bool isSymmetric(TreeNode* root) {\n return root == NULL || isSymmetricHelp(root->left, root->right);\n }\n bool isSymmetricHelp(TreeNode* left, TreeNode* right){\n if(left == NULL || right == NULL){\n return left == right;\n }\n if(left->val != right->val) return false;\n\n return isSymmetricHelp(left->left, right->right) && isSymmetricHelp(left->right, right->left);\n }\n};",
"memory": "17400"
} |
101 | <p>Given the <code>root</code> of a binary tree, <em>check whether it is a mirror of itself</em> (i.e., symmetric around its center).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/symtree1.jpg" style="width: 354px; height: 291px;" />
<pre>
<strong>Input:</strong> root = [1,2,2,3,4,4,3]
<strong>Output:</strong> true
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/symtree2.jpg" style="width: 308px; height: 258px;" />
<pre>
<strong>Input:</strong> root = [1,2,2,null,3,null,3]
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 1000]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you solve it both recursively and iteratively? | 2 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n bool isSymmetric(TreeNode* root) {\n if(root == nullptr){\n return 1;\n }\n return are_reflections(root->left, root->right);\n }\n\n bool are_reflections(TreeNode* p, TreeNode* q){\n if(p == nullptr || q == nullptr){\n if(p==q){\n return 1;\n }else{\n return 0;\n }\n }\n if(p->val != q->val){\n return 0;\n }\n return are_reflections(p->left, q->right) && are_reflections(p->right, q->left);\n }\n};",
"memory": "17700"
} |
101 | <p>Given the <code>root</code> of a binary tree, <em>check whether it is a mirror of itself</em> (i.e., symmetric around its center).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/symtree1.jpg" style="width: 354px; height: 291px;" />
<pre>
<strong>Input:</strong> root = [1,2,2,3,4,4,3]
<strong>Output:</strong> true
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/symtree2.jpg" style="width: 308px; height: 258px;" />
<pre>
<strong>Input:</strong> root = [1,2,2,null,3,null,3]
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 1000]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you solve it both recursively and iteratively? | 2 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\nint check(TreeNode* p,TreeNode* q){\n if(p==NULL&&q==NULL)return 1;\n if(p==NULL||q==NULL)return 0;\n if(p->val!=q->val)return 0;\n return (check(p->left,q->right)&&check(p->right,q->left));\n}\n bool isSymmetric(TreeNode* root) {\n int ans=check(root,root);\n return ans;\n }\n};",
"memory": "17700"
} |
101 | <p>Given the <code>root</code> of a binary tree, <em>check whether it is a mirror of itself</em> (i.e., symmetric around its center).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/symtree1.jpg" style="width: 354px; height: 291px;" />
<pre>
<strong>Input:</strong> root = [1,2,2,3,4,4,3]
<strong>Output:</strong> true
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/symtree2.jpg" style="width: 308px; height: 258px;" />
<pre>
<strong>Input:</strong> root = [1,2,2,null,3,null,3]
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 1000]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you solve it both recursively and iteratively? | 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\nprivate:\n // bool isSymmetricHelp(TreeNode* left , TreeNode* right)\n // {\n // if(left == nullptr && right == nullptr) return left == right;\n // if(left->val != right->val) return false;\n // return isSymmetricHelp(left->left,right->right) && isSymmetricHelp(left->right,right->left);\n // }\npublic:\n bool isSymmetric(TreeNode* root) {\n if(root == nullptr) return true;\n queue<TreeNode*> q;\n q.push(root->left);\n q.push(root->right);\n while(q.size())\n {\n TreeNode* left = q.front();q.pop();\n TreeNode* right = q.front();q.pop();\n if(left == nullptr && right == nullptr) continue;\n else if(left == nullptr || right == nullptr) return left == right;\n if(left->val != right->val) return false;\n q.push(left->left);\n q.push(right->right);\n q.push(left->right);\n q.push(right->left);\n }\n return true;\n //return root == nullptr || isSymmetricHelp(root->left,root->right);\n }\n};",
"memory": "17800"
} |
101 | <p>Given the <code>root</code> of a binary tree, <em>check whether it is a mirror of itself</em> (i.e., symmetric around its center).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/symtree1.jpg" style="width: 354px; height: 291px;" />
<pre>
<strong>Input:</strong> root = [1,2,2,3,4,4,3]
<strong>Output:</strong> true
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/symtree2.jpg" style="width: 308px; height: 258px;" />
<pre>
<strong>Input:</strong> root = [1,2,2,null,3,null,3]
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 1000]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you solve it both recursively and iteratively? | 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\n bool traverse(TreeNode* l , TreeNode* r){\n if(!l or !r){\n return l==r;\n }\n if(l->val != r->val)return false;\n return traverse(l->left,r->right) and traverse(l->right,r->left);\n }\npublic:\n bool isSymmetric(TreeNode* root) {\n return traverse(root->left,root->right);\n }\n};",
"memory": "17800"
} |
101 | <p>Given the <code>root</code> of a binary tree, <em>check whether it is a mirror of itself</em> (i.e., symmetric around its center).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/symtree1.jpg" style="width: 354px; height: 291px;" />
<pre>
<strong>Input:</strong> root = [1,2,2,3,4,4,3]
<strong>Output:</strong> true
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/symtree2.jpg" style="width: 308px; height: 258px;" />
<pre>
<strong>Input:</strong> root = [1,2,2,null,3,null,3]
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 1000]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you solve it both recursively and iteratively? | 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n bool isPalindrome(vector<int>&temp)\n {\n int n = temp.size();\n if(n>1 && n%2==1)\n return false;\n for(int i=0; i<n; i++)\n {\n if(temp[i]!=temp[n-i-1])\n return false;\n }\n return true;\n }\n\n bool isSymmetric(TreeNode* root) {\n queue<TreeNode*>q;\n q.push(root);\n\n while(!q.empty())\n {\n int size = q.size();\n vector<int>temp;\n for(int i=0; i<size; i++)\n {\n TreeNode *node = q.front();\n q.pop();\n\n if(node!=NULL)\n {\n q.push(node->left);\n q.push(node->right);\n\n temp.push_back(node->val);\n }\n\n else\n temp.push_back(INT_MAX);\n }\n\n if(!isPalindrome(temp))\n return false;\n }\n return true;\n }\n};",
"memory": "17900"
} |
101 | <p>Given the <code>root</code> of a binary tree, <em>check whether it is a mirror of itself</em> (i.e., symmetric around its center).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/symtree1.jpg" style="width: 354px; height: 291px;" />
<pre>
<strong>Input:</strong> root = [1,2,2,3,4,4,3]
<strong>Output:</strong> true
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/symtree2.jpg" style="width: 308px; height: 258px;" />
<pre>
<strong>Input:</strong> root = [1,2,2,null,3,null,3]
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 1000]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you solve it both recursively and iteratively? | 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n bool isSymmetric(TreeNode* root) {\n TreeNode* swapped=new TreeNode();\n if(root==nullptr){\n return root;\n }\n swapped->val=root->val;\n swaptree(root, swapped);\n\n bool ans;\n ans=issame(root,swapped);\n return ans;\n }\n\n void swaptree(TreeNode* root, TreeNode* swapped){\n if(root!=nullptr){\n if(root->left!=nullptr){\n swapped->right=new TreeNode(root->left->val);\n }\n if(root->right!=nullptr){\n swapped->left=new TreeNode(root->right->val);\n }\n swaptree(root->left,swapped->right);\n swaptree(root->right,swapped->left);\n }\n }\n\n bool issame(TreeNode* p, TreeNode* q){\n bool ans=true;\n if(p!=nullptr && q==nullptr){\n return false;\n }\n else if(p==nullptr && q!=nullptr){\n return false;\n }\n if(p!=nullptr && q!=nullptr){\n if(p->val!=q->val){\n return false;\n }\n ans=issame(p->left,q->left);\n if(ans==false){\n return ans;\n }\n ans=issame(p->right,q->right);\n if(ans==false){\n return ans;\n }\n }\n return ans;\n }\n};",
"memory": "17900"
} |
102 | <p>Given the <code>root</code> of a binary tree, return <em>the level order traversal of its nodes' values</em>. (i.e., from left to right, level by level).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree1.jpg" style="width: 277px; height: 302px;" />
<pre>
<strong>Input:</strong> root = [3,9,20,null,null,15,7]
<strong>Output:</strong> [[3],[9,20],[15,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = [1]
<strong>Output:</strong> [[1]]
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = []
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 2000]</code>.</li>
<li><code>-1000 <= Node.val <= 1000</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<vector<int>> levelOrder(TreeNode* root) {\n vector<vector<int>> order;\n if (!root) return order;\n\n queue<TreeNode*> q;\n q.push(root);\n int size;\n while (!q.empty()) {\n size = q.size();\n\n vector<int> currentLevel;\n for(int i = 0; i < size; ++i){\n TreeNode* n = q.front();\n q.pop();\n currentLevel.emplace_back(move(n->val));\n if(n->left) q.push(n->left);\n if(n->right) q.push(n->right);\n n->left = n->right = nullptr;\n }\n order.emplace_back( move(currentLevel));\n }\n\n return order;\n }\n};\n",
"memory": "11600"
} |
102 | <p>Given the <code>root</code> of a binary tree, return <em>the level order traversal of its nodes' values</em>. (i.e., from left to right, level by level).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree1.jpg" style="width: 277px; height: 302px;" />
<pre>
<strong>Input:</strong> root = [3,9,20,null,null,15,7]
<strong>Output:</strong> [[3],[9,20],[15,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = [1]
<strong>Output:</strong> [[1]]
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = []
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 2000]</code>.</li>
<li><code>-1000 <= Node.val <= 1000</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<vector<int>> levelOrder(TreeNode* root) {\n vector<vector<int>> order;\n if (!root) return order;\n\n queue<TreeNode*> q;\n q.push(root);\n int size;\n while (!q.empty()) {\n size = q.size();\n\n vector<int> currentLevel;\n for (int i = 1; i <= size; i++) {\n TreeNode* n = q.front();\n q.pop();\n currentLevel.emplace_back(move(n->val));\n if(n->left) q.push(n->left);\n if(n->right) q.push(n->right);\n n->left = n->right = nullptr;\n }\n order.emplace_back( move(currentLevel));\n }\n\n return order;\n }\n};\n",
"memory": "11600"
} |
102 | <p>Given the <code>root</code> of a binary tree, return <em>the level order traversal of its nodes' values</em>. (i.e., from left to right, level by level).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree1.jpg" style="width: 277px; height: 302px;" />
<pre>
<strong>Input:</strong> root = [3,9,20,null,null,15,7]
<strong>Output:</strong> [[3],[9,20],[15,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = [1]
<strong>Output:</strong> [[1]]
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = []
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 2000]</code>.</li>
<li><code>-1000 <= Node.val <= 1000</code></li>
</ul>
| 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<vector<int>> levelOrder(TreeNode* root) {\n vector<vector<int>> vec;\n if(root==NULL)\n return vec;\n queue<TreeNode*> q;\n q.push(root);\n int s;\n int i;\n while(!q.empty())\n {\n s = q.size();\n vector<int> currLevel;\n for(i = 0; i<s; i++)\n {\n TreeNode *node = q.front();\n q.pop();\n\n if(node->left!=NULL)\n {\n q.push(node->left);\n }\n \n if(node->right!=NULL)\n {\n q.push(node->right);\n }\n\n node->left = node->right = nullptr;\n \n currLevel.emplace_back(move(node->val));\n }\n\n vec.emplace_back(move(currLevel));\n\n }\n return vec;\n }\n};",
"memory": "11700"
} |
102 | <p>Given the <code>root</code> of a binary tree, return <em>the level order traversal of its nodes' values</em>. (i.e., from left to right, level by level).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree1.jpg" style="width: 277px; height: 302px;" />
<pre>
<strong>Input:</strong> root = [3,9,20,null,null,15,7]
<strong>Output:</strong> [[3],[9,20],[15,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = [1]
<strong>Output:</strong> [[1]]
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = []
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 2000]</code>.</li>
<li><code>-1000 <= Node.val <= 1000</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<vector<int>> levelOrder(TreeNode* root) {\n vector<vector<int>> order;\n if (!root) return order;\n\n queue<TreeNode*> q;\n q.push(root);\n int size;\n while (!q.empty()) {\n size = q.size();\n\n vector<int> currentLevel;\n for (int i = 1, s = q.size(); i <= s; i++) {\n TreeNode* n = q.front();\n q.pop();\n currentLevel.emplace_back(move(n->val));\n if(n->left) q.push(n->left);\n if(n->right) q.push(n->right);\n n->left = n->right = nullptr;\n }\n order.emplace_back( move(currentLevel));\n }\n\n return order;\n }\n};\n",
"memory": "11800"
} |
102 | <p>Given the <code>root</code> of a binary tree, return <em>the level order traversal of its nodes' values</em>. (i.e., from left to right, level by level).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree1.jpg" style="width: 277px; height: 302px;" />
<pre>
<strong>Input:</strong> root = [3,9,20,null,null,15,7]
<strong>Output:</strong> [[3],[9,20],[15,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = [1]
<strong>Output:</strong> [[1]]
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = []
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 2000]</code>.</li>
<li><code>-1000 <= Node.val <= 1000</code></li>
</ul>
| 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<vector<int>> levelOrder(TreeNode* root) {\n vector<vector<int>> ans;\n if (root == NULL) return ans;\n queue<TreeNode*> q;\n q.push(root);\n\n while(!q.empty()) {\n int n = q.size();\n vector<int> level;\n for (int i=0; i<n; i++) {\n TreeNode *node = q.front();\n q.pop();\n if (node->left != NULL) q.push(node->left);\n if (node->right != NULL) q.push(node->right);\n root->left = root->right = nullptr;\n level.push_back(node->val);\n }\n ans.push_back(level);\n }\n return ans;\n }\n};",
"memory": "11900"
} |
102 | <p>Given the <code>root</code> of a binary tree, return <em>the level order traversal of its nodes' values</em>. (i.e., from left to right, level by level).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree1.jpg" style="width: 277px; height: 302px;" />
<pre>
<strong>Input:</strong> root = [3,9,20,null,null,15,7]
<strong>Output:</strong> [[3],[9,20],[15,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = [1]
<strong>Output:</strong> [[1]]
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = []
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 2000]</code>.</li>
<li><code>-1000 <= Node.val <= 1000</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<vector<int>> levelOrder(TreeNode* root) {\n vector<vector<int>> order;\n if (!root) return order;\n\n queue<TreeNode*> q;\n q.push(root);\n int size;\n while (!q.empty()) {\n size = q.size();\n\n vector<int> currentLevel;\n for(int i = 0; i < size; ++i){\n auto n = q.front();\n q.pop();\n currentLevel.push_back(n->val);\n if(n->left) q.push(n->left);\n if(n->right) q.push(n->right);\n n->left = n->right = nullptr;\n }\n order.emplace_back(currentLevel);\n }\n\n return order;\n }\n};\n",
"memory": "12000"
} |
102 | <p>Given the <code>root</code> of a binary tree, return <em>the level order traversal of its nodes' values</em>. (i.e., from left to right, level by level).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree1.jpg" style="width: 277px; height: 302px;" />
<pre>
<strong>Input:</strong> root = [3,9,20,null,null,15,7]
<strong>Output:</strong> [[3],[9,20],[15,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = [1]
<strong>Output:</strong> [[1]]
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = []
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 2000]</code>.</li>
<li><code>-1000 <= Node.val <= 1000</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<vector<int>> levelOrder(TreeNode* root) {\n vector<vector<int>> order;\n if (!root) return order;\n\n queue<TreeNode*> q;\n q.push(root);\n int size;\n while (!q.empty()) {\n size = q.size();\n\n vector<int> currentLevel;\n for(int i = 0; i < size; ++i){\n TreeNode* n = q.front();\n q.pop();\n currentLevel.push_back(n->val);\n if(n->left) q.push(n->left);\n if(n->right) q.push(n->right);\n n->left = n->right = nullptr;\n }\n order.emplace_back(currentLevel);\n }\n\n return order;\n }\n};\n",
"memory": "12000"
} |
102 | <p>Given the <code>root</code> of a binary tree, return <em>the level order traversal of its nodes' values</em>. (i.e., from left to right, level by level).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree1.jpg" style="width: 277px; height: 302px;" />
<pre>
<strong>Input:</strong> root = [3,9,20,null,null,15,7]
<strong>Output:</strong> [[3],[9,20],[15,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = [1]
<strong>Output:</strong> [[1]]
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = []
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 2000]</code>.</li>
<li><code>-1000 <= Node.val <= 1000</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<vector<int>> levelOrder(TreeNode* root) {\n vector<vector<int>>ans;\n if(root==NULL)return ans;\n queue<TreeNode*>q;\n q.push(root);\n while(!q.empty()) {\n int s = q.size();\n vector<int>v;\n for(int i=0;i<s;i++) {\n TreeNode *node=q.front();\n q.pop();\n if(node->left!=NULL)q.push(node->left);\n if(node->right!=NULL)q.push(node->right);\n node->left = node->right = nullptr;\n v.push_back(node->val);\n }\n ans.push_back(v);\n }\n return ans;\n }\n};",
"memory": "12100"
} |
102 | <p>Given the <code>root</code> of a binary tree, return <em>the level order traversal of its nodes' values</em>. (i.e., from left to right, level by level).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree1.jpg" style="width: 277px; height: 302px;" />
<pre>
<strong>Input:</strong> root = [3,9,20,null,null,15,7]
<strong>Output:</strong> [[3],[9,20],[15,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = [1]
<strong>Output:</strong> [[1]]
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = []
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 2000]</code>.</li>
<li><code>-1000 <= Node.val <= 1000</code></li>
</ul>
| 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\n int getHeight(TreeNode* root){\n if (!root){\n return 0;\n }\n return max(getHeight(root -> left), getHeight(root -> right)) + 1;\n }\n\n void bfs (TreeNode* root, int level, vector<int>& vec) {\n if (!root || level <= 0) {\n return;\n }\n if (level == 1) {\n vec.push_back(root -> val);\n } else {\n bfs(root -> left, level - 1, vec);\n bfs(root -> right, level - 1, vec);\n }\n }\npublic:\n vector<vector<int>> levelOrder(TreeNode* root) {\n int h = getHeight(root);\n vector<vector<int>> vec (h);\n for (int i = 1; i <= h; ++i){\n bfs(root, i, vec[i - 1]);\n }\n return vec;\n }\n};",
"memory": "14500"
} |
102 | <p>Given the <code>root</code> of a binary tree, return <em>the level order traversal of its nodes' values</em>. (i.e., from left to right, level by level).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree1.jpg" style="width: 277px; height: 302px;" />
<pre>
<strong>Input:</strong> root = [3,9,20,null,null,15,7]
<strong>Output:</strong> [[3],[9,20],[15,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = [1]
<strong>Output:</strong> [[1]]
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = []
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 2000]</code>.</li>
<li><code>-1000 <= Node.val <= 1000</code></li>
</ul>
| 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int noOfLevel(TreeNode *root){\n if(root == NULL) return 0;\n return 1+ max(noOfLevel(root->left), noOfLevel(root->right));\n }\n void levelElement(TreeNode * root, vector<vector<int>> &ans, int level){\n if(root == NULL) return;\n ans[level].push_back(root->val);\n levelElement(root->left, ans, level + 1);\n levelElement(root->right, ans, level + 1);\n }\n vector<vector<int>> levelOrder(TreeNode* root) {\n int n = noOfLevel(root);\n vector<vector<int>> ans;\n for(int i = 1; i<=n; i++){\n vector<int> v;\n ans.push_back(v);\n }\n levelElement(root, ans, 0);\n return ans;\n }\n};",
"memory": "14600"
} |
102 | <p>Given the <code>root</code> of a binary tree, return <em>the level order traversal of its nodes' values</em>. (i.e., from left to right, level by level).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree1.jpg" style="width: 277px; height: 302px;" />
<pre>
<strong>Input:</strong> root = [3,9,20,null,null,15,7]
<strong>Output:</strong> [[3],[9,20],[15,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = [1]
<strong>Output:</strong> [[1]]
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = []
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 2000]</code>.</li>
<li><code>-1000 <= Node.val <= 1000</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n // void traverse(TreeNode* root, int n, vector<vector<int>>& ans){\n // if(root == NULL) return;\n // if (n >= ans.size()) {\n // ans.push_back(vector<int>());\n // }\n // ans[n].push_back(root -> val);\n // traverse(root -> left, n+1, ans);\n // traverse(root -> right, n+1, ans);\n\n // }\n // void helper(queue<TreeNode*>& q,vector<int>& ans;){\n \n // }\n vector<vector<int>> levelOrder(TreeNode* root) {\n ios_base::sync_with_stdio(false);\n vector<vector<int>> ans;\n queue<TreeNode*> q;\n // using queue\n if(root) q.push(root);\n\n while(!q.empty()){\n int levelSize = q.size();\n\n ans.push_back(vector<int>());\n\n for (int i = 0; i < levelSize; i++) {\n TreeNode* temp = q.front();\n q.pop();\n\n ans.back().push_back(temp->val); \n if(temp->left){\n q.push(temp->left);\n }\n if(temp->right){\n q.push(temp->right);\n }\n }\n }\n\n return ans;\n\n\n }\n};",
"memory": "14700"
} |
102 | <p>Given the <code>root</code> of a binary tree, return <em>the level order traversal of its nodes' values</em>. (i.e., from left to right, level by level).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree1.jpg" style="width: 277px; height: 302px;" />
<pre>
<strong>Input:</strong> root = [3,9,20,null,null,15,7]
<strong>Output:</strong> [[3],[9,20],[15,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = [1]
<strong>Output:</strong> [[1]]
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = []
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 2000]</code>.</li>
<li><code>-1000 <= Node.val <= 1000</code></li>
</ul>
| 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<vector<int>> levelOrder(TreeNode* root) {\n vector<vector<int>> res;\n solve(root, res, 0);\n return res;\n }\n\n void solve(TreeNode* root, vector<vector<int>>& res, const int level) {\n if (!root) return;\n if (level >= res.size()) res.emplace_back();\n res[level].push_back(root->val);\n if (root->left)\n solve(root->left, res, level + 1);\n if (root->right)\n solve(root->right, res, level + 1);\n }\n};",
"memory": "14700"
} |
102 | <p>Given the <code>root</code> of a binary tree, return <em>the level order traversal of its nodes' values</em>. (i.e., from left to right, level by level).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree1.jpg" style="width: 277px; height: 302px;" />
<pre>
<strong>Input:</strong> root = [3,9,20,null,null,15,7]
<strong>Output:</strong> [[3],[9,20],[15,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = [1]
<strong>Output:</strong> [[1]]
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = []
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 2000]</code>.</li>
<li><code>-1000 <= Node.val <= 1000</code></li>
</ul>
| 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<vector<int>> levelOrder(TreeNode* root) {\n vector<vector<int>> ans;\n if(root == NULL) return ans;\n queue<TreeNode*> bfs;\n bfs.push(root); bfs.push(NULL);\n int level = 0;\n ans.push_back({});\n while(bfs.size()) {\n TreeNode* node = bfs.front();\n bfs.pop();\n\n if(node == NULL){\n if(bfs.size() == 0) break;\n level++;\n bfs.push(NULL);\n ans.push_back({});\n continue;\n }\n ans[level].push_back(node->val);\n if(node->left) bfs.push(node->left);\n if(node->right) bfs.push(node->right);\n }\n return ans;\n }\n};",
"memory": "14800"
} |
102 | <p>Given the <code>root</code> of a binary tree, return <em>the level order traversal of its nodes' values</em>. (i.e., from left to right, level by level).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree1.jpg" style="width: 277px; height: 302px;" />
<pre>
<strong>Input:</strong> root = [3,9,20,null,null,15,7]
<strong>Output:</strong> [[3],[9,20],[15,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = [1]
<strong>Output:</strong> [[1]]
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = []
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 2000]</code>.</li>
<li><code>-1000 <= Node.val <= 1000</code></li>
</ul>
| 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<vector<int>> levelOrder(TreeNode* root) {\n return BFS(root);\n }\nprivate:\n vector<vector<int>> BFS(TreeNode* root) {\n if(!root) return {};\n vector<vector<int>> ans;\n vector<TreeNode*> curr,next;\n curr.push_back(root);\n while(!curr.empty()) {\n ans.push_back({});\n for(TreeNode* node : curr) {\n ans.back().push_back(node->val);\n if(node->left) next.push_back(node->left);\n if(node->right) next.push_back(node->right);\n }\n swap(curr,next);\n next.clear();\n }\n return ans;\n }\n};\n\n\n",
"memory": "14800"
} |
102 | <p>Given the <code>root</code> of a binary tree, return <em>the level order traversal of its nodes' values</em>. (i.e., from left to right, level by level).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree1.jpg" style="width: 277px; height: 302px;" />
<pre>
<strong>Input:</strong> root = [3,9,20,null,null,15,7]
<strong>Output:</strong> [[3],[9,20],[15,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = [1]
<strong>Output:</strong> [[1]]
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = []
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 2000]</code>.</li>
<li><code>-1000 <= Node.val <= 1000</code></li>
</ul>
| 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<vector<int>> levelOrder(TreeNode* root) {\n vector<vector<int>>result;\n vector<int>level;\n \n deque<TreeNode*>q;\n if (!root)\n return result;\n q.push_back(root);\n \n while (!q.empty()) {\n int levelSize = q.size();\n for (int i = 0; i < levelSize;++i) {\n TreeNode* pop = q.front();\n q.pop_front();\n \n level.push_back(pop->val);\n\n if (pop->left)\n q.push_back(pop->left);\n if (pop->right)\n q.push_back(pop->right);\n\n }\n result.push_back(level);\n level.clear();\n }\n return result;\n}\n};",
"memory": "14900"
} |
102 | <p>Given the <code>root</code> of a binary tree, return <em>the level order traversal of its nodes' values</em>. (i.e., from left to right, level by level).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree1.jpg" style="width: 277px; height: 302px;" />
<pre>
<strong>Input:</strong> root = [3,9,20,null,null,15,7]
<strong>Output:</strong> [[3],[9,20],[15,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = [1]
<strong>Output:</strong> [[1]]
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = []
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 2000]</code>.</li>
<li><code>-1000 <= Node.val <= 1000</code></li>
</ul>
| 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<vector<int>> levelOrder(TreeNode* root) {\n vector<vector<int>> ans;\n if(root==NULL) return ans;\n\n queue<TreeNode*> q;\n q.push(root);\n while(!q.empty()){\n vector<int> level;\n int size=q.size();\n for(int i=0;i<size;i++){\n TreeNode* node= q.front();\n q.pop();\n if(node->left!=NULL) q.push(node->left);\n if(node->right!=NULL) q.push(node->right);\n level.push_back(node->val);\n }\n ans.push_back(level);\n }\n return ans;\n }\n};",
"memory": "14900"
} |
102 | <p>Given the <code>root</code> of a binary tree, return <em>the level order traversal of its nodes' values</em>. (i.e., from left to right, level by level).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree1.jpg" style="width: 277px; height: 302px;" />
<pre>
<strong>Input:</strong> root = [3,9,20,null,null,15,7]
<strong>Output:</strong> [[3],[9,20],[15,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = [1]
<strong>Output:</strong> [[1]]
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = []
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 2000]</code>.</li>
<li><code>-1000 <= Node.val <= 1000</code></li>
</ul>
| 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<vector<int>> levelOrder(TreeNode* root) {\n\n vector<vector<int>>ans;\n if(root==NULL)\n {\n return ans;\n }\n vector<int>v;\n queue<TreeNode*>q;\n q.push(root);\n while(!q.empty())\n {\n int n = q.size();\n for(int i=0;i<n;i++)\n {\n TreeNode* node = q.front();\n q.pop();\n v.push_back(node->val);\n if(node->left!=NULL)\n {\n q.push(node->left);\n }\n if(node->right!=NULL)\n {\n q.push(node->right);\n }\n }\n ans.push_back(v);\n v.clear();\n }\n return ans;\n }\n};",
"memory": "15000"
} |
102 | <p>Given the <code>root</code> of a binary tree, return <em>the level order traversal of its nodes' values</em>. (i.e., from left to right, level by level).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree1.jpg" style="width: 277px; height: 302px;" />
<pre>
<strong>Input:</strong> root = [3,9,20,null,null,15,7]
<strong>Output:</strong> [[3],[9,20],[15,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = [1]
<strong>Output:</strong> [[1]]
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = []
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 2000]</code>.</li>
<li><code>-1000 <= Node.val <= 1000</code></li>
</ul>
| 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<vector<int>> levelOrder(TreeNode* root) {\n vector<vector<int>> ans;\n if(root == NULL) return ans;\n\n queue<TreeNode*> q;\n q.push(root);\n while(!q.empty()) {\n vector<int> level;\n int size = q.size();\n for(int i = 0; i < size; i++) {\n TreeNode* Node = q.front();\n q.pop();\n if(Node -> left != NULL) q.push(Node -> left);\n if(Node -> right != NULL) q.push(Node -> right);\n level.push_back(Node -> val);\n }\n ans.push_back(level);\n }\n\n return ans;\n }\n};",
"memory": "15000"
} |
102 | <p>Given the <code>root</code> of a binary tree, return <em>the level order traversal of its nodes' values</em>. (i.e., from left to right, level by level).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree1.jpg" style="width: 277px; height: 302px;" />
<pre>
<strong>Input:</strong> root = [3,9,20,null,null,15,7]
<strong>Output:</strong> [[3],[9,20],[15,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = [1]
<strong>Output:</strong> [[1]]
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = []
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 2000]</code>.</li>
<li><code>-1000 <= Node.val <= 1000</code></li>
</ul>
| 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<vector<int>> levelOrder(TreeNode* root) {\n vector<vector<int>> ans;\n if(!root){\n return ans;\n }\n queue<TreeNode*> q;\n q.push(root);\n while(!q.empty()){\n int size = q.size();\n vector<int> temp;\n while(size--){\n TreeNode* node = q.front();\n q.pop();\n temp.push_back(node->val);\n if(node->left){\n q.push(node->left);\n }\n if(node->right){\n q.push(node->right);\n }\n }\n ans.push_back(temp);\n }\n return ans;\n }\n};",
"memory": "15100"
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.