id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
287
<p>Given an array of integers <code>nums</code> containing&nbsp;<code>n + 1</code> integers where each integer is in the range <code>[1, n]</code> inclusive.</p> <p>There is only <strong>one repeated number</strong> in <code>nums</code>, return <em>this&nbsp;repeated&nbsp;number</em>.</p> <p>You must solve the problem <strong>without</strong> modifying the array <code>nums</code>&nbsp;and uses only constant extra space.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,4,2,2] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,3,4,2] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,3,3,3,3] <strong>Output:</strong> 3</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>nums.length == n + 1</code></li> <li><code>1 &lt;= nums[i] &lt;= n</code></li> <li>All the integers in <code>nums</code> appear only <strong>once</strong> except for <strong>precisely one integer</strong> which appears <strong>two or more</strong> times.</li> </ul> <p>&nbsp;</p> <p><b>Follow up:</b></p> <ul> <li>How can we prove that at least one duplicate number must exist in <code>nums</code>?</li> <li>Can you solve the problem in linear runtime complexity?</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findDuplicate(vector<int>& nums) {\n int i=0;\n vector<int> v=nums;\n sort(v.begin(),v.end());\n while(i!=(v.size()-1)){\n if(v[i]==v[i+1]) return v[i];\n i++;\n }\n return -1;\n }\n};", "memory": "51240" }
287
<p>Given an array of integers <code>nums</code> containing&nbsp;<code>n + 1</code> integers where each integer is in the range <code>[1, n]</code> inclusive.</p> <p>There is only <strong>one repeated number</strong> in <code>nums</code>, return <em>this&nbsp;repeated&nbsp;number</em>.</p> <p>You must solve the problem <strong>without</strong> modifying the array <code>nums</code>&nbsp;and uses only constant extra space.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,4,2,2] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,3,4,2] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,3,3,3,3] <strong>Output:</strong> 3</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>nums.length == n + 1</code></li> <li><code>1 &lt;= nums[i] &lt;= n</code></li> <li>All the integers in <code>nums</code> appear only <strong>once</strong> except for <strong>precisely one integer</strong> which appears <strong>two or more</strong> times.</li> </ul> <p>&nbsp;</p> <p><b>Follow up:</b></p> <ul> <li>How can we prove that at least one duplicate number must exist in <code>nums</code>?</li> <li>Can you solve the problem in linear runtime complexity?</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findDuplicate(vector<int>& nums) {\n int n = nums.size();\n int f[int(1e6)] = {0};\n for(int i=0; i<n; i++){\n f[nums[i]]++;\n if(f[nums[i]]==2)return nums[i];\n }\n return -1;\n }\n};", "memory": "52361" }
287
<p>Given an array of integers <code>nums</code> containing&nbsp;<code>n + 1</code> integers where each integer is in the range <code>[1, n]</code> inclusive.</p> <p>There is only <strong>one repeated number</strong> in <code>nums</code>, return <em>this&nbsp;repeated&nbsp;number</em>.</p> <p>You must solve the problem <strong>without</strong> modifying the array <code>nums</code>&nbsp;and uses only constant extra space.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,4,2,2] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,3,4,2] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,3,3,3,3] <strong>Output:</strong> 3</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>nums.length == n + 1</code></li> <li><code>1 &lt;= nums[i] &lt;= n</code></li> <li>All the integers in <code>nums</code> appear only <strong>once</strong> except for <strong>precisely one integer</strong> which appears <strong>two or more</strong> times.</li> </ul> <p>&nbsp;</p> <p><b>Follow up:</b></p> <ul> <li>How can we prove that at least one duplicate number must exist in <code>nums</code>?</li> <li>Can you solve the problem in linear runtime complexity?</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findDuplicate(vector<int>& nums) {\n int arr[1000001] ={0};\n int value;\n for(int i=0;i<=nums.size();i++ ){\n arr[nums[i]] = arr[nums[i]] +1; \n if (arr[nums[i]] == 2) { value = nums[i]; break;}\n }\n return value;\n }\n};", "memory": "53483" }
287
<p>Given an array of integers <code>nums</code> containing&nbsp;<code>n + 1</code> integers where each integer is in the range <code>[1, n]</code> inclusive.</p> <p>There is only <strong>one repeated number</strong> in <code>nums</code>, return <em>this&nbsp;repeated&nbsp;number</em>.</p> <p>You must solve the problem <strong>without</strong> modifying the array <code>nums</code>&nbsp;and uses only constant extra space.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,4,2,2] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,3,4,2] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,3,3,3,3] <strong>Output:</strong> 3</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>nums.length == n + 1</code></li> <li><code>1 &lt;= nums[i] &lt;= n</code></li> <li>All the integers in <code>nums</code> appear only <strong>once</strong> except for <strong>precisely one integer</strong> which appears <strong>two or more</strong> times.</li> </ul> <p>&nbsp;</p> <p><b>Follow up:</b></p> <ul> <li>How can we prove that at least one duplicate number must exist in <code>nums</code>?</li> <li>Can you solve the problem in linear runtime complexity?</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findDuplicate(vector<int>& nums) {\n int arr[1000000] = {0};\n for(int i = 0;i < nums.size();i++){\n if(arr[nums[i]] != 0) return nums[i];\n arr[nums[i]]++;\n }\n return -1;\n }\n};", "memory": "54604" }
287
<p>Given an array of integers <code>nums</code> containing&nbsp;<code>n + 1</code> integers where each integer is in the range <code>[1, n]</code> inclusive.</p> <p>There is only <strong>one repeated number</strong> in <code>nums</code>, return <em>this&nbsp;repeated&nbsp;number</em>.</p> <p>You must solve the problem <strong>without</strong> modifying the array <code>nums</code>&nbsp;and uses only constant extra space.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,4,2,2] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,3,4,2] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,3,3,3,3] <strong>Output:</strong> 3</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>nums.length == n + 1</code></li> <li><code>1 &lt;= nums[i] &lt;= n</code></li> <li>All the integers in <code>nums</code> appear only <strong>once</strong> except for <strong>precisely one integer</strong> which appears <strong>two or more</strong> times.</li> </ul> <p>&nbsp;</p> <p><b>Follow up:</b></p> <ul> <li>How can we prove that at least one duplicate number must exist in <code>nums</code>?</li> <li>Can you solve the problem in linear runtime complexity?</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findDuplicate(vector<int>& nums) {\n int n=nums.size();\n int hash[1000000]={0};\n for(int i=0;i<n;i++)\n {\n hash[nums[i]]++;\n if(hash[nums[i]]==2)\n {\n return nums[i];\n\n }\n } \n return -1;\n }\n};", "memory": "55725" }
287
<p>Given an array of integers <code>nums</code> containing&nbsp;<code>n + 1</code> integers where each integer is in the range <code>[1, n]</code> inclusive.</p> <p>There is only <strong>one repeated number</strong> in <code>nums</code>, return <em>this&nbsp;repeated&nbsp;number</em>.</p> <p>You must solve the problem <strong>without</strong> modifying the array <code>nums</code>&nbsp;and uses only constant extra space.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,4,2,2] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,3,4,2] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,3,3,3,3] <strong>Output:</strong> 3</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>nums.length == n + 1</code></li> <li><code>1 &lt;= nums[i] &lt;= n</code></li> <li>All the integers in <code>nums</code> appear only <strong>once</strong> except for <strong>precisely one integer</strong> which appears <strong>two or more</strong> times.</li> </ul> <p>&nbsp;</p> <p><b>Follow up:</b></p> <ul> <li>How can we prove that at least one duplicate number must exist in <code>nums</code>?</li> <li>Can you solve the problem in linear runtime complexity?</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findDuplicate(vector<int>& nums) {\n int n=nums.size();\n int a[1000000]={0};\n for(int i=0;i<nums.size();i++)\n { \n a[nums[i]]++;\n if(a[nums[i]]>1)\n return nums[i];\n }\n return nums[0];\n }\n};", "memory": "56846" }
287
<p>Given an array of integers <code>nums</code> containing&nbsp;<code>n + 1</code> integers where each integer is in the range <code>[1, n]</code> inclusive.</p> <p>There is only <strong>one repeated number</strong> in <code>nums</code>, return <em>this&nbsp;repeated&nbsp;number</em>.</p> <p>You must solve the problem <strong>without</strong> modifying the array <code>nums</code>&nbsp;and uses only constant extra space.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,4,2,2] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,3,4,2] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,3,3,3,3] <strong>Output:</strong> 3</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>nums.length == n + 1</code></li> <li><code>1 &lt;= nums[i] &lt;= n</code></li> <li>All the integers in <code>nums</code> appear only <strong>once</strong> except for <strong>precisely one integer</strong> which appears <strong>two or more</strong> times.</li> </ul> <p>&nbsp;</p> <p><b>Follow up:</b></p> <ul> <li>How can we prove that at least one duplicate number must exist in <code>nums</code>?</li> <li>Can you solve the problem in linear runtime complexity?</li> </ul>
3
{ "code": "class Solution {\npublic:\n// int findDuplicate(vector<int>& nums) {\n// sort(nums.begin(),nums.end());\n// for(int i=0;i<nums.size()-1;i++){\n// if(nums[i]==nums[i+1])\n// return nums[i];\n// }\n// return -1; }\n// };\n int findDuplicate(vector<int>& nums) {\n int n=nums.size();\n int total=0;\n for(int i=0;i<nums.size();i++){\n total=max(total+1,nums[i]);\n }\n vector<int>v(total,0);\n for(int i=0;i<nums.size();i++){\n v[nums[i]]++;\n }\n for(int i=0;i<v.size();i++){\n if(v[i]>=2)return i;\n }\n return -1;}\n};", "memory": "57968" }
287
<p>Given an array of integers <code>nums</code> containing&nbsp;<code>n + 1</code> integers where each integer is in the range <code>[1, n]</code> inclusive.</p> <p>There is only <strong>one repeated number</strong> in <code>nums</code>, return <em>this&nbsp;repeated&nbsp;number</em>.</p> <p>You must solve the problem <strong>without</strong> modifying the array <code>nums</code>&nbsp;and uses only constant extra space.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,4,2,2] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,3,4,2] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,3,3,3,3] <strong>Output:</strong> 3</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>nums.length == n + 1</code></li> <li><code>1 &lt;= nums[i] &lt;= n</code></li> <li>All the integers in <code>nums</code> appear only <strong>once</strong> except for <strong>precisely one integer</strong> which appears <strong>two or more</strong> times.</li> </ul> <p>&nbsp;</p> <p><b>Follow up:</b></p> <ul> <li>How can we prove that at least one duplicate number must exist in <code>nums</code>?</li> <li>Can you solve the problem in linear runtime complexity?</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findDuplicate(vector<int>& nums) {\n int max_element = INT_MIN;\n for(int i=0; i<nums.size() ; i++)\n max_element = max(max_element+1,nums[i]);\n\n vector<int>hash(max_element, 0);\n int ans=-1;\n for(auto num : nums)\n hash[num]++;\n\n for(int i=0; i<nums.size(); i++)\n {\n if(hash[nums[i]]>1)\n {\n ans = nums[i];\n break;\n }\n }\n return ans;\n }\n};", "memory": "59089" }
287
<p>Given an array of integers <code>nums</code> containing&nbsp;<code>n + 1</code> integers where each integer is in the range <code>[1, n]</code> inclusive.</p> <p>There is only <strong>one repeated number</strong> in <code>nums</code>, return <em>this&nbsp;repeated&nbsp;number</em>.</p> <p>You must solve the problem <strong>without</strong> modifying the array <code>nums</code>&nbsp;and uses only constant extra space.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,4,2,2] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,3,4,2] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,3,3,3,3] <strong>Output:</strong> 3</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>nums.length == n + 1</code></li> <li><code>1 &lt;= nums[i] &lt;= n</code></li> <li>All the integers in <code>nums</code> appear only <strong>once</strong> except for <strong>precisely one integer</strong> which appears <strong>two or more</strong> times.</li> </ul> <p>&nbsp;</p> <p><b>Follow up:</b></p> <ul> <li>How can we prove that at least one duplicate number must exist in <code>nums</code>?</li> <li>Can you solve the problem in linear runtime complexity?</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findDuplicate(vector<int>& nums) {\n int m = nums.size();\n vector<int> count(m, 0);\n vector<int> cumulativeCount(m, 0);\n\n for(auto num: nums){\n count[num]++;\n }\n cumulativeCount[1] = count[1];\n for(int i=2;i<m;i++){\n cumulativeCount[i] = cumulativeCount[i-1]+ count[i];\n }\n for(int i=1;i<m;i++){\n if(cumulativeCount[i] > i) return i;\n }\n return -1;\n }\n};", "memory": "60210" }
287
<p>Given an array of integers <code>nums</code> containing&nbsp;<code>n + 1</code> integers where each integer is in the range <code>[1, n]</code> inclusive.</p> <p>There is only <strong>one repeated number</strong> in <code>nums</code>, return <em>this&nbsp;repeated&nbsp;number</em>.</p> <p>You must solve the problem <strong>without</strong> modifying the array <code>nums</code>&nbsp;and uses only constant extra space.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,4,2,2] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,3,4,2] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,3,3,3,3] <strong>Output:</strong> 3</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>nums.length == n + 1</code></li> <li><code>1 &lt;= nums[i] &lt;= n</code></li> <li>All the integers in <code>nums</code> appear only <strong>once</strong> except for <strong>precisely one integer</strong> which appears <strong>two or more</strong> times.</li> </ul> <p>&nbsp;</p> <p><b>Follow up:</b></p> <ul> <li>How can we prove that at least one duplicate number must exist in <code>nums</code>?</li> <li>Can you solve the problem in linear runtime complexity?</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findDuplicate(vector<int>& nums) {\n\n int n= nums.size();\n\n vector<int> arr1(n,0);\n \n arr1.resize(n+1);\n for(int i=0;i<nums.size();i++){\n if(arr1[nums[i]]==0){\n arr1[nums[i]]+=1;\n }\n else{\n return nums[i];\n }\n }\n return 0;\n }\n};", "memory": "61331" }
287
<p>Given an array of integers <code>nums</code> containing&nbsp;<code>n + 1</code> integers where each integer is in the range <code>[1, n]</code> inclusive.</p> <p>There is only <strong>one repeated number</strong> in <code>nums</code>, return <em>this&nbsp;repeated&nbsp;number</em>.</p> <p>You must solve the problem <strong>without</strong> modifying the array <code>nums</code>&nbsp;and uses only constant extra space.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,4,2,2] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,3,4,2] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,3,3,3,3] <strong>Output:</strong> 3</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>nums.length == n + 1</code></li> <li><code>1 &lt;= nums[i] &lt;= n</code></li> <li>All the integers in <code>nums</code> appear only <strong>once</strong> except for <strong>precisely one integer</strong> which appears <strong>two or more</strong> times.</li> </ul> <p>&nbsp;</p> <p><b>Follow up:</b></p> <ul> <li>How can we prove that at least one duplicate number must exist in <code>nums</code>?</li> <li>Can you solve the problem in linear runtime complexity?</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findDuplicate(vector<int>& nums) {\n int n=nums.size();\n int mx=10000009;\n int dummy[mx];\n for(int i=0;i<n;i++){\n dummy[i]=0;\n }\n int ans=0;\n for(int i=0;i<n;i++){\n if(dummy[nums[i]]==0){\n dummy[nums[i]]=1;\n }\n else{\n ans=nums[i];\n }\n }\n return ans;\n }\n};", "memory": "62453" }
287
<p>Given an array of integers <code>nums</code> containing&nbsp;<code>n + 1</code> integers where each integer is in the range <code>[1, n]</code> inclusive.</p> <p>There is only <strong>one repeated number</strong> in <code>nums</code>, return <em>this&nbsp;repeated&nbsp;number</em>.</p> <p>You must solve the problem <strong>without</strong> modifying the array <code>nums</code>&nbsp;and uses only constant extra space.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,4,2,2] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,3,4,2] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,3,3,3,3] <strong>Output:</strong> 3</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>nums.length == n + 1</code></li> <li><code>1 &lt;= nums[i] &lt;= n</code></li> <li>All the integers in <code>nums</code> appear only <strong>once</strong> except for <strong>precisely one integer</strong> which appears <strong>two or more</strong> times.</li> </ul> <p>&nbsp;</p> <p><b>Follow up:</b></p> <ul> <li>How can we prove that at least one duplicate number must exist in <code>nums</code>?</li> <li>Can you solve the problem in linear runtime complexity?</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findDuplicate(vector<int>& nums) {\n int n= nums.size();\n vector<int> arr1(n,0);\n arr1.resize(n+1);\n for(int i=0;i<nums.size();i++){\n if(arr1[nums[i]]==0){\n arr1[nums[i]]+=1;\n }\n else{\n return nums[i];\n }\n\n }\n return 0;\n }\n};", "memory": "63574" }
287
<p>Given an array of integers <code>nums</code> containing&nbsp;<code>n + 1</code> integers where each integer is in the range <code>[1, n]</code> inclusive.</p> <p>There is only <strong>one repeated number</strong> in <code>nums</code>, return <em>this&nbsp;repeated&nbsp;number</em>.</p> <p>You must solve the problem <strong>without</strong> modifying the array <code>nums</code>&nbsp;and uses only constant extra space.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,4,2,2] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,3,4,2] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,3,3,3,3] <strong>Output:</strong> 3</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>nums.length == n + 1</code></li> <li><code>1 &lt;= nums[i] &lt;= n</code></li> <li>All the integers in <code>nums</code> appear only <strong>once</strong> except for <strong>precisely one integer</strong> which appears <strong>two or more</strong> times.</li> </ul> <p>&nbsp;</p> <p><b>Follow up:</b></p> <ul> <li>How can we prove that at least one duplicate number must exist in <code>nums</code>?</li> <li>Can you solve the problem in linear runtime complexity?</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findDuplicate(vector<int>& nums) {\n\n int n= nums.size();\n\n vector<int> arr1(n,0);\n\n arr1.resize(n+1);\n\n for(int i=0;i<nums.size();i++){\n\n if(arr1[nums[i]]==0){\n\n arr1[nums[i]]+=1;\n\n }\n else{\n\n return nums[i];\n\n }\n\n }\n\n \n return 0;\n \n }\n\n\n};", "memory": "63574" }
287
<p>Given an array of integers <code>nums</code> containing&nbsp;<code>n + 1</code> integers where each integer is in the range <code>[1, n]</code> inclusive.</p> <p>There is only <strong>one repeated number</strong> in <code>nums</code>, return <em>this&nbsp;repeated&nbsp;number</em>.</p> <p>You must solve the problem <strong>without</strong> modifying the array <code>nums</code>&nbsp;and uses only constant extra space.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,4,2,2] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,3,4,2] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,3,3,3,3] <strong>Output:</strong> 3</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>nums.length == n + 1</code></li> <li><code>1 &lt;= nums[i] &lt;= n</code></li> <li>All the integers in <code>nums</code> appear only <strong>once</strong> except for <strong>precisely one integer</strong> which appears <strong>two or more</strong> times.</li> </ul> <p>&nbsp;</p> <p><b>Follow up:</b></p> <ul> <li>How can we prove that at least one duplicate number must exist in <code>nums</code>?</li> <li>Can you solve the problem in linear runtime complexity?</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findDuplicate(vector<int>& nums) {\n int n= nums.size();\n vector<int> arr1(n,0);\n arr1.resize(n+1);\n for(int i=0;i<nums.size();i++){\n if(arr1[nums[i]]==0){\n arr1[nums[i]]+=1;\n }\n else{\n return nums[i];\n }\n }\n return 0;\n }\n};", "memory": "64695" }
287
<p>Given an array of integers <code>nums</code> containing&nbsp;<code>n + 1</code> integers where each integer is in the range <code>[1, n]</code> inclusive.</p> <p>There is only <strong>one repeated number</strong> in <code>nums</code>, return <em>this&nbsp;repeated&nbsp;number</em>.</p> <p>You must solve the problem <strong>without</strong> modifying the array <code>nums</code>&nbsp;and uses only constant extra space.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,4,2,2] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,3,4,2] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,3,3,3,3] <strong>Output:</strong> 3</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>nums.length == n + 1</code></li> <li><code>1 &lt;= nums[i] &lt;= n</code></li> <li>All the integers in <code>nums</code> appear only <strong>once</strong> except for <strong>precisely one integer</strong> which appears <strong>two or more</strong> times.</li> </ul> <p>&nbsp;</p> <p><b>Follow up:</b></p> <ul> <li>How can we prove that at least one duplicate number must exist in <code>nums</code>?</li> <li>Can you solve the problem in linear runtime complexity?</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findDuplicate(vector<int>& nums) {\n vector<int>v;\n int i,n=nums.size();\n for(i=0;i<n+1;i++){\n v.push_back(0);\n }\nn=nums.size();\nint ans;\n for(i=0;i<n;i++){\n if(v[nums[i]]==1) {ans=nums[i];break;}\n v[nums[i]]=1 ;\n }\n return ans;\n }\n};", "memory": "65816" }
287
<p>Given an array of integers <code>nums</code> containing&nbsp;<code>n + 1</code> integers where each integer is in the range <code>[1, n]</code> inclusive.</p> <p>There is only <strong>one repeated number</strong> in <code>nums</code>, return <em>this&nbsp;repeated&nbsp;number</em>.</p> <p>You must solve the problem <strong>without</strong> modifying the array <code>nums</code>&nbsp;and uses only constant extra space.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,4,2,2] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,3,4,2] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,3,3,3,3] <strong>Output:</strong> 3</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>nums.length == n + 1</code></li> <li><code>1 &lt;= nums[i] &lt;= n</code></li> <li>All the integers in <code>nums</code> appear only <strong>once</strong> except for <strong>precisely one integer</strong> which appears <strong>two or more</strong> times.</li> </ul> <p>&nbsp;</p> <p><b>Follow up:</b></p> <ul> <li>How can we prove that at least one duplicate number must exist in <code>nums</code>?</li> <li>Can you solve the problem in linear runtime complexity?</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findDuplicate(vector<int>& nums) {\n vector<int> point;\n int res = 0;\n int temp = 0;\n for(int i = 0 ; i<nums.size() ; i++)\n {\n point.push_back(0);\n }\n for(int i = 0 ; i < nums.size() ; i++)\n {\n temp = nums[i]-1;\n point[temp] +=1;\n }\n for(int i = 0 ; i <point.size() ; i++)\n {\n if(point[i]>1)\n {\n res = i + 1;\n }\n }\n return res;\n }\n};", "memory": "66938" }
287
<p>Given an array of integers <code>nums</code> containing&nbsp;<code>n + 1</code> integers where each integer is in the range <code>[1, n]</code> inclusive.</p> <p>There is only <strong>one repeated number</strong> in <code>nums</code>, return <em>this&nbsp;repeated&nbsp;number</em>.</p> <p>You must solve the problem <strong>without</strong> modifying the array <code>nums</code>&nbsp;and uses only constant extra space.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,4,2,2] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,3,4,2] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,3,3,3,3] <strong>Output:</strong> 3</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>nums.length == n + 1</code></li> <li><code>1 &lt;= nums[i] &lt;= n</code></li> <li>All the integers in <code>nums</code> appear only <strong>once</strong> except for <strong>precisely one integer</strong> which appears <strong>two or more</strong> times.</li> </ul> <p>&nbsp;</p> <p><b>Follow up:</b></p> <ul> <li>How can we prove that at least one duplicate number must exist in <code>nums</code>?</li> <li>Can you solve the problem in linear runtime complexity?</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findDuplicate(vector<int>& nums) {\n int size=nums.size();\n vector<int> count={0};\n for(int i=0;i<=size;i++)\n count.push_back(0);\n \n for(int i=0;i<size;i++){\n if(count[nums[i]]==1)\n return nums[i];\n count[nums[i]]=1;\n }\n return -1;\n }\n};", "memory": "68059" }
287
<p>Given an array of integers <code>nums</code> containing&nbsp;<code>n + 1</code> integers where each integer is in the range <code>[1, n]</code> inclusive.</p> <p>There is only <strong>one repeated number</strong> in <code>nums</code>, return <em>this&nbsp;repeated&nbsp;number</em>.</p> <p>You must solve the problem <strong>without</strong> modifying the array <code>nums</code>&nbsp;and uses only constant extra space.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,4,2,2] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,3,4,2] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,3,3,3,3] <strong>Output:</strong> 3</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>nums.length == n + 1</code></li> <li><code>1 &lt;= nums[i] &lt;= n</code></li> <li>All the integers in <code>nums</code> appear only <strong>once</strong> except for <strong>precisely one integer</strong> which appears <strong>two or more</strong> times.</li> </ul> <p>&nbsp;</p> <p><b>Follow up:</b></p> <ul> <li>How can we prove that at least one duplicate number must exist in <code>nums</code>?</li> <li>Can you solve the problem in linear runtime complexity?</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findDuplicate(vector<int>& nums) {\n // map<int,int>mp;\n // for(int i=0;i<nums.size();i++){\n // mp[nums[i]]++;\n // } \n // for(auto it:mp){\n // if(it.second>=2){\n // return it.first;\n // }\n // }return -1;\n vector<int>sol;\n for(int i=1;i<nums.size();i++){\n sol.push_back(i);\n }\n for(int i=0;i<nums.size();i++){\n int n=nums[i];\n if(n!=sol[n-1]){\n return n;\n }\n sol[n-1]=-1;\n }\n return 0;\n }\n};", "memory": "69180" }
287
<p>Given an array of integers <code>nums</code> containing&nbsp;<code>n + 1</code> integers where each integer is in the range <code>[1, n]</code> inclusive.</p> <p>There is only <strong>one repeated number</strong> in <code>nums</code>, return <em>this&nbsp;repeated&nbsp;number</em>.</p> <p>You must solve the problem <strong>without</strong> modifying the array <code>nums</code>&nbsp;and uses only constant extra space.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,4,2,2] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,3,4,2] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,3,3,3,3] <strong>Output:</strong> 3</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>nums.length == n + 1</code></li> <li><code>1 &lt;= nums[i] &lt;= n</code></li> <li>All the integers in <code>nums</code> appear only <strong>once</strong> except for <strong>precisely one integer</strong> which appears <strong>two or more</strong> times.</li> </ul> <p>&nbsp;</p> <p><b>Follow up:</b></p> <ul> <li>How can we prove that at least one duplicate number must exist in <code>nums</code>?</li> <li>Can you solve the problem in linear runtime complexity?</li> </ul>
3
{ "code": "class Solution {\npublic:\n int basic_solution(vector<int>arr)\n { \n int ans=0;\n for(int i=0;i<arr.size();i++)\n {\n for(int j=i+1;j<arr.size();j++)\n if(arr[i]==arr[j])\n {\n ans=arr[i];\n break;\n }\n }\n return ans;\n };\n\n int smart_solution(vector<int>arr)\n {\n vector<int>ans(arr);\n int ans1=0;\n sort(ans.begin(),ans.end());\n for(int i=0;i<ans.size();i++)\n {\n if(ans[i]==ans[i+1]&&i+1<arr.size())\n {\n ans1=ans[i];\n \n break;\n }\n }\n \nreturn ans1;\n }\n int findDuplicate(vector<int>& nums) {\n return smart_solution(nums);\n }\n};", "memory": "70301" }
287
<p>Given an array of integers <code>nums</code> containing&nbsp;<code>n + 1</code> integers where each integer is in the range <code>[1, n]</code> inclusive.</p> <p>There is only <strong>one repeated number</strong> in <code>nums</code>, return <em>this&nbsp;repeated&nbsp;number</em>.</p> <p>You must solve the problem <strong>without</strong> modifying the array <code>nums</code>&nbsp;and uses only constant extra space.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,4,2,2] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,3,4,2] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,3,3,3,3] <strong>Output:</strong> 3</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>nums.length == n + 1</code></li> <li><code>1 &lt;= nums[i] &lt;= n</code></li> <li>All the integers in <code>nums</code> appear only <strong>once</strong> except for <strong>precisely one integer</strong> which appears <strong>two or more</strong> times.</li> </ul> <p>&nbsp;</p> <p><b>Follow up:</b></p> <ul> <li>How can we prove that at least one duplicate number must exist in <code>nums</code>?</li> <li>Can you solve the problem in linear runtime complexity?</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findDuplicate(vector<int>& nums) {\n char *mp = new char[100002];\n memset(mp, 0, 100002);\n int i = 0;\n int idx = 0;\n\n for (; i < nums.size(); ++i) {\n if (mp[nums[i]]) {\n break;\n }\n\n mp[nums[i]] = 1;\n }\n\n return nums[i];\n }\n // int findDuplicate(vector<int>& nums) {\n // unordered_map<int, int> numCnt;\n // int tmp = 0;\n // for (int num : nums) {\n // if (0 < numCnt.count(num)) {\n // tmp = num;\n // break;\n // }\n // ++numCnt[num];\n // }\n \n // return tmp;\n // }\n};", "memory": "70301" }
287
<p>Given an array of integers <code>nums</code> containing&nbsp;<code>n + 1</code> integers where each integer is in the range <code>[1, n]</code> inclusive.</p> <p>There is only <strong>one repeated number</strong> in <code>nums</code>, return <em>this&nbsp;repeated&nbsp;number</em>.</p> <p>You must solve the problem <strong>without</strong> modifying the array <code>nums</code>&nbsp;and uses only constant extra space.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,4,2,2] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,3,4,2] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,3,3,3,3] <strong>Output:</strong> 3</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>nums.length == n + 1</code></li> <li><code>1 &lt;= nums[i] &lt;= n</code></li> <li>All the integers in <code>nums</code> appear only <strong>once</strong> except for <strong>precisely one integer</strong> which appears <strong>two or more</strong> times.</li> </ul> <p>&nbsp;</p> <p><b>Follow up:</b></p> <ul> <li>How can we prove that at least one duplicate number must exist in <code>nums</code>?</li> <li>Can you solve the problem in linear runtime complexity?</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findDuplicate(vector<int>& nums) {\n int n = nums.size();\n long long int arr[1000000] = {0}; // Auxiliary array to track occurrences\n \n for(int i = 0; i < n; i++) {\n if(arr[nums[i]] == 0) {\n arr[nums[i]] = 1; \n } else {\n return nums[i]; \n }\n }\n return -1; // This line will never be reached if there's always a duplicate\n }\n};\n", "memory": "71423" }
287
<p>Given an array of integers <code>nums</code> containing&nbsp;<code>n + 1</code> integers where each integer is in the range <code>[1, n]</code> inclusive.</p> <p>There is only <strong>one repeated number</strong> in <code>nums</code>, return <em>this&nbsp;repeated&nbsp;number</em>.</p> <p>You must solve the problem <strong>without</strong> modifying the array <code>nums</code>&nbsp;and uses only constant extra space.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,4,2,2] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,3,4,2] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,3,3,3,3] <strong>Output:</strong> 3</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>nums.length == n + 1</code></li> <li><code>1 &lt;= nums[i] &lt;= n</code></li> <li>All the integers in <code>nums</code> appear only <strong>once</strong> except for <strong>precisely one integer</strong> which appears <strong>two or more</strong> times.</li> </ul> <p>&nbsp;</p> <p><b>Follow up:</b></p> <ul> <li>How can we prove that at least one duplicate number must exist in <code>nums</code>?</li> <li>Can you solve the problem in linear runtime complexity?</li> </ul>
3
{ "code": "#include <iostream>\n#include <vector>\n#include <cstdlib> // For malloc and free\n\nclass Solution {\npublic:\n // Linked List Node\n struct node {\n // key and value are both integers\n int key;\n int value;\n struct node* next;\n };\n\n // Function to initialize a node\n void setNode(struct node* n, int key, int value) {\n n->key = key;\n n->value = value;\n n->next = nullptr;\n }\n\n // HashMap structure\n struct hashMap {\n int numOfElements, capacity;\n struct node** arr;\n };\n\n // Function to initialize the hashMap\n void initializeHashMap(struct hashMap* mp) {\n mp->capacity = 100; // Set capacity to 100\n mp->numOfElements = 0;\n mp->arr = (struct node**)malloc(sizeof(struct node*) * mp->capacity);\n // Initialize all buckets to NULL to avoid undefined behavior\n for (int i = 0; i < mp->capacity; ++i) {\n mp->arr[i] = nullptr;\n }\n }\n\n // Hash function for int keys\n int hashFunction(struct hashMap* mp, int key) {\n // Simple hash function based on modulus\n return (abs(key) / 10) % mp->capacity; // Use absolute value to avoid negative indices\n }\n\n // Function to insert a key-value pair into the hashMap\n void insert(struct hashMap* mp, int key, int value) {\n int bucketIndex = hashFunction(mp, key);\n struct node* newNode = (struct node*)malloc(sizeof(struct node));\n setNode(newNode, key, value);\n\n if (mp->arr[bucketIndex] == nullptr) {\n mp->arr[bucketIndex] = newNode;\n } else {\n newNode->next = mp->arr[bucketIndex];\n mp->arr[bucketIndex] = newNode;\n }\n mp->numOfElements++;\n }\n\n // Function to search for a key in the hashMap\n int search(struct hashMap* mp, int key) {\n int bucketIndex = hashFunction(mp, key);\n struct node* bucketHead = mp->arr[bucketIndex];\n\n while (bucketHead != nullptr) {\n if (bucketHead->key == key) {\n return bucketHead->value;\n }\n bucketHead = bucketHead->next;\n }\n\n return -1; // Key not found\n }\n\n // Function to detect if there is any duplicate\n int findDuplicate(std::vector<int>& nums) {\n // Initialize hash map\n struct hashMap* mp = (struct hashMap*)malloc(sizeof(struct hashMap));\n initializeHashMap(mp);\n\n // Search for duplicates using the Hash Map\n for (int i = 0; i < nums.size(); i++) {\n if (search(mp, nums[i]) != -1) {\n // Duplicate exists\n freeHashMap(mp); // Ensure to free allocated memory\n return nums[i];\n } else {\n // Element doesn't exist in the HashMap, add it\n insert(mp, nums[i], nums[i]);\n }\n }\n\n freeHashMap(mp); // Ensure to free allocated memory\n return 0;\n }\n\n // Function to free the allocated memory of the hashMap\n void freeHashMap(struct hashMap* mp) {\n for (int i = 0; i < mp->capacity; ++i) {\n struct node* current = mp->arr[i];\n while (current != nullptr) {\n struct node* temp = current;\n current = current->next;\n free(temp);\n }\n }\n free(mp->arr);\n free(mp);\n }\n};\n", "memory": "74786" }
287
<p>Given an array of integers <code>nums</code> containing&nbsp;<code>n + 1</code> integers where each integer is in the range <code>[1, n]</code> inclusive.</p> <p>There is only <strong>one repeated number</strong> in <code>nums</code>, return <em>this&nbsp;repeated&nbsp;number</em>.</p> <p>You must solve the problem <strong>without</strong> modifying the array <code>nums</code>&nbsp;and uses only constant extra space.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,4,2,2] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,3,4,2] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,3,3,3,3] <strong>Output:</strong> 3</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>nums.length == n + 1</code></li> <li><code>1 &lt;= nums[i] &lt;= n</code></li> <li>All the integers in <code>nums</code> appear only <strong>once</strong> except for <strong>precisely one integer</strong> which appears <strong>two or more</strong> times.</li> </ul> <p>&nbsp;</p> <p><b>Follow up:</b></p> <ul> <li>How can we prove that at least one duplicate number must exist in <code>nums</code>?</li> <li>Can you solve the problem in linear runtime complexity?</li> </ul>
3
{ "code": "#define opt() ios::sync_with_stdio(0);cin.tie(0)\nclass Solution {\npublic:\n int findDuplicate(vector<int>& nums) {\n opt();\n sort(nums.begin(),nums.end());\n unordered_map<int,int> map;\n for(auto i:nums){\n map[i]++;\n if(map[i]==2){\n return i;\n }\n }\n return 0;\n }\n};", "memory": "75908" }
287
<p>Given an array of integers <code>nums</code> containing&nbsp;<code>n + 1</code> integers where each integer is in the range <code>[1, n]</code> inclusive.</p> <p>There is only <strong>one repeated number</strong> in <code>nums</code>, return <em>this&nbsp;repeated&nbsp;number</em>.</p> <p>You must solve the problem <strong>without</strong> modifying the array <code>nums</code>&nbsp;and uses only constant extra space.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,4,2,2] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,3,4,2] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,3,3,3,3] <strong>Output:</strong> 3</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>nums.length == n + 1</code></li> <li><code>1 &lt;= nums[i] &lt;= n</code></li> <li>All the integers in <code>nums</code> appear only <strong>once</strong> except for <strong>precisely one integer</strong> which appears <strong>two or more</strong> times.</li> </ul> <p>&nbsp;</p> <p><b>Follow up:</b></p> <ul> <li>How can we prove that at least one duplicate number must exist in <code>nums</code>?</li> <li>Can you solve the problem in linear runtime complexity?</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findDuplicate(vector<int>& nums) {\n int n=nums.size();\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n sort(nums.begin(),nums.end());\n unordered_map<int,int>mp;\n for(int i=0;i<n;i++){\n mp[nums[i]]++;\n if(mp[nums[i]]>=2)return nums[i];\n }\n return 0;\n }\n};", "memory": "77029" }
287
<p>Given an array of integers <code>nums</code> containing&nbsp;<code>n + 1</code> integers where each integer is in the range <code>[1, n]</code> inclusive.</p> <p>There is only <strong>one repeated number</strong> in <code>nums</code>, return <em>this&nbsp;repeated&nbsp;number</em>.</p> <p>You must solve the problem <strong>without</strong> modifying the array <code>nums</code>&nbsp;and uses only constant extra space.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,4,2,2] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,3,4,2] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,3,3,3,3] <strong>Output:</strong> 3</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>nums.length == n + 1</code></li> <li><code>1 &lt;= nums[i] &lt;= n</code></li> <li>All the integers in <code>nums</code> appear only <strong>once</strong> except for <strong>precisely one integer</strong> which appears <strong>two or more</strong> times.</li> </ul> <p>&nbsp;</p> <p><b>Follow up:</b></p> <ul> <li>How can we prove that at least one duplicate number must exist in <code>nums</code>?</li> <li>Can you solve the problem in linear runtime complexity?</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findDuplicate(vector<int>& nums) {\n int n=nums.size();\n unordered_map<int,int>mp;\n sort(nums.begin(),nums.end());\n int ans=0;\n for(int i=0;i<n;i++){\n mp[nums[i]]++;\n if(mp[nums[i]]>=2){\n ans=nums[i];\n break;\n }\n }\n\n\n\n\n return ans;\n\n\n\n\n\n\n\n\n\n\n\n }\n};", "memory": "78150" }
287
<p>Given an array of integers <code>nums</code> containing&nbsp;<code>n + 1</code> integers where each integer is in the range <code>[1, n]</code> inclusive.</p> <p>There is only <strong>one repeated number</strong> in <code>nums</code>, return <em>this&nbsp;repeated&nbsp;number</em>.</p> <p>You must solve the problem <strong>without</strong> modifying the array <code>nums</code>&nbsp;and uses only constant extra space.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,4,2,2] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,3,4,2] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,3,3,3,3] <strong>Output:</strong> 3</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>nums.length == n + 1</code></li> <li><code>1 &lt;= nums[i] &lt;= n</code></li> <li>All the integers in <code>nums</code> appear only <strong>once</strong> except for <strong>precisely one integer</strong> which appears <strong>two or more</strong> times.</li> </ul> <p>&nbsp;</p> <p><b>Follow up:</b></p> <ul> <li>How can we prove that at least one duplicate number must exist in <code>nums</code>?</li> <li>Can you solve the problem in linear runtime complexity?</li> </ul>
3
{ "code": "// struct TreeNode{\n// int val;\n// TreeNode* right;\n// TreeNode* left;\n// };\n\nclass Solution {\npublic:\n bool add_node(TreeNode*root , int num){ \n if(root->val>num){ ///// go left\n if(root->left==NULL){\n TreeNode* node = new TreeNode();\n node->val = num;\n root->left = node;\n return true;\n }\n return add_node(root->left,num);\n }\n else if(root->val < num){ //// go right\n if(root->right==NULL){\n TreeNode* node = new TreeNode();\n node->val = num;\n root->right = node;\n return true;\n }\n return add_node(root->right,num);\n }\n else{ //// duplicate\n return false;\n }\n }\n int findDuplicate(vector<int>& Arr) {\n TreeNode* root = new TreeNode();\n root->val = Arr[0];\n for(int i =1;i<Arr.size();i++){\n if(!add_node(root,Arr[i])){\n return Arr[i];\n }\n }\n return 0;\n }\n};", "memory": "79271" }
287
<p>Given an array of integers <code>nums</code> containing&nbsp;<code>n + 1</code> integers where each integer is in the range <code>[1, n]</code> inclusive.</p> <p>There is only <strong>one repeated number</strong> in <code>nums</code>, return <em>this&nbsp;repeated&nbsp;number</em>.</p> <p>You must solve the problem <strong>without</strong> modifying the array <code>nums</code>&nbsp;and uses only constant extra space.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,4,2,2] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,3,4,2] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,3,3,3,3] <strong>Output:</strong> 3</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>nums.length == n + 1</code></li> <li><code>1 &lt;= nums[i] &lt;= n</code></li> <li>All the integers in <code>nums</code> appear only <strong>once</strong> except for <strong>precisely one integer</strong> which appears <strong>two or more</strong> times.</li> </ul> <p>&nbsp;</p> <p><b>Follow up:</b></p> <ul> <li>How can we prove that at least one duplicate number must exist in <code>nums</code>?</li> <li>Can you solve the problem in linear runtime complexity?</li> </ul>
3
{ "code": "// struct TreeNode{\n// int val;\n// TreeNode* right;\n// TreeNode* left;\n// };\n\nclass Solution {\npublic:\n bool f(TreeNode*root , int num){\n if(root->val>num){\n if(root->left==NULL){\n TreeNode* node = new TreeNode();\n node->val = num;\n root->left = node;\n return false;\n }\n return f(root->left,num);\n }\n else if(root->val<num){\n if(root->right==NULL){\n TreeNode* node = new TreeNode();\n node->val = num;\n root->right = node;\n return false;\n }\n return f(root->right,num);\n }\n else{\n return true;\n }\n }\n int findDuplicate(vector<int>& nums) {\n TreeNode* root = new TreeNode();\n root->val = nums[0];\n for(int i =1;i<nums.size();i++){\n if(f(root,nums[i])){\n return nums[i];\n }\n }\n return 0;\n }\n};", "memory": "80393" }
287
<p>Given an array of integers <code>nums</code> containing&nbsp;<code>n + 1</code> integers where each integer is in the range <code>[1, n]</code> inclusive.</p> <p>There is only <strong>one repeated number</strong> in <code>nums</code>, return <em>this&nbsp;repeated&nbsp;number</em>.</p> <p>You must solve the problem <strong>without</strong> modifying the array <code>nums</code>&nbsp;and uses only constant extra space.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,4,2,2] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,3,4,2] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,3,3,3,3] <strong>Output:</strong> 3</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>nums.length == n + 1</code></li> <li><code>1 &lt;= nums[i] &lt;= n</code></li> <li>All the integers in <code>nums</code> appear only <strong>once</strong> except for <strong>precisely one integer</strong> which appears <strong>two or more</strong> times.</li> </ul> <p>&nbsp;</p> <p><b>Follow up:</b></p> <ul> <li>How can we prove that at least one duplicate number must exist in <code>nums</code>?</li> <li>Can you solve the problem in linear runtime complexity?</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findDuplicate(vector<int>& nums) {\n sort(nums.begin(), nums.end());\n map<int, int> mp;\n for(int x : nums){\n if(mp[x] == 1) return x;\n mp[x] = 1;\n }\n return -1;\n }\n};", "memory": "80393" }
287
<p>Given an array of integers <code>nums</code> containing&nbsp;<code>n + 1</code> integers where each integer is in the range <code>[1, n]</code> inclusive.</p> <p>There is only <strong>one repeated number</strong> in <code>nums</code>, return <em>this&nbsp;repeated&nbsp;number</em>.</p> <p>You must solve the problem <strong>without</strong> modifying the array <code>nums</code>&nbsp;and uses only constant extra space.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,4,2,2] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,3,4,2] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,3,3,3,3] <strong>Output:</strong> 3</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>nums.length == n + 1</code></li> <li><code>1 &lt;= nums[i] &lt;= n</code></li> <li>All the integers in <code>nums</code> appear only <strong>once</strong> except for <strong>precisely one integer</strong> which appears <strong>two or more</strong> times.</li> </ul> <p>&nbsp;</p> <p><b>Follow up:</b></p> <ul> <li>How can we prove that at least one duplicate number must exist in <code>nums</code>?</li> <li>Can you solve the problem in linear runtime complexity?</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findDuplicate(vector<int>& nums) {\n ios_base::sync_with_stdio(0);\n unordered_map<int, bool> q;\n q.reserve(nums.size());\n\n for(int kys : nums){\n if(!q[kys]){\n q[kys] = 1;\n } else {\n return kys;\n }\n }\n\n return 0;\n }\n};", "memory": "81514" }
287
<p>Given an array of integers <code>nums</code> containing&nbsp;<code>n + 1</code> integers where each integer is in the range <code>[1, n]</code> inclusive.</p> <p>There is only <strong>one repeated number</strong> in <code>nums</code>, return <em>this&nbsp;repeated&nbsp;number</em>.</p> <p>You must solve the problem <strong>without</strong> modifying the array <code>nums</code>&nbsp;and uses only constant extra space.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,4,2,2] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,3,4,2] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,3,3,3,3] <strong>Output:</strong> 3</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>nums.length == n + 1</code></li> <li><code>1 &lt;= nums[i] &lt;= n</code></li> <li>All the integers in <code>nums</code> appear only <strong>once</strong> except for <strong>precisely one integer</strong> which appears <strong>two or more</strong> times.</li> </ul> <p>&nbsp;</p> <p><b>Follow up:</b></p> <ul> <li>How can we prove that at least one duplicate number must exist in <code>nums</code>?</li> <li>Can you solve the problem in linear runtime complexity?</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findDuplicate(vector<int>& nums) {\n const auto n = nums.size();\n unordered_set<int> set;\n set.reserve(n);\n for (const auto num : nums) {\n auto[_, isInserted] = set.insert(num);\n if (!isInserted) {\n return num;\n }\n }\n return -1;\n }\n};", "memory": "82635" }
287
<p>Given an array of integers <code>nums</code> containing&nbsp;<code>n + 1</code> integers where each integer is in the range <code>[1, n]</code> inclusive.</p> <p>There is only <strong>one repeated number</strong> in <code>nums</code>, return <em>this&nbsp;repeated&nbsp;number</em>.</p> <p>You must solve the problem <strong>without</strong> modifying the array <code>nums</code>&nbsp;and uses only constant extra space.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,4,2,2] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,3,4,2] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,3,3,3,3] <strong>Output:</strong> 3</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>nums.length == n + 1</code></li> <li><code>1 &lt;= nums[i] &lt;= n</code></li> <li>All the integers in <code>nums</code> appear only <strong>once</strong> except for <strong>precisely one integer</strong> which appears <strong>two or more</strong> times.</li> </ul> <p>&nbsp;</p> <p><b>Follow up:</b></p> <ul> <li>How can we prove that at least one duplicate number must exist in <code>nums</code>?</li> <li>Can you solve the problem in linear runtime complexity?</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findDuplicate(vector<int>& nums) {\n int n = nums.size();\n unordered_map<int , int> umap(n+1);\n for(auto it:nums){\n umap[it]++;\n if(umap[it] == 2){\n return it;\n }\n }\n\n return 0;\n \n }\n};", "memory": "83756" }
287
<p>Given an array of integers <code>nums</code> containing&nbsp;<code>n + 1</code> integers where each integer is in the range <code>[1, n]</code> inclusive.</p> <p>There is only <strong>one repeated number</strong> in <code>nums</code>, return <em>this&nbsp;repeated&nbsp;number</em>.</p> <p>You must solve the problem <strong>without</strong> modifying the array <code>nums</code>&nbsp;and uses only constant extra space.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,4,2,2] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,3,4,2] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,3,3,3,3] <strong>Output:</strong> 3</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>nums.length == n + 1</code></li> <li><code>1 &lt;= nums[i] &lt;= n</code></li> <li>All the integers in <code>nums</code> appear only <strong>once</strong> except for <strong>precisely one integer</strong> which appears <strong>two or more</strong> times.</li> </ul> <p>&nbsp;</p> <p><b>Follow up:</b></p> <ul> <li>How can we prove that at least one duplicate number must exist in <code>nums</code>?</li> <li>Can you solve the problem in linear runtime complexity?</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findDuplicate(vector<int>& nums) {\n int ans;\n unordered_map<int,int> mp(nums.size());\n for(int i =0; i < nums.size(); i++){\n if(mp[nums[i]]!=1){\n mp[nums[i]]=1;\n }\n else {\n ans = nums[i];\n break;\n }\n \n } \n return ans;}\n};", "memory": "83756" }
287
<p>Given an array of integers <code>nums</code> containing&nbsp;<code>n + 1</code> integers where each integer is in the range <code>[1, n]</code> inclusive.</p> <p>There is only <strong>one repeated number</strong> in <code>nums</code>, return <em>this&nbsp;repeated&nbsp;number</em>.</p> <p>You must solve the problem <strong>without</strong> modifying the array <code>nums</code>&nbsp;and uses only constant extra space.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,4,2,2] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,3,4,2] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,3,3,3,3] <strong>Output:</strong> 3</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>nums.length == n + 1</code></li> <li><code>1 &lt;= nums[i] &lt;= n</code></li> <li>All the integers in <code>nums</code> appear only <strong>once</strong> except for <strong>precisely one integer</strong> which appears <strong>two or more</strong> times.</li> </ul> <p>&nbsp;</p> <p><b>Follow up:</b></p> <ul> <li>How can we prove that at least one duplicate number must exist in <code>nums</code>?</li> <li>Can you solve the problem in linear runtime complexity?</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findDuplicate(vector<int>& nums) {\n unordered_map<int,int> mpp;\n for(int i = 0; i<nums.size(); i++){\n mpp[nums[i]]++;\n if(mpp[nums[i]]>1){\n return nums[i];\n }\n }\n return -1;\n }\n};", "memory": "84878" }
287
<p>Given an array of integers <code>nums</code> containing&nbsp;<code>n + 1</code> integers where each integer is in the range <code>[1, n]</code> inclusive.</p> <p>There is only <strong>one repeated number</strong> in <code>nums</code>, return <em>this&nbsp;repeated&nbsp;number</em>.</p> <p>You must solve the problem <strong>without</strong> modifying the array <code>nums</code>&nbsp;and uses only constant extra space.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,4,2,2] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,3,4,2] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,3,3,3,3] <strong>Output:</strong> 3</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>nums.length == n + 1</code></li> <li><code>1 &lt;= nums[i] &lt;= n</code></li> <li>All the integers in <code>nums</code> appear only <strong>once</strong> except for <strong>precisely one integer</strong> which appears <strong>two or more</strong> times.</li> </ul> <p>&nbsp;</p> <p><b>Follow up:</b></p> <ul> <li>How can we prove that at least one duplicate number must exist in <code>nums</code>?</li> <li>Can you solve the problem in linear runtime complexity?</li> </ul>
3
{ "code": "#include <unordered_map>\nclass Solution {\npublic:\n int findDuplicate(vector<int>& nums) {\n std::unordered_map<int, int> mapset;\n for(int i=0;i<nums.size();i++){\n if(mapset[nums[i]]++==1)\n return nums[i];\n }\n return -1;\n }\n};", "memory": "84878" }
287
<p>Given an array of integers <code>nums</code> containing&nbsp;<code>n + 1</code> integers where each integer is in the range <code>[1, n]</code> inclusive.</p> <p>There is only <strong>one repeated number</strong> in <code>nums</code>, return <em>this&nbsp;repeated&nbsp;number</em>.</p> <p>You must solve the problem <strong>without</strong> modifying the array <code>nums</code>&nbsp;and uses only constant extra space.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,4,2,2] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,3,4,2] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,3,3,3,3] <strong>Output:</strong> 3</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>nums.length == n + 1</code></li> <li><code>1 &lt;= nums[i] &lt;= n</code></li> <li>All the integers in <code>nums</code> appear only <strong>once</strong> except for <strong>precisely one integer</strong> which appears <strong>two or more</strong> times.</li> </ul> <p>&nbsp;</p> <p><b>Follow up:</b></p> <ul> <li>How can we prove that at least one duplicate number must exist in <code>nums</code>?</li> <li>Can you solve the problem in linear runtime complexity?</li> </ul>
3
{ "code": "#include <bits/stdc++.h>\nusing namespace std;\n\nclass Solution {\npublic:\n int findDuplicate(vector<int>& nums) {\n unordered_map<int,int> counter;\n for (int i=0;i<nums.size();i++) {\n if (counter.find(nums[i]) != counter.end()) {\n return nums[i];\n }\n counter[nums[i]] = 1;\n }\n return 0;\n }\n};", "memory": "85999" }
287
<p>Given an array of integers <code>nums</code> containing&nbsp;<code>n + 1</code> integers where each integer is in the range <code>[1, n]</code> inclusive.</p> <p>There is only <strong>one repeated number</strong> in <code>nums</code>, return <em>this&nbsp;repeated&nbsp;number</em>.</p> <p>You must solve the problem <strong>without</strong> modifying the array <code>nums</code>&nbsp;and uses only constant extra space.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,4,2,2] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,3,4,2] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,3,3,3,3] <strong>Output:</strong> 3</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>nums.length == n + 1</code></li> <li><code>1 &lt;= nums[i] &lt;= n</code></li> <li>All the integers in <code>nums</code> appear only <strong>once</strong> except for <strong>precisely one integer</strong> which appears <strong>two or more</strong> times.</li> </ul> <p>&nbsp;</p> <p><b>Follow up:</b></p> <ul> <li>How can we prove that at least one duplicate number must exist in <code>nums</code>?</li> <li>Can you solve the problem in linear runtime complexity?</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findDuplicate(vector<int>& nums) {\n unordered_map<int, int> count;\n int ans = 0;\n for (int i : nums) {\n count[i]++;\n if (count[i] > 1) {\n ans = i;\n break;\n }\n }\n return ans;\n }\n};", "memory": "85999" }
287
<p>Given an array of integers <code>nums</code> containing&nbsp;<code>n + 1</code> integers where each integer is in the range <code>[1, n]</code> inclusive.</p> <p>There is only <strong>one repeated number</strong> in <code>nums</code>, return <em>this&nbsp;repeated&nbsp;number</em>.</p> <p>You must solve the problem <strong>without</strong> modifying the array <code>nums</code>&nbsp;and uses only constant extra space.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,4,2,2] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,3,4,2] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,3,3,3,3] <strong>Output:</strong> 3</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>nums.length == n + 1</code></li> <li><code>1 &lt;= nums[i] &lt;= n</code></li> <li>All the integers in <code>nums</code> appear only <strong>once</strong> except for <strong>precisely one integer</strong> which appears <strong>two or more</strong> times.</li> </ul> <p>&nbsp;</p> <p><b>Follow up:</b></p> <ul> <li>How can we prove that at least one duplicate number must exist in <code>nums</code>?</li> <li>Can you solve the problem in linear runtime complexity?</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findDuplicate(vector<int>& n) {\n unordered_set<int> hs;\n for(auto i:n){\n if(!(hs.find(i)!=hs.end())) hs.insert(i);\n else return i;\n }\n return -1;\n }\n};", "memory": "87120" }
287
<p>Given an array of integers <code>nums</code> containing&nbsp;<code>n + 1</code> integers where each integer is in the range <code>[1, n]</code> inclusive.</p> <p>There is only <strong>one repeated number</strong> in <code>nums</code>, return <em>this&nbsp;repeated&nbsp;number</em>.</p> <p>You must solve the problem <strong>without</strong> modifying the array <code>nums</code>&nbsp;and uses only constant extra space.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,4,2,2] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,3,4,2] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,3,3,3,3] <strong>Output:</strong> 3</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>nums.length == n + 1</code></li> <li><code>1 &lt;= nums[i] &lt;= n</code></li> <li>All the integers in <code>nums</code> appear only <strong>once</strong> except for <strong>precisely one integer</strong> which appears <strong>two or more</strong> times.</li> </ul> <p>&nbsp;</p> <p><b>Follow up:</b></p> <ul> <li>How can we prove that at least one duplicate number must exist in <code>nums</code>?</li> <li>Can you solve the problem in linear runtime complexity?</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findDuplicate(vector<int>& nums) {\n unordered_set<int> set;\n\n for(auto num : nums) {\n if(set.find(num) == set.end()) set.insert(num);\n else return num;\n }\n\n return -1;\n }\n};", "memory": "87120" }
287
<p>Given an array of integers <code>nums</code> containing&nbsp;<code>n + 1</code> integers where each integer is in the range <code>[1, n]</code> inclusive.</p> <p>There is only <strong>one repeated number</strong> in <code>nums</code>, return <em>this&nbsp;repeated&nbsp;number</em>.</p> <p>You must solve the problem <strong>without</strong> modifying the array <code>nums</code>&nbsp;and uses only constant extra space.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,4,2,2] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,3,4,2] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,3,3,3,3] <strong>Output:</strong> 3</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>nums.length == n + 1</code></li> <li><code>1 &lt;= nums[i] &lt;= n</code></li> <li>All the integers in <code>nums</code> appear only <strong>once</strong> except for <strong>precisely one integer</strong> which appears <strong>two or more</strong> times.</li> </ul> <p>&nbsp;</p> <p><b>Follow up:</b></p> <ul> <li>How can we prove that at least one duplicate number must exist in <code>nums</code>?</li> <li>Can you solve the problem in linear runtime complexity?</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findDuplicate(vector<int>& nums) {\n unordered_map<int, int> list;\n for(auto& n : nums)\n {\n if(list.find(n) != list.end())\n {\n return n;\n }\n else\n {\n list[n]++;\n }\n }\n return 0;\n }\n};", "memory": "88241" }
287
<p>Given an array of integers <code>nums</code> containing&nbsp;<code>n + 1</code> integers where each integer is in the range <code>[1, n]</code> inclusive.</p> <p>There is only <strong>one repeated number</strong> in <code>nums</code>, return <em>this&nbsp;repeated&nbsp;number</em>.</p> <p>You must solve the problem <strong>without</strong> modifying the array <code>nums</code>&nbsp;and uses only constant extra space.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,4,2,2] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,3,4,2] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,3,3,3,3] <strong>Output:</strong> 3</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>nums.length == n + 1</code></li> <li><code>1 &lt;= nums[i] &lt;= n</code></li> <li>All the integers in <code>nums</code> appear only <strong>once</strong> except for <strong>precisely one integer</strong> which appears <strong>two or more</strong> times.</li> </ul> <p>&nbsp;</p> <p><b>Follow up:</b></p> <ul> <li>How can we prove that at least one duplicate number must exist in <code>nums</code>?</li> <li>Can you solve the problem in linear runtime complexity?</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findDuplicate(vector<int>& nums) {\n unordered_set<int> seen;\n for (int n : nums) {\n if (seen.find(n) != seen.end()) {\n return n;\n }\n seen.insert(n);\n }\n return 0;\n }\n};\n", "memory": "88241" }
287
<p>Given an array of integers <code>nums</code> containing&nbsp;<code>n + 1</code> integers where each integer is in the range <code>[1, n]</code> inclusive.</p> <p>There is only <strong>one repeated number</strong> in <code>nums</code>, return <em>this&nbsp;repeated&nbsp;number</em>.</p> <p>You must solve the problem <strong>without</strong> modifying the array <code>nums</code>&nbsp;and uses only constant extra space.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,4,2,2] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,3,4,2] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,3,3,3,3] <strong>Output:</strong> 3</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>nums.length == n + 1</code></li> <li><code>1 &lt;= nums[i] &lt;= n</code></li> <li>All the integers in <code>nums</code> appear only <strong>once</strong> except for <strong>precisely one integer</strong> which appears <strong>two or more</strong> times.</li> </ul> <p>&nbsp;</p> <p><b>Follow up:</b></p> <ul> <li>How can we prove that at least one duplicate number must exist in <code>nums</code>?</li> <li>Can you solve the problem in linear runtime complexity?</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findDuplicate(vector<int>& nums) {\n int n = nums.size();\n map<int,int>mp;\n int ans;\n for(int i=0;i<n;i++){\n if(mp.find(nums[i])!=mp.end()){\n ans = nums[i];\n break;\n }else mp[nums[i]]++;\n }\n return ans;\n }\n};", "memory": "89363" }
287
<p>Given an array of integers <code>nums</code> containing&nbsp;<code>n + 1</code> integers where each integer is in the range <code>[1, n]</code> inclusive.</p> <p>There is only <strong>one repeated number</strong> in <code>nums</code>, return <em>this&nbsp;repeated&nbsp;number</em>.</p> <p>You must solve the problem <strong>without</strong> modifying the array <code>nums</code>&nbsp;and uses only constant extra space.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,4,2,2] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,3,4,2] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,3,3,3,3] <strong>Output:</strong> 3</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>nums.length == n + 1</code></li> <li><code>1 &lt;= nums[i] &lt;= n</code></li> <li>All the integers in <code>nums</code> appear only <strong>once</strong> except for <strong>precisely one integer</strong> which appears <strong>two or more</strong> times.</li> </ul> <p>&nbsp;</p> <p><b>Follow up:</b></p> <ul> <li>How can we prove that at least one duplicate number must exist in <code>nums</code>?</li> <li>Can you solve the problem in linear runtime complexity?</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findDuplicate(vector<int>& nums) {\n map<int, int> m;\n for (const auto it : nums) {\n m[it]++;\n if (m[it] > 1) {\n return it;\n }\n }\n return 0;\n }\n};", "memory": "89363" }
287
<p>Given an array of integers <code>nums</code> containing&nbsp;<code>n + 1</code> integers where each integer is in the range <code>[1, n]</code> inclusive.</p> <p>There is only <strong>one repeated number</strong> in <code>nums</code>, return <em>this&nbsp;repeated&nbsp;number</em>.</p> <p>You must solve the problem <strong>without</strong> modifying the array <code>nums</code>&nbsp;and uses only constant extra space.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,4,2,2] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,3,4,2] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,3,3,3,3] <strong>Output:</strong> 3</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>nums.length == n + 1</code></li> <li><code>1 &lt;= nums[i] &lt;= n</code></li> <li>All the integers in <code>nums</code> appear only <strong>once</strong> except for <strong>precisely one integer</strong> which appears <strong>two or more</strong> times.</li> </ul> <p>&nbsp;</p> <p><b>Follow up:</b></p> <ul> <li>How can we prove that at least one duplicate number must exist in <code>nums</code>?</li> <li>Can you solve the problem in linear runtime complexity?</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findDuplicate(vector<int>& nums) {\n ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);\n vector<int> count (1e5+1,0);\n for (int i: nums){\n if (count[i]>=1) return i;\n count[i]++;\n }\n return -1;\n }\n};", "memory": "90484" }
287
<p>Given an array of integers <code>nums</code> containing&nbsp;<code>n + 1</code> integers where each integer is in the range <code>[1, n]</code> inclusive.</p> <p>There is only <strong>one repeated number</strong> in <code>nums</code>, return <em>this&nbsp;repeated&nbsp;number</em>.</p> <p>You must solve the problem <strong>without</strong> modifying the array <code>nums</code>&nbsp;and uses only constant extra space.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,4,2,2] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,3,4,2] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,3,3,3,3] <strong>Output:</strong> 3</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>nums.length == n + 1</code></li> <li><code>1 &lt;= nums[i] &lt;= n</code></li> <li>All the integers in <code>nums</code> appear only <strong>once</strong> except for <strong>precisely one integer</strong> which appears <strong>two or more</strong> times.</li> </ul> <p>&nbsp;</p> <p><b>Follow up:</b></p> <ul> <li>How can we prove that at least one duplicate number must exist in <code>nums</code>?</li> <li>Can you solve the problem in linear runtime complexity?</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findDuplicate(vector<int>& nums) {\n vector<int>res(100001,0);\n for(int num:nums){\n res[num]++;\n if(res[num]>1)return num;\n }\n return -1;\n }\n};", "memory": "90484" }
287
<p>Given an array of integers <code>nums</code> containing&nbsp;<code>n + 1</code> integers where each integer is in the range <code>[1, n]</code> inclusive.</p> <p>There is only <strong>one repeated number</strong> in <code>nums</code>, return <em>this&nbsp;repeated&nbsp;number</em>.</p> <p>You must solve the problem <strong>without</strong> modifying the array <code>nums</code>&nbsp;and uses only constant extra space.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,4,2,2] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,3,4,2] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,3,3,3,3] <strong>Output:</strong> 3</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>nums.length == n + 1</code></li> <li><code>1 &lt;= nums[i] &lt;= n</code></li> <li>All the integers in <code>nums</code> appear only <strong>once</strong> except for <strong>precisely one integer</strong> which appears <strong>two or more</strong> times.</li> </ul> <p>&nbsp;</p> <p><b>Follow up:</b></p> <ul> <li>How can we prove that at least one duplicate number must exist in <code>nums</code>?</li> <li>Can you solve the problem in linear runtime complexity?</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findDuplicate(vector<int>v) {\n int ans;\n vector<int>freq(100005);\n\n for(int i=0; i<v.size(); i++) {\n freq[v[i]]++;\n\n if(freq[v[i]] >= 2) {\n ans = v[i];\n break;\n }\n }\n return ans;\n }\n};", "memory": "91605" }
287
<p>Given an array of integers <code>nums</code> containing&nbsp;<code>n + 1</code> integers where each integer is in the range <code>[1, n]</code> inclusive.</p> <p>There is only <strong>one repeated number</strong> in <code>nums</code>, return <em>this&nbsp;repeated&nbsp;number</em>.</p> <p>You must solve the problem <strong>without</strong> modifying the array <code>nums</code>&nbsp;and uses only constant extra space.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,4,2,2] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,3,4,2] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,3,3,3,3] <strong>Output:</strong> 3</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>nums.length == n + 1</code></li> <li><code>1 &lt;= nums[i] &lt;= n</code></li> <li>All the integers in <code>nums</code> appear only <strong>once</strong> except for <strong>precisely one integer</strong> which appears <strong>two or more</strong> times.</li> </ul> <p>&nbsp;</p> <p><b>Follow up:</b></p> <ul> <li>How can we prove that at least one duplicate number must exist in <code>nums</code>?</li> <li>Can you solve the problem in linear runtime complexity?</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findDuplicate(vector<int>& nums) {\n //create the hashtable\n unordered_map<int, int> mp(nums.size());\n\n for(int i=0; i<nums.size(); i++){\n mp[nums[i]]++;\n }\n\n // return the key with value > 1\n for(auto it:mp){\n if(it.second > 1){\n return it.first;\n }\n }\n\n return -1;\n }\n};", "memory": "92726" }
287
<p>Given an array of integers <code>nums</code> containing&nbsp;<code>n + 1</code> integers where each integer is in the range <code>[1, n]</code> inclusive.</p> <p>There is only <strong>one repeated number</strong> in <code>nums</code>, return <em>this&nbsp;repeated&nbsp;number</em>.</p> <p>You must solve the problem <strong>without</strong> modifying the array <code>nums</code>&nbsp;and uses only constant extra space.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,4,2,2] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,3,4,2] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,3,3,3,3] <strong>Output:</strong> 3</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>nums.length == n + 1</code></li> <li><code>1 &lt;= nums[i] &lt;= n</code></li> <li>All the integers in <code>nums</code> appear only <strong>once</strong> except for <strong>precisely one integer</strong> which appears <strong>two or more</strong> times.</li> </ul> <p>&nbsp;</p> <p><b>Follow up:</b></p> <ul> <li>How can we prove that at least one duplicate number must exist in <code>nums</code>?</li> <li>Can you solve the problem in linear runtime complexity?</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findDuplicate(vector<int>& nums) {\n int n = nums.size();\n unordered_map<int, int> map(n);\n for(auto it: nums){\n map[it]++;\n }\n for(auto [x,y] : map){\n if(y>1){\n return x;\n }\n else continue;\n }\n return 0;\n }\n};", "memory": "93848" }
287
<p>Given an array of integers <code>nums</code> containing&nbsp;<code>n + 1</code> integers where each integer is in the range <code>[1, n]</code> inclusive.</p> <p>There is only <strong>one repeated number</strong> in <code>nums</code>, return <em>this&nbsp;repeated&nbsp;number</em>.</p> <p>You must solve the problem <strong>without</strong> modifying the array <code>nums</code>&nbsp;and uses only constant extra space.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,4,2,2] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,3,4,2] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,3,3,3,3] <strong>Output:</strong> 3</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>nums.length == n + 1</code></li> <li><code>1 &lt;= nums[i] &lt;= n</code></li> <li>All the integers in <code>nums</code> appear only <strong>once</strong> except for <strong>precisely one integer</strong> which appears <strong>two or more</strong> times.</li> </ul> <p>&nbsp;</p> <p><b>Follow up:</b></p> <ul> <li>How can we prove that at least one duplicate number must exist in <code>nums</code>?</li> <li>Can you solve the problem in linear runtime complexity?</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findDuplicate(vector<int>& nums) {\n int n=nums.size();\n unordered_map<int,int> freq(n+1);\n for(int num:nums){\n freq[num]++;\n }\n sort(nums.begin(),nums.end());\n for(int i=0;i<n;i++){\n if(freq[nums[i]]>\n 1){\n return nums[i];\n }\n }\n return 0;\n }\n};", "memory": "93848" }
287
<p>Given an array of integers <code>nums</code> containing&nbsp;<code>n + 1</code> integers where each integer is in the range <code>[1, n]</code> inclusive.</p> <p>There is only <strong>one repeated number</strong> in <code>nums</code>, return <em>this&nbsp;repeated&nbsp;number</em>.</p> <p>You must solve the problem <strong>without</strong> modifying the array <code>nums</code>&nbsp;and uses only constant extra space.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,4,2,2] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,3,4,2] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,3,3,3,3] <strong>Output:</strong> 3</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>nums.length == n + 1</code></li> <li><code>1 &lt;= nums[i] &lt;= n</code></li> <li>All the integers in <code>nums</code> appear only <strong>once</strong> except for <strong>precisely one integer</strong> which appears <strong>two or more</strong> times.</li> </ul> <p>&nbsp;</p> <p><b>Follow up:</b></p> <ul> <li>How can we prove that at least one duplicate number must exist in <code>nums</code>?</li> <li>Can you solve the problem in linear runtime complexity?</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findDuplicate(vector<int>& nums) {\n unordered_map <int,int> m1;\n int l=0;\n int r=nums.size()-1;\n for(int i=0;i<nums.size();i++){\n m1[nums[l]]++;\n m1[nums[r]]++;\n if(m1[nums[l]]>1){\n return nums[l];\n }\n else if(m1[nums[r]]>1){\n return nums[r];\n }\n l++;\n r--;\n }\n return 0;\n }\n};", "memory": "94969" }
287
<p>Given an array of integers <code>nums</code> containing&nbsp;<code>n + 1</code> integers where each integer is in the range <code>[1, n]</code> inclusive.</p> <p>There is only <strong>one repeated number</strong> in <code>nums</code>, return <em>this&nbsp;repeated&nbsp;number</em>.</p> <p>You must solve the problem <strong>without</strong> modifying the array <code>nums</code>&nbsp;and uses only constant extra space.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,4,2,2] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,3,4,2] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,3,3,3,3] <strong>Output:</strong> 3</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>nums.length == n + 1</code></li> <li><code>1 &lt;= nums[i] &lt;= n</code></li> <li>All the integers in <code>nums</code> appear only <strong>once</strong> except for <strong>precisely one integer</strong> which appears <strong>two or more</strong> times.</li> </ul> <p>&nbsp;</p> <p><b>Follow up:</b></p> <ul> <li>How can we prove that at least one duplicate number must exist in <code>nums</code>?</li> <li>Can you solve the problem in linear runtime complexity?</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findDuplicate(vector<int>& nums) {\n \n if (nums.size() < 1) return 0;\n if (nums.size() == 2) return nums[0] == nums[1] ? nums[0] : 0;\n unordered_map<int, int> dup;\n int l = 0, r = nums.size()-1;\n\n while(l <= r) {\n cout << \"l:\" << nums[l] << \", r:\" << nums[r] << endl;\n if (dup.count(nums[l]) > 0) {\n return nums[l];\n } else {\n dup[nums[l]] = 1;\n }\n if (dup.count(nums[r]) > 0) {\n return nums[r];\n } else {\n dup[nums[r]] = 1;\n }\n\n ++l;\n --r;\n }\n\n return 0;\n }\n};", "memory": "96090" }
287
<p>Given an array of integers <code>nums</code> containing&nbsp;<code>n + 1</code> integers where each integer is in the range <code>[1, n]</code> inclusive.</p> <p>There is only <strong>one repeated number</strong> in <code>nums</code>, return <em>this&nbsp;repeated&nbsp;number</em>.</p> <p>You must solve the problem <strong>without</strong> modifying the array <code>nums</code>&nbsp;and uses only constant extra space.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,4,2,2] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,3,4,2] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,3,3,3,3] <strong>Output:</strong> 3</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>nums.length == n + 1</code></li> <li><code>1 &lt;= nums[i] &lt;= n</code></li> <li>All the integers in <code>nums</code> appear only <strong>once</strong> except for <strong>precisely one integer</strong> which appears <strong>two or more</strong> times.</li> </ul> <p>&nbsp;</p> <p><b>Follow up:</b></p> <ul> <li>How can we prove that at least one duplicate number must exist in <code>nums</code>?</li> <li>Can you solve the problem in linear runtime complexity?</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findDuplicate(vector<int>& nums) {\n unordered_set<int>order;\n int i=0,j=nums.size()-1;\n if((j+1)%2!=0) {\n order.insert(nums[j/2]);\n }\n while(i<j) {\n int it=nums[i];\n int ns=nums[j];\n if(order.find(it)!=order.end()) {\n return it;\n }\n order.insert(it);\n if(order.find(ns)!=order.end()) {\n return ns;\n }\n order.insert(ns);\n i++,j--;\n }\n return -1;\n }\n};\n", "memory": "97211" }
287
<p>Given an array of integers <code>nums</code> containing&nbsp;<code>n + 1</code> integers where each integer is in the range <code>[1, n]</code> inclusive.</p> <p>There is only <strong>one repeated number</strong> in <code>nums</code>, return <em>this&nbsp;repeated&nbsp;number</em>.</p> <p>You must solve the problem <strong>without</strong> modifying the array <code>nums</code>&nbsp;and uses only constant extra space.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,4,2,2] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,3,4,2] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,3,3,3,3] <strong>Output:</strong> 3</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>nums.length == n + 1</code></li> <li><code>1 &lt;= nums[i] &lt;= n</code></li> <li>All the integers in <code>nums</code> appear only <strong>once</strong> except for <strong>precisely one integer</strong> which appears <strong>two or more</strong> times.</li> </ul> <p>&nbsp;</p> <p><b>Follow up:</b></p> <ul> <li>How can we prove that at least one duplicate number must exist in <code>nums</code>?</li> <li>Can you solve the problem in linear runtime complexity?</li> </ul>
3
{ "code": "class Solution {\npublic:\n int checkCount(vector<int> nums,int m)\n {\n int cnt=0;\n for(int i=0;i<nums.size();i++)\n {\n if(nums[i]<=m)\n cnt++;\n }\n return cnt;\n }\n int findDuplicate(vector<int>& nums) {\n // approach 1 (cyclic sort)\n // int n=nums.size();\n // for(int i=0;i<n;)\n // {\n // int j=nums[i]-1;\n // if(j<n && nums[i]!=nums[j])\n // {\n // swap(nums[i],nums[j]);\n // }\n // else\n // i++;\n // }\n // for(int i=0;i<n;i++)\n // {\n // if(nums[i]-1 != i)\n // {\n // return nums[i];\n // }\n // }\n // return n;\n\n // approach 2 (by binary search)\n int n=nums.size()-1;\n int f=1,l=n;\n int m;\n while(f<l)\n {\n m=f+(l-f)/2;\n if(checkCount(nums,m) > m)\n l=m;\n else\n f=m+1;\n }\n return f;\n\n }\n};", "memory": "102818" }
287
<p>Given an array of integers <code>nums</code> containing&nbsp;<code>n + 1</code> integers where each integer is in the range <code>[1, n]</code> inclusive.</p> <p>There is only <strong>one repeated number</strong> in <code>nums</code>, return <em>this&nbsp;repeated&nbsp;number</em>.</p> <p>You must solve the problem <strong>without</strong> modifying the array <code>nums</code>&nbsp;and uses only constant extra space.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,4,2,2] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,3,4,2] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,3,3,3,3] <strong>Output:</strong> 3</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>nums.length == n + 1</code></li> <li><code>1 &lt;= nums[i] &lt;= n</code></li> <li>All the integers in <code>nums</code> appear only <strong>once</strong> except for <strong>precisely one integer</strong> which appears <strong>two or more</strong> times.</li> </ul> <p>&nbsp;</p> <p><b>Follow up:</b></p> <ul> <li>How can we prove that at least one duplicate number must exist in <code>nums</code>?</li> <li>Can you solve the problem in linear runtime complexity?</li> </ul>
3
{ "code": "class Solution {\npublic:\n int check(int num, vector<int> nums){\n int cnt=0;\n for (auto i:nums){\n if(i<=num){\n cnt++;\n }\n }\n return cnt;\n }\n int findDuplicate(vector<int>& nums) {\n int r= nums.size()-1;\n int l=1;\n\n while(l<=r){\n int mid = (l+r)/2;\n\n if (check(mid,nums)>mid){\n r=mid-1;\n }else{\n l=mid+1;\n }\n }\n return l;\n }\n};", "memory": "102818" }
287
<p>Given an array of integers <code>nums</code> containing&nbsp;<code>n + 1</code> integers where each integer is in the range <code>[1, n]</code> inclusive.</p> <p>There is only <strong>one repeated number</strong> in <code>nums</code>, return <em>this&nbsp;repeated&nbsp;number</em>.</p> <p>You must solve the problem <strong>without</strong> modifying the array <code>nums</code>&nbsp;and uses only constant extra space.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,4,2,2] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,3,4,2] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,3,3,3,3] <strong>Output:</strong> 3</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>nums.length == n + 1</code></li> <li><code>1 &lt;= nums[i] &lt;= n</code></li> <li>All the integers in <code>nums</code> appear only <strong>once</strong> except for <strong>precisely one integer</strong> which appears <strong>two or more</strong> times.</li> </ul> <p>&nbsp;</p> <p><b>Follow up:</b></p> <ul> <li>How can we prove that at least one duplicate number must exist in <code>nums</code>?</li> <li>Can you solve the problem in linear runtime complexity?</li> </ul>
3
{ "code": "class Solution {\npublic:\n bool f (int x, vector<int> a){\n int cnt = 0;\n for (int k: a) {\n if (k <= x) {\n cnt ++;\n }\n }\n return cnt > x;\n }\n int findDuplicate(vector<int>& nums) {\n \n int n = nums.size()-1;\n int lb = 1, ub = n, ans;\n while (lb <= ub){\n int mid = (lb + ub) / 2;\n if (f (mid, nums)) {\n ans = mid;\n ub = mid-1;\n }\n else {\n lb = mid+1;\n }\n }\n return ans;\n }\n};", "memory": "103939" }
287
<p>Given an array of integers <code>nums</code> containing&nbsp;<code>n + 1</code> integers where each integer is in the range <code>[1, n]</code> inclusive.</p> <p>There is only <strong>one repeated number</strong> in <code>nums</code>, return <em>this&nbsp;repeated&nbsp;number</em>.</p> <p>You must solve the problem <strong>without</strong> modifying the array <code>nums</code>&nbsp;and uses only constant extra space.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,4,2,2] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,3,4,2] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,3,3,3,3] <strong>Output:</strong> 3</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>nums.length == n + 1</code></li> <li><code>1 &lt;= nums[i] &lt;= n</code></li> <li>All the integers in <code>nums</code> appear only <strong>once</strong> except for <strong>precisely one integer</strong> which appears <strong>two or more</strong> times.</li> </ul> <p>&nbsp;</p> <p><b>Follow up:</b></p> <ul> <li>How can we prove that at least one duplicate number must exist in <code>nums</code>?</li> <li>Can you solve the problem in linear runtime complexity?</li> </ul>
3
{ "code": "class Solution {\npublic:\nbool func(vector<int>v,int mid){\n int k=0;\n int n=v.size();\n for(int i=0;i<n;i++){\n if(mid>=v[i]){\n k++;\n }\n }\n if(k>mid)return true;\n return false;\n}\n int findDuplicate(vector<int>& nums) {\n int lo=1;\n int hi=nums.size()-1;\n int ans=nums.size();\n int mid;\n while(lo<=hi){\n mid=(lo+hi)>>1;\n if(func(nums,mid)){\n ans=mid;\n hi=mid-1;\n }\n else{\n lo=mid+1;\n }\n }\n return ans;\n }\n};", "memory": "103939" }
915
<p>Given the radius and the position of the center of a circle, implement the function <code>randPoint</code> which generates a uniform random point inside the circle.</p> <p>Implement the <code>Solution</code> class:</p> <ul> <li><code>Solution(double radius, double x_center, double y_center)</code> initializes the object with the radius of the circle <code>radius</code> and the position of the center <code>(x_center, y_center)</code>.</li> <li><code>randPoint()</code> returns a random point inside the circle. A point on the circumference of the circle is considered to be in the circle. The answer is returned as an array <code>[x, y]</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input</strong> [&quot;Solution&quot;, &quot;randPoint&quot;, &quot;randPoint&quot;, &quot;randPoint&quot;] [[1.0, 0.0, 0.0], [], [], []] <strong>Output</strong> [null, [-0.02493, -0.38077], [0.82314, 0.38945], [0.36572, 0.17248]] <strong>Explanation</strong> Solution solution = new Solution(1.0, 0.0, 0.0); solution.randPoint(); // return [-0.02493, -0.38077] solution.randPoint(); // return [0.82314, 0.38945] solution.randPoint(); // return [0.36572, 0.17248] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>0 &lt;&nbsp;radius &lt;= 10<sup>8</sup></code></li> <li><code>-10<sup>7</sup> &lt;= x_center, y_center &lt;= 10<sup>7</sup></code></li> <li>At most <code>3 * 10<sup>4</sup></code> calls will be made to <code>randPoint</code>.</li> </ul>
0
{ "code": "class Solution{\npublic:\n double r,x,y;\n Solution(double radius, double x_center, double y_center){\n r = radius;\n x = x_center;\n y = y_center;\n }\n vector<double> randPoint(){\n double x_r = ((double)rand()/RAND_MAX * (2*r)) + (x-r);\n double y_r = ((double)rand()/RAND_MAX * (2*r)) + (y-r);\n while(solve(x_r,y_r,x,y)>=r*r){\n x_r = ((double)rand()/RAND_MAX * (2*r)) + (x-r);\n y_r = ((double)rand()/RAND_MAX * (2*r)) + (y-r);\n }\n return {x_r,y_r};\n }\n double solve(double x_r,double y_r,double x,double y){\n return (x-x_r)*(x-x_r) + (y-y_r)*(y-y_r);\n }\n};", "memory": "31800" }
915
<p>Given the radius and the position of the center of a circle, implement the function <code>randPoint</code> which generates a uniform random point inside the circle.</p> <p>Implement the <code>Solution</code> class:</p> <ul> <li><code>Solution(double radius, double x_center, double y_center)</code> initializes the object with the radius of the circle <code>radius</code> and the position of the center <code>(x_center, y_center)</code>.</li> <li><code>randPoint()</code> returns a random point inside the circle. A point on the circumference of the circle is considered to be in the circle. The answer is returned as an array <code>[x, y]</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input</strong> [&quot;Solution&quot;, &quot;randPoint&quot;, &quot;randPoint&quot;, &quot;randPoint&quot;] [[1.0, 0.0, 0.0], [], [], []] <strong>Output</strong> [null, [-0.02493, -0.38077], [0.82314, 0.38945], [0.36572, 0.17248]] <strong>Explanation</strong> Solution solution = new Solution(1.0, 0.0, 0.0); solution.randPoint(); // return [-0.02493, -0.38077] solution.randPoint(); // return [0.82314, 0.38945] solution.randPoint(); // return [0.36572, 0.17248] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>0 &lt;&nbsp;radius &lt;= 10<sup>8</sup></code></li> <li><code>-10<sup>7</sup> &lt;= x_center, y_center &lt;= 10<sup>7</sup></code></li> <li>At most <code>3 * 10<sup>4</sup></code> calls will be made to <code>randPoint</code>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n double x0 = 0;\n double y0 = 0;\n double r0 = 0;\n Solution(double radius, double x_center, double y_center) {\n x0 = x_center;\n y0 = y_center;\n r0 = radius;\n }\n \n vector<double> randPoint() {\n while (true) {\n double ran = (double)rand()/RAND_MAX;\n cout << (double)ran << endl;\n double x = (r0*ran)*2 - r0;\n ran = (double)rand()/RAND_MAX;\n double y = (r0*ran)*2 - r0;\n if ((x*x + y*y) > r0*r0) continue;\n return {x0+x, y0+y};\n }\n return {};\n }\n};\n\n/**\n * Your Solution object will be instantiated and called as such:\n * Solution* obj = new Solution(radius, x_center, y_center);\n * vector<double> param_1 = obj->randPoint();\n */", "memory": "31900" }
915
<p>Given the radius and the position of the center of a circle, implement the function <code>randPoint</code> which generates a uniform random point inside the circle.</p> <p>Implement the <code>Solution</code> class:</p> <ul> <li><code>Solution(double radius, double x_center, double y_center)</code> initializes the object with the radius of the circle <code>radius</code> and the position of the center <code>(x_center, y_center)</code>.</li> <li><code>randPoint()</code> returns a random point inside the circle. A point on the circumference of the circle is considered to be in the circle. The answer is returned as an array <code>[x, y]</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input</strong> [&quot;Solution&quot;, &quot;randPoint&quot;, &quot;randPoint&quot;, &quot;randPoint&quot;] [[1.0, 0.0, 0.0], [], [], []] <strong>Output</strong> [null, [-0.02493, -0.38077], [0.82314, 0.38945], [0.36572, 0.17248]] <strong>Explanation</strong> Solution solution = new Solution(1.0, 0.0, 0.0); solution.randPoint(); // return [-0.02493, -0.38077] solution.randPoint(); // return [0.82314, 0.38945] solution.randPoint(); // return [0.36572, 0.17248] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>0 &lt;&nbsp;radius &lt;= 10<sup>8</sup></code></li> <li><code>-10<sup>7</sup> &lt;= x_center, y_center &lt;= 10<sup>7</sup></code></li> <li>At most <code>3 * 10<sup>4</sup></code> calls will be made to <code>randPoint</code>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n double x0 = 0;\n double y0 = 0;\n double r0 = 0;\n Solution(double radius, double x_center, double y_center) {\n x0 = x_center;\n y0 = y_center;\n r0 = radius;\n }\n \n vector<double> randPoint() {\n double ran = (double)rand()/RAND_MAX;\n // cout << (double)ran << endl;\n double x = (r0*ran)*2 - r0;\n ran = (double)rand()/RAND_MAX;\n double y = (r0*ran)*2 - r0;\n if ((x*x + y*y) > r0*r0) return randPoint();\n return {x0+x, y0+y};\n }\n};\n\n/**\n * Your Solution object will be instantiated and called as such:\n * Solution* obj = new Solution(radius, x_center, y_center);\n * vector<double> param_1 = obj->randPoint();\n */", "memory": "31900" }
915
<p>Given the radius and the position of the center of a circle, implement the function <code>randPoint</code> which generates a uniform random point inside the circle.</p> <p>Implement the <code>Solution</code> class:</p> <ul> <li><code>Solution(double radius, double x_center, double y_center)</code> initializes the object with the radius of the circle <code>radius</code> and the position of the center <code>(x_center, y_center)</code>.</li> <li><code>randPoint()</code> returns a random point inside the circle. A point on the circumference of the circle is considered to be in the circle. The answer is returned as an array <code>[x, y]</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input</strong> [&quot;Solution&quot;, &quot;randPoint&quot;, &quot;randPoint&quot;, &quot;randPoint&quot;] [[1.0, 0.0, 0.0], [], [], []] <strong>Output</strong> [null, [-0.02493, -0.38077], [0.82314, 0.38945], [0.36572, 0.17248]] <strong>Explanation</strong> Solution solution = new Solution(1.0, 0.0, 0.0); solution.randPoint(); // return [-0.02493, -0.38077] solution.randPoint(); // return [0.82314, 0.38945] solution.randPoint(); // return [0.36572, 0.17248] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>0 &lt;&nbsp;radius &lt;= 10<sup>8</sup></code></li> <li><code>-10<sup>7</sup> &lt;= x_center, y_center &lt;= 10<sup>7</sup></code></li> <li>At most <code>3 * 10<sup>4</sup></code> calls will be made to <code>randPoint</code>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n double r;\n double x;\n double y;\n int range;\n Solution(double radius, double x_center, double y_center) {\n r = radius;\n x = x_center;\n y = y_center;\n }\n \n vector<double> randPoint() {\n double x_tmp = x - r + 2*r * (rand() / (RAND_MAX + 1.));\n double y_tmp = y - r + 2*r * (rand() / (RAND_MAX + 1.));\n while(sqrt(pow(x_tmp-x, 2) + pow(y_tmp-y, 2)) > r){\n x_tmp = x - r + 2*r * (rand() / (RAND_MAX + 1.));\n y_tmp = y - r + 2*r * (rand() / (RAND_MAX + 1.));\n }\n return {x_tmp, y_tmp};\n }\n};\n\n/**\n * Your Solution object will be instantiated and called as such:\n * Solution* obj = new Solution(radius, x_center, y_center);\n * vector<double> param_1 = obj->randPoint();\n */", "memory": "32000" }
915
<p>Given the radius and the position of the center of a circle, implement the function <code>randPoint</code> which generates a uniform random point inside the circle.</p> <p>Implement the <code>Solution</code> class:</p> <ul> <li><code>Solution(double radius, double x_center, double y_center)</code> initializes the object with the radius of the circle <code>radius</code> and the position of the center <code>(x_center, y_center)</code>.</li> <li><code>randPoint()</code> returns a random point inside the circle. A point on the circumference of the circle is considered to be in the circle. The answer is returned as an array <code>[x, y]</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input</strong> [&quot;Solution&quot;, &quot;randPoint&quot;, &quot;randPoint&quot;, &quot;randPoint&quot;] [[1.0, 0.0, 0.0], [], [], []] <strong>Output</strong> [null, [-0.02493, -0.38077], [0.82314, 0.38945], [0.36572, 0.17248]] <strong>Explanation</strong> Solution solution = new Solution(1.0, 0.0, 0.0); solution.randPoint(); // return [-0.02493, -0.38077] solution.randPoint(); // return [0.82314, 0.38945] solution.randPoint(); // return [0.36572, 0.17248] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>0 &lt;&nbsp;radius &lt;= 10<sup>8</sup></code></li> <li><code>-10<sup>7</sup> &lt;= x_center, y_center &lt;= 10<sup>7</sup></code></li> <li>At most <code>3 * 10<sup>4</sup></code> calls will be made to <code>randPoint</code>.</li> </ul>
1
{ "code": "class Solution {\npublic:\n double RAD,XC,YC;\n Solution(double radius, double x_center, double y_center) {\n RAD=radius;\n XC=x_center;\n YC=y_center;\n }\n \n vector<double> randPoint() {\n double phi=2*M_PI*((double) rand()/RAND_MAX);\n double r=sqrt((double) rand()/RAND_MAX)*RAD;\n double adj=cos(phi)*r;\n double opp=sin(phi)*r;\n return vector<double> {XC+adj,YC+opp};\n }\n};\n\n/**\n * Your Solution object will be instantiated and called as such:\n * Solution* obj = new Solution(radius, x_center, y_center);\n * vector<double> param_1 = obj->randPoint();\n */", "memory": "32100" }
915
<p>Given the radius and the position of the center of a circle, implement the function <code>randPoint</code> which generates a uniform random point inside the circle.</p> <p>Implement the <code>Solution</code> class:</p> <ul> <li><code>Solution(double radius, double x_center, double y_center)</code> initializes the object with the radius of the circle <code>radius</code> and the position of the center <code>(x_center, y_center)</code>.</li> <li><code>randPoint()</code> returns a random point inside the circle. A point on the circumference of the circle is considered to be in the circle. The answer is returned as an array <code>[x, y]</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input</strong> [&quot;Solution&quot;, &quot;randPoint&quot;, &quot;randPoint&quot;, &quot;randPoint&quot;] [[1.0, 0.0, 0.0], [], [], []] <strong>Output</strong> [null, [-0.02493, -0.38077], [0.82314, 0.38945], [0.36572, 0.17248]] <strong>Explanation</strong> Solution solution = new Solution(1.0, 0.0, 0.0); solution.randPoint(); // return [-0.02493, -0.38077] solution.randPoint(); // return [0.82314, 0.38945] solution.randPoint(); // return [0.36572, 0.17248] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>0 &lt;&nbsp;radius &lt;= 10<sup>8</sup></code></li> <li><code>-10<sup>7</sup> &lt;= x_center, y_center &lt;= 10<sup>7</sup></code></li> <li>At most <code>3 * 10<sup>4</sup></code> calls will be made to <code>randPoint</code>.</li> </ul>
2
{ "code": "class Solution \n{\n double x{}, y{}, RR{};\n public:\n Solution(double r, double x, double y) : RR{r*r}, x{x}, y{y}\n { \n }\n \n vector<double> randPoint() \n {\n \t double phi = 2.*M_PI*((double) rand() / RAND_MAX),\n \t\t\t r = sqrt(RR*((double) rand() / RAND_MAX));\n \t return {x+r*cos(phi), y+r*sin(phi)};\n }\n };", "memory": "32200" }
915
<p>Given the radius and the position of the center of a circle, implement the function <code>randPoint</code> which generates a uniform random point inside the circle.</p> <p>Implement the <code>Solution</code> class:</p> <ul> <li><code>Solution(double radius, double x_center, double y_center)</code> initializes the object with the radius of the circle <code>radius</code> and the position of the center <code>(x_center, y_center)</code>.</li> <li><code>randPoint()</code> returns a random point inside the circle. A point on the circumference of the circle is considered to be in the circle. The answer is returned as an array <code>[x, y]</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input</strong> [&quot;Solution&quot;, &quot;randPoint&quot;, &quot;randPoint&quot;, &quot;randPoint&quot;] [[1.0, 0.0, 0.0], [], [], []] <strong>Output</strong> [null, [-0.02493, -0.38077], [0.82314, 0.38945], [0.36572, 0.17248]] <strong>Explanation</strong> Solution solution = new Solution(1.0, 0.0, 0.0); solution.randPoint(); // return [-0.02493, -0.38077] solution.randPoint(); // return [0.82314, 0.38945] solution.randPoint(); // return [0.36572, 0.17248] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>0 &lt;&nbsp;radius &lt;= 10<sup>8</sup></code></li> <li><code>-10<sup>7</sup> &lt;= x_center, y_center &lt;= 10<sup>7</sup></code></li> <li>At most <code>3 * 10<sup>4</sup></code> calls will be made to <code>randPoint</code>.</li> </ul>
2
{ "code": "class Solution {\nprivate:\n double centerX,centerY,rad;\npublic:\n Solution(double radius, double x_center, double y_center) {\n centerX = x_center;\n centerY = y_center;\n rad = radius;\n srand(time(NULL));\n }\n \n vector<double> randPoint() {\n vector<double>ans(2);\n double randAngle = rand()/(RAND_MAX*1.0) * 6.28;\n double randRad = sqrt(rand()/(RAND_MAX*1.0)) * rad;\n ans[0] = centerX + randRad*cos(randAngle);\n ans[1] = centerY + randRad*sin(randAngle);\n return ans;\n }\n};\n\n/**\n * Your Solution object will be instantiated and called as such:\n * Solution* obj = new Solution(radius, x_center, y_center);\n * vector<double> param_1 = obj->randPoint();\n */", "memory": "32299" }
915
<p>Given the radius and the position of the center of a circle, implement the function <code>randPoint</code> which generates a uniform random point inside the circle.</p> <p>Implement the <code>Solution</code> class:</p> <ul> <li><code>Solution(double radius, double x_center, double y_center)</code> initializes the object with the radius of the circle <code>radius</code> and the position of the center <code>(x_center, y_center)</code>.</li> <li><code>randPoint()</code> returns a random point inside the circle. A point on the circumference of the circle is considered to be in the circle. The answer is returned as an array <code>[x, y]</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input</strong> [&quot;Solution&quot;, &quot;randPoint&quot;, &quot;randPoint&quot;, &quot;randPoint&quot;] [[1.0, 0.0, 0.0], [], [], []] <strong>Output</strong> [null, [-0.02493, -0.38077], [0.82314, 0.38945], [0.36572, 0.17248]] <strong>Explanation</strong> Solution solution = new Solution(1.0, 0.0, 0.0); solution.randPoint(); // return [-0.02493, -0.38077] solution.randPoint(); // return [0.82314, 0.38945] solution.randPoint(); // return [0.36572, 0.17248] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>0 &lt;&nbsp;radius &lt;= 10<sup>8</sup></code></li> <li><code>-10<sup>7</sup> &lt;= x_center, y_center &lt;= 10<sup>7</sup></code></li> <li>At most <code>3 * 10<sup>4</sup></code> calls will be made to <code>randPoint</code>.</li> </ul>
3
{ "code": "class Solution final {\nprivate:\n using dbl = double;\n\n static inline piecewise_linear_distribution<dbl> radiusDistribution(\n const dbl radius\n ) noexcept {\n const auto i = {0.0, radius}, w = {0.0, 2 / radius};\n return piecewise_linear_distribution(begin(i), end(i), begin(w));\n }\n\n default_random_engine eng{random_device{}()};\n const dbl xCenter{0.0}, yCenter{0.0};\n piecewise_linear_distribution<dbl> rDist;\n uniform_real_distribution<dbl> phiDist{0.0, 2.0 * numbers::pi};\n\n inline vector<dbl> toCircle(\n const dbl r,\n const dbl phi\n ) const noexcept {\n return {cos(phi) * r + xCenter, sin(phi) * r + yCenter};\n }\n\npublic:\n inline Solution(\n const dbl radius,\n const dbl xCenter,\n const dbl yCenter\n ) noexcept : xCenter{xCenter}, yCenter{yCenter},\n rDist{radiusDistribution(radius)} {\n }\n\n inline vector<dbl> randPoint() noexcept {\n return toCircle(rDist(eng), phiDist(eng));\n }\n};\n/**\n * Your Solution object will be instantiated and called as such:\n * Solution* obj = new Solution(radius, x_center, y_center);\n * vector<double> param_1 = obj->randPoint();\n */\n", "memory": "32400" }
915
<p>Given the radius and the position of the center of a circle, implement the function <code>randPoint</code> which generates a uniform random point inside the circle.</p> <p>Implement the <code>Solution</code> class:</p> <ul> <li><code>Solution(double radius, double x_center, double y_center)</code> initializes the object with the radius of the circle <code>radius</code> and the position of the center <code>(x_center, y_center)</code>.</li> <li><code>randPoint()</code> returns a random point inside the circle. A point on the circumference of the circle is considered to be in the circle. The answer is returned as an array <code>[x, y]</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input</strong> [&quot;Solution&quot;, &quot;randPoint&quot;, &quot;randPoint&quot;, &quot;randPoint&quot;] [[1.0, 0.0, 0.0], [], [], []] <strong>Output</strong> [null, [-0.02493, -0.38077], [0.82314, 0.38945], [0.36572, 0.17248]] <strong>Explanation</strong> Solution solution = new Solution(1.0, 0.0, 0.0); solution.randPoint(); // return [-0.02493, -0.38077] solution.randPoint(); // return [0.82314, 0.38945] solution.randPoint(); // return [0.36572, 0.17248] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>0 &lt;&nbsp;radius &lt;= 10<sup>8</sup></code></li> <li><code>-10<sup>7</sup> &lt;= x_center, y_center &lt;= 10<sup>7</sup></code></li> <li>At most <code>3 * 10<sup>4</sup></code> calls will be made to <code>randPoint</code>.</li> </ul>
3
{ "code": "class Solution final {\nprivate:\n using dbl = double;\n\n static inline piecewise_linear_distribution<dbl> radiusDistribution(\n const dbl radius\n ) noexcept {\n const auto i = {0.0, radius}, w = {0.0, 2 / radius};\n return piecewise_linear_distribution(begin(i), end(i), begin(w));\n }\n\n default_random_engine eng{random_device{}()};\n const dbl xCenter{0.0}, yCenter{0.0};\n piecewise_linear_distribution<dbl> rDist;\n uniform_real_distribution<dbl> phiDist{0.0, 2.0 * numbers::pi};\n\n inline vector<dbl> toCircle(\n const dbl r,\n const dbl phi\n ) const noexcept {\n return {cos(phi) * r + xCenter, sin(phi) * r + yCenter};\n }\n\npublic:\n inline Solution(\n const dbl radius,\n const dbl xCenter,\n const dbl yCenter\n ) noexcept : xCenter{xCenter}, yCenter{yCenter},\n rDist{radiusDistribution(radius)} {\n }\n\n inline vector<dbl> randPoint() noexcept {\n return toCircle(rDist(eng), phiDist(eng));\n }\n};\n/**\n * Your Solution object will be instantiated and called as such:\n * Solution* obj = new Solution(radius, x_center, y_center);\n * vector<double> param_1 = obj->randPoint();\n */\n", "memory": "32400" }
915
<p>Given the radius and the position of the center of a circle, implement the function <code>randPoint</code> which generates a uniform random point inside the circle.</p> <p>Implement the <code>Solution</code> class:</p> <ul> <li><code>Solution(double radius, double x_center, double y_center)</code> initializes the object with the radius of the circle <code>radius</code> and the position of the center <code>(x_center, y_center)</code>.</li> <li><code>randPoint()</code> returns a random point inside the circle. A point on the circumference of the circle is considered to be in the circle. The answer is returned as an array <code>[x, y]</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input</strong> [&quot;Solution&quot;, &quot;randPoint&quot;, &quot;randPoint&quot;, &quot;randPoint&quot;] [[1.0, 0.0, 0.0], [], [], []] <strong>Output</strong> [null, [-0.02493, -0.38077], [0.82314, 0.38945], [0.36572, 0.17248]] <strong>Explanation</strong> Solution solution = new Solution(1.0, 0.0, 0.0); solution.randPoint(); // return [-0.02493, -0.38077] solution.randPoint(); // return [0.82314, 0.38945] solution.randPoint(); // return [0.36572, 0.17248] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>0 &lt;&nbsp;radius &lt;= 10<sup>8</sup></code></li> <li><code>-10<sup>7</sup> &lt;= x_center, y_center &lt;= 10<sup>7</sup></code></li> <li>At most <code>3 * 10<sup>4</sup></code> calls will be made to <code>randPoint</code>.</li> </ul>
3
{ "code": "class Solution {\n public:\n Solution(double radius, double x_center, double y_center)\n : radius(radius), x_center(x_center), y_center(y_center) {}\n\n vector<double> randPoint() {\n const double length = sqrt(distribution(generator)) * radius;\n const double degree = distribution(generator) * 2 * M_PI;\n const double x = x_center + length * cos(degree);\n const double y = y_center + length * sin(degree);\n return {x, y};\n }\n\n private:\n const double radius;\n const double x_center;\n const double y_center;\n default_random_engine generator;\n uniform_real_distribution<double> distribution =\n uniform_real_distribution<double>(0.0, 1.0);\n};", "memory": "32500" }
915
<p>Given the radius and the position of the center of a circle, implement the function <code>randPoint</code> which generates a uniform random point inside the circle.</p> <p>Implement the <code>Solution</code> class:</p> <ul> <li><code>Solution(double radius, double x_center, double y_center)</code> initializes the object with the radius of the circle <code>radius</code> and the position of the center <code>(x_center, y_center)</code>.</li> <li><code>randPoint()</code> returns a random point inside the circle. A point on the circumference of the circle is considered to be in the circle. The answer is returned as an array <code>[x, y]</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input</strong> [&quot;Solution&quot;, &quot;randPoint&quot;, &quot;randPoint&quot;, &quot;randPoint&quot;] [[1.0, 0.0, 0.0], [], [], []] <strong>Output</strong> [null, [-0.02493, -0.38077], [0.82314, 0.38945], [0.36572, 0.17248]] <strong>Explanation</strong> Solution solution = new Solution(1.0, 0.0, 0.0); solution.randPoint(); // return [-0.02493, -0.38077] solution.randPoint(); // return [0.82314, 0.38945] solution.randPoint(); // return [0.36572, 0.17248] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>0 &lt;&nbsp;radius &lt;= 10<sup>8</sup></code></li> <li><code>-10<sup>7</sup> &lt;= x_center, y_center &lt;= 10<sup>7</sup></code></li> <li>At most <code>3 * 10<sup>4</sup></code> calls will be made to <code>randPoint</code>.</li> </ul>
3
{ "code": "char c = [](){ios::sync_with_stdio(false); cin.tie(NULL); return 'c';}();\nclass Solution {\npublic:\n double radius, x_center, y_center;\n std::random_device rd;\n std::mt19937 gen;\n std::uniform_real_distribution<double> dis {0.0};\n Solution(double radius, double x_center, double y_center) : \n radius{radius}, x_center{x_center}, y_center{y_center},\n gen(rd()) {}\n \n vector<double> randPoint() {\n double r = radius * sqrt(dis(gen));\n double theta = dis(gen) * 2 * numbers::pi;\n return {x_center + r * cos(theta), y_center + r * sin(theta)};\n }\n};\n\n\n/**\n * Your Solution object will be instantiated and called as such:\n * Solution* obj = new Solution(radius, x_center, y_center);\n * vector<double> param_1 = obj->randPoint();\n */", "memory": "32600" }
915
<p>Given the radius and the position of the center of a circle, implement the function <code>randPoint</code> which generates a uniform random point inside the circle.</p> <p>Implement the <code>Solution</code> class:</p> <ul> <li><code>Solution(double radius, double x_center, double y_center)</code> initializes the object with the radius of the circle <code>radius</code> and the position of the center <code>(x_center, y_center)</code>.</li> <li><code>randPoint()</code> returns a random point inside the circle. A point on the circumference of the circle is considered to be in the circle. The answer is returned as an array <code>[x, y]</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input</strong> [&quot;Solution&quot;, &quot;randPoint&quot;, &quot;randPoint&quot;, &quot;randPoint&quot;] [[1.0, 0.0, 0.0], [], [], []] <strong>Output</strong> [null, [-0.02493, -0.38077], [0.82314, 0.38945], [0.36572, 0.17248]] <strong>Explanation</strong> Solution solution = new Solution(1.0, 0.0, 0.0); solution.randPoint(); // return [-0.02493, -0.38077] solution.randPoint(); // return [0.82314, 0.38945] solution.randPoint(); // return [0.36572, 0.17248] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>0 &lt;&nbsp;radius &lt;= 10<sup>8</sup></code></li> <li><code>-10<sup>7</sup> &lt;= x_center, y_center &lt;= 10<sup>7</sup></code></li> <li>At most <code>3 * 10<sup>4</sup></code> calls will be made to <code>randPoint</code>.</li> </ul>
3
{ "code": "class Solution {\nprivate:\n double radius, x_center, y_center; \n\npublic:\n Solution(double radius, double x_center, double y_center) {\n this->radius = radius; \n this->x_center = x_center; \n this->y_center = y_center; \n }\n\n vector<double> randPoint() {\n random_device rd;\n mt19937 gen(rd());\n uniform_real_distribution<double> dis_angle(0, 2 * M_PI); \n uniform_real_distribution<double> dis_radius(0, 1); \n\n double angle = dis_angle(gen);\n double distance = sqrt(dis_radius(gen)) * radius; \n\n double pt_x = distance * cos(angle); \n double pt_y = distance * sin(angle); \n\n return {x_center + pt_x, y_center + pt_y}; \n }\n};\n", "memory": "33200" }
915
<p>Given the radius and the position of the center of a circle, implement the function <code>randPoint</code> which generates a uniform random point inside the circle.</p> <p>Implement the <code>Solution</code> class:</p> <ul> <li><code>Solution(double radius, double x_center, double y_center)</code> initializes the object with the radius of the circle <code>radius</code> and the position of the center <code>(x_center, y_center)</code>.</li> <li><code>randPoint()</code> returns a random point inside the circle. A point on the circumference of the circle is considered to be in the circle. The answer is returned as an array <code>[x, y]</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input</strong> [&quot;Solution&quot;, &quot;randPoint&quot;, &quot;randPoint&quot;, &quot;randPoint&quot;] [[1.0, 0.0, 0.0], [], [], []] <strong>Output</strong> [null, [-0.02493, -0.38077], [0.82314, 0.38945], [0.36572, 0.17248]] <strong>Explanation</strong> Solution solution = new Solution(1.0, 0.0, 0.0); solution.randPoint(); // return [-0.02493, -0.38077] solution.randPoint(); // return [0.82314, 0.38945] solution.randPoint(); // return [0.36572, 0.17248] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>0 &lt;&nbsp;radius &lt;= 10<sup>8</sup></code></li> <li><code>-10<sup>7</sup> &lt;= x_center, y_center &lt;= 10<sup>7</sup></code></li> <li>At most <code>3 * 10<sup>4</sup></code> calls will be made to <code>randPoint</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n double rad,x,y;\n Solution(double radius, double x_center, double y_center) {\n rad = radius;\n x = x_center;\n y = y_center;\n }\n \n vector<double> randPoint() {\n random_device rd;\n mt19937 gen(rd());\n uniform_int_distribution<> distrib(0,360);\n int randomValue = distrib(gen);\n \n double randradii = sqrt((double)rand() / RAND_MAX) * rad;\n\n \n return {x + randradii * cos(randomValue),y + randradii * sin(randomValue)};\n }\n};\n\n/**\n * Your Solution object will be instantiated and called as such:\n * Solution* obj = new Solution(radius, x_center, y_center);\n * vector<double> param_1 = obj->randPoint();\n */", "memory": "33300" }
480
<p>The <strong>median</strong> is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle values.</p> <ul> <li>For examples, if <code>arr = [2,<u>3</u>,4]</code>, the median is <code>3</code>.</li> <li>For examples, if <code>arr = [1,<u>2,3</u>,4]</code>, the median is <code>(2 + 3) / 2 = 2.5</code>.</li> </ul> <p>You are given an integer array <code>nums</code> and an integer <code>k</code>. There is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the median array for each window in the original array</em>. Answers within <code>10<sup>-5</sup></code> of the actual value will be accepted.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,-1,-3,5,3,6,7], k = 3 <strong>Output:</strong> [1.00000,-1.00000,-1.00000,3.00000,5.00000,6.00000] <strong>Explanation:</strong> Window position Median --------------- ----- [<strong>1 3 -1</strong>] -3 5 3 6 7 1 1 [<strong>3 -1 -3</strong>] 5 3 6 7 -1 1 3 [<strong>-1 -3 5</strong>] 3 6 7 -1 1 3 -1 [<strong>-3 5 3</strong>] 6 7 3 1 3 -1 -3 [<strong>5 3 6</strong>] 7 5 1 3 -1 -3 5 [<strong>3 6 7</strong>] 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,2,3,1,4,2], k = 3 <strong>Output:</strong> [2.00000,3.00000,3.00000,3.00000,2.00000,3.00000,2.00000] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= k &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n Solution() {\n std::ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n }\n\n double median(const std::vector<int> &nums) {\n const int N = nums.size();\n if (N % 2) return nums[N / 2];\n\n double d = (double) nums[N / 2] + nums[N / 2 - 1];\n return d / 2.0f;\n }\n\n std::vector<double> medianSlidingWindow(const std::vector<int>& nums, const int k) {\n if (nums.empty()) return {};\n\n const int N = nums.size();\n std::vector<double> out(N - k + 1);\n std::vector<int> v;\n v.reserve(k);\n\n for (int i = 0; i < k; ++i)\n v.push_back(nums[i]);\n \n std::sort(v.begin(), v.end());\n out[0] = median(v);\n\n for (int i = 0; i < N - k; ++i) {\n auto it = std::lower_bound(v.begin(), v.end(), nums[i]);\n v.erase(it);\n it = std::lower_bound(v.begin(), v.end(), nums[i + k]);\n v.insert(it, nums[i + k]);\n out[i + 1] = median(v);\n }\n\n return out;\n }\n};", "memory": "31790" }
480
<p>The <strong>median</strong> is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle values.</p> <ul> <li>For examples, if <code>arr = [2,<u>3</u>,4]</code>, the median is <code>3</code>.</li> <li>For examples, if <code>arr = [1,<u>2,3</u>,4]</code>, the median is <code>(2 + 3) / 2 = 2.5</code>.</li> </ul> <p>You are given an integer array <code>nums</code> and an integer <code>k</code>. There is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the median array for each window in the original array</em>. Answers within <code>10<sup>-5</sup></code> of the actual value will be accepted.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,-1,-3,5,3,6,7], k = 3 <strong>Output:</strong> [1.00000,-1.00000,-1.00000,3.00000,5.00000,6.00000] <strong>Explanation:</strong> Window position Median --------------- ----- [<strong>1 3 -1</strong>] -3 5 3 6 7 1 1 [<strong>3 -1 -3</strong>] 5 3 6 7 -1 1 3 [<strong>-1 -3 5</strong>] 3 6 7 -1 1 3 -1 [<strong>-3 5 3</strong>] 6 7 3 1 3 -1 -3 [<strong>5 3 6</strong>] 7 5 1 3 -1 -3 5 [<strong>3 6 7</strong>] 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,2,3,1,4,2], k = 3 <strong>Output:</strong> [2.00000,3.00000,3.00000,3.00000,2.00000,3.00000,2.00000] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= k &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul>
0
{ "code": "class sorted_vector {\nprivate:\n vector<int> arr;\n int k;\npublic:\n sorted_vector(int k) {\n this->k = k;\n arr = vector<int>();\n }\n \n void add(int val) {\n auto lb = lower_bound(arr.begin(), arr.end(), val);\n arr.insert(lb, val);\n }\n \n void remove(int val) {\n auto lb = lower_bound(arr.begin(), arr.end(), val);\n arr.erase(lb);\n }\n \n double median() {\n if (k%2 == 0) {\n return ((double) arr[k/2] + arr[k/2-1])/2;\n }\n else\n return arr[k/2];\n }\n};\n\nclass Solution {\npublic:\n vector<double> medianSlidingWindow(vector<int>& nums, int k) {\n int n = nums.size();\n int i = 0;\n \n vector<double> ans(n-k+1);\n \n sorted_vector arr(k);\n \n while (i < k-1) {\n arr.add(nums[i]);\n i++;\n }\n \n while (i < n) {\n arr.add(nums[i]);\n ans[i-k+1] = arr.median();\n arr.remove(nums[i-k+1]);\n i++;\n }\n \n return ans;\n }\n};", "memory": "31790" }
480
<p>The <strong>median</strong> is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle values.</p> <ul> <li>For examples, if <code>arr = [2,<u>3</u>,4]</code>, the median is <code>3</code>.</li> <li>For examples, if <code>arr = [1,<u>2,3</u>,4]</code>, the median is <code>(2 + 3) / 2 = 2.5</code>.</li> </ul> <p>You are given an integer array <code>nums</code> and an integer <code>k</code>. There is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the median array for each window in the original array</em>. Answers within <code>10<sup>-5</sup></code> of the actual value will be accepted.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,-1,-3,5,3,6,7], k = 3 <strong>Output:</strong> [1.00000,-1.00000,-1.00000,3.00000,5.00000,6.00000] <strong>Explanation:</strong> Window position Median --------------- ----- [<strong>1 3 -1</strong>] -3 5 3 6 7 1 1 [<strong>3 -1 -3</strong>] 5 3 6 7 -1 1 3 [<strong>-1 -3 5</strong>] 3 6 7 -1 1 3 -1 [<strong>-3 5 3</strong>] 6 7 3 1 3 -1 -3 [<strong>5 3 6</strong>] 7 5 1 3 -1 -3 5 [<strong>3 6 7</strong>] 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,2,3,1,4,2], k = 3 <strong>Output:</strong> [2.00000,3.00000,3.00000,3.00000,2.00000,3.00000,2.00000] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= k &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n vector<double> medianSlidingWindow(vector<int>& nums, int k) {\n vector<long>store;\n int ws = k;\n int i=0;\n while(k--){\n store.push_back(nums[i]);\n i++;\n }\n sort(store.begin(),store.end());\n vector<double>ans;\n int x = store.size();\n long z; \n if(x%2==0){\n z = long(store[(x/2)-1])+long(store[x/2]);\n }else{\n z = store[x/2];\n }\n double y =(x%2==0)?(z)/2.0:z;\n ans.push_back(y);\n if(i == nums.size()){\n return ans;\n }\n while(i<nums.size()){\n int lasttoremove = nums[i-ws];\n auto indx = find(store.begin(),store.end(),lasttoremove);\n store.erase(indx);\n indx = lower_bound(store.begin(),store.end(),nums[i]);\n store.insert(indx,nums[i]);\n long z; \n if(x%2==0){\n z = long(store[(x/2)-1])+long(store[x/2]);\n }else{\n z = store[x/2];\n }\n double y =(x%2==0)?(z)/2.0:z;\n ans.push_back(y);\n i++;\n }\n return ans;\n }\n};", "memory": "33690" }
480
<p>The <strong>median</strong> is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle values.</p> <ul> <li>For examples, if <code>arr = [2,<u>3</u>,4]</code>, the median is <code>3</code>.</li> <li>For examples, if <code>arr = [1,<u>2,3</u>,4]</code>, the median is <code>(2 + 3) / 2 = 2.5</code>.</li> </ul> <p>You are given an integer array <code>nums</code> and an integer <code>k</code>. There is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the median array for each window in the original array</em>. Answers within <code>10<sup>-5</sup></code> of the actual value will be accepted.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,-1,-3,5,3,6,7], k = 3 <strong>Output:</strong> [1.00000,-1.00000,-1.00000,3.00000,5.00000,6.00000] <strong>Explanation:</strong> Window position Median --------------- ----- [<strong>1 3 -1</strong>] -3 5 3 6 7 1 1 [<strong>3 -1 -3</strong>] 5 3 6 7 -1 1 3 [<strong>-1 -3 5</strong>] 3 6 7 -1 1 3 -1 [<strong>-3 5 3</strong>] 6 7 3 1 3 -1 -3 [<strong>5 3 6</strong>] 7 5 1 3 -1 -3 5 [<strong>3 6 7</strong>] 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,2,3,1,4,2], k = 3 <strong>Output:</strong> [2.00000,3.00000,3.00000,3.00000,2.00000,3.00000,2.00000] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= k &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n vector<double> medianSlidingWindow(vector<int>& nums, int k) {\n vector<double>ans;\n vector<long long>ab;\n\n for(int i=0;i<k;i++)\n {\n ab.insert(lower_bound(ab.begin(), ab.end(),nums[i]), nums[i]);\n }\n\n if(k%2==1) ans.push_back((double)ab[k/2]);\n else ans.push_back((double)(ab[k/2]+ab[k/2-1])/2);\n\n for(int i=k;i<nums.size();i++)\n {\n ab.erase(lower_bound(ab.begin(), ab.end(), nums[i-k]));\n ab.insert(lower_bound(ab.begin(), ab.end(), nums[i]), nums[i]);\n\n if(k%2==1) ans.push_back((double)ab[k/2]);\n else ans.push_back((double)(ab[k/2]+ab[k/2-1])/2);\n\n }\n return ans;\n\n }\n};", "memory": "33690" }
480
<p>The <strong>median</strong> is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle values.</p> <ul> <li>For examples, if <code>arr = [2,<u>3</u>,4]</code>, the median is <code>3</code>.</li> <li>For examples, if <code>arr = [1,<u>2,3</u>,4]</code>, the median is <code>(2 + 3) / 2 = 2.5</code>.</li> </ul> <p>You are given an integer array <code>nums</code> and an integer <code>k</code>. There is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the median array for each window in the original array</em>. Answers within <code>10<sup>-5</sup></code> of the actual value will be accepted.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,-1,-3,5,3,6,7], k = 3 <strong>Output:</strong> [1.00000,-1.00000,-1.00000,3.00000,5.00000,6.00000] <strong>Explanation:</strong> Window position Median --------------- ----- [<strong>1 3 -1</strong>] -3 5 3 6 7 1 1 [<strong>3 -1 -3</strong>] 5 3 6 7 -1 1 3 [<strong>-1 -3 5</strong>] 3 6 7 -1 1 3 -1 [<strong>-3 5 3</strong>] 6 7 3 1 3 -1 -3 [<strong>5 3 6</strong>] 7 5 1 3 -1 -3 5 [<strong>3 6 7</strong>] 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,2,3,1,4,2], k = 3 <strong>Output:</strong> [2.00000,3.00000,3.00000,3.00000,2.00000,3.00000,2.00000] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= k &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul>
0
{ "code": "class maxHeapCom{\n public : \n bool operator()(pair<int,int>&a, pair<int,int>&b){\n if(a.first != b.first) return a.first < b.first;\n return a.second < b.second;\n }\n};\nclass minHeap{\n public : \n bool operator()(pair<int,int>& a, pair<int,int> &b){\n if(a.first!=b.first) return a.first > b.first;\n return a.second > b.second;\n }\n};\n\nclass Solution {\npublic:\n void removeEle(priority_queue<pair<int,int>, vector<pair<int,int>>, maxHeapCom>& pq, int l) {\n while (!pq.empty() && pq.top().second < l) pq.pop();\n }\n\n void removeEle1(priority_queue<pair<int,int>, vector<pair<int,int>>, minHeap>& pq, int l) {\n while (!pq.empty() && pq.top().second < l) pq.pop();\n }\n vector<double> medianSlidingWindow(vector<int>& nums, int k) {\n priority_queue<pair<int,int>,vector<pair<int,int>>,maxHeapCom>maxpq;\n priority_queue<pair<int,int>,vector<pair<int,int>>,minHeap>minpq;\n int n = nums.size();\n vector<double>result(n-k+1,0.0);\n int rightsize=0,leftsize=0;\n int diff = k%2 == 0 ? 0 : 1;\n for(int i=0,l=-k+1;i<n;i++,l++){\n // then we will have to remove \n if(l>0 && !maxpq.empty() && nums[l-1]<=maxpq.top().first){\n leftsize--;\n }else if(l>0) rightsize--;\n\n removeEle(maxpq,l);\n removeEle1(minpq,l);\n int num = nums[i];\n // cout << num << \"###\";\n \n if(!minpq.empty() && minpq.top().first<=num){\n minpq.push({num,i});\n rightsize++;\n }else{\n maxpq.push({num,i});\n leftsize++;\n }\n // print(maxpq);\n // print(minpq);\n if(rightsize>leftsize){\n auto pair = minpq.top();\n minpq.pop();\n maxpq.push({pair.first,pair.second});\n leftsize++;\n rightsize--;\n }else if(leftsize-rightsize>diff){\n auto pair = maxpq.top();\n maxpq.pop();\n minpq.push({pair.first,pair.second});\n leftsize--;\n rightsize++;\n }\n removeEle(maxpq,l);\n removeEle1(minpq,l);\n cout<<\"upper bound\\n\";\n // print(maxpq);\n // print(minpq);\n if(l>=0){\n if(k%2){\n double x = nums[maxpq.top().second]/1.0;\n // cout << x << \" ---------- ans \";\n result[l] = x;\n }else {\n double x = ((long)nums[maxpq.top().second] + (long)nums[minpq.top().second])/2.0;\n result[l] = x;\n }\n }\n }\n\n return result;\n }\n};", "memory": "34070" }
480
<p>The <strong>median</strong> is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle values.</p> <ul> <li>For examples, if <code>arr = [2,<u>3</u>,4]</code>, the median is <code>3</code>.</li> <li>For examples, if <code>arr = [1,<u>2,3</u>,4]</code>, the median is <code>(2 + 3) / 2 = 2.5</code>.</li> </ul> <p>You are given an integer array <code>nums</code> and an integer <code>k</code>. There is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the median array for each window in the original array</em>. Answers within <code>10<sup>-5</sup></code> of the actual value will be accepted.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,-1,-3,5,3,6,7], k = 3 <strong>Output:</strong> [1.00000,-1.00000,-1.00000,3.00000,5.00000,6.00000] <strong>Explanation:</strong> Window position Median --------------- ----- [<strong>1 3 -1</strong>] -3 5 3 6 7 1 1 [<strong>3 -1 -3</strong>] 5 3 6 7 -1 1 3 [<strong>-1 -3 5</strong>] 3 6 7 -1 1 3 -1 [<strong>-3 5 3</strong>] 6 7 3 1 3 -1 -3 [<strong>5 3 6</strong>] 7 5 1 3 -1 -3 5 [<strong>3 6 7</strong>] 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,2,3,1,4,2], k = 3 <strong>Output:</strong> [2.00000,3.00000,3.00000,3.00000,2.00000,3.00000,2.00000] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= k &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul>
0
{ "code": "class maxHeapCom{\n public : \n bool operator()(pair<int,int>&a, pair<int,int>&b){\n if(a.first != b.first) return a.first < b.first;\n return a.second < b.second;\n }\n};\nclass minHeap{\n public : \n bool operator()(pair<int,int>& a, pair<int,int> &b){\n if(a.first!=b.first) return a.first > b.first;\n return a.second > b.second;\n }\n};\n\nclass Solution {\npublic:\n void removeEle(priority_queue<pair<int,int>, vector<pair<int,int>>, maxHeapCom>& pq, int l) {\n while (!pq.empty() && pq.top().second < l) pq.pop();\n }\n\n void removeEle1(priority_queue<pair<int,int>, vector<pair<int,int>>, minHeap>& pq, int l) {\n while (!pq.empty() && pq.top().second < l) pq.pop();\n }\n vector<double> medianSlidingWindow(vector<int>& nums, int k) {\n priority_queue<pair<int,int>,vector<pair<int,int>>,maxHeapCom>maxpq;\n priority_queue<pair<int,int>,vector<pair<int,int>>,minHeap>minpq;\n int n = nums.size();\n vector<double>result(n-k+1,0.0);\n int rightsize=0,leftsize=0;\n int diff = k%2 == 0 ? 0 : 1;\n for(int i=0,l=-k+1;i<n;i++,l++){\n // then we will have to remove \n if(l>0 && !maxpq.empty() && nums[l-1]<=nums[maxpq.top().second]){\n leftsize--;\n }else if(l>0) rightsize--;\n\n removeEle(maxpq,l);\n removeEle1(minpq,l);\n int num = nums[i];\n // cout << num << \"###\";\n \n if(!minpq.empty() && nums[minpq.top().second]<=num){\n minpq.push({num,i});\n rightsize++;\n }else{\n maxpq.push({num,i});\n leftsize++;\n }\n // print(maxpq);\n // print(minpq);\n if(rightsize>leftsize){\n auto pair = minpq.top();\n minpq.pop();\n maxpq.push({pair.first,pair.second});\n leftsize++;\n rightsize--;\n }else if(leftsize-rightsize>diff){\n auto pair = maxpq.top();\n maxpq.pop();\n minpq.push({pair.first,pair.second});\n leftsize--;\n rightsize++;\n }\n removeEle(maxpq,l);\n removeEle1(minpq,l);\n cout<<\"upper bound\\n\";\n // print(maxpq);\n // print(minpq);\n if(l>=0){\n if(k%2){\n double x = nums[maxpq.top().second]/1.0;\n // cout << x << \" ---------- ans \";\n result[l] = x;\n }else {\n double x = ((long)nums[maxpq.top().second] + (long)nums[minpq.top().second])/2.0;\n result[l] = x;\n }\n }\n }\n\n return result;\n }\n};", "memory": "34450" }
480
<p>The <strong>median</strong> is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle values.</p> <ul> <li>For examples, if <code>arr = [2,<u>3</u>,4]</code>, the median is <code>3</code>.</li> <li>For examples, if <code>arr = [1,<u>2,3</u>,4]</code>, the median is <code>(2 + 3) / 2 = 2.5</code>.</li> </ul> <p>You are given an integer array <code>nums</code> and an integer <code>k</code>. There is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the median array for each window in the original array</em>. Answers within <code>10<sup>-5</sup></code> of the actual value will be accepted.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,-1,-3,5,3,6,7], k = 3 <strong>Output:</strong> [1.00000,-1.00000,-1.00000,3.00000,5.00000,6.00000] <strong>Explanation:</strong> Window position Median --------------- ----- [<strong>1 3 -1</strong>] -3 5 3 6 7 1 1 [<strong>3 -1 -3</strong>] 5 3 6 7 -1 1 3 [<strong>-1 -3 5</strong>] 3 6 7 -1 1 3 -1 [<strong>-3 5 3</strong>] 6 7 3 1 3 -1 -3 [<strong>5 3 6</strong>] 7 5 1 3 -1 -3 5 [<strong>3 6 7</strong>] 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,2,3,1,4,2], k = 3 <strong>Output:</strong> [2.00000,3.00000,3.00000,3.00000,2.00000,3.00000,2.00000] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= k &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul>
0
{ "code": "class maxHeapCom{\n public : \n bool operator()(pair<int,int>&a, pair<int,int>&b){\n if(a.first != b.first) return a.first < b.first;\n return a.second < b.second;\n }\n};\nclass minHeap{\n public : \n bool operator()(pair<int,int>& a, pair<int,int> &b){\n if(a.first!=b.first) return a.first > b.first;\n return a.second > b.second;\n }\n};\n\nclass Solution {\npublic:\n void removeEle(priority_queue<pair<int,int>, vector<pair<int,int>>, maxHeapCom>& pq, int l) {\n while (!pq.empty() && pq.top().second < l) pq.pop();\n }\n\n void removeEle1(priority_queue<pair<int,int>, vector<pair<int,int>>, minHeap>& pq, int l) {\n while (!pq.empty() && pq.top().second < l) pq.pop();\n }\n vector<double> medianSlidingWindow(vector<int>& nums, int k) {\n priority_queue<pair<int,int>,vector<pair<int,int>>,maxHeapCom>maxpq;\n priority_queue<pair<int,int>,vector<pair<int,int>>,minHeap>minpq;\n int n = nums.size();\n vector<double>result(n-k+1,0.0);\n int rightsize=0,leftsize=0;\n int diff = k%2 == 0 ? 0 : 1;\n for(int i=0,l=-k+1;i<n;i++,l++){\n // then we will have to remove \n if(l>0 && !maxpq.empty() && nums[l-1]<=maxpq.top().first){\n leftsize--;\n }else if(l>0) rightsize--;\n\n removeEle(maxpq,l);\n removeEle1(minpq,l);\n int num = nums[i];\n // cout << num << \"###\";\n \n if(!minpq.empty() && nums[minpq.top().second]<=num){\n minpq.push({num,i});\n rightsize++;\n }else{\n maxpq.push({num,i});\n leftsize++;\n }\n // print(maxpq);\n // print(minpq);\n if(rightsize>leftsize){\n auto pair = minpq.top();\n minpq.pop();\n maxpq.push({pair.first,pair.second});\n leftsize++;\n rightsize--;\n }else if(leftsize-rightsize>diff){\n auto pair = maxpq.top();\n maxpq.pop();\n minpq.push({pair.first,pair.second});\n leftsize--;\n rightsize++;\n }\n removeEle(maxpq,l);\n removeEle1(minpq,l);\n cout<<\"upper bound\\n\";\n // print(maxpq);\n // print(minpq);\n if(l>=0){\n if(k%2){\n double x = nums[maxpq.top().second]/1.0;\n // cout << x << \" ---------- ans \";\n result[l] = x;\n }else {\n double x = ((long)nums[maxpq.top().second] + (long)nums[minpq.top().second])/2.0;\n result[l] = x;\n }\n }\n }\n\n return result;\n }\n};", "memory": "34830" }
480
<p>The <strong>median</strong> is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle values.</p> <ul> <li>For examples, if <code>arr = [2,<u>3</u>,4]</code>, the median is <code>3</code>.</li> <li>For examples, if <code>arr = [1,<u>2,3</u>,4]</code>, the median is <code>(2 + 3) / 2 = 2.5</code>.</li> </ul> <p>You are given an integer array <code>nums</code> and an integer <code>k</code>. There is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the median array for each window in the original array</em>. Answers within <code>10<sup>-5</sup></code> of the actual value will be accepted.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,-1,-3,5,3,6,7], k = 3 <strong>Output:</strong> [1.00000,-1.00000,-1.00000,3.00000,5.00000,6.00000] <strong>Explanation:</strong> Window position Median --------------- ----- [<strong>1 3 -1</strong>] -3 5 3 6 7 1 1 [<strong>3 -1 -3</strong>] 5 3 6 7 -1 1 3 [<strong>-1 -3 5</strong>] 3 6 7 -1 1 3 -1 [<strong>-3 5 3</strong>] 6 7 3 1 3 -1 -3 [<strong>5 3 6</strong>] 7 5 1 3 -1 -3 5 [<strong>3 6 7</strong>] 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,2,3,1,4,2], k = 3 <strong>Output:</strong> [2.00000,3.00000,3.00000,3.00000,2.00000,3.00000,2.00000] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= k &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n\n typedef std::pair<int, int> Val;\n static bool max_heap_comp(Val a, Val b) {\n return a.second < b.second;\n };\n static bool min_heap_comp(Val a, Val b) {\n return a.second > b.second;\n };\n priority_queue<Val, vector<Val>, bool(*)(Val, Val)> left;\n priority_queue<Val, vector<Val>, bool(*)(Val, Val)> right;\n\n Solution() : left(max_heap_comp), right(min_heap_comp) {}\n\n void rebalance(int &left_size, int &right_size, vector<int> &side) {\n // Rebalance left and right\n if (left_size - right_size >= 2) {\n Val top_left = left.top();\n right.push(top_left);\n left.pop();\n right_size++; left_size--;\n side[top_left.first] = 1;\n } else if (right_size - left_size >= 2) {\n Val top_right = right.top();\n left.push(top_right);\n right.pop();\n left_size++; right_size--;\n side[top_right.first] = -1;\n }\n }\n\n vector<double> medianSlidingWindow(vector<int>& nums, int k) {\n int size = nums.size();\n vector<double> ans(size - k + 1);\n int left_size = 0;\n int right_size = 0;\n vector<int> side(size, 0);\n\n for (int i = 0; i < size; i++) {\n // Push an element\n int to_insert = nums[i];\n if (!left.empty() && to_insert < left.top().second) {\n left.push({i, to_insert});\n left_size++;\n side[i] = -1;\n } else if (!right.empty() && to_insert > right.top().second) {\n right.push({i, to_insert});\n right_size++;\n side[i] = 1;\n } else {\n if (left_size <= right_size) {\n left.push({i, to_insert});\n left_size++;\n side[i] = -1;\n } else {\n right.push({i, to_insert});\n right_size++;\n side[i] = 1;\n }\n }\n\n rebalance(left_size, right_size, side);\n // if (!left.empty()) cout << \"left: \" << left.top().second << endl;\n // if (!right.empty()) cout << \"right: \" << right.top().second << endl;\n\n if (left_size + right_size == k+1) {\n // Delete an element;\n // int to_delete = nums[i-k];\n int partition = side[i-k];\n if (partition == -1) {\n left_size--;\n } else {\n right_size--;\n }\n }\n rebalance(left_size, right_size, side);\n\n if (left_size + right_size == k) {\n // Sanitize the tops;\n while (!left.empty() && left.top().first <= i - k) {\n left.pop();\n rebalance(left_size, right_size, side);\n }\n while (!right.empty() && right.top().first <= i - k) {\n right.pop();\n rebalance(left_size, right_size, side);\n }\n // if (!left.empty()) cout << \"final left: \" << left.top().second << endl;\n // cout << \"final left size: \" << left_size << endl;\n // if (!right.empty()) cout << \"final right: \" << right.top().second << endl;\n // cout << \"final right size: \" << right_size << endl;\n double left_val = (!left.empty()) ? left.top().second : 0;\n double right_val = (!right.empty()) ? right.top().second : 0;\n ans[i-k+1] = (k & 1) ? (left_size < right_size ? right_val : left_val) : ((double)right_val + left_val) / 2.0; \n }\n\n // left.push({i, nums[i]});\n // right.push({i, nums[i]});\n }\n // cout << \"left: \" << left.top().second;\n // cout << \"right: \" << right.top().second;\n return ans;\n }\n};", "memory": "34830" }
480
<p>The <strong>median</strong> is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle values.</p> <ul> <li>For examples, if <code>arr = [2,<u>3</u>,4]</code>, the median is <code>3</code>.</li> <li>For examples, if <code>arr = [1,<u>2,3</u>,4]</code>, the median is <code>(2 + 3) / 2 = 2.5</code>.</li> </ul> <p>You are given an integer array <code>nums</code> and an integer <code>k</code>. There is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the median array for each window in the original array</em>. Answers within <code>10<sup>-5</sup></code> of the actual value will be accepted.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,-1,-3,5,3,6,7], k = 3 <strong>Output:</strong> [1.00000,-1.00000,-1.00000,3.00000,5.00000,6.00000] <strong>Explanation:</strong> Window position Median --------------- ----- [<strong>1 3 -1</strong>] -3 5 3 6 7 1 1 [<strong>3 -1 -3</strong>] 5 3 6 7 -1 1 3 [<strong>-1 -3 5</strong>] 3 6 7 -1 1 3 -1 [<strong>-3 5 3</strong>] 6 7 3 1 3 -1 -3 [<strong>5 3 6</strong>] 7 5 1 3 -1 -3 5 [<strong>3 6 7</strong>] 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,2,3,1,4,2], k = 3 <strong>Output:</strong> [2.00000,3.00000,3.00000,3.00000,2.00000,3.00000,2.00000] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= k &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul>
0
{ "code": "class maxHeap{\n public:\n bool operator()(pair<int,int>&a, pair<int,int>&b)\n {\n if(a.first!=b.first)\n {\n return a.first<b.first;\n }\n return a.second < b.second;\n }\n};\n\nclass minHeap{\n public:\n bool operator()(pair<int,int>&a, pair<int,int> &b)\n {\n if(a.first!=b.first)\n {\n return a.first>b.first;\n }\n return a.second > b.second;\n }\n};\n\nclass Solution {\npublic:\n void removeEle(priority_queue<pair<int,int>, vector<pair<int,int>>, maxHeap>& pq, int l) {\n while (!pq.empty() && pq.top().second < l) pq.pop();\n }\n\n void removeEle1(priority_queue<pair<int,int>, vector<pair<int,int>>, minHeap>& pq, int l) {\n while (!pq.empty() && pq.top().second < l) pq.pop();\n }\n vector<double> medianSlidingWindow(vector<int>& nums, int k) {\n \n //Max Heap is to contain left half of elements\n\n // Min heap is to contain right half of elements\n priority_queue<pair<int,int>, vector<pair<int,int>>, maxHeap > maxpq;\n priority_queue<pair<int,int>, vector<pair<int,int>>, minHeap > minpq;\n\n vector<double> ans;\n int n=nums.size();\n int leftSize=0;\n int rightSize=0;\n int diff = k%2 == 0 ? 0 : 1;\n for(int i=0,l=-k+1;i<n;i++,l++)\n {\n if(l>0&&!maxpq.empty()&&maxpq.top().first>=nums[l-1])\n {\n leftSize--;\n }\n else\n {\n if(l>0)\n {\n rightSize--;\n }\n }\n\n removeEle(maxpq,l);\n removeEle1(minpq,l);\n\n if(!minpq.empty() && minpq.top().first<=nums[i]){\n minpq.push({nums[i],i});\n rightSize++;\n }else{\n maxpq.push({nums[i],i});\n leftSize++;\n }\n\n if(rightSize>leftSize)\n {\n auto f=minpq.top();\n minpq.pop();\n maxpq.push({f.first,f.second});\n leftSize++;\n rightSize--;\n }\n else\n {\n if(leftSize-rightSize>diff)\n {\n auto f=maxpq.top();\n maxpq.pop();\n minpq.push({f.first,f.second});\n leftSize--;\n rightSize++;\n }\n }\n\n removeEle(maxpq,l);\n removeEle1(minpq,l);\n\n if(l>=0)\n {\n if(k&1)\n {\n double x = nums[maxpq.top().second]/1.0;\n ans.push_back(x);\n }\n else\n {\n double x = ((long)nums[maxpq.top().second] + (long)nums[minpq.top().second])/2.0;\n ans.push_back(x);\n }\n }\n }\n return ans;\n }\n};", "memory": "35210" }
480
<p>The <strong>median</strong> is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle values.</p> <ul> <li>For examples, if <code>arr = [2,<u>3</u>,4]</code>, the median is <code>3</code>.</li> <li>For examples, if <code>arr = [1,<u>2,3</u>,4]</code>, the median is <code>(2 + 3) / 2 = 2.5</code>.</li> </ul> <p>You are given an integer array <code>nums</code> and an integer <code>k</code>. There is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the median array for each window in the original array</em>. Answers within <code>10<sup>-5</sup></code> of the actual value will be accepted.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,-1,-3,5,3,6,7], k = 3 <strong>Output:</strong> [1.00000,-1.00000,-1.00000,3.00000,5.00000,6.00000] <strong>Explanation:</strong> Window position Median --------------- ----- [<strong>1 3 -1</strong>] -3 5 3 6 7 1 1 [<strong>3 -1 -3</strong>] 5 3 6 7 -1 1 3 [<strong>-1 -3 5</strong>] 3 6 7 -1 1 3 -1 [<strong>-3 5 3</strong>] 6 7 3 1 3 -1 -3 [<strong>5 3 6</strong>] 7 5 1 3 -1 -3 5 [<strong>3 6 7</strong>] 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,2,3,1,4,2], k = 3 <strong>Output:</strong> [2.00000,3.00000,3.00000,3.00000,2.00000,3.00000,2.00000] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= k &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul>
0
{ "code": "class maxHeapCom{\n public : \n bool operator()(pair<int,int> a,pair<int,int> b){\n if(a.first!=b.first) return a.first < b.first;\n return a.second < b.second;\n }\n};\nclass minHeap{\n public : \n bool operator()(pair<int,int> a,pair<int,int> b){\n if(a.first!=b.first) return a.first > b.first;\n return a.second > b.second;\n }\n};\nclass Solution {\npublic:\n vector<double> medianSlidingWindow(vector<int>& nums, int k) {\n vector<double>result;\n int n = nums.size();\n\n if(k==1){\n for(int x : nums){\n result.push_back(x*1.0);\n }\n return result;\n }\n priority_queue<pair<int,int>,vector<pair<int,int>>,maxHeapCom>maxpq;\n priority_queue<pair<int,int>,vector<pair<int,int>>,minHeap>minpq;\n\n int i=0,j=0;\n int left = 0; //maxPQ\n int right = 0; // minPQ\n\n while(j<n){\n\n int diff = (j-i+1)%2 == 0 ? 0 : 1;\n\n while(!maxpq.empty() && maxpq.top().second<i) maxpq.pop();\n \n while(!minpq.empty() && minpq.top().second<i) minpq.pop();\n\n if(!minpq.empty() && nums[j]>=nums[minpq.top().second]){\n minpq.push(make_pair(nums[j],j));\n right++;\n }else{\n maxpq.push(make_pair(nums[j],j));\n left++;\n }\n if(left - right > diff){\n // then move from maxpq to minpq\n auto p = maxpq.top();\n int ind = p.second;\n maxpq.pop();\n minpq.push(make_pair(p.first,p.second));\n left--;\n right++;\n }else if(right>left){\n auto p = minpq.top();\n int ind = p.second;\n minpq.pop();\n maxpq.push(make_pair(p.first,p.second));\n right--;\n left++;\n }\n\n while(!maxpq.empty() && maxpq.top().second<i) maxpq.pop();\n \n while(!minpq.empty() && minpq.top().second<i) minpq.pop();\n if(j>=k-1){\n if(k%2){\n double x = nums[maxpq.top().second]/1.0;\n result.push_back(x);\n }else {\n double x = ((long)nums[maxpq.top().second] + (long)nums[minpq.top().second])/2.0;\n result.push_back(x);\n }\n if(!maxpq.empty() && nums[i]<= nums[maxpq.top().second]){\n left--;\n }else{\n right--;\n }\n i++;\n }\n j++;\n }\n return result;\n }\n};", "memory": "35590" }
480
<p>The <strong>median</strong> is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle values.</p> <ul> <li>For examples, if <code>arr = [2,<u>3</u>,4]</code>, the median is <code>3</code>.</li> <li>For examples, if <code>arr = [1,<u>2,3</u>,4]</code>, the median is <code>(2 + 3) / 2 = 2.5</code>.</li> </ul> <p>You are given an integer array <code>nums</code> and an integer <code>k</code>. There is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the median array for each window in the original array</em>. Answers within <code>10<sup>-5</sup></code> of the actual value will be accepted.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,-1,-3,5,3,6,7], k = 3 <strong>Output:</strong> [1.00000,-1.00000,-1.00000,3.00000,5.00000,6.00000] <strong>Explanation:</strong> Window position Median --------------- ----- [<strong>1 3 -1</strong>] -3 5 3 6 7 1 1 [<strong>3 -1 -3</strong>] 5 3 6 7 -1 1 3 [<strong>-1 -3 5</strong>] 3 6 7 -1 1 3 -1 [<strong>-3 5 3</strong>] 6 7 3 1 3 -1 -3 [<strong>5 3 6</strong>] 7 5 1 3 -1 -3 5 [<strong>3 6 7</strong>] 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,2,3,1,4,2], k = 3 <strong>Output:</strong> [2.00000,3.00000,3.00000,3.00000,2.00000,3.00000,2.00000] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= k &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n template<typename T> \n class Queue {\n int size_ = 0;\n T q, remove_q;\n public:\n explicit Queue(T q_, T remove_q_) : q(std::move(q_)), remove_q(std::move(remove_q_)) {}\n\n int top() {\n while (!remove_q.empty() && q.top() == remove_q.top()) {\n q.pop();\n remove_q.pop();\n }\n\n return q.top();\n }\n\n void add(int x) {\n q.push(x);\n size_ += 1;\n }\n\n void remove(int x) {\n remove_q.push(x);\n size_ -= 1;\n }\n\n int size() {\n return size_;\n }\n };\n vector<double> medianSlidingWindow(vector<int>& nums, int k) {\n // <= median\n Queue left_q(priority_queue<int>{}, priority_queue<int>{});\n // > median\n Queue right_q(priority_queue<int, vector<int>, greater<int>>{}, priority_queue<int, vector<int>, greater<int>>{});\n\n bool isEven = (k % 2 == 0);\n\n vector<int> initialNums = nums;\n\n sort(nums.begin(), nums.begin() + k);\n\n for (int i = 0; i < k / 2; ++i) {\n left_q.add(nums[i]);\n }\n\n for (int i = (k + 1) / 2; i < k; ++i) {\n right_q.add(nums[i]);\n }\n\n if (!isEven) {\n left_q.add(nums[k / 2]);\n }\n\n double cur = isEven ? ((double)left_q.top() + right_q.top()) / 2 : left_q.top();\n int curInt = floor(cur);\n\n vector<double> ans = {cur};\n\n for (int i = k; i < nums.size(); ++i) {\n cout << \"cur \" << cur << \" curInt \" << curInt << '\\n';\n int x = initialNums[i];\n int prev = initialNums[i - k];\n cout << \"x \" << x << \" prev \" << prev << '\\n'; \n if (prev <= curInt) {\n left_q.remove(prev);\n } else {\n right_q.remove(prev);\n }\n\n cout << \"remove prev left_q size \" << left_q.size() << \" right_q size \" << right_q.size() << '\\n';\n\n if (x <= curInt) {\n left_q.add(x);\n } else {\n right_q.add(x);\n }\n\n cout << \"add next left_q size \" << left_q.size() << \" right_q size \" << right_q.size() << '\\n';\n\n int diff = isEven ? 0 : 1;\n\n while (right_q.size() - left_q.size() > -diff) {\n cout << \"left_q size \" << left_q.size() << \" right_q size \" << right_q.size() << '\\n';\n int rightTop = right_q.top();\n \n cout << \"rightTop \" << rightTop << '\\n';\n right_q.remove(rightTop);\n left_q.add(rightTop);\n }\n\n while (left_q.size() - right_q.size() > diff) {\n int leftTop = left_q.top();\n cout << 'left_q size ' << left_q.size() << ' ' << 'right_q size ' << right_q.size() << '\\n';\n cout << 'leftTop ' << leftTop << '\\n';\n left_q.remove(leftTop);\n right_q.add(leftTop);\n }\n\n cur = isEven ? ((double)left_q.top() + right_q.top()) / 2 : left_q.top();\n curInt = floor(cur);\n ans.push_back(cur);\n }\n\n return ans;\n }\n};", "memory": "35590" }
480
<p>The <strong>median</strong> is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle values.</p> <ul> <li>For examples, if <code>arr = [2,<u>3</u>,4]</code>, the median is <code>3</code>.</li> <li>For examples, if <code>arr = [1,<u>2,3</u>,4]</code>, the median is <code>(2 + 3) / 2 = 2.5</code>.</li> </ul> <p>You are given an integer array <code>nums</code> and an integer <code>k</code>. There is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the median array for each window in the original array</em>. Answers within <code>10<sup>-5</sup></code> of the actual value will be accepted.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,-1,-3,5,3,6,7], k = 3 <strong>Output:</strong> [1.00000,-1.00000,-1.00000,3.00000,5.00000,6.00000] <strong>Explanation:</strong> Window position Median --------------- ----- [<strong>1 3 -1</strong>] -3 5 3 6 7 1 1 [<strong>3 -1 -3</strong>] 5 3 6 7 -1 1 3 [<strong>-1 -3 5</strong>] 3 6 7 -1 1 3 -1 [<strong>-3 5 3</strong>] 6 7 3 1 3 -1 -3 [<strong>5 3 6</strong>] 7 5 1 3 -1 -3 5 [<strong>3 6 7</strong>] 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,2,3,1,4,2], k = 3 <strong>Output:</strong> [2.00000,3.00000,3.00000,3.00000,2.00000,3.00000,2.00000] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= k &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul>
0
{ "code": "\n\nclass Solution {\npublic:\n priority_queue< pair<int, int> > q_left;\n priority_queue< pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>> > q_right;\n int left = 0, right = 0;\n vector<int> where;\n\n void clean(int l){\n // cout << \"Cleaning\" << endl;\n while(q_right.top().second < l) {\n // display();\n q_right.pop();\n }\n while(q_left.top().second < l) {\n q_left.pop();\n // display();\n }\n // cout << \"cleaned\" << endl;\n }\n\n\n void push(vector<int> & nums, int l, int r){\n // cout << \"Pushing \" << nums[r+1] << \" after receiving left: \"<<left << \" right: \" << right << endl;\n\n if (left < right) {\n q_left.push({nums[r+1], r+1});\n where[r+1] = -1;\n left++;\n }\n else {\n q_right.push({nums[r+1], r+1});\n where[r+1] = 1;\n right++;\n }\n clean(l);\n if (q_left.top().first > q_right.top().first) {\n where[q_left.top().second] = 1;\n where[q_right.top().second] = -1;\n q_left.push(q_right.top());\n q_right.push(q_left.top());\n q_left.pop();\n q_right.pop();\n }\n // cout << \"Pushed\" << endl;\n }\n\n void display(){\n priority_queue< pair<int, int> > temp_q_left;\n priority_queue< pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>> > temp_q_right;\n cout << \"q_left: \";\n while(!q_left.empty()){\n cout << \"(\" << q_left.top().first << \" \" << q_left.top().second << \") \";\n temp_q_left.push(q_left.top());\n q_left.pop();\n }\n cout << endl;\n cout << \"q_right: \";\n while(!q_right.empty()){\n cout << \"(\" << q_right.top().first << \" \" << q_right.top().second << \") \";\n temp_q_right.push(q_right.top());\n q_right.pop();\n }\n cout << endl;\n while(!temp_q_left.empty()) {\n q_left.push(temp_q_left.top());\n temp_q_left.pop();\n }\n while(!temp_q_right.empty()) {\n q_right.push(temp_q_right.top());\n temp_q_right.pop();\n }\n }\n\n vector<double> medianSlidingWindow(vector<int>& nums, int k) {\n int n = nums.size();\n vector<double> ans;\n if (k == 1) {\n for (int i = 0; i < n; i++) ans.push_back(nums[i]);\n return ans;\n }\n\n where.resize(n, 0);\n q_left.push({nums[0], 0});\n where[0] = -1;\n left++;\n for (int i = 1; i < k; i++) push(nums, 0, i-1);\n int l = 0, r = k-1;\n while(r < n){\n \n clean(l);\n \n if (left < right) ans.push_back(q_right.top().first);\n else if (left > right) ans.push_back(q_left.top().first);\n else ans.push_back(((double)(q_left.top().first) + q_right.top().first) / 2.0);\n if (where[l] == -1) left--;\n else if (where[l] == 1) right--;\n else {\n cout << \"aborted\";\n exit(1);\n }\n if (r + 1 < n) push(nums, l, r);\n l++; r++;\n // cout << \"while exit\" << endl;\n // cout << \"left: \" << left << \" right: \"<<right << endl;\n // display();\n }\n return ans;\n\n }\n};\n\n\n", "memory": "35970" }
480
<p>The <strong>median</strong> is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle values.</p> <ul> <li>For examples, if <code>arr = [2,<u>3</u>,4]</code>, the median is <code>3</code>.</li> <li>For examples, if <code>arr = [1,<u>2,3</u>,4]</code>, the median is <code>(2 + 3) / 2 = 2.5</code>.</li> </ul> <p>You are given an integer array <code>nums</code> and an integer <code>k</code>. There is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the median array for each window in the original array</em>. Answers within <code>10<sup>-5</sup></code> of the actual value will be accepted.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,-1,-3,5,3,6,7], k = 3 <strong>Output:</strong> [1.00000,-1.00000,-1.00000,3.00000,5.00000,6.00000] <strong>Explanation:</strong> Window position Median --------------- ----- [<strong>1 3 -1</strong>] -3 5 3 6 7 1 1 [<strong>3 -1 -3</strong>] 5 3 6 7 -1 1 3 [<strong>-1 -3 5</strong>] 3 6 7 -1 1 3 -1 [<strong>-3 5 3</strong>] 6 7 3 1 3 -1 -3 [<strong>5 3 6</strong>] 7 5 1 3 -1 -3 5 [<strong>3 6 7</strong>] 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,2,3,1,4,2], k = 3 <strong>Output:</strong> [2.00000,3.00000,3.00000,3.00000,2.00000,3.00000,2.00000] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= k &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul>
0
{ "code": "\n\nclass Solution {\npublic:\n priority_queue< pair<int, int> > q_left;\n priority_queue< pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>> > q_right;\n int left = 0, right = 0;\n vector<int> where;\n\n void clean(int l){\n // cout << \"Cleaning\" << endl;\n while(q_right.top().second < l) {\n // display();\n q_right.pop();\n }\n while(q_left.top().second < l) {\n q_left.pop();\n // display();\n }\n // cout << \"cleaned\" << endl;\n }\n\n\n void push(vector<int> & nums, int l, int r){\n // cout << \"Pushing \" << nums[r+1] << \" after receiving left: \"<<left << \" right: \" << right << endl;\n\n if (left < right) {\n q_left.push({nums[r+1], r+1});\n where[r+1] = -1;\n left++;\n }\n else {\n q_right.push({nums[r+1], r+1});\n where[r+1] = 1;\n right++;\n }\n clean(l);\n if (q_left.top().first > q_right.top().first) {\n where[q_left.top().second] = 1;\n where[q_right.top().second] = -1;\n q_left.push(q_right.top());\n q_right.push(q_left.top());\n q_left.pop();\n q_right.pop();\n }\n // cout << \"Pushed\" << endl;\n }\n\n void display(){\n priority_queue< pair<int, int> > temp_q_left;\n priority_queue< pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>> > temp_q_right;\n cout << \"q_left: \";\n while(!q_left.empty()){\n cout << \"(\" << q_left.top().first << \" \" << q_left.top().second << \") \";\n temp_q_left.push(q_left.top());\n q_left.pop();\n }\n cout << endl;\n cout << \"q_right: \";\n while(!q_right.empty()){\n cout << \"(\" << q_right.top().first << \" \" << q_right.top().second << \") \";\n temp_q_right.push(q_right.top());\n q_right.pop();\n }\n cout << endl;\n while(!temp_q_left.empty()) {\n q_left.push(temp_q_left.top());\n temp_q_left.pop();\n }\n while(!temp_q_right.empty()) {\n q_right.push(temp_q_right.top());\n temp_q_right.pop();\n }\n }\n\n vector<double> medianSlidingWindow(vector<int>& nums, int k) {\n int n = nums.size();\n vector<double> ans;\n if (k == 1) {\n for (int i = 0; i < n; i++) ans.push_back(nums[i]);\n return ans;\n }\n\n where.resize(n, 0);\n q_left.push({nums[0], 0});\n where[0] = -1;\n left++;\n for (int i = 1; i < k; i++) push(nums, 0, i-1);\n int l = 0, r = k-1;\n while(r < n){\n \n clean(l);\n \n if (left < right) ans.push_back(q_right.top().first);\n else if (left > right) ans.push_back(q_left.top().first);\n else ans.push_back(((double)(q_left.top().first) + q_right.top().first) / 2.0);\n if (where[l] == -1) left--;\n else if (where[l] == 1) right--;\n else {\n cout << \"aborted\";\n exit(1);\n }\n if (r + 1 < n) push(nums, l, r);\n l++; r++;\n // cout << \"while exit\" << endl;\n // cout << \"left: \" << left << \" right: \"<<right << endl;\n // display();\n }\n return ans;\n\n }\n};\n\n\n", "memory": "35970" }
480
<p>The <strong>median</strong> is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle values.</p> <ul> <li>For examples, if <code>arr = [2,<u>3</u>,4]</code>, the median is <code>3</code>.</li> <li>For examples, if <code>arr = [1,<u>2,3</u>,4]</code>, the median is <code>(2 + 3) / 2 = 2.5</code>.</li> </ul> <p>You are given an integer array <code>nums</code> and an integer <code>k</code>. There is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the median array for each window in the original array</em>. Answers within <code>10<sup>-5</sup></code> of the actual value will be accepted.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,-1,-3,5,3,6,7], k = 3 <strong>Output:</strong> [1.00000,-1.00000,-1.00000,3.00000,5.00000,6.00000] <strong>Explanation:</strong> Window position Median --------------- ----- [<strong>1 3 -1</strong>] -3 5 3 6 7 1 1 [<strong>3 -1 -3</strong>] 5 3 6 7 -1 1 3 [<strong>-1 -3 5</strong>] 3 6 7 -1 1 3 -1 [<strong>-3 5 3</strong>] 6 7 3 1 3 -1 -3 [<strong>5 3 6</strong>] 7 5 1 3 -1 -3 5 [<strong>3 6 7</strong>] 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,2,3,1,4,2], k = 3 <strong>Output:</strong> [2.00000,3.00000,3.00000,3.00000,2.00000,3.00000,2.00000] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= k &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul>
0
{ "code": "\n\nclass Solution {\npublic:\n priority_queue< pair<int, int> > q_left;\n priority_queue< pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>> > q_right;\n int left = 0, right = 0;\n vector<int> where;\n\n void clean(int l){\n // cout << \"Cleaning\" << endl;\n while(q_right.top().second < l) {\n // display();\n q_right.pop();\n }\n while(q_left.top().second < l) {\n q_left.pop();\n // display();\n }\n // cout << \"cleaned\" << endl;\n }\n\n\n void push(vector<int> & nums, int l, int r){\n // cout << \"Pushing \" << nums[r+1] << \" after receiving left: \"<<left << \" right: \" << right << endl;\n\n if (left < right) {\n q_left.push({nums[r+1], r+1});\n where[r+1] = -1;\n left++;\n }\n else {\n q_right.push({nums[r+1], r+1});\n where[r+1] = 1;\n right++;\n }\n clean(l);\n if (q_left.top().first > q_right.top().first) {\n where[q_left.top().second] = 1;\n where[q_right.top().second] = -1;\n q_left.push(q_right.top());\n q_right.push(q_left.top());\n q_left.pop();\n q_right.pop();\n }\n // cout << \"Pushed\" << endl;\n }\n\n void display(){\n priority_queue< pair<int, int> > temp_q_left;\n priority_queue< pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>> > temp_q_right;\n cout << \"q_left: \";\n while(!q_left.empty()){\n cout << \"(\" << q_left.top().first << \" \" << q_left.top().second << \") \";\n temp_q_left.push(q_left.top());\n q_left.pop();\n }\n cout << endl;\n cout << \"q_right: \";\n while(!q_right.empty()){\n cout << \"(\" << q_right.top().first << \" \" << q_right.top().second << \") \";\n temp_q_right.push(q_right.top());\n q_right.pop();\n }\n cout << endl;\n while(!temp_q_left.empty()) {\n q_left.push(temp_q_left.top());\n temp_q_left.pop();\n }\n while(!temp_q_right.empty()) {\n q_right.push(temp_q_right.top());\n temp_q_right.pop();\n }\n }\n\n vector<double> medianSlidingWindow(vector<int>& nums, int k) {\n int n = nums.size();\n vector<double> ans;\n if (k == 1) {\n for (int i = 0; i < n; i++) ans.push_back(nums[i]);\n return ans;\n }\n\n where.resize(n, 0);\n q_left.push({nums[0], 0});\n where[0] = -1;\n left++;\n for (int i = 1; i < k; i++) push(nums, 0, i-1);\n int l = 0, r = k-1;\n while(r < n){\n \n clean(l);\n \n if (left < right) ans.push_back(q_right.top().first);\n else if (left > right) ans.push_back(q_left.top().first);\n else ans.push_back(((double)(q_left.top().first) + q_right.top().first) / 2.0);\n if (where[l] == -1) left--;\n else if (where[l] == 1) right--;\n else {\n cout << \"aborted\";\n exit(1);\n }\n if (r + 1 < n) push(nums, l, r);\n l++; r++;\n // cout << \"while exit\" << endl;\n // cout << \"left: \" << left << \" right: \"<<right << endl;\n // display();\n }\n return ans;\n\n }\n};\n\n\n", "memory": "36350" }
480
<p>The <strong>median</strong> is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle values.</p> <ul> <li>For examples, if <code>arr = [2,<u>3</u>,4]</code>, the median is <code>3</code>.</li> <li>For examples, if <code>arr = [1,<u>2,3</u>,4]</code>, the median is <code>(2 + 3) / 2 = 2.5</code>.</li> </ul> <p>You are given an integer array <code>nums</code> and an integer <code>k</code>. There is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the median array for each window in the original array</em>. Answers within <code>10<sup>-5</sup></code> of the actual value will be accepted.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,-1,-3,5,3,6,7], k = 3 <strong>Output:</strong> [1.00000,-1.00000,-1.00000,3.00000,5.00000,6.00000] <strong>Explanation:</strong> Window position Median --------------- ----- [<strong>1 3 -1</strong>] -3 5 3 6 7 1 1 [<strong>3 -1 -3</strong>] 5 3 6 7 -1 1 3 [<strong>-1 -3 5</strong>] 3 6 7 -1 1 3 -1 [<strong>-3 5 3</strong>] 6 7 3 1 3 -1 -3 [<strong>5 3 6</strong>] 7 5 1 3 -1 -3 5 [<strong>3 6 7</strong>] 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,2,3,1,4,2], k = 3 <strong>Output:</strong> [2.00000,3.00000,3.00000,3.00000,2.00000,3.00000,2.00000] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= k &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul>
0
{ "code": "\n\nclass Solution {\npublic:\n priority_queue< pair<int, int> > q_left;\n priority_queue< pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>> > q_right;\n int left = 0, right = 0;\n vector<int> where;\n\n void clean(int l){\n // cout << \"Cleaning\" << endl;\n while(q_right.top().second < l) {\n // display();\n q_right.pop();\n }\n while(q_left.top().second < l) {\n q_left.pop();\n // display();\n }\n // cout << \"cleaned\" << endl;\n }\n\n\n void push(vector<int> & nums, int l, int r){\n // cout << \"Pushing \" << nums[r+1] << \" after receiving left: \"<<left << \" right: \" << right << endl;\n\n if (left < right) {\n q_left.push({nums[r+1], r+1});\n where[r+1] = -1;\n left++;\n }\n else {\n q_right.push({nums[r+1], r+1});\n where[r+1] = 1;\n right++;\n }\n clean(l);\n if (q_left.top().first > q_right.top().first) {\n where[q_left.top().second] = 1;\n where[q_right.top().second] = -1;\n q_left.push(q_right.top());\n q_right.push(q_left.top());\n q_left.pop();\n q_right.pop();\n }\n // cout << \"Pushed\" << endl;\n }\n\n void display(){\n priority_queue< pair<int, int> > temp_q_left;\n priority_queue< pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>> > temp_q_right;\n cout << \"q_left: \";\n while(!q_left.empty()){\n cout << \"(\" << q_left.top().first << \" \" << q_left.top().second << \") \";\n temp_q_left.push(q_left.top());\n q_left.pop();\n }\n cout << endl;\n cout << \"q_right: \";\n while(!q_right.empty()){\n cout << \"(\" << q_right.top().first << \" \" << q_right.top().second << \") \";\n temp_q_right.push(q_right.top());\n q_right.pop();\n }\n cout << endl;\n while(!temp_q_left.empty()) {\n q_left.push(temp_q_left.top());\n temp_q_left.pop();\n }\n while(!temp_q_right.empty()) {\n q_right.push(temp_q_right.top());\n temp_q_right.pop();\n }\n }\n\n vector<double> medianSlidingWindow(vector<int>& nums, int k) {\n int n = nums.size();\n vector<double> ans;\n if (k == 1) {\n for (int i = 0; i < n; i++) ans.push_back(nums[i]);\n return ans;\n }\n\n where.resize(n, 0);\n q_left.push({nums[0], 0});\n where[0] = -1;\n left++;\n for (int i = 1; i < k; i++) push(nums, 0, i-1);\n int l = 0, r = k-1;\n while(r < n){\n \n clean(l);\n \n if (left < right) ans.push_back(q_right.top().first);\n else if (left > right) ans.push_back(q_left.top().first);\n else ans.push_back(((double)(q_left.top().first) + q_right.top().first) / 2.0);\n if (where[l] == -1) left--;\n else if (where[l] == 1) right--;\n else {\n cout << \"aborted\";\n exit(1);\n }\n if (r + 1 < n) push(nums, l, r);\n l++; r++;\n // cout << \"while exit\" << endl;\n // cout << \"left: \" << left << \" right: \"<<right << endl;\n // display();\n }\n return ans;\n\n }\n};\n\n\n", "memory": "36350" }
480
<p>The <strong>median</strong> is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle values.</p> <ul> <li>For examples, if <code>arr = [2,<u>3</u>,4]</code>, the median is <code>3</code>.</li> <li>For examples, if <code>arr = [1,<u>2,3</u>,4]</code>, the median is <code>(2 + 3) / 2 = 2.5</code>.</li> </ul> <p>You are given an integer array <code>nums</code> and an integer <code>k</code>. There is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the median array for each window in the original array</em>. Answers within <code>10<sup>-5</sup></code> of the actual value will be accepted.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,-1,-3,5,3,6,7], k = 3 <strong>Output:</strong> [1.00000,-1.00000,-1.00000,3.00000,5.00000,6.00000] <strong>Explanation:</strong> Window position Median --------------- ----- [<strong>1 3 -1</strong>] -3 5 3 6 7 1 1 [<strong>3 -1 -3</strong>] 5 3 6 7 -1 1 3 [<strong>-1 -3 5</strong>] 3 6 7 -1 1 3 -1 [<strong>-3 5 3</strong>] 6 7 3 1 3 -1 -3 [<strong>5 3 6</strong>] 7 5 1 3 -1 -3 5 [<strong>3 6 7</strong>] 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,2,3,1,4,2], k = 3 <strong>Output:</strong> [2.00000,3.00000,3.00000,3.00000,2.00000,3.00000,2.00000] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= k &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n\n void replace(vector<int>& rsq, int mod, int i, int x) {\n i += mod;\n rsq[i] = x;\n i /= 2;\n while (i > 0) {\n rsq[i] = rsq[i + i] + rsq[i + i + 1];\n i /= 2;\n }\n }\n\n int getsum(vector<int>& rsq, int mod, int l, int r) {\n int i = l + mod, j = r + mod, ans = 0;\n while (i <= j) {\n if (i % 2 == 1) ans += rsq[i];\n if (j % 2 == 0) ans += rsq[j];\n i = (i + 1) / 2;\n j = (j - 1) / 2;\n }\n return ans;\n }\n\n int get(vector<int>& rsq, int mod, int n, int id) {\n int l = 0, r = n - 1;\n while (r - l > 1) {\n int mid = (l + r) / 2;\n if (getsum(rsq, mod, 0, mid) >= id) r = mid; else l = mid;\n }\n if (getsum(rsq, mod, 0, l) >= id) return l;\n return r;\n }\n\n vector<double> medianSlidingWindow(vector<int>& nums, int k) {\n int n = nums.size();\n vector<pair<int, int> > a;\n for (int i = 0; i < nums.size(); ++i) {\n a.push_back({nums[i], i});\n }\n sort(a.begin(), a.end());\n vector<int> ids(n);\n for (int i = 0; i < n; ++i) ids[a[i].second] = i;\n vector<int> rsq(2 * n + 2, 0);\n int mod = n + 1;\n for (int i = 0; i < k; ++i) replace(rsq, mod, ids[i], 1);\n vector<double> answer;\n for (int i = k; i < n; ++i) {\n if (k & 1) {\n answer.push_back(a[get(rsq, mod, n, (k + 1) / 2)].first);\n } else {\n answer.push_back(0.5 * (1. * a[get(rsq, mod, n, k / 2)].first + a[get(rsq, mod, n, k / 2 + 1)].first));\n }\n replace(rsq, mod, ids[i], 1);\n replace(rsq, mod, ids[i - k], 0);\n }\n if (k & 1) {\n answer.push_back(a[get(rsq, mod, n, (k + 1) / 2)].first);\n } else {\n answer.push_back(0.5 * (1. * a[get(rsq, mod, n, k / 2)].first + a[get(rsq, mod, n, k / 2 + 1)].first));\n }\n return answer;\n }\n};", "memory": "36730" }
480
<p>The <strong>median</strong> is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle values.</p> <ul> <li>For examples, if <code>arr = [2,<u>3</u>,4]</code>, the median is <code>3</code>.</li> <li>For examples, if <code>arr = [1,<u>2,3</u>,4]</code>, the median is <code>(2 + 3) / 2 = 2.5</code>.</li> </ul> <p>You are given an integer array <code>nums</code> and an integer <code>k</code>. There is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the median array for each window in the original array</em>. Answers within <code>10<sup>-5</sup></code> of the actual value will be accepted.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,-1,-3,5,3,6,7], k = 3 <strong>Output:</strong> [1.00000,-1.00000,-1.00000,3.00000,5.00000,6.00000] <strong>Explanation:</strong> Window position Median --------------- ----- [<strong>1 3 -1</strong>] -3 5 3 6 7 1 1 [<strong>3 -1 -3</strong>] 5 3 6 7 -1 1 3 [<strong>-1 -3 5</strong>] 3 6 7 -1 1 3 -1 [<strong>-3 5 3</strong>] 6 7 3 1 3 -1 -3 [<strong>5 3 6</strong>] 7 5 1 3 -1 -3 5 [<strong>3 6 7</strong>] 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,2,3,1,4,2], k = 3 <strong>Output:</strong> [2.00000,3.00000,3.00000,3.00000,2.00000,3.00000,2.00000] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= k &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n Solution() {\n std::ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n }\n\n double median(int a, int b, int k) {\n if (k % 2) return a;\n double d = static_cast<double>(a) + b;\n return d / 2.0f;\n }\n\n std::vector<double> medianSlidingWindow(const std::vector<int>& nums, const int k) {\n \n if (nums.empty()) return {};\n const int N = nums.size();\n\n std::vector<double> out(N - k + 1);\n if (k == 1) {\n for (int i = 0; i < N; ++i)\n out[i] = nums[i];\n return out;\n }\n \n std::priority_queue<int> lHeap;\n std::priority_queue<int, std::vector<int>, std::greater<int>> rHeap;\n std::unordered_map<int,int> deleted;\n\n\n for (int i = 0; i < k; ++i) {\n lHeap.push(nums[i]);\n }\n\n while (rHeap.size() < k / 2) {\n rHeap.push(lHeap.top());\n lHeap.pop();\n }\n\n out[0] = median(lHeap.top(), rHeap.top(), k);\n int balance = 0;\n\n for (int i = 0; i < N - k; ++i) {\n int oelm = nums[i];\n int nelm = nums[i + k];\n\n // delete the old element replace it with the new one\n if (lHeap.top() == oelm) {\n lHeap.pop();\n lHeap.push(nelm);\n } \n else if (rHeap.top() == oelm) {\n rHeap.pop();\n rHeap.push(nelm);\n }\n else {\n // if the element is not on top save it for later\n if (deleted.count(oelm))\n deleted[oelm]++;\n else \n deleted[oelm] = 1;\n if (oelm > rHeap.top()) rHeap.push(nelm);\n else lHeap.push(nelm);\n }\n\n // balance the heads\n if (lHeap.top() > rHeap.top()) {\n auto l = lHeap.top();\n auto r = rHeap.top();\n lHeap.pop();\n rHeap.pop();\n lHeap.push(r);\n rHeap.push(l);\n }\n\n // remove previously deleted heads\n while (!lHeap.empty() && deleted.count(lHeap.top()) && deleted[lHeap.top()] > 0) {\n deleted[lHeap.top()]--;\n lHeap.pop();\n }\n\n while (!rHeap.empty() && deleted.count(rHeap.top()) && deleted[rHeap.top()] > 0) {\n deleted[rHeap.top()]--;\n rHeap.pop();\n }\n\n out[i + 1] = median(lHeap.top(), rHeap.top(), k);\n }\n\n return out;\n }\n};", "memory": "37110" }
480
<p>The <strong>median</strong> is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle values.</p> <ul> <li>For examples, if <code>arr = [2,<u>3</u>,4]</code>, the median is <code>3</code>.</li> <li>For examples, if <code>arr = [1,<u>2,3</u>,4]</code>, the median is <code>(2 + 3) / 2 = 2.5</code>.</li> </ul> <p>You are given an integer array <code>nums</code> and an integer <code>k</code>. There is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the median array for each window in the original array</em>. Answers within <code>10<sup>-5</sup></code> of the actual value will be accepted.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,-1,-3,5,3,6,7], k = 3 <strong>Output:</strong> [1.00000,-1.00000,-1.00000,3.00000,5.00000,6.00000] <strong>Explanation:</strong> Window position Median --------------- ----- [<strong>1 3 -1</strong>] -3 5 3 6 7 1 1 [<strong>3 -1 -3</strong>] 5 3 6 7 -1 1 3 [<strong>-1 -3 5</strong>] 3 6 7 -1 1 3 -1 [<strong>-3 5 3</strong>] 6 7 3 1 3 -1 -3 [<strong>5 3 6</strong>] 7 5 1 3 -1 -3 5 [<strong>3 6 7</strong>] 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,2,3,1,4,2], k = 3 <strong>Output:</strong> [2.00000,3.00000,3.00000,3.00000,2.00000,3.00000,2.00000] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= k &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul>
0
{ "code": "class Solution {\nprivate:\n priority_queue<int> max_heap;\n priority_queue<int, vector<int>, greater<int>> min_heap;\n\n int cnt_max_heap;\n int cnt_min_heap;\n\n unordered_map<int,int> removed_elems;\n\npublic:\n Solution() {\n cnt_max_heap = 0;\n cnt_min_heap = 0;\n }\n\n void addNum(int num) { \n if (cnt_min_heap == 0) {\n max_heap.push(num);\n cnt_max_heap++;\n }\n else if (num >= min_heap.top()) {\n min_heap.push(num);\n cnt_min_heap++;\n }\n else {\n max_heap.push(num);\n cnt_max_heap++;\n }\n\n if (cnt_max_heap - cnt_min_heap >= 2) {\n // while (removed_elems.find(max_heap.top()) != removed_elems.end() && removed_elems[max_heap.top()] > 0) {\n // removed_elems[max_heap.top()]--;\n // max_heap.pop();\n // }\n min_heap.push(max_heap.top());\n max_heap.pop();\n cnt_max_heap--;\n cnt_min_heap++;\n }\n else if (cnt_min_heap - cnt_max_heap >= 2) {\n // while (removed_elems.find(min_heap.top()) != removed_elems.end() && removed_elems[min_heap.top()] > 0) {\n // removed_elems[min_heap.top()]--;\n // min_heap.pop();\n // }\n max_heap.push(min_heap.top());\n min_heap.pop();\n cnt_max_heap++;\n cnt_min_heap--;\n }\n }\n\n void removeNum(int num) {\n removed_elems[num]++;\n if (cnt_min_heap > 0 && num >= min_heap.top()) {\n cnt_min_heap--;\n }\n else {\n cnt_max_heap--;\n }\n }\n\n double findMedian() {\n while (!min_heap.empty() && removed_elems.find(min_heap.top()) != removed_elems.end() && removed_elems[min_heap.top()] > 0) {\n removed_elems[min_heap.top()]--;\n min_heap.pop();\n }\n while (!max_heap.empty() && removed_elems.find(max_heap.top()) != removed_elems.end() && removed_elems[max_heap.top()] > 0) {\n removed_elems[max_heap.top()]--;\n max_heap.pop();\n }\n\n if (cnt_max_heap > cnt_min_heap) {\n return max_heap.top();\n }\n else if (cnt_min_heap > cnt_max_heap) {\n return min_heap.top();\n }\n else {\n return ((double)max_heap.top() + (double)min_heap.top()) / 2;\n }\n }\n\n vector<double> medianSlidingWindow(vector<int>& nums, int k) {\n for (int i = 0; i < k; i++) {\n addNum(nums[i]);\n }\n\n vector<double> ret;\n ret.push_back(findMedian());\n\n int left = 0, right = k;\n while (right < nums.size()) {\n removeNum(nums[left++]);\n addNum(nums[right++]);\n ret.push_back(findMedian());\n }\n\n return ret;\n }\n};\n\n", "memory": "37490" }
480
<p>The <strong>median</strong> is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle values.</p> <ul> <li>For examples, if <code>arr = [2,<u>3</u>,4]</code>, the median is <code>3</code>.</li> <li>For examples, if <code>arr = [1,<u>2,3</u>,4]</code>, the median is <code>(2 + 3) / 2 = 2.5</code>.</li> </ul> <p>You are given an integer array <code>nums</code> and an integer <code>k</code>. There is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the median array for each window in the original array</em>. Answers within <code>10<sup>-5</sup></code> of the actual value will be accepted.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,-1,-3,5,3,6,7], k = 3 <strong>Output:</strong> [1.00000,-1.00000,-1.00000,3.00000,5.00000,6.00000] <strong>Explanation:</strong> Window position Median --------------- ----- [<strong>1 3 -1</strong>] -3 5 3 6 7 1 1 [<strong>3 -1 -3</strong>] 5 3 6 7 -1 1 3 [<strong>-1 -3 5</strong>] 3 6 7 -1 1 3 -1 [<strong>-3 5 3</strong>] 6 7 3 1 3 -1 -3 [<strong>5 3 6</strong>] 7 5 1 3 -1 -3 5 [<strong>3 6 7</strong>] 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,2,3,1,4,2], k = 3 <strong>Output:</strong> [2.00000,3.00000,3.00000,3.00000,2.00000,3.00000,2.00000] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= k &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n vector<double> medianSlidingWindow(vector<int>& nums, int k) {\n unordered_map<int, int> freq;\n priority_queue<int> maxPQ;\n priority_queue<int, vector<int>, greater<int>> minPQ;\n vector<double> ans;\n\n int n = nums.size();\n int i = 0;\n\n for(; i<k; i++)\n maxPQ.push(nums[i]);\n\n for(int j = 0; j < k/2; j++)\n {\n minPQ.push(maxPQ.top());\n maxPQ.pop();\n }\n\n while(true)\n {\n double median = 0;\n if(k%2 == 0)\n median = ((double)minPQ.top() + (double)maxPQ.top()) * 0.5;\n else\n median = maxPQ.top();\n\n ans.push_back(median);\n\n if(i == n)\n break;\n\n if(i >= k)\n freq[nums[i-k]]++;\n \n int in_num = nums[i];\n int out_num = nums[i-k];\n int balancer = 0;\n\n if(maxPQ.top() >= in_num)\n { \n balancer++;\n maxPQ.push(nums[i]);\n }\n else\n {\n balancer--;\n minPQ.push(nums[i]);\n }\n\n if(maxPQ.top() >= out_num)\n balancer--;\n else\n balancer++;\n\n if(balancer > 0)\n {\n minPQ.push(maxPQ.top());\n maxPQ.pop();\n }\n else if(balancer < 0)\n {\n maxPQ.push(minPQ.top());\n minPQ.pop();\n }\n \n while(!maxPQ.empty() && freq.find(maxPQ.top()) != freq.end())\n {\n freq[maxPQ.top()]--;\n if(freq[maxPQ.top()] == 0)\n freq.erase(maxPQ.top());\n maxPQ.pop();\n }\n \n while(!minPQ.empty() && freq.find(minPQ.top()) != freq.end())\n {\n freq[minPQ.top()]--;\n if(freq[minPQ.top()] == 0)\n freq.erase(minPQ.top());\n minPQ.pop();\n }\n\n i++;\n }\n\n return ans;\n }\n};", "memory": "37870" }
480
<p>The <strong>median</strong> is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle values.</p> <ul> <li>For examples, if <code>arr = [2,<u>3</u>,4]</code>, the median is <code>3</code>.</li> <li>For examples, if <code>arr = [1,<u>2,3</u>,4]</code>, the median is <code>(2 + 3) / 2 = 2.5</code>.</li> </ul> <p>You are given an integer array <code>nums</code> and an integer <code>k</code>. There is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the median array for each window in the original array</em>. Answers within <code>10<sup>-5</sup></code> of the actual value will be accepted.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,-1,-3,5,3,6,7], k = 3 <strong>Output:</strong> [1.00000,-1.00000,-1.00000,3.00000,5.00000,6.00000] <strong>Explanation:</strong> Window position Median --------------- ----- [<strong>1 3 -1</strong>] -3 5 3 6 7 1 1 [<strong>3 -1 -3</strong>] 5 3 6 7 -1 1 3 [<strong>-1 -3 5</strong>] 3 6 7 -1 1 3 -1 [<strong>-3 5 3</strong>] 6 7 3 1 3 -1 -3 [<strong>5 3 6</strong>] 7 5 1 3 -1 -3 5 [<strong>3 6 7</strong>] 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,2,3,1,4,2], k = 3 <strong>Output:</strong> [2.00000,3.00000,3.00000,3.00000,2.00000,3.00000,2.00000] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= k &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n vector<double> medianSlidingWindow(vector<int>& nums, int k) {\n vector<double> ans;\n\n int n = nums.size();\n\n priority_queue<int> maxh;\n priority_queue<int,vector<int>,greater<int>> minh;\n\n unordered_map<int,int> m;\n\n int max_len = 0, min_len = 0;\n\n for(int i=0;i<n;i++){\n cout << \"i = \" << i << \" x = \" << max_len << \" y = \" << min_len << '\\n';\n\n int len = (max_len-min_len);\n \n if(len%2){\n if(!maxh.empty() && nums[i]>=maxh.top()) minh.push(nums[i]);\n else{\n int tmp = maxh.top();\n maxh.pop();\n minh.push(tmp);\n maxh.push(nums[i]);\n }\n min_len++;\n }\n else{\n if(max_len==0 || nums[i]<=minh.top()) maxh.push(nums[i]);\n else{\n int tmp = minh.top();\n minh.pop();\n minh.push(nums[i]);\n maxh.push(tmp);\n }\n max_len++;\n }\n\n while(!maxh.empty() && m.find(maxh.top())!=m.end()){\n m.erase(maxh.top());\n maxh.pop();\n }\n\n while(!minh.empty() && m.find(minh.top())!=m.end()){\n m.erase(minh.top());\n minh.pop();\n }\n\n if(i>=k-1){\n double median;\n\n int l = i-k+1;\n\n m[nums[l]] = l;\n\n if(k%2){\n median = (double)maxh.top();\n ans.push_back(median);\n cout << median << '\\n';\n\n if(!maxh.empty() && nums[l]<maxh.top()){\n max_len--;\n continue;\n }\n else if(!minh.empty() && nums[l]>minh.top()){\n // cout << '\\n' << nums[l] << \" \" << minh.top();\n // cout << \" buj\";\n if(!maxh.empty() && m.find(maxh.top())!=m.end()){\n m.erase(maxh.top());\n maxh.pop();\n }\n else if(!maxh.empty()){\n int tmp = maxh.top();\n maxh.pop();\n minh.push(tmp);\n max_len--;\n }\n }\n else{\n // cout << \" in\";\n if(!minh.empty() && minh.top()==nums[l]){\n if(!maxh.empty() && m.find(maxh.top())!=m.end()){\n m.erase(maxh.top());\n maxh.pop();\n }\n else if(!maxh.empty()){\n int tmp = maxh.top();\n maxh.pop();\n minh.push(tmp);\n }\n }\n max_len--;\n }\n\n }\n else{\n double x = (double)maxh.top();\n double y = (double)minh.top();\n\n median = (x+y)/2.0;\n\n ans.push_back(median);\n\n cout << median << '\\n';\n\n if(!maxh.empty() && nums[l]<maxh.top()){\n if(!minh.empty() && m.find(minh.top())!=m.end()){\n m.erase(minh.top());\n minh.pop();\n }\n else if(!minh.empty()){\n int tmp = minh.top();\n minh.pop();\n maxh.push(tmp);\n }\n min_len--;\n }\n else if(!minh.empty() && nums[l]>minh.top()){\n min_len--;\n continue;\n }\n else{\n if(!minh.empty() && m.find(minh.top())!=m.end()){\n m.erase(minh.top());\n minh.pop();\n }\n else if(!minh.empty()){\n int tmp = minh.top();\n minh.pop();\n maxh.push(tmp);\n }\n min_len--;\n\n cout << \"max = \" << maxh.top() << \" \" << \" min = \" << minh.top() << '\\n';\n }\n\n\n }\n \n }\n }\n\n return ans;\n }\n};", "memory": "38250" }
480
<p>The <strong>median</strong> is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle values.</p> <ul> <li>For examples, if <code>arr = [2,<u>3</u>,4]</code>, the median is <code>3</code>.</li> <li>For examples, if <code>arr = [1,<u>2,3</u>,4]</code>, the median is <code>(2 + 3) / 2 = 2.5</code>.</li> </ul> <p>You are given an integer array <code>nums</code> and an integer <code>k</code>. There is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the median array for each window in the original array</em>. Answers within <code>10<sup>-5</sup></code> of the actual value will be accepted.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,-1,-3,5,3,6,7], k = 3 <strong>Output:</strong> [1.00000,-1.00000,-1.00000,3.00000,5.00000,6.00000] <strong>Explanation:</strong> Window position Median --------------- ----- [<strong>1 3 -1</strong>] -3 5 3 6 7 1 1 [<strong>3 -1 -3</strong>] 5 3 6 7 -1 1 3 [<strong>-1 -3 5</strong>] 3 6 7 -1 1 3 -1 [<strong>-3 5 3</strong>] 6 7 3 1 3 -1 -3 [<strong>5 3 6</strong>] 7 5 1 3 -1 -3 5 [<strong>3 6 7</strong>] 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,2,3,1,4,2], k = 3 <strong>Output:</strong> [2.00000,3.00000,3.00000,3.00000,2.00000,3.00000,2.00000] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= k &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n vector<double> medianSlidingWindow(vector<int>& nums, int k) {\n priority_queue<int> small;\n priority_queue<int, vector<int>, greater<int>> large;\n\n int i = 0, count = k;\n while (count--) {\n addNum(nums[i], small, large);\n i++;\n }\n\n smallSize = small.size();\n largeSize = large.size();\n\n unordered_map<int, int> toRemoveSmall;\n unordered_map<int, int> toRemoveLarge;\n\n vector<double> ans;\n\n i = 0;\n int j = k - 1;\n while (j < nums.size()) {\n while (!small.empty() && toRemoveSmall.contains(small.top())) {\n toRemoveSmall[small.top()]--;\n if (toRemoveSmall[small.top()] <= 0)\n toRemoveSmall.erase(small.top());\n small.pop();\n }\n while (!large.empty() && toRemoveLarge.contains(large.top())) {\n toRemoveLarge[large.top()]--;\n if (toRemoveLarge[large.top()] <= 0)\n toRemoveLarge.erase(large.top());\n large.pop();\n }\n\n if (k % 2) {\n if (smallSize > largeSize)\n ans.push_back((double)small.top());\n else if (largeSize > smallSize)\n ans.push_back((double)large.top());\n } else\n ans.push_back(((double)small.top() + (double)large.top()) / 2);\n\n if (nums[i] == small.top()) {\n small.pop();\n smallSize--;\n }\n else if (nums[i] == large.top()) {\n large.pop();\n largeSize--;\n }\n else if (nums[i] < small.top()) {\n smallSize--;\n toRemoveSmall[nums[i]]++;\n } else if (nums[i] > large.top()) {\n largeSize--;\n toRemoveLarge[nums[i]]++;\n }\n\n i++;\n j++;\n\n if (j < nums.size())\n addNum(nums[j], small, large);\n }\n\n return ans;\n }\n\nprivate:\n int smallSize = 0, largeSize = 0;\n\n void addNum(int num, priority_queue<int>& small, priority_queue<int, vector<int>, greater<int>>& large) {\n small.push(num);\n smallSize++;\n\n if (!large.empty() && large.top() < small.top()) {\n large.push(small.top());\n small.pop();\n smallSize--;\n largeSize++;\n }\n\n if ((int)largeSize - (int)smallSize > 1) {\n small.push(large.top());\n large.pop();\n smallSize++;\n largeSize--;\n }\n if ((int)smallSize - (int)largeSize > 1) {\n large.push(small.top());\n small.pop();\n smallSize--;\n largeSize++;\n }\n }\n};", "memory": "38630" }
480
<p>The <strong>median</strong> is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle values.</p> <ul> <li>For examples, if <code>arr = [2,<u>3</u>,4]</code>, the median is <code>3</code>.</li> <li>For examples, if <code>arr = [1,<u>2,3</u>,4]</code>, the median is <code>(2 + 3) / 2 = 2.5</code>.</li> </ul> <p>You are given an integer array <code>nums</code> and an integer <code>k</code>. There is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the median array for each window in the original array</em>. Answers within <code>10<sup>-5</sup></code> of the actual value will be accepted.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,-1,-3,5,3,6,7], k = 3 <strong>Output:</strong> [1.00000,-1.00000,-1.00000,3.00000,5.00000,6.00000] <strong>Explanation:</strong> Window position Median --------------- ----- [<strong>1 3 -1</strong>] -3 5 3 6 7 1 1 [<strong>3 -1 -3</strong>] 5 3 6 7 -1 1 3 [<strong>-1 -3 5</strong>] 3 6 7 -1 1 3 -1 [<strong>-3 5 3</strong>] 6 7 3 1 3 -1 -3 [<strong>5 3 6</strong>] 7 5 1 3 -1 -3 5 [<strong>3 6 7</strong>] 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,2,3,1,4,2], k = 3 <strong>Output:</strong> [2.00000,3.00000,3.00000,3.00000,2.00000,3.00000,2.00000] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= k &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul>
0
{ "code": "\nclass Solution {\n void heap_move(priority_queue<pair<double, long>>& h1,priority_queue<pair<double, long>>& h2) {\n auto [x, i] = h1.top();\n h1.pop();\n h2.emplace(-x, i);\n }\n double get_median(priority_queue<pair<double, long>>& small, priority_queue<pair<double, long>>& large, int k) {\n if (k % 2 == 1) {\n return -large.top().first;\n }\n return (small.top().first-large.top().first) / 2.0;\n }\npublic:\n vector<double> medianSlidingWindow(vector<int>& nums, int k) {\n\n priority_queue<pair<double, long>> small;\n priority_queue<pair<double, long>> large;\n for (long i = 0; i < k; i++) {\n small.emplace(nums[i], -i);\n }\n // ensure that large is bigger\n for (long i = 0; i < (k+1) / 2; i++) {\n heap_move(small, large);\n }\n\n vector<double> ans = {get_median(small, large, k)};\n for (long i = k; i < nums.size(); i++) {\n long x = nums[i];\n if (x >= -large.top().first) {\n large.emplace(-x, -i);\n if (nums[i-k] <= -large.top().first) {\n heap_move(large, small);\n }\n } else {\n small.emplace(x, -i);\n if (nums[i-k] >= -large.top().first) {\n heap_move(small, large);\n }\n }\n while (!small.empty() && -small.top().second <= i - k) {\n small.pop();\n }\n while (!large.empty() && -large.top().second <= i - k) {\n large.pop();\n }\n ans.push_back(get_median(small, large, k));\n }\n return ans;\n }\n};", "memory": "39010" }
480
<p>The <strong>median</strong> is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle values.</p> <ul> <li>For examples, if <code>arr = [2,<u>3</u>,4]</code>, the median is <code>3</code>.</li> <li>For examples, if <code>arr = [1,<u>2,3</u>,4]</code>, the median is <code>(2 + 3) / 2 = 2.5</code>.</li> </ul> <p>You are given an integer array <code>nums</code> and an integer <code>k</code>. There is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the median array for each window in the original array</em>. Answers within <code>10<sup>-5</sup></code> of the actual value will be accepted.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,-1,-3,5,3,6,7], k = 3 <strong>Output:</strong> [1.00000,-1.00000,-1.00000,3.00000,5.00000,6.00000] <strong>Explanation:</strong> Window position Median --------------- ----- [<strong>1 3 -1</strong>] -3 5 3 6 7 1 1 [<strong>3 -1 -3</strong>] 5 3 6 7 -1 1 3 [<strong>-1 -3 5</strong>] 3 6 7 -1 1 3 -1 [<strong>-3 5 3</strong>] 6 7 3 1 3 -1 -3 [<strong>5 3 6</strong>] 7 5 1 3 -1 -3 5 [<strong>3 6 7</strong>] 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,2,3,1,4,2], k = 3 <strong>Output:</strong> [2.00000,3.00000,3.00000,3.00000,2.00000,3.00000,2.00000] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= k &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul>
0
{ "code": "class Solution {\nprivate:\n priority_queue<int> maxHeap;\n\n priority_queue<int, vector<int>, greater<int>> minHeap;\n\n map<int, int> toRemoveNumM;\n\n int maxHeapSize = 0;\n\n int minHeapSize = 0;\n \n void insertNum(int num);\n\n void removeNum(int num);\n\n void balance();\n\n void prune();\n\n double getMedian();\npublic:\n vector<double> medianSlidingWindow(vector<int>& nums, int k);\n};\n\nvoid Solution::prune()\n{\n while(minHeap.size())\n {\n int top = minHeap.top();\n if(toRemoveNumM.find(top) == toRemoveNumM.end())\n {\n break;\n }\n toRemoveNumM[top] -= 1;\n if(toRemoveNumM[top] == 0) toRemoveNumM.erase(top);\n minHeap.pop();\n }\n while(maxHeap.size())\n {\n int top = maxHeap.top();\n if(toRemoveNumM.find(top) == toRemoveNumM.end())\n {\n break;\n }\n toRemoveNumM[top] -= 1;\n if(toRemoveNumM[top] == 0) toRemoveNumM.erase(top);\n maxHeap.pop();\n }\n return;\n}\n\nvoid Solution::balance()\n{\n while(minHeapSize >= (maxHeapSize + 2))\n {\n int top = minHeap.top();\n minHeap.pop();\n maxHeap.push(top);\n minHeapSize--;\n maxHeapSize++;\n prune();\n balance();\n }\n\n while(maxHeapSize > minHeapSize)\n {\n int top = maxHeap.top();\n maxHeap.pop();\n minHeap.push(top);\n maxHeapSize--;\n minHeapSize++;\n prune();\n balance();\n }\n return;\n}\n\nvoid Solution::removeNum(int num)\n{\n if(num >= minHeap.top())\n {\n minHeapSize--;\n }\n else\n {\n maxHeapSize--;\n }\n toRemoveNumM[num] += 1;\n prune();\n balance();\n return;\n}\n\nvoid Solution::insertNum(int num)\n{\n if(minHeap.size() <= 0)\n {\n minHeap.push(num);\n minHeapSize++;\n return;\n }\n if(num >= minHeap.top())\n {\n minHeap.push(num);\n minHeapSize++;\n }\n else\n {\n maxHeap.push(num);\n maxHeapSize++;\n }\n balance();\n return;\n}\n\ndouble Solution::getMedian()\n{\n if(minHeapSize > maxHeapSize) return (double)minHeap.top();\n return ((double)minHeap.top() + (double)maxHeap.top()) / 2;\n}\n\nvector<double> Solution::medianSlidingWindow(vector<int>& nums, int k) \n{\n int size = nums.size();\n vector<double> result;\n for(int i = 0; i < k; i++)\n {\n insertNum(nums[i]);\n }\n double median = getMedian();\n result.push_back(median);\n for(int i = k; i < size; i++)\n {\n removeNum(nums[i - k]);\n insertNum(nums[i]);\n median = getMedian();\n result.push_back(median);\n }\n return result;\n}", "memory": "39390" }
480
<p>The <strong>median</strong> is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle values.</p> <ul> <li>For examples, if <code>arr = [2,<u>3</u>,4]</code>, the median is <code>3</code>.</li> <li>For examples, if <code>arr = [1,<u>2,3</u>,4]</code>, the median is <code>(2 + 3) / 2 = 2.5</code>.</li> </ul> <p>You are given an integer array <code>nums</code> and an integer <code>k</code>. There is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the median array for each window in the original array</em>. Answers within <code>10<sup>-5</sup></code> of the actual value will be accepted.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,-1,-3,5,3,6,7], k = 3 <strong>Output:</strong> [1.00000,-1.00000,-1.00000,3.00000,5.00000,6.00000] <strong>Explanation:</strong> Window position Median --------------- ----- [<strong>1 3 -1</strong>] -3 5 3 6 7 1 1 [<strong>3 -1 -3</strong>] 5 3 6 7 -1 1 3 [<strong>-1 -3 5</strong>] 3 6 7 -1 1 3 -1 [<strong>-3 5 3</strong>] 6 7 3 1 3 -1 -3 [<strong>5 3 6</strong>] 7 5 1 3 -1 -3 5 [<strong>3 6 7</strong>] 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,2,3,1,4,2], k = 3 <strong>Output:</strong> [2.00000,3.00000,3.00000,3.00000,2.00000,3.00000,2.00000] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= k &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul>
0
{ "code": "class Solution {\nprivate:\n priority_queue<int> maxHeap;\n\n priority_queue<int, vector<int>, greater<int>> minHeap;\n\n map<int, int> toRemoveNumM;\n\n int maxHeapSize = 0;\n\n int minHeapSize = 0;\n \n void insertNum(int num);\n\n void removeNum(int num);\n\n void balance();\n\n void prune();\n\n double getMedian();\npublic:\n vector<double> medianSlidingWindow(vector<int>& nums, int k);\n};\n\nvoid Solution::prune()\n{\n while(minHeap.size())\n {\n int top = minHeap.top();\n if(toRemoveNumM.find(top) == toRemoveNumM.end())\n {\n break;\n }\n toRemoveNumM[top] -= 1;\n if(toRemoveNumM[top] == 0) toRemoveNumM.erase(top);\n minHeap.pop();\n }\n while(maxHeap.size())\n {\n int top = maxHeap.top();\n if(toRemoveNumM.find(top) == toRemoveNumM.end())\n {\n break;\n }\n toRemoveNumM[top] -= 1;\n if(toRemoveNumM[top] == 0) toRemoveNumM.erase(top);\n maxHeap.pop();\n }\n return;\n}\n\nvoid Solution::balance()\n{\n while(minHeapSize >= (maxHeapSize + 2))\n {\n int top = minHeap.top();\n minHeap.pop();\n maxHeap.push(top);\n minHeapSize--;\n maxHeapSize++;\n prune();\n balance();\n }\n\n while(maxHeapSize > minHeapSize)\n {\n int top = maxHeap.top();\n maxHeap.pop();\n minHeap.push(top);\n maxHeapSize--;\n minHeapSize++;\n prune();\n balance();\n }\n return;\n}\n\nvoid Solution::removeNum(int num)\n{\n if(num >= minHeap.top())\n {\n minHeapSize--;\n }\n else\n {\n maxHeapSize--;\n }\n toRemoveNumM[num] += 1;\n prune();\n balance();\n return;\n}\n\nvoid Solution::insertNum(int num)\n{\n if(minHeap.size() <= 0)\n {\n minHeap.push(num);\n minHeapSize++;\n return;\n }\n if(num >= minHeap.top())\n {\n minHeap.push(num);\n minHeapSize++;\n }\n else\n {\n maxHeap.push(num);\n maxHeapSize++;\n }\n balance();\n return;\n}\n\ndouble Solution::getMedian()\n{\n if(minHeapSize > maxHeapSize) return (double)minHeap.top();\n return ((double)minHeap.top() + (double)maxHeap.top()) / 2;\n}\n\nvector<double> Solution::medianSlidingWindow(vector<int>& nums, int k) \n{\n int size = nums.size();\n vector<double> result;\n for(int i = 0; i < k; i++)\n {\n insertNum(nums[i]);\n }\n double median = getMedian();\n result.push_back(median);\n for(int i = k; i < size; i++)\n {\n removeNum(nums[i - k]);\n insertNum(nums[i]);\n median = getMedian();\n result.push_back(median);\n }\n return result;\n}", "memory": "39390" }
480
<p>The <strong>median</strong> is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle values.</p> <ul> <li>For examples, if <code>arr = [2,<u>3</u>,4]</code>, the median is <code>3</code>.</li> <li>For examples, if <code>arr = [1,<u>2,3</u>,4]</code>, the median is <code>(2 + 3) / 2 = 2.5</code>.</li> </ul> <p>You are given an integer array <code>nums</code> and an integer <code>k</code>. There is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the median array for each window in the original array</em>. Answers within <code>10<sup>-5</sup></code> of the actual value will be accepted.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,-1,-3,5,3,6,7], k = 3 <strong>Output:</strong> [1.00000,-1.00000,-1.00000,3.00000,5.00000,6.00000] <strong>Explanation:</strong> Window position Median --------------- ----- [<strong>1 3 -1</strong>] -3 5 3 6 7 1 1 [<strong>3 -1 -3</strong>] 5 3 6 7 -1 1 3 [<strong>-1 -3 5</strong>] 3 6 7 -1 1 3 -1 [<strong>-3 5 3</strong>] 6 7 3 1 3 -1 -3 [<strong>5 3 6</strong>] 7 5 1 3 -1 -3 5 [<strong>3 6 7</strong>] 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,2,3,1,4,2], k = 3 <strong>Output:</strong> [2.00000,3.00000,3.00000,3.00000,2.00000,3.00000,2.00000] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= k &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n vector<double> medianSlidingWindow(vector<int>& nums, int k) {\n //vector to store median \n vector<double> median;\n //map to delete the elements that are to be removed from sliding window \n unordered_map<int,int> mp;\n //to store the element in heap in descending order\n priority_queue<int> maxHeap;\n //to store the element in heap in ascending order\n priority_queue<int, vector<int>, greater<int>> minHeap;\n\n //first insert the element upto the size of window in maxHeap\n for(int i=0;i<k;i++)\n {\n maxHeap.push(nums[i]);\n }\n\n //Now to balance the size of maxHeap and minHeap, insert half elements in minHeap\n for(int i=0; i<k/2; i++)\n {\n minHeap.push(maxHeap.top());\n maxHeap.pop();\n }\n\n //for rest of the elements\n for(int i=k; i<nums.size(); i++)\n {\n\n if(k%2 != 0) //if k is odd\n median.push_back(maxHeap.top()*1.0);\n else // if k is even\n median.push_back((maxHeap.top()*1.0 + minHeap.top()*1.0)/2);\n\n //to remove previous element from sliding window and inserting new element and checking the balance of maxHeap and minHeap\n int p = nums[i-k], q = nums[i], balance = 0;\n\n //if p is present in maxHeap\n if(p <= maxHeap.top())\n {\n balance--;\n if(p == maxHeap.top())\n {\n maxHeap.pop();\n }\n else\n {\n mp[p]++;\n }\n }\n else //if p is present in minHeap\n {\n balance++;\n if(p == minHeap.top())\n {\n minHeap.pop();\n }\n else\n {\n mp[p]++;\n }\n }\n\n //to insert q in maxHeap when q is smaller than top of the maxHeap\n if(!maxHeap.empty() && q<=maxHeap.top())\n {\n maxHeap.push(q);\n balance++;\n }\n else //if q is greater the top of maxHeap then insert it into minHeap\n {\n minHeap.push(q);\n balance--;\n }\n \n //if balance > 0\n if(balance > 0)\n {\n minHeap.push(maxHeap.top());\n maxHeap.pop();\n } //if balance < 0\n else if(balance < 0)\n {\n maxHeap.push(minHeap.top());\n minHeap.pop();\n }\n\n while(!maxHeap.empty() && mp[maxHeap.top()])\n {\n mp[maxHeap.top()]--;\n maxHeap.pop();\n }\n while(!minHeap.empty() && mp[minHeap.top()])\n {\n mp[minHeap.top()]--;\n minHeap.pop();\n }\n }\n if(k%2 != 0)\n median.push_back(maxHeap.top()*1.0);\n else \n median.push_back((maxHeap.top()*1.0 + minHeap.top()*1.0)/2);\n return median;\n }\n};", "memory": "39770" }
480
<p>The <strong>median</strong> is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle values.</p> <ul> <li>For examples, if <code>arr = [2,<u>3</u>,4]</code>, the median is <code>3</code>.</li> <li>For examples, if <code>arr = [1,<u>2,3</u>,4]</code>, the median is <code>(2 + 3) / 2 = 2.5</code>.</li> </ul> <p>You are given an integer array <code>nums</code> and an integer <code>k</code>. There is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the median array for each window in the original array</em>. Answers within <code>10<sup>-5</sup></code> of the actual value will be accepted.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,-1,-3,5,3,6,7], k = 3 <strong>Output:</strong> [1.00000,-1.00000,-1.00000,3.00000,5.00000,6.00000] <strong>Explanation:</strong> Window position Median --------------- ----- [<strong>1 3 -1</strong>] -3 5 3 6 7 1 1 [<strong>3 -1 -3</strong>] 5 3 6 7 -1 1 3 [<strong>-1 -3 5</strong>] 3 6 7 -1 1 3 -1 [<strong>-3 5 3</strong>] 6 7 3 1 3 -1 -3 [<strong>5 3 6</strong>] 7 5 1 3 -1 -3 5 [<strong>3 6 7</strong>] 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,2,3,1,4,2], k = 3 <strong>Output:</strong> [2.00000,3.00000,3.00000,3.00000,2.00000,3.00000,2.00000] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= k &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul>
0
{ "code": "class Solution {\n public:\n vector<double> medianSlidingWindow(vector<int>& nums, int k) {\n vector<double> medians;\n unordered_map<int, int> outgoingNum;\n priority_queue<int> smallList;\n priority_queue<int, vector<int>, greater<int>> largeList;\n\n for (int i = 0; i < k; i++) {\n smallList.push(nums[i]);\n }\n\n for (int i = 0; k && i < k / 2; i++) {\n largeList.push(smallList.top());\n smallList.pop();\n }\n\n int balance = 0;\n int i = k;\n\n while (true) {\n if (k % 2 == 1) {\n medians.push_back(smallList.top());\n } else {\n medians.push_back(\n ((long long)smallList.top() + (long long)largeList.top()) *\n 0.5);\n }\n\n if (i >= nums.size()) {\n break;\n }\n\n int outNum = nums[i - k];\n int inNum = nums[i];\n i++;\n\n if (outNum <= smallList.top()) {\n balance -= 1;\n } else {\n balance += 1;\n }\n\n outgoingNum[outNum]++;\n\n if (!smallList.empty() && inNum <= smallList.top()) {\n balance += 1;\n smallList.push(inNum);\n } else {\n balance -= 1;\n largeList.push(inNum);\n }\n\n if (balance < 0) {\n smallList.push(largeList.top());\n largeList.pop();\n } else if (balance > 0) {\n largeList.push(smallList.top());\n smallList.pop();\n }\n\n balance = 0;\n\n while (!smallList.empty() && outgoingNum[smallList.top()]) {\n outgoingNum[smallList.top()]--;\n smallList.pop();\n }\n\n while (!largeList.empty() && outgoingNum[largeList.top()]) {\n outgoingNum[largeList.top()]--;\n largeList.pop();\n }\n }\n\n return medians;\n }\n };\n", "memory": "39770" }
480
<p>The <strong>median</strong> is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle values.</p> <ul> <li>For examples, if <code>arr = [2,<u>3</u>,4]</code>, the median is <code>3</code>.</li> <li>For examples, if <code>arr = [1,<u>2,3</u>,4]</code>, the median is <code>(2 + 3) / 2 = 2.5</code>.</li> </ul> <p>You are given an integer array <code>nums</code> and an integer <code>k</code>. There is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the median array for each window in the original array</em>. Answers within <code>10<sup>-5</sup></code> of the actual value will be accepted.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,-1,-3,5,3,6,7], k = 3 <strong>Output:</strong> [1.00000,-1.00000,-1.00000,3.00000,5.00000,6.00000] <strong>Explanation:</strong> Window position Median --------------- ----- [<strong>1 3 -1</strong>] -3 5 3 6 7 1 1 [<strong>3 -1 -3</strong>] 5 3 6 7 -1 1 3 [<strong>-1 -3 5</strong>] 3 6 7 -1 1 3 -1 [<strong>-3 5 3</strong>] 6 7 3 1 3 -1 -3 [<strong>5 3 6</strong>] 7 5 1 3 -1 -3 5 [<strong>3 6 7</strong>] 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,2,3,1,4,2], k = 3 <strong>Output:</strong> [2.00000,3.00000,3.00000,3.00000,2.00000,3.00000,2.00000] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= k &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n vector<double> medianSlidingWindow(vector<int>& nums, int k) {\n vector<double> medians;\n unordered_map<int, int> outgoingNum;\n priority_queue<int> smallList;\n priority_queue<int, vector<int>, greater<int>> largeList;\n\n for (int i = 0; i < k; i++) {\n smallList.push(nums[i]);\n }\n\n for (int i = 0; k && i < k / 2; i++) {\n largeList.push(smallList.top());\n smallList.pop();\n }\n\n int balance = 0;\n int i = k;\n\n while (true) {\n if (k % 2 == 1) {\n medians.push_back(smallList.top());\n } else {\n medians.push_back(\n ((long long)smallList.top() + (long long)largeList.top()) *\n 0.5);\n }\n\n if (i >= nums.size()) {\n break;\n }\n\n int outNum = nums[i - k];\n int inNum = nums[i];\n i++;\n\n if (outNum <= smallList.top()) {\n balance -= 1;\n } else {\n balance += 1;\n }\n\n outgoingNum[outNum]++;\n\n if (!smallList.empty() && inNum <= smallList.top()) {\n balance += 1;\n smallList.push(inNum);\n } else {\n balance -= 1;\n largeList.push(inNum);\n }\n\n if (balance < 0) {\n smallList.push(largeList.top());\n largeList.pop();\n } else if (balance > 0) {\n largeList.push(smallList.top());\n smallList.pop();\n }\n\n balance = 0;\n\n while (!smallList.empty() && outgoingNum[smallList.top()]) {\n outgoingNum[smallList.top()]--;\n smallList.pop();\n }\n\n while (!largeList.empty() && outgoingNum[largeList.top()]) {\n outgoingNum[largeList.top()]--;\n largeList.pop();\n }\n }\n\n return medians;\n }\n};", "memory": "40150" }
480
<p>The <strong>median</strong> is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle values.</p> <ul> <li>For examples, if <code>arr = [2,<u>3</u>,4]</code>, the median is <code>3</code>.</li> <li>For examples, if <code>arr = [1,<u>2,3</u>,4]</code>, the median is <code>(2 + 3) / 2 = 2.5</code>.</li> </ul> <p>You are given an integer array <code>nums</code> and an integer <code>k</code>. There is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the median array for each window in the original array</em>. Answers within <code>10<sup>-5</sup></code> of the actual value will be accepted.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,-1,-3,5,3,6,7], k = 3 <strong>Output:</strong> [1.00000,-1.00000,-1.00000,3.00000,5.00000,6.00000] <strong>Explanation:</strong> Window position Median --------------- ----- [<strong>1 3 -1</strong>] -3 5 3 6 7 1 1 [<strong>3 -1 -3</strong>] 5 3 6 7 -1 1 3 [<strong>-1 -3 5</strong>] 3 6 7 -1 1 3 -1 [<strong>-3 5 3</strong>] 6 7 3 1 3 -1 -3 [<strong>5 3 6</strong>] 7 5 1 3 -1 -3 5 [<strong>3 6 7</strong>] 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,2,3,1,4,2], k = 3 <strong>Output:</strong> [2.00000,3.00000,3.00000,3.00000,2.00000,3.00000,2.00000] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= k &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul>
1
{ "code": "#include <queue>\n#include <vector>\n#include <unordered_map> // already functions like a defaultdict\n\nclass Solution {\npublic:\n vector<double> medianSlidingWindow(vector<int>& nums, int k) {\n /*\n - two heap approach\n - assume that min is allowed to have one more than max\n - median is in min if min is bigger, otherwise average of the tops of the heaps\n - lazy removal, when an element leaves our window make note\n - if a removed element ever shows up at the top of a heap, pop that element\n - if k is odd, the min heap has one more element\n */\n std::priority_queue<double> min_heap; // large half of values, multiply by -1\n std::priority_queue<double> max_heap; // small half of values, allowed to have one more value\n std::unordered_map<int,int> to_remove; // key: number to remove, value: counter\n std::vector<double> result;\n\n // populate the two heaps for the first window\n for (int i = 0; i < k; ++i) {\n // push to max heap by defualt and swap over to min heap\n max_heap.push(double(nums[i]));\n double swap = max_heap.top();\n max_heap.pop();\n min_heap.push(-1 * swap);\n\n // make sure the min heap is not bigger\n if(min_heap.size() > max_heap.size()) {\n double swap = min_heap.top() * -1;\n min_heap.pop();\n max_heap.push(swap);\n }\n }\n\n // get the median for the first window\n // if the window is odd the median is on the max_heap\n double median;\n if (k % 2 == 1) {\n median = max_heap.top();\n result.push_back(median);\n }\n else {\n median = (max_heap.top() + (-1 * min_heap.top())) / 2.0;\n result.push_back(median);\n }\n\n // slide the window, getting the median and removing values when necessary\n for (int i = k; i < nums.size(); ++i) {\n // record the number leaving the window\n auto remove = nums[i-k];\n ++to_remove[remove];\n\n // how does removal impact heap size?\n int balance;\n if (remove <= median) {\n balance = -1; // removed from max_heap, one less value in max heap\n }\n else {\n balance = 1; // removed from min_heap, one extra value in max heap\n }\n\n // insert the next number according to its relationship to the median\n if (nums[i] <= median) {\n ++balance;\n max_heap.push(double(nums[i]));\n }\n else{\n --balance;\n min_heap.push(-1 * double(nums[i]));\n }\n\n // swap if necessary\n if (balance < 0) { // need to add to the max heap to rebalance\n max_heap.push(-1 * min_heap.top());\n min_heap.pop();\n }\n else if (balance > 0) { // max heap has too many elements\n min_heap.push(-1 * max_heap.top());\n max_heap.pop();\n }\n\n // remove values only if they are at the top of either respective heap\n while (!max_heap.empty() && to_remove[max_heap.top()] > 0) { // pop if the top of the max_heap is outside the window\n --to_remove[max_heap.top()];\n max_heap.pop();\n }\n while (!min_heap.empty() && to_remove[-1 * min_heap.top()] > 0) { // pop if the top of the min_heap is outside the window\n --to_remove[-1 * min_heap.top()];\n min_heap.pop();\n }\n\n // get the median of this window\n if (k % 2 == 1) {\n median = max_heap.top();\n result.push_back(median);\n }\n else {\n median = (max_heap.top() + (-1 * min_heap.top())) / 2.0;\n result.push_back(median);\n }\n }\n\n return result;\n }\n};", "memory": "41290" }
480
<p>The <strong>median</strong> is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle values.</p> <ul> <li>For examples, if <code>arr = [2,<u>3</u>,4]</code>, the median is <code>3</code>.</li> <li>For examples, if <code>arr = [1,<u>2,3</u>,4]</code>, the median is <code>(2 + 3) / 2 = 2.5</code>.</li> </ul> <p>You are given an integer array <code>nums</code> and an integer <code>k</code>. There is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the median array for each window in the original array</em>. Answers within <code>10<sup>-5</sup></code> of the actual value will be accepted.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,-1,-3,5,3,6,7], k = 3 <strong>Output:</strong> [1.00000,-1.00000,-1.00000,3.00000,5.00000,6.00000] <strong>Explanation:</strong> Window position Median --------------- ----- [<strong>1 3 -1</strong>] -3 5 3 6 7 1 1 [<strong>3 -1 -3</strong>] 5 3 6 7 -1 1 3 [<strong>-1 -3 5</strong>] 3 6 7 -1 1 3 -1 [<strong>-3 5 3</strong>] 6 7 3 1 3 -1 -3 [<strong>5 3 6</strong>] 7 5 1 3 -1 -3 5 [<strong>3 6 7</strong>] 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,2,3,1,4,2], k = 3 <strong>Output:</strong> [2.00000,3.00000,3.00000,3.00000,2.00000,3.00000,2.00000] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= k &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul>
1
{ "code": "class Solution {\npublic:\n vector<double> medianSlidingWindow(vector<int>& nums, int k) {\n vector<double> ans;\n map<int, int> hm;\n priority_queue<int> lo;\n priority_queue<int, vector<int>, greater<int>> hi;\n\n for (int i = 0; i < k; i++) {\n lo.push(nums[i]);\n }\n for (int i = 0; i < k/2; i++) {\n hi.push(lo.top());\n lo.pop();\n }\n\n if (k % 2 == 0) {\n ans.push_back(((double)lo.top() + (double)hi.top())/2.0);\n } else {\n ans.push_back(lo.top());\n }\n\n for (int i = k; i < nums.size(); i++) {\n int out_num = nums[i - k];\n int in_num = nums[i];\n int balance = (out_num <= lo.top()) ? -1 : 1;\n hm[out_num]++;\n if (!lo.empty() && in_num <= lo.top()) {\n balance++;\n lo.push(in_num);\n } else {\n balance--;\n hi.push(in_num);\n }\n\n if (balance < 0) {\n lo.push(hi.top());\n hi.pop();\n balance++;\n }\n if (balance > 0) {\n hi.push(lo.top());\n lo.pop();\n balance--;\n }\n while (!lo.empty() && hm[lo.top()]) {\n hm[lo.top()]--;\n lo.pop();\n }\n\n while (!hi.empty() && hm[hi.top()]) {\n hm[hi.top()]--;\n hi.pop();\n }\n\n if (k % 2 == 0) {\n ans.push_back(((double)lo.top() + (double)hi.top())/2.0);\n } else {\n ans.push_back(lo.top());\n }\n }\n\n return ans;\n }\n};", "memory": "41670" }
480
<p>The <strong>median</strong> is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle values.</p> <ul> <li>For examples, if <code>arr = [2,<u>3</u>,4]</code>, the median is <code>3</code>.</li> <li>For examples, if <code>arr = [1,<u>2,3</u>,4]</code>, the median is <code>(2 + 3) / 2 = 2.5</code>.</li> </ul> <p>You are given an integer array <code>nums</code> and an integer <code>k</code>. There is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the median array for each window in the original array</em>. Answers within <code>10<sup>-5</sup></code> of the actual value will be accepted.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,-1,-3,5,3,6,7], k = 3 <strong>Output:</strong> [1.00000,-1.00000,-1.00000,3.00000,5.00000,6.00000] <strong>Explanation:</strong> Window position Median --------------- ----- [<strong>1 3 -1</strong>] -3 5 3 6 7 1 1 [<strong>3 -1 -3</strong>] 5 3 6 7 -1 1 3 [<strong>-1 -3 5</strong>] 3 6 7 -1 1 3 -1 [<strong>-3 5 3</strong>] 6 7 3 1 3 -1 -3 [<strong>5 3 6</strong>] 7 5 1 3 -1 -3 5 [<strong>3 6 7</strong>] 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,2,3,1,4,2], k = 3 <strong>Output:</strong> [2.00000,3.00000,3.00000,3.00000,2.00000,3.00000,2.00000] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= k &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul>
1
{ "code": "class Solution {\npublic:\n vector<double> medianSlidingWindow(vector<int>& nums, int k) {\n vector<double>ans;\n unordered_map<int,int>mp;//to account for late deletions\n priority_queue<double>left; //maxHeap\n priority_queue<double,vector<double>,greater<double>>right; //minHeap\n for(int i=0;i<k;i++) {\n left.push(nums[i]);\n }\n\n for(int i=0;i<k/2;i++) {\n right.push(left.top());\n left.pop();\n }\n\n for(int i=k;i<nums.size();i++) {\n if(k%2 == 1) {\n ans.push_back(left.top());\n }else{\n ans.push_back((left.top()+right.top())/2);\n }\n\n int toDelete=nums[i-k];\n int toAdd=nums[i];\n\n //firstly we will delete the first element from the window;\n int balance=0;\n if(toDelete <= left.top()) {\n balance--;\n\n if(toDelete == left.top()) { //delete it immediately\n left.pop();\n }else{\n mp[toDelete]++; //schedule for later deletion\n }\n }\n else{\n balance++;\n if(toDelete == right.top()) { //delete it immediately\n right.pop();\n }else{\n mp[toDelete]++; //schedule for later deletion\n }\n }\n\n //add the new element into the window\n if(!left.empty() && toAdd <= left.top()) {\n left.push(toAdd);\n balance++;\n }else{\n right.push(toAdd);\n balance--;\n }\n\n //balance the heap\n if(balance < 0) { //means left heap has less elements\n left.push(right.top());\n right.pop();\n \n }else if(balance>0){\n right.push(left.top());\n left.pop();\n \n }\n\n //delete the elements scheduled for later deletion\n while(!left.empty() && mp[left.top()] > 0) {\n \n mp[left.top()]--;\n left.pop();\n }\n while(!right.empty() && mp[right.top()] > 0) {\n \n mp[right.top()]--;\n right.pop();\n }\n\n }\n if(k%2 == 1) {\n ans.push_back(left.top());\n }else{\n ans.push_back((left.top()+right.top())/2);\n }\n\n return ans;\n\n }\n};", "memory": "41670" }
480
<p>The <strong>median</strong> is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle values.</p> <ul> <li>For examples, if <code>arr = [2,<u>3</u>,4]</code>, the median is <code>3</code>.</li> <li>For examples, if <code>arr = [1,<u>2,3</u>,4]</code>, the median is <code>(2 + 3) / 2 = 2.5</code>.</li> </ul> <p>You are given an integer array <code>nums</code> and an integer <code>k</code>. There is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the median array for each window in the original array</em>. Answers within <code>10<sup>-5</sup></code> of the actual value will be accepted.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,-1,-3,5,3,6,7], k = 3 <strong>Output:</strong> [1.00000,-1.00000,-1.00000,3.00000,5.00000,6.00000] <strong>Explanation:</strong> Window position Median --------------- ----- [<strong>1 3 -1</strong>] -3 5 3 6 7 1 1 [<strong>3 -1 -3</strong>] 5 3 6 7 -1 1 3 [<strong>-1 -3 5</strong>] 3 6 7 -1 1 3 -1 [<strong>-3 5 3</strong>] 6 7 3 1 3 -1 -3 [<strong>5 3 6</strong>] 7 5 1 3 -1 -3 5 [<strong>3 6 7</strong>] 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,2,3,1,4,2], k = 3 <strong>Output:</strong> [2.00000,3.00000,3.00000,3.00000,2.00000,3.00000,2.00000] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= k &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul>
1
{ "code": "class Solution {\npublic:\n vector<double> medianSlidingWindow(vector<int>& nums, int k) {\n vector<double> result;\n result.reserve(nums.size() - k + 1);\n multiset<long int> sorted_window;\n auto iter = sorted_window.begin();\n for (size_t i = 0; i < nums.size(); ++i) {\n auto ins_iter = sorted_window.insert(nums[i]);\n if (i == k-1) {\n iter = sorted_window.begin();\n for (size_t i = 0; i < (k-1) / 2; ++i) {\n ++iter;\n }\n }\n if (i >= k-1) {\n if (i > k-1) {\n auto erase_iter = --sorted_window.upper_bound(nums[i-k]);\n if (erase_iter == iter) {\n if (*ins_iter >= *iter) {\n ++iter;\n } else {\n --iter;\n }\n } else if (!(*erase_iter < *iter && *ins_iter < *iter || *erase_iter >= *iter && *ins_iter >= *iter)) {\n if (*erase_iter >= *iter && *ins_iter < *iter) {\n --iter;\n } else {\n ++iter;\n }\n }\n sorted_window.erase(erase_iter);\n }\n if (k % 2 == 0) {\n result.emplace_back((*iter + *(++iter) + 0.) / 2);\n --iter;\n } else {\n result.emplace_back(*iter + 0.);\n }\n }\n }\n return result;\n }\n};", "memory": "42050" }
480
<p>The <strong>median</strong> is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle values.</p> <ul> <li>For examples, if <code>arr = [2,<u>3</u>,4]</code>, the median is <code>3</code>.</li> <li>For examples, if <code>arr = [1,<u>2,3</u>,4]</code>, the median is <code>(2 + 3) / 2 = 2.5</code>.</li> </ul> <p>You are given an integer array <code>nums</code> and an integer <code>k</code>. There is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the median array for each window in the original array</em>. Answers within <code>10<sup>-5</sup></code> of the actual value will be accepted.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,-1,-3,5,3,6,7], k = 3 <strong>Output:</strong> [1.00000,-1.00000,-1.00000,3.00000,5.00000,6.00000] <strong>Explanation:</strong> Window position Median --------------- ----- [<strong>1 3 -1</strong>] -3 5 3 6 7 1 1 [<strong>3 -1 -3</strong>] 5 3 6 7 -1 1 3 [<strong>-1 -3 5</strong>] 3 6 7 -1 1 3 -1 [<strong>-3 5 3</strong>] 6 7 3 1 3 -1 -3 [<strong>5 3 6</strong>] 7 5 1 3 -1 -3 5 [<strong>3 6 7</strong>] 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,2,3,1,4,2], k = 3 <strong>Output:</strong> [2.00000,3.00000,3.00000,3.00000,2.00000,3.00000,2.00000] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= k &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul>
1
{ "code": "class Solution {\npublic:\n vector<double> medianSlidingWindow(vector<int>& nums, int k) {\n int n = nums.size();\n vector<double> ans(n - k + 1);\n multiset<double> window(nums.begin(), nums.begin() + k);\n multiset<double>::iterator it = next(window.begin(), (k - 1) / 2);\n\n for (int i = k; ; ++i){\n const double medians = k & 1 ? *it : (*it + *next(it)) / 2.0;\n ans[i-k] = medians;\n if (i == n){\n break;\n }\n window.emplace(nums[i]);\n if (nums[i] < *it){\n --it;\n }\n if (nums[i-k] <= *it){\n ++it;\n }\n window.erase(window.lower_bound(nums[i-k]));\n }\n\n return ans;\n }\n};", "memory": "42050" }