id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
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 const int N = nums.size();\n\n multiset<int> window(nums.begin(), nums.begin() + k);\n auto mid = next(window.begin(), k / 2);\n\n vector<double> ans(N - k + 1);\n int i = 0;\n\n function<void(void)> calcAndStoreMedian = [&]() {\n if (k % 2 == 0) {\n ans[i++] = (double(*mid) + *prev(mid, 1)) / 2;\n } else {\n ans[i++] = *mid;\n }\n };\n\n calcAndStoreMedian();\n\n for (int i = k; i < N; i++) {\n window.insert(nums[i]);\n\n if (nums[i] < *mid)\n mid--;\n\n if (nums[i - k] <= *mid)\n mid++;\n\n window.erase(window.lower_bound(nums[i - k]));\n\n calcAndStoreMedian();\n }\n\n return ans;\n }\n};", "memory": "42430" }
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 {\n public:\n vector<double> medianSlidingWindow(vector<int>& nums, int k) {\n vector<double> ans;\n multiset<double> window(nums.begin(), nums.begin() + k);\n auto it = next(window.begin(), (k - 1) / 2);\n\n for (int i = k;; ++i) {\n const double median = k % 2 == 0 ? (*it + *next(it)) / 2.0 : *it;\n ans.push_back(median);\n if (i == nums.size())\n break;\n window.insert(nums[i]);\n if (nums[i] < *it)\n --it;\n if (nums[i - k] <= *it)\n ++it;\n window.erase(window.lower_bound(nums[i - k]));\n }\n\n return ans;\n }\n};", "memory": "42430" }
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# define ll long long\n vector<double> medianSlidingWindow(vector<int>& nums, int k) {\n priority_queue<pair<ll,ll>>mx;\n int n=nums.size();\n priority_queue<pair<ll,ll>,vector<pair<ll,ll>>,greater<pair<ll,ll>>>mn;\n int ctmx=0,ctmn=0;\n vector<pair<ll,ll>>v;\n for(int i=0;i<k;i++)\n {\n v.push_back({nums[i],i});\n }\n sort(v.begin(),v.end());\n int val=(k+1)/2;\n for(int i=0;i<val;i++)\n {\n mx.push({v[i].first,v[i].second});\n }\n for(int i=val;i<k;i++)\n {\n mn.push({v[i].first,v[i].second});\n }\n vector<double>ans;\n if(k%2==0)\n {\n ans.push_back(double(mn.top().first+mx.top().first)/2);\n }\n else\n {\n ans.push_back(mx.top().first);\n }\n int i=0,j=k;\n vector<int>vis(n,0);\n while(j<n)\n {\n if(mx.top().first>nums[i])\n {\n ctmx++;\n }\n else if(mx.top().first<nums[i])\n {\n if(mn.top().first==nums[i])\n {\n if(mn.top().second==i)mn.pop();\n else ctmn++;\n }\n else ctmn++;\n }\n else\n {\n if(mx.top().second==i)mx.pop();\n else ctmx++;\n }\n\n if(nums[j]>=mx.top().first)\n {\n mn.push({nums[j],j});\n }\n else\n {\n mx.push({nums[j],j});\n }\n\n if(int(mx.size()-ctmx-mn.size()+ctmn)>1)\n {\n mn.push({mx.top().first,mx.top().second});\n mx.pop();\n }\n else if(int(mx.size()-ctmx)<int(mn.size()-ctmn))\n {\n mx.push({mn.top().first,mn.top().second});\n \n mn.pop();\n }\n while(mn.size()>0&&vis[mn.top().second]==1)\n {\n mn.pop();\n ctmn--;\n }\n while(mx.size()>0&&vis[mx.top().second]==1)\n {\n mx.pop();\n ctmx--;\n }\n\n if(mn.size()-ctmn==mx.size()-ctmx)\n {\n ans.push_back(double(mn.top().first+mx.top().first)/2);\n }\n else\n {\n ans.push_back(double(mx.top().first));\n }\n j++;\n vis[i]=1;\n i++;\n // cout<<mn.top()<<\" \"<<mx.top()<<endl;\n }\n return ans;\n }\n};", "memory": "42810" }
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 multiset<int> window(nums.begin(), nums.begin() + k);\n auto mid = next(window.begin(), k / 2);\n vector<double> result;\n \n for (int i = k; ; i++) {\n // Для четных и нечетных k вычисляем медиану\n result.push_back(((double(*mid) + *prev(mid, 1 - k % 2)) / 2));\n \n if (i == nums.size()) break;\n \n // Добавляем новый элемент в окно\n window.insert(nums[i]);\n if (nums[i] < *mid) mid--;\n \n // Убираем старый элемент из окна\n if (nums[i - k] <= *mid) mid++;\n window.erase(window.lower_bound(nums[i - k]));\n }\n \n return result;\n }\n};\n", "memory": "42810" }
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 const int n = nums.size();\n vector<double> ans;\n multiset<double> set(nums.begin(), nums.begin() + k);\n auto it = next(set.begin(), (k - 1) / 2);\n\n for (int i = k; i <= n; ++i) {\n const double val = k % 2 == 0? ((double)*it * 1.0 + *next(it)) * 1.0 / 2: *it;\n ans.push_back(val);\n if (i == n) break;\n set.insert(nums[i]);\n if (nums[i] < *it) --it;\n if (nums[i - k] <= *it) ++it;\n set.erase(set.lower_bound(nums[i - k]));\n }\n return ans;\n }\n};", "memory": "43190" }
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 {\n public:\n vector<double> medianSlidingWindow(vector<int>& nums, int k) {\n vector<double> ans;\n multiset<double> window(nums.begin(), nums.begin() + k);\n auto it = next(window.begin(), (k - 1) / 2);\n\n for (int i = k;; ++i) {\n const double median = k % 2 == 0 ? (*it + *next(it)) / 2.0 : *it;\n ans.push_back(median);\n if (i == nums.size())\n break;\n window.insert(nums[i]);\n if (nums[i] < *it)\n --it;\n if (nums[i - k] <= *it)\n ++it;\n window.erase(window.lower_bound(nums[i - k]));\n }\n\n return ans;\n }\n};", "memory": "43190" }
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 <algorithm>\n#include <deque>\n#include <iostream>\n#include <optional>\n#include <queue>\n#include <tuple>\n#include <vector>\n#include <unordered_map>\n\n// min-heap\nusing HeapType = std::priority_queue<long, std::vector<long>, std::greater<long>>;\n\nclass Solution {\npublic:\n double get_median(const HeapType& max_heap, const HeapType& min_heap, int k) {\n if (k % 2 == 1) {\n return -(double)max_heap.top();\n }\n else {\n const double x = -max_heap.top();\n const double y = min_heap.top();\n return (x+y) * 0.5;\n }\n }\n\n std::vector<double> medianSlidingWindow(std::vector<int>& nums, int k) {\n HeapType max_heap;\n HeapType min_heap;\n std::unordered_map<long, int> heap_dict;\n std::vector<double> result;\n\n for (auto i = 0; i < k; i++) {\n max_heap.push(-(long)nums[i]);\n min_heap.push(-(long)max_heap.top());\n max_heap.pop();\n\n if (min_heap.size() > max_heap.size()) {\n max_heap.push(-(long)min_heap.top());\n min_heap.pop();\n }\n }\n\n auto median = get_median(max_heap, min_heap, k);\n result.push_back(median);\n\n for (auto i = k; i < nums.size(); i++ ) {\n auto prev = nums[i-k];\n if (heap_dict.find(prev) == heap_dict.end()) {\n heap_dict.emplace(prev, 1);\n }\n else {\n heap_dict[prev]++;\n }\n\n auto balance = prev <= median ? -1 : 1;\n\n if (nums[i] <= median) {\n balance++;\n max_heap.push(-(long)nums[i]);\n }\n else {\n balance--;\n min_heap.push((long)nums[i]);\n }\n\n if (balance < 0) {\n max_heap.push(-min_heap.top());\n min_heap.pop();\n }\n else if (balance > 0) {\n min_heap.push(-max_heap.top());\n max_heap.pop();\n }\n\n while ((!max_heap.empty()) && (heap_dict[-(long)max_heap.top()] > 0)) {\n heap_dict[-(long)max_heap.top()]--;\n max_heap.pop();\n }\n\n while ((!min_heap.empty()) && (heap_dict[min_heap.top()] > 0)) {\n heap_dict[min_heap.top()]--;\n min_heap.pop();\n }\n\n median = get_median(max_heap, min_heap, k);\n result.push_back(median);\n }\n\n return result;\n }\n};", "memory": "43570" }
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, int64_t k) {\n std::vector<double> medians;\n std::unordered_map<int64_t, int64_t> hash_table;\n priority_queue<int64_t> lo;\n priority_queue<int64_t, vector<int64_t>, greater<int64_t>> hi;\n\n int64_t i = 0;\n while (i < k) {\n lo.push(nums[i++]);\n }\n for (int64_t j = 0; j < k / 2; ++j) {\n hi.push(lo.top());\n lo.pop();\n }\n\n while (true) {\n if (k & 1) {\n medians.push_back(static_cast<double>(lo.top()));\n } else {\n medians.push_back(static_cast<double>(lo.top() + hi.top()) * 0.5);\n }\n\n if (i >= nums.size())\n break;\n\n int64_t out_num = nums[i - k];\n int64_t in_num = nums[i++];\n int64_t balance = (out_num <= lo.top()) ? -1 : 1;\n hash_table[out_num]++;\n\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\n while (!lo.empty() && hash_table[lo.top()]) {\n hash_table[lo.top()]--;\n lo.pop();\n }\n\n while (!hi.empty() && hash_table[hi.top()]) {\n hash_table[hi.top()]--;\n hi.pop();\n }\n }\n return medians;\n }\n};", "memory": "43570" }
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": "struct Queue\n{\n multiset<int> max;\n multiset<int> min;\n\n int topMax()\n {\n return *max.rbegin();\n }\n\n int topMin()\n {\n return *min.begin();\n }\n\n void push(int val)\n {\n if(max.size() == min.size())\n {\n max.emplace(val);\n }\n else\n {\n min.emplace(val);\n }\n\n if(min.empty())\n {\n return;\n }\n if(topMax() > topMin())\n {\n auto min_it = max.rbegin();\n auto max_it = min.begin();\n auto lhs = max.extract(next(min_it).base());\n auto rhs = min.extract(max_it);\n\n // TODO why extract hangs??\n max.insert(std::move(rhs));\n min.insert(std::move(lhs));\n }\n }\n\n void remove(int val)\n {\n // v Max Min\n if(val <= topMax())\n {\n max.extract(val);\n }\n else\n {\n min.extract(val);\n }\n\n if(max.size() < min.size())\n {\n auto rhs = min.extract(min.begin());\n\n max.insert(std::move(rhs));\n }\n }\n\n double median()\n {\n if(max.size() == min.size())\n {\n long long sum = *max.rbegin();\n sum += *min.begin();\n return sum / double(2);\n }\n else\n {\n return *max.rbegin();\n }\n }\n};\n\n\n\n\nclass Solution {\npublic:\n vector<double> medianSlidingWindow(vector<int>& nums, int k) {\n Queue q;\n vector<double> result;\n for(int i = 0; i < nums.size(); i++)\n {\n if(i >= k)\n {\n q.remove(nums[i - k]);\n }\n q.push(nums[i]);\n if(i + 1 >= k)\n {\n result.push_back(q.median());\n }\n }\n\n return result;\n\n }\n};", "memory": "43950" }
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 {\n \n unordered_map<int,int>mp1;\n unordered_map<int,int>mp2;\n \n priority_queue<int>pq1;\n priority_queue<int,vector<int>,greater<int>>pq2;\n \n int s1,s2;\n \n void filterPQ(){\n while( !pq1.empty() && mp1[pq1.top()] > 0){\n mp1[pq1.top()]--;\n pq1.pop();\n }\n while(!pq2.empty() && mp2[pq2.top()] > 0){\n mp2[pq2.top()]--;\n pq2.pop(); \n }\n }\n \n double getMedian(){\n filterPQ();\n if(s1 > s2) return pq1.top();\n if(s1 < s2) return pq2.top();\n return ((double)pq1.top() + (double)pq2.top())/2.00;\n }\n \n void insertInPQ(int &num){\n filterPQ();\n if(s1 == 0 || num <= pq1.top()){\n pq1.push(num);\n s1++;\n }\n else{\n pq2.push(num);\n s2++;\n }\n \n if(s1 - s2 > 1){\n pq2.push(pq1.top());\n s2++;\n pq1.pop();\n s1--;\n }\n else if(s2 - s1 > 1){\n pq1.push(pq2.top());\n s1++;\n pq2.pop();\n s2--;\n }\n }\n \npublic:\n vector<double> medianSlidingWindow(vector<int>& nums, int k) {\n s1=0,s2=0;\n \n for(int i=0;i<k;i++){\n insertInPQ(nums[i]);\n }\n \n vector<double>ans;\n int i=0;\n int ub = nums.size()-k;\n ans.push_back(getMedian());\n \n while(i < ub){\n filterPQ();\n if(s1 > 0 && nums[i] <= pq1.top()){\n \n mp1[nums[i]]++;\n s1--;\n if(s2 > 0){\n s2--;\n pq1.push(pq2.top());\n s1++;\n pq2.pop();\n }\n \n }\n else{\n \n mp2[nums[i]]++;\n s2--;\n if(s1 - s2 > 1){\n pq2.push(pq1.top());\n s2++;\n pq1.pop();\n s1--;\n }\n \n }\n insertInPQ(nums[i+k]);\n i++;\n ans.push_back(getMedian());\n }\n return ans;\n }\n};", "memory": "44330" }
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 std::priority_queue<int> low;\n std::priority_queue<int, vector<int>, std::greater<int>> high;\n\n std::unordered_map<int, int> todeletelow;\n std::unordered_map<int, int> todeletehigh;\n vector<double> res;\n int lowsize = 0;\n int highsize = 0;\n for (int i = 0; i < nums.size(); ++i) {\n if (!lowsize) {\n low.push(nums[i]);\n lowsize++;\n } else if (nums[i] < low.top()) {\n low.push(nums[i]);\n lowsize++;\n } else {\n high.push(nums[i]);\n highsize++;\n }\n\n if (i < k - 1) {continue;}\n if (i>=k) {\n if (nums[i - k] >= high.top()) {\n highsize -=1;\n todeletehigh[nums[i-k]]++;\n } else {\n lowsize -=1;\n todeletelow[nums[i-k]]++;\n\n }\n }\n\n while (highsize > lowsize + 1) {\n if (todeletehigh[high.top()]) {\n todeletehigh[high.top()]--;\n high.pop();\n continue;\n }\n low.push(high.top());\n high.pop();\n highsize--;\n lowsize++;\n }\n while (lowsize > highsize + 1) {\n\n if (todeletelow[low.top()]) {\n todeletelow[low.top()]--;\n low.pop();\n continue;\n }\n high.push(low.top());\n low.pop();\n highsize++;\n lowsize--;\n }\n while (high.size() && todeletehigh[high.top()]) {\n\n todeletehigh[high.top()]--;\n high.pop();\n }\n while (low.size() && todeletelow[low.top()]) {\n\n todeletelow[low.top()]--;\n low.pop();\n }\n \n // find median\n if (lowsize > highsize) {\n res.push_back(low.top());\n } else if (highsize > lowsize) {\n res.push_back(high.top());\n } else {\n res.push_back(((double)((double)high.top() - (double)low.top() )/ 2.0) + low.top());\n }\n }\n return res;\n }\n};", "memory": "44330" }
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 double median(const vector<int>& sorted) {\n long median = sorted[sorted.size() / 2];\n if (sorted.size() % 2 == 0) {\n return (median + sorted[sorted.size() / 2 - 1]) / 2.0;\n }\n\n return median;\n }\n\n void medianSlidingWindow(vector<int>& nums, vector<int>& sortedK, int nextStartIdx, vector<double>& result) {\n if (nextStartIdx + sortedK.size() > nums.size()) {\n return;\n }\n\n int toRemove = nums[nextStartIdx - 1];\n int toAdd = nums[nextStartIdx + sortedK.size() - 1];\n\n auto removeIt = std::lower_bound(sortedK.begin(), sortedK.end(), toRemove);\n auto addIt = std::lower_bound(sortedK.begin(), sortedK.end(), toAdd);\n\n if (removeIt == addIt) {\n *removeIt = toAdd;\n } else if (addIt == sortedK.end()) {\n sortedK.erase(removeIt);\n sortedK.push_back(toAdd);\n } else if (removeIt < addIt) {\n for (auto it = removeIt + 1; it < addIt; ++it) {\n *(it - 1) = *it;\n }\n *(addIt - 1) = toAdd;\n } else {\n int tmp = toAdd;\n for (auto it = addIt; it <= removeIt; ++it) {\n std::swap(tmp, *it); \n }\n }\n\n /*\n for (int i = 0, size = previousSorted.size(); i < size; ++i) {\n if (previousSorted[i] == toRemove) {\n\n }\n } */\n\n result.push_back(median(sortedK));\n medianSlidingWindow(nums, sortedK, nextStartIdx + 1, result);\n }\n\n vector<double> medianSlidingWindow(vector<int>& nums, int k) {\n std::vector<int> sortedK(nums.begin(), nums.begin() + k);\n std::sort(sortedK.begin(), sortedK.end());\n\n std::vector<double> result;\n result.reserve(nums.size() - k + 2);\n result.push_back(median(sortedK));\n\n medianSlidingWindow(nums, sortedK, 1, result);\n return result;\n }\n};", "memory": "44710" }
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 double median(const vector<int>& sorted) {\n long median = sorted[sorted.size() / 2];\n if (sorted.size() % 2 == 0) {\n return (median + sorted[sorted.size() / 2 - 1]) / 2.0;\n }\n\n return median;\n }\n\n void medianSlidingWindow(vector<int>& nums, vector<int>& sortedK, int nextStartIdx, vector<double>& result) {\n if (nextStartIdx + sortedK.size() > nums.size()) {\n return;\n }\n\n int toRemove = nums[nextStartIdx - 1];\n int toAdd = nums[nextStartIdx + sortedK.size() - 1];\n\n auto removeIt = std::lower_bound(sortedK.begin(), sortedK.end(), toRemove);\n auto addIt = std::lower_bound(sortedK.begin(), sortedK.end(), toAdd);\n\n if (removeIt == addIt) {\n *removeIt = toAdd;\n } else if (addIt == sortedK.end()) {\n sortedK.erase(removeIt);\n sortedK.push_back(toAdd);\n } else if (removeIt < addIt) {\n for (auto it = removeIt + 1; it < addIt; ++it) {\n *(it - 1) = *it;\n }\n *(addIt - 1) = toAdd;\n } else {\n int tmp = toAdd;\n for (auto it = addIt; it <= removeIt; ++it) {\n std::swap(tmp, *it); \n }\n }\n\n /*\n for (int i = 0, size = previousSorted.size(); i < size; ++i) {\n if (previousSorted[i] == toRemove) {\n\n }\n } */\n\n result.push_back(median(sortedK));\n medianSlidingWindow(nums, sortedK, nextStartIdx + 1, result);\n }\n\n vector<double> medianSlidingWindow(vector<int>& nums, int k) {\n std::vector<int> sortedK(nums.begin(), nums.begin() + k);\n std::sort(sortedK.begin(), sortedK.end());\n\n std::vector<double> result;\n result.reserve(nums.size() - k + 2);\n result.push_back(median(sortedK));\n\n medianSlidingWindow(nums, sortedK, 1, result);\n return result;\n }\n};", "memory": "44710" }
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 double median(const vector<int>& sorted) {\n long median = sorted[sorted.size() / 2];\n if (sorted.size() % 2 == 0) {\n return (median + sorted[sorted.size() / 2 - 1]) / 2.0;\n }\n\n return median;\n }\n\n void medianSlidingWindow(vector<int>& nums, vector<int>& sortedK, int nextStartIdx, vector<double>& result) {\n if (nextStartIdx + sortedK.size() > nums.size()) {\n return;\n }\n\n int toRemove = nums[nextStartIdx - 1];\n int toAdd = nums[nextStartIdx + sortedK.size() - 1];\n\n auto removeIt = std::lower_bound(sortedK.begin(), sortedK.end(), toRemove);\n auto addIt = std::lower_bound(sortedK.begin(), sortedK.end(), toAdd);\n\n if (removeIt == addIt) {\n *removeIt = toAdd;\n } else if (addIt == sortedK.end()) {\n sortedK.erase(removeIt);\n sortedK.push_back(toAdd);\n } else if (removeIt < addIt) {\n for (auto it = removeIt + 1; it < addIt; ++it) {\n *(it - 1) = *it;\n }\n *(addIt - 1) = toAdd;\n } else {\n int tmp = toAdd;\n for (auto it = addIt; it <= removeIt; ++it) {\n std::swap(tmp, *it); \n }\n }\n\n /*\n for (int i = 0, size = previousSorted.size(); i < size; ++i) {\n if (previousSorted[i] == toRemove) {\n\n }\n } */\n\n result.push_back(median(sortedK));\n medianSlidingWindow(nums, sortedK, nextStartIdx + 1, result);\n }\n\n vector<double> medianSlidingWindow(vector<int>& nums, int k) {\n std::vector<int> sortedK(nums.begin(), nums.begin() + k);\n std::sort(sortedK.begin(), sortedK.end());\n\n std::vector<double> result;\n result.reserve(nums.size() - k + 10);\n result.push_back(median(sortedK));\n\n medianSlidingWindow(nums, sortedK, 1, result);\n return result;\n }\n};", "memory": "45090" }
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 double median(const vector<int>& sorted) {\n long median = sorted[sorted.size() / 2];\n if (sorted.size() % 2 == 0) {\n return (median + sorted[sorted.size() / 2 - 1]) / 2.0;\n }\n\n return median;\n }\n\n void medianSlidingWindow(vector<int>& nums, vector<int>& sortedK, int nextStartIdx, vector<double>& result) {\n if (nextStartIdx + sortedK.size() > nums.size()) {\n return;\n }\n\n int toRemove = nums[nextStartIdx - 1];\n int toAdd = nums[nextStartIdx + sortedK.size() - 1];\n\n auto removeIt = std::lower_bound(sortedK.begin(), sortedK.end(), toRemove);\n assert(removeIt != sortedK.end());\n assert(*removeIt == toRemove);\n\n *removeIt = toAdd;\n sortedK.erase(removeIt);\n auto addIt = std::lower_bound(sortedK.begin(), sortedK.end(), toAdd);\n sortedK.insert(addIt, toAdd);\n\n /*\n for (int i = 0, size = previousSorted.size(); i < size; ++i) {\n if (previousSorted[i] == toRemove) {\n\n }\n } */\n\n result.push_back(median(sortedK));\n medianSlidingWindow(nums, sortedK, nextStartIdx + 1, result);\n }\n\n vector<double> medianSlidingWindow(vector<int>& nums, int k) {\n std::vector<int> sortedK(nums.begin(), nums.begin() + k);\n std::sort(sortedK.begin(), sortedK.end());\n\n std::vector<double> result;\n result.push_back(median(sortedK));\n\n medianSlidingWindow(nums, sortedK, 1, result);\n return result;\n }\n};", "memory": "45470" }
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 double median(const vector<int>& sorted) {\n long median = sorted[sorted.size() / 2];\n if (sorted.size() % 2 == 0) {\n return (median + sorted[sorted.size() / 2 - 1]) / 2.0;\n }\n\n return median;\n }\n\n void medianSlidingWindow(vector<int>& nums, vector<int>& sortedK, int nextStartIdx, vector<double>& result) {\n if (nextStartIdx + sortedK.size() > nums.size()) {\n return;\n }\n\n int toRemove = nums[nextStartIdx - 1];\n int toAdd = nums[nextStartIdx + sortedK.size() - 1];\n\n auto removeIt = std::lower_bound(sortedK.begin(), sortedK.end(), toRemove);\n assert(removeIt != sortedK.end());\n assert(*removeIt == toRemove);\n\n sortedK.erase(removeIt);\n auto addIt = std::lower_bound(sortedK.begin(), sortedK.end(), toAdd);\n sortedK.insert(addIt, toAdd);\n\n /*\n for (int i = 0, size = previousSorted.size(); i < size; ++i) {\n if (previousSorted[i] == toRemove) {\n\n }\n } */\n\n result.push_back(median(sortedK));\n medianSlidingWindow(nums, sortedK, nextStartIdx + 1, result);\n }\n\n vector<double> medianSlidingWindow(vector<int>& nums, int k) {\n std::vector<int> sortedK(nums.begin(), nums.begin() + k);\n std::sort(sortedK.begin(), sortedK.end());\n\n std::vector<double> result;\n result.push_back(median(sortedK));\n\n medianSlidingWindow(nums, sortedK, 1, result);\n return result;\n }\n};", "memory": "45850" }
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 double median(const vector<int>& sorted) {\n long median = sorted[sorted.size() / 2];\n if (sorted.size() % 2 == 0) {\n return (median + sorted[sorted.size() / 2 - 1]) / 2.0;\n }\n\n return median;\n }\n\n void medianSlidingWindow(vector<int>& nums, vector<int>& sortedK, int nextStartIdx, vector<double>& result) {\n if (nextStartIdx + sortedK.size() > nums.size()) {\n return;\n }\n\n int toRemove = nums[nextStartIdx - 1];\n int toAdd = nums[nextStartIdx + sortedK.size() - 1];\n\n auto removeIt = std::lower_bound(sortedK.begin(), sortedK.end(), toRemove);\n auto addIt = std::lower_bound(sortedK.begin(), sortedK.end(), toAdd);\n\n if (removeIt == addIt) {\n *removeIt = toAdd;\n } else if (addIt == sortedK.end()) {\n sortedK.erase(removeIt);\n sortedK.push_back(toAdd);\n } else if (removeIt < addIt) {\n for (auto it = removeIt + 1; it < addIt; ++it) {\n *(it - 1) = *it;\n }\n *(addIt - 1) = toAdd;\n } else {\n int tmp = toAdd;\n for (auto it = addIt; it <= removeIt; ++it) {\n std::swap(tmp, *it); \n }\n }\n\n /*\n for (int i = 0, size = previousSorted.size(); i < size; ++i) {\n if (previousSorted[i] == toRemove) {\n\n }\n } */\n\n result.push_back(median(sortedK));\n medianSlidingWindow(nums, sortedK, nextStartIdx + 1, result);\n }\n\n vector<double> medianSlidingWindow(vector<int>& nums, int k) {\n std::vector<int> sortedK(nums.begin(), nums.begin() + k);\n std::sort(sortedK.begin(), sortedK.end());\n\n std::vector<double> result;\n result.push_back(median(sortedK));\n\n medianSlidingWindow(nums, sortedK, 1, result);\n return result;\n }\n};", "memory": "45850" }
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>
2
{ "code": "class Solution {\npublic:\n double median(const vector<int>& sorted) {\n long median = sorted[sorted.size() / 2];\n if (sorted.size() % 2 == 0) {\n return (median + sorted[sorted.size() / 2 - 1]) / 2.0;\n }\n\n return median;\n }\n\n void medianSlidingWindow(vector<int>& nums, vector<int>& sortedK, int nextStartIdx, vector<double>& result) {\n if (nextStartIdx + sortedK.size() > nums.size()) {\n return;\n }\n\n int toRemove = nums[nextStartIdx - 1];\n int toAdd = nums[nextStartIdx + sortedK.size() - 1];\n\n auto removeIt = std::lower_bound(sortedK.begin(), sortedK.end(), toRemove);\n assert(removeIt != sortedK.end());\n assert(*removeIt == toRemove);\n\n *removeIt = toAdd;\n sortedK.erase(removeIt);\n auto addIt = std::lower_bound(sortedK.begin(), sortedK.end(), toAdd);\n sortedK.insert(addIt, toAdd);\n\n /*\n for (int i = 0, size = previousSorted.size(); i < size; ++i) {\n if (previousSorted[i] == toRemove) {\n\n }\n } */\n\n result.push_back(median(sortedK));\n medianSlidingWindow(nums, sortedK, nextStartIdx + 1, result);\n }\n\n vector<double> medianSlidingWindow(vector<int>& nums, int k) {\n std::vector<int> sortedK(nums.begin(), nums.begin() + k);\n std::sort(sortedK.begin(), sortedK.end());\n\n std::vector<double> result;\n result.push_back(median(sortedK));\n\n medianSlidingWindow(nums, sortedK, 1, result);\n return result;\n }\n};", "memory": "46230" }
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>
2
{ "code": "class Solution {\npublic:\n double median(const vector<int>& sorted) {\n long median = sorted[sorted.size() / 2];\n if (sorted.size() % 2 == 0) {\n return (median + sorted[sorted.size() / 2 - 1]) / 2.0;\n }\n\n return median;\n }\n\n void medianSlidingWindow(vector<int>& nums, vector<int>& sortedK, int nextStartIdx, vector<double>& result) {\n if (nextStartIdx + sortedK.size() > nums.size()) {\n return;\n }\n\n int toRemove = nums[nextStartIdx - 1];\n int toAdd = nums[nextStartIdx + sortedK.size() - 1];\n\n auto removeIt = std::lower_bound(sortedK.begin(), sortedK.end(), toRemove);\n\n sortedK.erase(removeIt);\n auto addIt = std::lower_bound(sortedK.begin(), sortedK.end(), toAdd);\n sortedK.insert(addIt, toAdd);\n\n /*\n for (int i = 0, size = previousSorted.size(); i < size; ++i) {\n if (previousSorted[i] == toRemove) {\n\n }\n } */\n\n result.push_back(median(sortedK));\n medianSlidingWindow(nums, sortedK, nextStartIdx + 1, result);\n }\n\n vector<double> medianSlidingWindow(vector<int>& nums, int k) {\n std::vector<int> sortedK(nums.begin(), nums.begin() + k);\n std::sort(sortedK.begin(), sortedK.end());\n\n std::vector<double> result;\n result.push_back(median(sortedK));\n\n medianSlidingWindow(nums, sortedK, 1, result);\n return result;\n }\n};", "memory": "46230" }
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>
2
{ "code": "class Solution {\npublic:\n vector<int> segtree;\n map<int,int> mp;\n\n int n;\n void update(int pos,int l,int r, int idx,int sum)\n {\n if(pos<l|| pos>r)\n return;\n if(l==r)\n {\n segtree[idx]+=sum;\n return;\n }\n int mid=(l+r)/2;\n update(pos,l,mid,2*idx+1,sum);\n update(pos,mid+1,r,2*idx+2,sum);\n segtree[idx]+=sum;\n\n }\n int query(int l,int r,int rem,int idx)\n {\n if(l==r)\n return l;\n int mid=(l+r)/2;\n if(segtree[2*idx+1]<rem)\n {\n return query(mid+1,r,rem-segtree[2*idx+1],2*idx+2);\n }\n return query(l,mid,rem,2*idx+1);\n }\n vector<double> medianSlidingWindow(vector<int>& nums, int k) {\n vector<int> an;\n n=nums.size();\n segtree.resize(4*n+4,0);\n for(auto i:nums)\n an.push_back(i);\n sort(an.begin(),an.end());\n for(int i=0;i<nums.size();i++)\n mp[an[i]]=i+1;\n for(int i=0;i<k-1;i++)\n update(mp[nums[i]],0,n,0,1);\n vector<double> ans;\n if(k%2)\n {\n for(int i=k-1;i<n;i++)\n {\n update(mp[nums[i]],0,n,0,1);\n ans.push_back(an[query(0,n,k/2+1,0)-1]);\n update(mp[nums[i-k+1]],0,n,0,-1);\n }\n }\n else\n {\n for(int i=k-1;i<n;i++)\n {\n update(mp[nums[i]],0,n,0,1);\n long double x=an[query(0,n,k/2,0)-1];\n long double y=an[query(0,n,k/2+1,0)-1];\n // cout<<x<<\" \"<<y;\n ans.push_back((x+y)/2);\n update(mp[nums[i-k+1]],0,n,0,-1);\n }\n }\n return ans;\n }\n};", "memory": "46610" }
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>
2
{ "code": "class Solution {\npublic:\n vector<double> medianSlidingWindow(vector<int>& v, int k) {\n int n=v.size();\n vector<double>medians(n-k+1);\n multiset<int>low,high;\n vector<int>first(k);\n for(int i=0;i<k;i++){\n first[i]=v[i];\n }\n sort(first.begin(),first.end());\n if((k&1)==0){\n for(int i=0;i<k;i++){\n if(i<k/2)\n low.insert(first[i]);\n else high.insert(first[i]);\n }\n for(int i=0;i<medians.size();i++){\n double med=((double)*low.rbegin()+(double)*high.begin())/(double(2));\n medians[i]=med;\n if(i==medians.size()-1){\n break;\n }\n int next=v[i+k];\n if(next<=(*low.rbegin())){\n low.insert(next);\n }\n else{\n high.insert(next);\n }\n if(low.find(v[i])!=low.end()){\n low.erase(low.find(v[i]));\n }\n else{\n high.erase(high.find(v[i]));\n }\n if(low.size()>high.size()){\n int lst=*low.rbegin();\n auto iter=low.find(*low.rbegin());\n \n low.erase(iter);\n high.insert(lst);\n }\n else if(low.size()<high.size()) {\n int fst=*high.begin();\n high.erase(high.begin());\n low.insert(fst);\n }\n }\n \n }\n else{\n for(int i=0;i<k;i++){\n if(i<=k/2)\n low.insert(first[i]);\n else high.insert(first[i]);\n }\n for(int i=0;i<medians.size();i++){\n double med=(double)*low.rbegin();\n medians[i]=med;\n if(i==medians.size()-1){\n break;\n }\n int next=v[i+k];\n cout<<next<<endl;\n if(next<=(*low.rbegin())){\n low.insert(next);\n }\n else{\n high.insert(next);\n }\n if(low.find(v[i])!=low.end()){\n low.erase(low.find(v[i]));\n }\n else{\n high.erase(high.find(v[i]));\n }\n // cout<<low.size()<<\" \"<<high.size()<<endl;\n if(low.size()>high.size()+1){\n int lst=*low.rbegin();\n auto iter=low.find(*low.rbegin());\n \n low.erase(iter);\n high.insert(lst);\n }\n else if(low.size()+1==high.size()) {\n int fst=*high.begin();\n high.erase(high.begin());\n low.insert(fst);\n }\n }\n }\n return medians;\n }\n};", "memory": "46990" }
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>
2
{ "code": "class Solution {\npublic:\n vector<double> medianSlidingWindow(vector<int>& nums, int k) {\n // 2 multiset - lower and upper\n vector<double> medians(nums.size()-k+1);\n if (k==1) {\n for (int i=0; i<nums.size(); i++) medians[i] = nums[i];\n return medians;\n }\n int m = k/2; \n int n = k-m;\n multiset<int> lower;\n multiset<int> upper;\n vector<int> v(nums.begin(), nums.begin()+k);\n sort(v.begin(), v.end());\n for (int i=0; i<k; i++) {\n if (i<m) {\n lower.insert(v[i]);\n } else {\n upper.insert(v[i]);\n }\n }\n for (int i=0; i+k<=nums.size(); i++) {\n // cout << \"i:\" << i << \", lower:\" << *prev(lower.end()) << \",upper:\" << *upper.begin() << endl;\n double median = lower.size()==upper.size()? ((double)(*upper.begin()) + *prev(lower.end()))/2 : *upper.begin();\n medians[i] = median;\n if (i+k == nums.size()) break;\n // remove\n int to_remove = nums[i];\n int to_insert = nums[i+k];\n if (to_remove == to_insert) continue;\n if (to_insert >= *upper.begin()) {\n upper.insert(to_insert);\n } else {\n lower.insert(to_insert);\n }\n if (to_remove >= *upper.begin()) {\n auto it = upper.find(to_remove);\n upper.erase(it);\n } else {\n auto it = lower.find(to_remove);\n lower.erase(it);\n }\n // readjust\n while (lower.size() > upper.size()) {\n int to_move = *prev(lower.end());\n lower.erase(prev(lower.end()));\n upper.insert(to_move);\n }\n while (upper.size() > lower.size() + 1) {\n int to_move = *upper.begin();\n upper.erase(upper.begin());\n lower.insert(to_move);\n }\n // cout << \"lower:\";\n // for (auto num : lower) {\n // cout << num << \",\";\n // }\n // cout << endl;\n // cout << \"upper:\";\n // for (auto num : upper) {\n // cout << num << \",\";\n // }\n // cout << endl;\n }\n return medians;\n }\n};", "memory": "47370" }
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>
2
{ "code": "class Solution {\npublic:\n vector<double> medianSlidingWindow(vector<int>& nums, int k) {\n // similar to median problem\n // stores lower (k+1)/2 values\n int n = nums.size();\n multiset<int> lower;\n // stores k/2 values\n multiset<int> upper;\n\n for(int i = 0; i < k; i ++) {\n if(lower.size() < (k+1)/2) {\n lower.insert(nums[i]);\n }\n else {\n if(nums[i] >= *lower.rbegin()) {\n upper.insert(nums[i]);\n } else {\n int x = *lower.rbegin();\n upper.insert(x);\n lower.erase(lower.find(x));\n lower.insert(nums[i]);\n }\n }\n }\n\n vector<double> ans(n-k+1);\n\n if(k % 2 == 0) ans[0] = ((double)(*lower.rbegin()) + *upper.begin())/2;\n else ans[0] = *lower.rbegin();\n\n for(int i = k; i < n; i ++) {\n if(nums[i-k] <= *lower.rbegin()) {\n lower.erase(lower.find(nums[i-k]));\n }\n else upper.erase(upper.find(nums[i-k]));\n\n if(lower.empty() || nums[i] >= *lower.rbegin()) {\n upper.insert(nums[i]);\n }\n else {\n lower.insert(nums[i]);\n }\n\n if(lower.size() > (k+1)/2) {\n int x = *lower.rbegin();\n upper.insert(x);\n lower.erase(lower.find(x));\n }\n else if(lower.size() < (k+1)/2) {\n int x = *upper.begin();\n lower.insert(x);\n upper.erase(upper.find(x));\n }\n\n if(k % 2 == 0) ans[i-k+1] = ((double)(*lower.rbegin()) + *upper.begin())/2;\n else ans[i-k+1] = *lower.rbegin();\n }\n\n return ans;\n }\n};", "memory": "47750" }
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>
2
{ "code": "class Solution {\npublic:\n vector<double> medianSlidingWindow(vector<int>& nums, int k) {\n // similar to median problem\n // stores lower (k+1)/2 values\n int n = nums.size();\n multiset<int> lower;\n // stores k/2 values\n multiset<int> upper;\n\n for(int i = 0; i < k; i ++) {\n if(lower.size() < (k+1)/2) {\n lower.insert(nums[i]);\n }\n else {\n if(nums[i] >= *lower.rbegin()) {\n upper.insert(nums[i]);\n } else {\n int x = *lower.rbegin();\n upper.insert(x);\n lower.erase(lower.find(x));\n lower.insert(nums[i]);\n }\n }\n }\n\n vector<double> ans(n-k+1);\n\n if(k % 2 == 0) ans[0] = ((double)(*lower.rbegin()) + *upper.begin())/2;\n else ans[0] = *lower.rbegin();\n\n for(int i = k; i < n; i ++) {\n if(nums[i] >= *lower.rbegin()) upper.insert(nums[i]);\n else lower.insert(nums[i]);\n\n if(nums[i-k] <= *lower.rbegin()) {\n lower.erase(lower.find(nums[i-k]));\n }\n else upper.erase(upper.find(nums[i-k]));\n\n if(lower.size() > (k+1)/2) {\n int x = *lower.rbegin();\n upper.insert(x);\n lower.erase(--lower.end());\n }\n else if(lower.size() < (k+1)/2) {\n int x = *upper.begin();\n lower.insert(x);\n upper.erase(upper.find(x));\n }\n\n if(k % 2 == 0) ans[i-k+1] = ((double)(*lower.rbegin()) + *upper.begin())/2;\n else ans[i-k+1] = *lower.rbegin();\n }\n\n return ans;\n }\n};", "memory": "47750" }
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>
2
{ "code": "class Solution {\npublic:\n vector<double> medianSlidingWindow(vector<int>& nums, int k) {\n // build the 2 set\n vector<int> first_k(nums.begin(), nums.begin() + k-1);\n sort(first_k.begin(), first_k.end());\n multiset<int> lower, higher;\n for (int i = 0; i < k / 2; i ++) {\n lower.insert(first_k[i]);\n }\n for (int i = k / 2; i < k-1; i ++) {\n higher.insert(first_k[i]);\n }\n int n = nums.size();\n vector<double> ans;\n for (int i = k-1; i < n; i ++) {\n // insert\n if (lower.size() == 0 or *lower.rbegin() > nums[i]) {\n lower.insert(nums[i]);\n } else {\n higher.insert(nums[i]);\n }\n // delete\n if (i-k >= 0) {\n if (lower.count(nums[i-k]) != 0) {\n lower.erase(lower.find(nums[i-k]));\n } else {\n higher.erase(higher.find(nums[i-k]));\n }\n }\n cout << lower.size() << \" \" << higher.size() << endl;\n // rebalance\n for (int j = 0; j < 2; j ++) {\n if (lower.size() > higher.size() + 1) {\n int temp = *lower.rbegin();\n lower.erase(lower.find(temp));\n higher.insert(temp);\n } else if (higher.size() > lower.size() + 1) {\n int temp = *higher.begin();\n higher.erase(higher.find(temp));\n lower.insert(temp);\n }\n }\n cout << lower.size() << \" \" << higher.size() << endl;\n if (k % 2 == 1) {\n if (lower.size() == higher.size() + 1) {\n ans.push_back(*lower.rbegin());\n } else {\n ans.push_back(*higher.begin());\n }\n } else {\n cout << *lower.rbegin() << \" \" << *higher.begin() << endl;\n ans.push_back((1.0*(*lower.rbegin()) + 1.0*(*higher.begin()))/2.0);\n }\n }\n return ans;\n }\n};", "memory": "48130" }
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>
2
{ "code": "class Solution {\npublic:\n vector<double> medianSlidingWindow(vector<int>& nums, int k) {\n multiset<int> left, right;\n vector<int> win;\n for (int i = 0; i<k; i++){\n win.push_back(nums[i]);\n }\n // cout << win[0] << win[1] << win[2] << \"\\n\";\n sort(win.begin(), win.end());\n double median = (k%2 == 1) ? win[k/2] : (((double)win[k/2]+(double)win[(k-1)/2]))/2;\n // cout << median << win[0] << win[1] << win[2] << \"\\n\";\n vector<double> ans = {median};\n for (int i = 0; i<k; i++){\n if (nums[i] <= median+0.1) left.insert(nums[i]);\n else right.insert(nums[i]);\n }\n // make_heap(left.begin(), left.end());\n // make_heap(right.begin(), right.end());\n for (int i=0; i<nums.size()-k; i++){\n if (nums[i] <= median+0.1){\n // pop_heap(left.begin(), left.end());\n // left.pop_back(nums[i]);\n left.erase(left.find(nums[i]));\n }\n else{\n // pop_heap(right.begin(), right.end());\n // right.pop(-nums[i]);\n right.erase(right.find(nums[i]));\n }\n if (nums[i+k] <= median+0.1){\n // pop_heap(left.begin(), left.end());\n // left.pop(nums[i+k]);\n left.insert(nums[i+k]);\n }\n else{\n // pop_heap(right.begin(), right.end());\n // right.pop(-nums[i]);\n right.insert(nums[i+k]);\n }\n while (left.size() - right.size() != k%2){\n // pop_heap(left.begin(), left.end());\n // int elem = left.pop_back();\n // right.push_back(-elem);\n // push_heap(right.begin(), right.end());\n // cout << left.size() << right.size() << \"\\n\";\n if (left.size() > right.size()){\n if (left.empty()){\n cout << \"left\";\n return {};\n }\n int elem = *(left.rbegin());\n left.erase(left.find(elem));\n right.insert(elem);\n }\n else{\n if (right.empty()){\n cout << \"right\";\n return {};\n }\n int elem = *(right.begin());\n // cout << \"right\" << elem << \"\\n\";\n right.erase(right.find(elem));\n left.insert(elem);\n }\n }\n if (left.empty()){\n cout << \"med\";\n return {};\n }\n median = (k%2 == 1) ? *(left.rbegin()) : ((double)((double)(*(left.rbegin()) + (double)*(right.begin()))))/2;\n ans.push_back(median);\n }\n return ans;\n }\n};", "memory": "48130" }
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>
2
{ "code": "class Solution {\npublic:\n vector<double> medianSlidingWindow(vector<int>& nums, int k) {\n vector<double> result;\n if(k == 1) {\n for(int i : nums) result.push_back(i);\n return result;\n }\n int n = nums.size();\n int lsz = k/2, rsz = k-k/2;\n multiset<long long> l, r;\n vector<int> initialWindow;\n for(int i=0;i<k;i++) initialWindow.push_back(nums[i]);\n sort(initialWindow.begin(), initialWindow.end());\n for(int i=0;i<lsz;i++) l.insert(initialWindow[i]);\n for(int i=lsz;i<lsz+rsz;i++) r.insert(initialWindow[i]);\n for(int i=k;;i++) {\n //-------------\n // cout << \"Left Set : \\n\";\n // for(int j : l) cout << j << \", \";\n // cout << \"\\nRight Set : \\n\";\n // for(int j : r) cout << j << \", \";\n // cout << \"\\n----------------\\n\";\n //-------------\n if(k%2) result.push_back(*(r.begin()));\n else result.push_back((*(l.rbegin()) + *(r.begin()))/2.0);\n if(i == n) break;\n if(l.count(nums[i-k])) l.erase(l.find(nums[i-k]));\n else r.erase(r.find(nums[i-k]));\n if(int(l.size()) != lsz) {\n if(nums[i] <= *(r.begin())) l.insert(nums[i]);\n else {\n l.insert(*(r.begin()));\n r.erase(r.begin());\n r.insert(nums[i]);\n }\n }\n else {\n if(nums[i] >= *(l.rbegin())) r.insert(nums[i]);\n else {\n r.insert(*(l.rbegin()));\n l.erase(l.find(*(l.rbegin())));\n l.insert(nums[i]);\n }\n }\n }\n return result;\n }\n};", "memory": "48510" }
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>
2
{ "code": "class Solution {\npublic:\n vector<double> medianSlidingWindow(vector<int>& nums, int k) {\n if (k == 1)\n {\n vector<double> res;\n for (auto it : nums)\n {\n res.push_back(it * 1.0);\n }\n\n return res;\n }\n vector<int> arr;\n for (int i = 0; i < k; i++)\n {\n arr.push_back(nums[i]);\n }\n\n sort(arr.begin(), arr.end());\n\n multiset<int> s1;\n multiset<int> s2;\n for (int i = 0; i < k/2; i++)\n {\n s1.insert(arr[i]);\n }\n\n for (int i = k/2; i < k; i++)\n {\n s2.insert(arr[i]);\n }\n\n vector<double> res;\n\n if (k % 2)\n {\n auto it = s2.begin();\n res.push_back(*it);\n }\n else\n {\n auto it1 = s1.rbegin();\n auto it2 = s2.begin();\n res.push_back(((long) ((long) *it1 + (long) *it2)) / 2.0);\n }\n\n int n = nums.size();\n for (int i = k; i < n; i++)\n {\n auto it1 = s1.rbegin();\n auto it2 = s2.begin();\n\n int v1 = *it1;\n int v2 = *it2;\n\n bool delete1 = false;\n\n if (nums[i - k] <= v1)\n {\n int count = s1.count(nums[i-k]);\n s1.erase(nums[i - k]);\n for (int j = 0; j < count-1; j++)\n {\n s1.insert(nums[i - k]);\n }\n delete1 = true;\n }\n else\n {\n int count = s2.count(nums[i-k]);\n s2.erase(nums[i - k]);\n for (int j = 0; j < count-1; j++)\n {\n s2.insert(nums[i - k]);\n }\n }\n\n bool insert1 = false;\n if (nums[i] <= v1)\n {\n s1.insert(nums[i]);\n insert1 = true;\n }\n else\n {\n s2.insert(nums[i]);\n }\n\n if (delete1)\n {\n if (insert1)\n {\n // do nothing\n }\n else\n {\n auto it = s2.begin();\n int val = *it;\n int count = s2.count(val);\n s2.erase(val);\n s1.insert(val);\n for (int j = 0; j < count-1; j++)\n {\n s2.insert(val);\n }\n }\n }\n else\n {\n if (insert1)\n {\n auto it = s1.rbegin();\n int val = *it;\n int count = s1.count(val);\n s1.erase(val);\n s2.insert(val);\n for (int j = 0; j < count-1; j++)\n {\n s1.insert(val);\n }\n }\n else\n {\n //do nothing\n }\n }\n\n if (k % 2)\n {\n auto it = s2.begin();\n res.push_back(*it);\n }\n else\n {\n auto it1 = s1.rbegin();\n auto it2 = s2.begin();\n res.push_back(((long) ((long) *it1 + (long) *it2)) / 2.0);\n }\n }\n\n return res;\n }\n};", "memory": "48890" }
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>
2
{ "code": "class Solution {\npublic:\n vector<double> medianSlidingWindow(vector<int>& nums, int k) {\n vector<double> res;\n res.reserve(nums.size());\n multiset<int> leftPart, rightPart;\n int leftSize = (k+1) / 2;\n int rightSize = k - leftSize;\n for (size_t i = 0; i < nums.size(); ++i) {\n if (leftPart.size() < leftSize) {\n leftPart.insert(nums[i]);\n } else {\n auto it = --leftPart.end();\n if (*it <= nums[i]) {\n rightPart.insert(nums[i]);\n } else {\n auto val = *it;\n leftPart.erase(it);\n leftPart.insert(nums[i]);\n rightPart.insert(val);\n }\n }\n if (leftPart.size() + rightPart.size() == k) {\n if (leftSize == rightSize) {\n auto it1 = --leftPart.end();\n auto it2 = rightPart.begin();\n res.push_back((*it1 +(long long) *it2) / (double) 2);\n } else {\n auto it = --leftPart.end();\n res.push_back(*it);\n }\n int erase_val = nums[i+1-k];\n auto it = leftPart.find(erase_val);\n if (it != leftPart.end()) {\n leftPart.erase(it);\n if (rightPart.size()) {\n leftPart.insert(*rightPart.begin());\n rightPart.erase(rightPart.begin());\n }\n } else {\n it = rightPart.find(erase_val);\n rightPart.erase(it); \n }\n }\n }\n return res;\n }\n};", "memory": "48890" }
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>
2
{ "code": "//5:48\n/*\n1,3,-1,-3,5,3,6,7\n\n1,3,-1,-3\n\n-3,-1,1,3\n\n3,1\n\n-1,-3\n\ngreater k/2 min and smaller k/2 max ka average is our answer\n\ntake first k elements\nsort them,\nput first k/2 in a max heap\nand last k/2 in a min heap\n\n3,-1,-3,5\n-3,-1,3,5\n\n\n\n1,3,-1,-3,5,3,6,7\n1,3,-1,-3\n\n-3,-1,1,3\n\npop out 1\n\nall elements of minh > all elements of maxh\n\n-3,-1 3\nnew element is 5\nif 5 > maxh.top then push 5 in min heap\nif 5 < maxh.top then push 5 in max h\n\nnow if heaps are unbalanced, then pop from larger and push in smaller\nget median same way\n\nif it was odd\n\n-3 -1 3\n\n-3 is gone, push middle value to that heap\nnow again same\n\n\n\nlet's say I can pop out if it is a node structure\nwhere to put the new one in?\n\nif k is odd\n-1,1,3\n\n-1\ncenter chap will always be the median\n3\n\n\n-1,1,3: 1\n-3,-1,3: -1\n\nk is even or k is odd\n\nif k is odd\n\nbrute force is every time sort the k elements\nk can go to 1e5, klogk = 1e7 * n = 1e5 - will not pass\n\nsince we can have n-k traversals - n log k will work\n\n1,3,-1 sorted\n\nremove 1 and add -3\n\nwhat about two heaps? \n\nif k is even\n\nhave a max heap of k/2\nmin heap of k/2\n\n\n\n\n\n*/\n\nclass Solution {\nprivate:\n typedef pair<int,int> pii;\n class mapheap {\n private:\n map<int,int> hm;\n int nsize;\n bool ismaxheap;\n public:\n mapheap(vector<int> &vals, bool ismaxheap = false) {\n nsize = vals.size();\n this->ismaxheap = ismaxheap;\n for (int val: vals) hm[val]++;\n }\n int size() {\n return nsize;\n }\n int top() {\n if (!hm.size()) return INT_MAX;\n if (ismaxheap) \n return hm.rbegin()->first;\n else\n return hm.begin()->first;\n }\n void pop() {\n if (!hm.size()) return;\n if (ismaxheap) {\n auto it = hm.rbegin();\n it->second--;\n nsize--;\n if (!it->second) hm.erase(it->first);\n }\n else {\n auto it = hm.begin();\n it->second--;\n nsize--;\n if (!it->second) hm.erase(it->first);\n }\n }\n void push(int val) {\n hm[val]++; nsize++;\n }\n bool isPresent(int val) {\n return hm.count(val);\n }\n void remove(int val) {\n if (!hm.count(val)) return;\n hm[val]--;\n nsize--;\n if (!hm[val]) hm.erase(val);\n }\n };\n void removefromPQ(int val, mapheap *pq, int k, int center) {\n pq->remove(val);\n if (k & 1) pq->push(center);\n }\n void removePrev(int val, mapheap *pqMax, mapheap *pqMin, int k, int center) {\n if (pqMax->isPresent(val))\n removefromPQ(val, pqMax, k, center);\n else if (pqMin->isPresent(val))\n removefromPQ(val, pqMin, k, center);\n }\n \n void addNew(int val, mapheap *pqMax, mapheap *pqMin, int k, int center) {\n if (val <= pqMax->top())\n pqMax->push(val);\n else\n pqMin->push(val);\n }\n\n void shuffleheaps(mapheap *larger,mapheap *smaller,int k,int &center) {\n while (larger->size() > smaller->size()) {\n if (k & 1)\n center = larger->top();\n else\n smaller->push(larger->top());\n larger->pop();\n }\n }\n vector<double> k1case(vector<int>& nums) {\n int n = nums.size();\n vector<double> rv;\n for (int i = 0; i < n; i++) {\n rv.push_back(nums[i]);\n }\n return rv;\n }\n vector<double> k2case(vector<int>& nums) {\n int n = nums.size();\n vector<double> rv;\n double sum = 0;\n sum = (double) nums[0] + (double) nums[1];\n rv.push_back(sum/2);\n\n for (int i = 1; i < n-1; i++) {\n sum -= nums[i-1];\n sum += nums[i+1];\n rv.push_back(sum/2);\n }\n return rv;\n }\npublic:\n vector<double> medianSlidingWindow(vector<int>& nums, int k) {\n int n = nums.size();\n if (k == 1) return k1case(nums);\n if (k == 2) return k2case(nums);\n vector<int> firstk;\n for (int i = 0; i < k; i++) {\n firstk.push_back(nums[i]);\n }\n sort(firstk.begin(), firstk.end());\n vector<int> vfirst, vsecond;\n for (int i = 0; i < k/2; i++) {\n vfirst.push_back(firstk[i]);\n vsecond.push_back(firstk[k-1-i]);\n } \n mapheap* pqMax = new mapheap(vfirst, true);\n mapheap* pqMin = new mapheap(vsecond, false);\n int center = 0;\n if (k & 1) center = firstk[k/2];\n int s = 0, e = k-1;\n vector<double> rv;\n while (e < n) {\n double median;\n if (k & 1) {\n median = center;\n }\n else {\n median = (double) pqMax->top() + (double) pqMin->top();\n median /= 2;\n }\n rv.push_back(median);\n removePrev(nums[s], pqMax, pqMin, k, center);\n s++;\n e++;\n if (e >= n) break;\n addNew(nums[e],pqMax, pqMin, k, center);\n if (pqMax->size() < pqMin->size())\n shuffleheaps(pqMin, pqMax, k, center);\n else\n shuffleheaps(pqMax, pqMin, k, center);\n\n //cout << \"\\ns = \" << s << \"; ismaxlarger = \" << ismaxlarger << \"; maxh size = \" << maxh.size() << \"; minh.size = \" << minh.size();\n //remove nums[s] from the applicable heap\n //if k is even, put center in the same heap\n //add nums[e] to the applicable heap\n //if k is even then pop element from the larger size heap to center\n //if k is odd then resize heaps until both become similar\n }\n return rv;\n \n }\n};", "memory": "49270" }
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>
2
{ "code": "class Solution {\npublic:\n vector<double> medianSlidingWindow(vector<int>& nums, int k) {\n set<pair<int, int>> maxh; // get max element using *(maxh.rbegin());\n set<pair<int, int>> minh; // get min element using *(minh.begin());\n \n for(int i = 0; i < k; i++) {\n maxh.insert({nums[i], i}); // a {number, index} pair will always be unique (for handling duplicates)\n }\n \n for(int i = 0; i < k/2; i++) {\n minh.insert(*maxh.rbegin());\n maxh.erase(*maxh.rbegin());\n }\n \n vector<double> res;\n \n int n = nums.size();\n \n for(int i = k; i < n; i++) {\n if(k % 2 == 0) {\n res.push_back(((double)(maxh.rbegin())->first + (double)(minh.begin())->first) / 2.0);\n } else {\n res.push_back((double) (maxh.rbegin())->first);\n }\n \n // delete the element that is out of window, deletion in O(log k)\n \n if(maxh.count({nums[i-k], i-k})) {\n maxh.erase({nums[i-k], i-k});\n }\n \n if(minh.count({nums[i-k], i-k})) {\n minh.erase({nums[i-k], i-k});\n }\n \n // insert new element\n \n if(maxh.size() == 0 || nums[i] <= (maxh.rbegin())->first) {\n maxh.insert({nums[i], i});\n } else {\n minh.insert({nums[i], i});\n }\n \n // balance the sets/heaps\n \n if(minh.size() > maxh.size()) {\n pair<int, int> x = *(minh.begin());\n minh.erase(x);\n maxh.insert(x);\n }\n \n // maxh can have at max one more element than minh\n \n if(maxh.size() > minh.size()+1) {\n pair<int, int> x = *(maxh.rbegin());\n maxh.erase(x);\n minh.insert(x);\n }\n }\n \n if(k % 2 == 0) {\n res.push_back(((double)(maxh.rbegin())->first + (double)(minh.begin())->first) / 2.0);\n } else {\n res.push_back((double) (maxh.rbegin())->first);\n }\n \n return res;\n }\n};", "memory": "50790" }
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>
2
{ "code": "class Solution {\nprivate:\n\n multiset<long long,greater<long long>> win_l;\n multiset<long long> win_r;\n\n void balanceSets() {\n while (win_l.size() > win_r.size() + 1){\n win_r.insert(*win_l.begin());\n win_l.erase(win_l.begin());\n }\n\n while (win_l.size() < win_r.size()) {\n win_l.insert(*win_r.begin());\n win_r.erase(win_r.begin());\n }\n }\n\n double getMedian() {\n if (win_l.size() > win_r.size()) return (double)*win_l.begin();\n else return ((double)(*win_l.begin() + *win_r.begin()))/2.0;\n }\n\npublic:\n vector<double> medianSlidingWindow(vector<int> &nums, int k) {\n vector<double> median;\n for (int i = 0; i < nums.size(); i++) {\n if (i < k) {\n win_l.insert(nums[i]);\n } \n else {\n balanceSets();\n median.push_back(getMedian());\n \n if (nums[i-k] <= *win_l.begin()) win_l.erase(win_l.find(nums[i-k]));\n else win_r.erase(win_r.find(nums[i-k]));\n \n if (nums[i] <= *win_l.begin()) win_l.insert(nums[i]);\n else win_r.insert(nums[i]);\n }\n }\n balanceSets();\n median.push_back(getMedian());\n return median;\n }\n};\n", "memory": "50790" }
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>
2
{ "code": "#define pb push_back\n#define ll long long\nclass Solution {\n struct comp{\n bool operator()(const ll &l,const ll &r) const{\n return l > r;\n }\n };\npublic:\n vector<double> medianSlidingWindow(vector<int>& nums, int k) {\n int n = nums.size();\n multiset<ll>st; // right\n multiset<ll,comp>st1; // left\n int med = 0;\n for(int i=0;i<k;i++){\n st.insert(nums[i]);\n }\n for(int i =0;i<(k+1)/2;i++){\n st1.insert(*st.begin());\n st.erase(st.begin());\n }\n vector<double>ans;\n if(k == 1){\n for(auto it:nums)ans.pb(it*1.0);\n return ans;\n }\n for(int i =k;i<=n;i++){\n // cout<<(*st.begin())<<\" \"<<(*st1.begin())<<endl;\n if(k&1)ans.pb((*st1.begin()));\n else ans.pb( ((*st.begin()) +(*st1.begin()))/2.0);\n if(i == n)break;\n if(k&1){\n if(st.count(nums[i-k])){\n st.erase(st.find(nums[i-k]));\n }\n else st1.erase(st1.find(nums[i-k]));\n while(st.size()!=st1.size()){\n st.insert(*st1.begin());\n st1.erase(st1.begin());\n }\n if(nums[i]>*st.begin()){\n st1.insert(*st.begin()); st.erase(st.begin());\n st.insert(nums[i]);\n }\n else st1.insert(nums[i]);\n } \n else{\n if(st.count(nums[i-k])){\n st.erase(st.find(nums[i-k]));\n }\n else st1.erase(st1.find(nums[i-k]));\n\n if(st1.size()<st.size()){\n if(nums[i]>*st.begin()){\n st1.insert(*st.begin()); st.erase(st.begin());\n st.insert(nums[i]);\n }\n else st1.insert(nums[i]);\n }\n else{\n if(nums[i]<*st1.begin()){\n st.insert(*st1.begin()); st1.erase(st1.begin());\n st1.insert(nums[i]);\n }\n else st.insert(nums[i]); \n\n }\n } \n } \n return ans;\n }\n};", "memory": "51170" }
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>
2
{ "code": "class Solution {\npublic:\n vector<double> medianSlidingWindow(vector<int>& nums, int k) {\n \n if ((k&1) == 1)\n {\n vector<double> ans(nums.size()+1-k);\n multiset<int> lower, upper;\n for (int i = 0 ; i < k - 1 ; i++)\n {\n upper.insert(nums[i]);\n }\n while (int(lower.size()) - int(upper.size()) < 0)\n {\n auto ele = (upper.begin());\n lower.insert(*ele);\n upper.erase(ele);\n }\n for (int start = 0 ; start < ans.size() ; start++)\n {\n int val = nums[start+k-1];\n if (val < *(upper.begin()))\n {\n lower.insert(val);\n }\n else\n {\n upper.insert(val);\n lower.insert(*upper.begin());\n upper.erase(upper.begin());\n }\n \n auto ele = lower.crbegin();\n ans[start] = *(ele);\n \n if (lower.find(nums[start]) == lower.end())\n {\n upper.erase(upper.find(nums[start]));\n upper.insert(*(lower.crbegin()));\n lower.erase(lower.find(*(lower.crbegin())));\n }\n else\n {\n lower.erase(lower.find(nums[start]));\n }\n \n }\n return ans;\n }\n vector<double> ans(nums.size()+1-k);\n multiset<int> lower, upper;\n for (int i = 0 ; i < k - 1 ; i++)\n {\n upper.insert(nums[i]);\n }\n while (lower.size() < upper.size())\n {\n auto ele = (upper.begin());\n lower.insert(*ele);\n upper.erase(ele); \n }\n for (int start = 0 ; start < ans.size() ; start++)\n {\n int val = nums[start+k-1];\n if (val >= *(lower.crbegin()))\n {\n upper.insert(val);\n }\n else\n {\n int a = *(lower.crbegin());\n upper.insert(a);\n lower.erase(lower.find(a));\n lower.insert(val);\n }\n \n \n ans[start] = (double(*lower.crbegin()) + (*upper.begin()) ) / 2.0;\n \n if (lower.find(nums[start]) == lower.end())\n {\n upper.erase(upper.find(nums[start]));\n }\n else\n { \n lower.erase(lower.find(nums[start]));\n lower.insert(*(upper.begin()));\n upper.erase(upper.begin());\n }\n \n } \n\n return ans;\n }\n};", "memory": "51170" }
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>
2
{ "code": "class Solution {\nprivate:\n double GetMedian(multiset<int, greater<int> >& left, multiset<int>& right, int k) {\n if(k%2) {\n return double(*left.begin());\n }\n return (*left.begin())/2.0 + (*right.begin())/2.0;\n }\n void Balance(multiset<int, greater<int> >& left, multiset<int>& right) {\n if(right.size() > left.size()) {\n left.insert(*right.begin());\n right.erase(right.begin());\n }\n else if(left.size() > right.size() + 1) {\n right.insert(*left.begin());\n left.erase(left.begin());\n }\n }\n void Insert(multiset<int, greater<int> >& left, multiset<int>& right, int key) {\n if(left.size() == 0 || key > *left.begin()) {\n right.insert(key);\n }\n else {\n left.insert(key);\n }\n Balance(left, right);\n }\n void Delete(multiset<int, greater<int> >& left, multiset<int>& right, int key) {\n if(left.find(key) != left.end()) {\n left.erase(left.find(key));\n Balance(left, right);\n }\n else if(right.find(key) != right.end()) {\n right.erase(right.find(key));\n Balance(left, right);\n }\n }\npublic:\n vector<double> medianSlidingWindow(vector<int>& nums, int k) {\n multiset<int, greater<int> > left;\n multiset<int> right;\n int n = nums.size();\n for(int i=0; i<k; ++i) {\n Insert(left, right, nums[i]);\n }\n vector<double> medians;\n medians.push_back(GetMedian(left, right, k));\n for(int i=k; i<n; ++i) {\n Delete(left, right, nums[i-k]);\n Insert(left, right, nums[i]);\n medians.push_back(GetMedian(left, right, k));\n }\n return medians;\n }\n};", "memory": "51550" }
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>
2
{ "code": "class Solution {\npublic:\n \n\n\n vector<double> medianSlidingWindow(vector<int>& nums, int k) {\n multiset<int> minS;\n multiset<int> maxS;\n\n for (int i = 0; i < k; i++)\n add(maxS, minS, nums[i]);\n\n vector<double> sol;\n sol.push_back(getMedian(maxS, minS));\n\n for (int i = k; i < nums.size(); i++) {\n add(maxS, minS, nums[i]);\n remove(maxS, minS, nums[i - k]);\n\n sol.push_back(getMedian(maxS, minS));\n }\n\n return sol;\n\n }\n\n void remove(multiset<int>& maxS, multiset<int>& minS, int num) {\n auto mxi = maxS.find(num);\n\n if (mxi != maxS.end()) {\n maxS.erase(mxi);\n } else {\n auto mni = minS.find(num);\n\n minS.erase(mni);\n }\n\n if (maxS.size() > minS.size() + 1) {\n minS.insert(*maxS.rbegin());\n maxS.erase(prev(maxS.end()));\n } else if (minS.size() > maxS.size() + 1) {\n maxS.insert(*minS.begin());\n minS.erase(minS.begin());\n }\n }\n\n void add(multiset<int>& maxS, multiset<int>& minS, int num) {\n if (maxS.empty()) {\n maxS.insert(num);\n return;\n }\n \n \n if (num < *maxS.rbegin())\n maxS.insert(num);\n else\n minS.insert(num);\n\n\n if (maxS.size() > minS.size() + 1) {\n minS.insert(*maxS.rbegin());\n maxS.erase(prev(maxS.end()));\n } else if (minS.size() > maxS.size() + 1) {\n maxS.insert(*minS.begin());\n minS.erase(minS.begin());\n }\n } \n\n double getMedian(multiset<int>& maxS, multiset<int>& minS) {\n int tot = maxS.size() + minS.size();\n\n if (tot == 1)\n return (double) !maxS.empty() ? *maxS.begin() : *minS.begin();\n\n if (tot % 2) {\n return maxS.size() > minS.size() ? *maxS.rbegin() : *minS.begin();\n } else {\n return (double) ((double) *maxS.rbegin() + *minS.begin())/2.0;\n }\n }\n\n};", "memory": "51550" }
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>
2
{ "code": "\n\nclass Solution {\npublic:\nmultiset<int>left,right;\nint siz;\nvoid ins(int x){\n siz++;\n if(left.empty()&&right.empty()){\n right.insert(x);\n return;\n }\n if(x>(*right.begin())){\n right.insert(x);\n if(right.size()>left.size()+siz%2){\n left.insert(*right.begin());\n right.erase(right.begin());\n }\n }\n else{\n left.insert(x);\n \n if(right.size()<left.size()+siz%2){\n right.insert(*(left.rbegin()));\n left.erase((--left.end()));\n }\n }\n \n\n}\ndouble getmeadin(){\n if(siz%2==1){\n return *right.begin();\n }\n else{\n double ans=*right.begin();\n ans=ans+(*(left.rbegin()));\n ans/=2;\n return ans;\n }\n}\nvoid del(int x){\n siz--;\n if(left.find(x)!=left.end()){\n left.erase(left.find(x));\n }\n else{\n if(right.find(x)!=right.end())right.erase(right.find(x));\n }\n if(right.size()>left.size()+siz%2){\n left.insert(*right.begin());\n right.erase(right.begin());\n }\n if(right.size()<left.size()+siz%2){\n right.insert(*(left.rbegin()));\n left.erase((--left.end()));\n }\n}\n vector<double> medianSlidingWindow(vector<int>& nums, int k) {\n int n=nums.size();\n left.clear();right.clear();siz=0;\n vector<double>ans(n-k+1);\n for(int i=0;i<k-1;i++)ins(nums[i]);\n for(int i=k-1;i<n;i++){\n // cout<<nums[i]<<\" L \";\n ins(nums[i]);\n ans[i-k+1]=getmeadin();\n del(nums[i-k+1]);\n }\n return ans; \n }\n};", "memory": "51930" }
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>
2
{ "code": "class Solution {\npublic:\n vector<double> medianSlidingWindow(vector<int>& nums, int k) {\n set<pair<int, int>> maxh, minh;\n \n // Initialize maxh with the first k elements\n for (int i = 0; i < k; i++) {\n maxh.insert({nums[i], i});\n }\n \n // Balance maxh and minh\n for (int i = 0; i < k / 2; i++) {\n minh.insert(*maxh.rbegin());\n maxh.erase(*maxh.rbegin());\n }\n \n vector<double> res;\n int n = nums.size();\n \n for (int i = k; i < n; i++) {\n // Calculate the median\n res.push_back(k % 2 == 0 ? \n ((double)(maxh.rbegin())->first + (double)(minh.begin())->first) / 2.0 : \n (double)(maxh.rbegin())->first);\n \n // Remove the element that is out of the window\n removeElement(maxh, nums[i - k], i - k);\n removeElement(minh, nums[i - k], i - k);\n \n // Insert the new element\n insertElement(maxh, minh, nums[i], i);\n \n // Balance the sets\n balanceSets(maxh, minh);\n }\n \n // Calculate the final median\n res.push_back(k % 2 == 0 ? \n ((double)(maxh.rbegin())->first + (double)(minh.begin())->first) / 2.0 : \n (double)(maxh.rbegin())->first);\n \n return res;\n }\n \nprivate:\n void removeElement(set<pair<int, int>>& s, int num, int idx) {\n if (s.count({num, idx})) {\n s.erase({num, idx});\n }\n }\n \n void insertElement(set<pair<int, int>>& maxh, set<pair<int, int>>& minh, int num, int idx) {\n if (maxh.empty() || num <= maxh.rbegin()->first) {\n maxh.insert({num, idx});\n } else {\n minh.insert({num, idx});\n }\n }\n \n void balanceSets(set<pair<int, int>>& maxh, set<pair<int, int>>& minh) {\n if (minh.size() > maxh.size()) {\n pair<int, int> x = *minh.begin();\n minh.erase(x);\n maxh.insert(x);\n }\n if (maxh.size() > minh.size() + 1) {\n pair<int, int> x = *maxh.rbegin();\n maxh.erase(x);\n minh.insert(x);\n }\n }\n};", "memory": "51930" }
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>
2
{ "code": "class Solution {\n private:\n multiset<double>MinH, MaxH;\n vector<double> ans;\n \n public: \n void balance()\n {\n if(MaxH.size() > MinH.size() + 1)\n {\n MinH.insert(*MaxH.rbegin());\n MaxH.erase(MaxH.find(*MaxH.rbegin()));\n }\n else if(MaxH.size() + 1 < MinH.size())\n {\n MaxH.insert(*MinH.begin());\n MinH.erase(MinH.find(*MinH.begin()));\n }\n }\n \n void addNum(double n)\n {\n \n if(MaxH.size()==0)\n MaxH.insert(n);\n else\n {\n if(n < *MaxH.rbegin())\n MaxH.insert(n);\n else\n MinH.insert(n);\n \n balance();\n } \n }\n \n void addAns()\n {\n if(MaxH.size() == MinH.size())\n ans.push_back((*MaxH.rbegin() + *MinH.begin())/2);\n else\n {\n if(MaxH.size() > MinH.size())\n ans.push_back(*MaxH.rbegin());\n else\n ans.push_back(*MinH.begin());\n }\n }\n \n void slideWindow(double n)\n {\n if(MaxH.size() and MaxH.find(n) != MaxH.end())\n MaxH.erase(MaxH.find(n));\n else\n MinH.erase(MinH.find(n));\n \n balance();\n }\n \n vector<double> medianSlidingWindow(vector<int>& nums, int k) {\n \n for(int i=0; i<nums.size(); i++)\n {\n addNum(nums[i]*1.0);\n if(i + 1 >= k)\n {\n addAns();\n slideWindow(nums[i-k+1]*1.0);\n }\n }\n return ans;\n }\n};", "memory": "52310" }
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>
2
{ "code": "class Solution {\npublic:\n class Two_Sets {\n public:\n multiset<int> max_set;\n multiset<int> min_set;\n int size;\n Two_Sets (){\n size = 0;\n }\n void addNum(int num){\n if ( max_set.empty() ) max_set.insert(num);\n else{\n if ( *max_set.begin() > num ){\n min_set.insert(num);\n if ( size % 2 == 0 ){\n max_set.insert(*min_set.rbegin());\n min_set.erase(min_set.find(*min_set.rbegin()));\n }\n }\n else{\n max_set.insert(num);\n if ( size % 2 == 1 ){\n min_set.insert(*max_set.begin());\n max_set.erase(max_set.find(*max_set.begin()));\n }\n }\n }\n size++;\n }\n void remNum(int num){\n if ( max_set.contains(num) ){\n max_set.erase(max_set.find(num));\n if ( size % 2 == 0 ){\n max_set.insert(*min_set.rbegin());\n min_set.erase(min_set.find(*min_set.rbegin()));\n }\n }\n else{\n min_set.erase(min_set.find(num));\n if ( size % 2 == 1 ){\n min_set.insert(*max_set.begin());\n max_set.erase(max_set.find(*max_set.begin()));\n }\n }\n size--;\n }\n double GetMedian(){\n if ( size % 2 == 1 ) return *max_set.begin();\n else{\n double sum = (double)*max_set.begin() + *min_set.rbegin();\n return sum*0.5;\n // return (*max_set.begin() + *min_set.rbegin())*1.0/2;\n }\n // return min_set.size();\n }\n };\n vector<double> medianSlidingWindow(vector<int>& nums, int k) {\n vector<double> ans;\n int len = nums.size();\n Two_Sets S;\n for ( int i = 0; i < k; i++ ) S.addNum(nums[i]);\n ans.push_back(S.GetMedian());\n for ( int i = 0; i < len - k; i++ ){\n S.addNum(nums[k+i]);\n S.remNum(nums[i]);\n ans.push_back(S.GetMedian());\n }\n return ans; \n }\n};", "memory": "52310" }
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>
2
{ "code": "class TwoSet {\n multiset<int> low, high;\n bool is_odd;\n\npublic:\n TwoSet(){\n is_odd=false;\n }\n\n void remove(int& x){\n if (x<=*low.rbegin()) {\n low.erase(low.find(x));\n if (!is_odd) {\n low.insert(*high.begin());\n high.erase(high.begin());\n }\n }\n else {\n high.erase(high.find(x));\n if (is_odd){\n high.insert(*low.rbegin());\n low.erase(prev(low.end()));\n }\n }\n is_odd=!is_odd;\n }\n\n void add(int& x){\n if (low.empty()&&high.empty()){\n low.insert(x);\n }\n else if (x<=*low.rbegin()) {\n low.insert(x);\n if (is_odd) {\n high.insert(*low.rbegin());\n low.erase(prev(low.end()));\n }\n }\n else {\n high.insert(x);\n if (!is_odd){\n low.insert(*high.begin());\n high.erase(high.begin());\n }\n }\n is_odd=!is_odd;\n }\n\n double getMedian(){\n if (is_odd) return *low.rbegin();\n else return (double(*low.rbegin())+*high.begin())/2;\n }\n};\n\nclass Solution {\npublic:\n vector<double> medianSlidingWindow(vector<int>& nums, int k) {\n vector<double> soln;\n TwoSet t;\n for (int i=0; i<k; i++) t.add(nums[i]);\n soln.push_back(t.getMedian());\n\n for (int i=k; i<nums.size(); i++){\n t.remove(nums[i-k]);\n t.add(nums[i]);\n soln.push_back(t.getMedian());\n }\n\n return soln;\n }\n};", "memory": "52690" }
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>
2
{ "code": "class Solution {\npublic:\n class Two_Sets {\n public:\n multiset<int> max_set;\n multiset<int> min_set;\n int size;\n Two_Sets (){\n size = 0;\n }\n void addNum(int num){\n if ( max_set.empty() ) max_set.insert(num);\n else{\n if ( *max_set.begin() > num ){\n min_set.insert(num);\n if ( size % 2 == 0 ){\n max_set.insert(*min_set.rbegin());\n min_set.erase(min_set.find(*min_set.rbegin()));\n }\n }\n else{\n max_set.insert(num);\n if ( size % 2 == 1 ){\n min_set.insert(*max_set.begin());\n max_set.erase(max_set.find(*max_set.begin()));\n }\n }\n }\n size++;\n }\n void remNum(int num){\n if ( max_set.contains(num) ){\n max_set.erase(max_set.find(num));\n if ( size % 2 == 0 ){\n max_set.insert(*min_set.rbegin());\n min_set.erase(min_set.find(*min_set.rbegin()));\n }\n }\n else{\n min_set.erase(min_set.find(num));\n if ( size % 2 == 1 ){\n min_set.insert(*max_set.begin());\n max_set.erase(max_set.find(*max_set.begin()));\n }\n }\n size--;\n }\n double GetMedian(){\n if ( size % 2 == 1 ) return *max_set.begin();\n else{\n double sum = (double)*max_set.begin() + *min_set.rbegin();\n return sum*0.5;\n // return (*max_set.begin() + *min_set.rbegin())*1.0/2;\n }\n // return min_set.size();\n }\n };\n vector<double> medianSlidingWindow(vector<int>& nums, int k) {\n vector<double> ans;\n int len = nums.size();\n Two_Sets S;\n for ( int i = 0; i < k; i++ ) S.addNum(nums[i]);\n ans.push_back(S.GetMedian());\n for ( int i = 0; i < len - k; i++ ){\n S.addNum(nums[k+i]);\n S.remNum(nums[i]);\n ans.push_back(S.GetMedian());\n }\n return ans; \n }\n};", "memory": "52690" }
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>
2
{ "code": "class Solution {\npublic:\n vector<double> medianSlidingWindow(vector<int>& nums, int k) {\n int n=nums.size();\n int i=0;\n priority_queue<pair<double,int>>q1,q2;\n int count1=0,count2=0;\n vector<double>ans;\n unordered_set<int>us1,us2;\n // if(k%2==0){\n int j=0;\n while(j<i+k){\n q1.push({nums[j],j});\n us1.insert(j);\n j++;\n }\n int size=q1.size();\n size/=2;\n \n while(size>0){\n q2.push({-q1.top().first,q1.top().second});\n us2.insert(q1.top().second);\n us1.erase(q1.top().second);\n q1.pop();\n size--;\n }\n while(i+k-1<n){\n while(!q1.empty()&&q1.top().second<i){\n count1--;\n us1.erase(q1.top().second);\n q1.pop();\n }\n \n while(!q2.empty()&&q2.top().second<i){\n count2--;\n us2.erase(q2.top().second);\n q2.pop();\n }\n double first=q1.top().first;\n \n //cout<<q1.top().second<<\" \"<<q2.top().second<<\" \";\n if(k%2==0){double second=-q2.top().first; ans.push_back((first+second)/2);}\n else ans.push_back(first);\n if(j>=n) break;\n // int value=nums[i];\n if(us1.find(i)!=us1.end())\n count1++;\n else if(us2.find(i)!=us2.end()) count2++;\n else cout<<\"wtf\";\n\n i++;\n\n while(!q1.empty()&&q1.top().second<i){\n count1--;\n us1.erase(q1.top().second);\n q1.pop();\n }\n \n while(!q2.empty()&&q2.top().second<i){\n count2--;\n us2.erase(q2.top().second);\n q2.pop();\n }\n \n if(q1.empty()||q1.top().first<nums[j]){\n double value=nums[j];\n value*=-1;\n q2.push({value,j});\n us2.insert(j);\n }\n else{q1.push({nums[j],j});us1.insert(j);}\n\n while(!q1.empty()&&!q2.empty()&&q1.top().first>(-q2.top().first)){\n if(q2.top().second<i) {\n q2.pop();\n us2.erase(q2.top().second);\n count2--;\n continue;\n }\n if(q1.top().second<i) {\n q1.pop();\n us1.erase(q1.top().second);\n count1--;\n continue;\n }\n int value=-q2.top().first;\n int index=q2.top().second;\n us2.erase(index);\n q2.push({-q1.top().first,q1.top().second});\n \n us2.insert(q1.top().second);\n us1.erase(q1.top().second);\n q1.pop();\n q1.push({value,index});\n us1.insert(index);\n }\n \n int size1=q1.size()-max(count1,0);\n int size2=q2.size()-max(count2,0);\n if(k%2==1) size2++;\n // cout<<size1<<\" \"<<size2<<\" \"<<i+1<<\" \";\n while(size1<size2){\n int check=q2.top().second;\n if(check>=i){\n q1.push({-q2.top().first,q2.top().second});\n us1.insert(q2.top().second);\n us2.erase(q2.top().second);\n q2.pop();\n }\n else {\n q2.pop();\n us2.erase(check);\n count2--;\n }\n size1=q1.size()-max(count1,0);\n size2=q2.size()-max(count2,0);\n if(k%2==1) size2++;\n }\n while(size2<size1){\n int check=q1.top().second;\n if(check>=i){\n q2.push({-q1.top().first,q1.top().second});\n us2.insert(q1.top().second);\n us1.erase(q1.top().second);\n q1.pop();\n }\n else {\n q1.pop();\n us1.erase(check);\n count1--;\n }\n size1=q1.size()-max(count1,0);\n size2=q2.size()-max(count2,0);\n if(k%2==1) size2++;\n }\n j++;\n }\n \n return ans;\n }\n};", "memory": "53070" }
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>
2
{ "code": "class Solution {\npublic:\n vector<double> medianSlidingWindow(vector<int>& nums, int k) {\n int n=nums.size();\n int i=0;\n priority_queue<pair<double,int>>q1,q2;\n int count1=0,count2=0;\n vector<double>ans;\n unordered_set<int>us1,us2;\n // if(k%2==0){\n int j=0;\n while(j<i+k){\n q1.push({nums[j],j});\n us1.insert(j);\n j++;\n }\n int size=q1.size();\n size/=2;\n \n while(size>0){\n q2.push({-q1.top().first,q1.top().second});\n us2.insert(q1.top().second);\n us1.erase(q1.top().second);\n q1.pop();\n size--;\n }\n while(i+k-1<n){\n while(!q1.empty()&&q1.top().second<i){\n count1--;\n us1.erase(q1.top().second);\n q1.pop();\n }\n \n while(!q2.empty()&&q2.top().second<i){\n count2--;\n us2.erase(q2.top().second);\n q2.pop();\n }\n double first=q1.top().first;\n \n //cout<<q1.top().second<<\" \"<<q2.top().second<<\" \";\n if(k%2==0){double second=-q2.top().first; ans.push_back((first+second)/2);}\n else ans.push_back(first);\n if(j>=n) break;\n // int value=nums[i];\n if(us1.find(i)!=us1.end())\n count1++;\n else if(us2.find(i)!=us2.end()) count2++;\n else cout<<\"wtf\";\n\n i++;\n\n // while(!q1.empty()&&q1.top().second<i){\n // count1--;\n // us1.erase(q1.top().second);\n // q1.pop();\n // }\n \n // while(!q2.empty()&&q2.top().second<i){\n // count2--;\n // us2.erase(q2.top().second);\n // q2.pop();\n // }\n \n if(q1.empty()||q1.top().first<nums[j]){\n double value=nums[j];\n value*=-1;\n q2.push({value,j});\n us2.insert(j);\n }\n else{q1.push({nums[j],j});us1.insert(j);}\n\n // while(!q1.empty()&&!q2.empty()&&q1.top().first>(-q2.top().first)){\n // if(q2.top().second<i) {\n // q2.pop();\n // us2.erase(q2.top().second);\n // count2--;\n // continue;\n // }\n // if(q1.top().second<i) {\n // q1.pop();\n // us1.erase(q1.top().second);\n // count1--;\n // continue;\n // }\n // int value=-q2.top().first;\n // int index=q2.top().second;\n // us2.erase(index);\n // q2.push({-q1.top().first,q1.top().second});\n \n // us2.insert(q1.top().second);\n // us1.erase(q1.top().second);\n // q1.pop();\n // q1.push({value,index});\n // us1.insert(index);\n // }\n \n int size1=q1.size()-max(count1,0);\n int size2=q2.size()-max(count2,0);\n if(k%2==1) size2++;\n // cout<<size1<<\" \"<<size2<<\" \"<<i+1<<\" \";\n while(size1<size2){\n int check=q2.top().second;\n if(check>=i){\n q1.push({-q2.top().first,q2.top().second});\n us1.insert(q2.top().second);\n us2.erase(q2.top().second);\n q2.pop();\n }\n else {\n q2.pop();\n us2.erase(check);\n count2--;\n }\n size1=q1.size()-max(count1,0);\n size2=q2.size()-max(count2,0);\n if(k%2==1) size2++;\n }\n while(size2<size1){\n int check=q1.top().second;\n if(check>=i){\n q2.push({-q1.top().first,q1.top().second});\n us2.insert(q1.top().second);\n us1.erase(q1.top().second);\n q1.pop();\n }\n else {\n q1.pop();\n us1.erase(check);\n count1--;\n }\n size1=q1.size()-max(count1,0);\n size2=q2.size()-max(count2,0);\n if(k%2==1) size2++;\n }\n j++;\n }\n \n return ans;\n }\n};", "memory": "53070" }
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>
2
{ "code": "class Solution {\npublic:\n vector<double> medianSlidingWindow(vector<int>& nums, int k) {\n vector<double> ans;\n multiset<int, greater<int>> main;\n multiset<int> second;\n int i = 0, j = 0;\n while (j < nums.size()) {\n second.insert(nums.at(j));\n if (main.size() < (1 + k / 2)) {\n main.insert(*second.begin());\n second.erase(second.begin());\n }\n\n else if (*second.begin() < *main.begin()) {\n int temp = *main.begin();\n main.erase(main.begin());\n main.insert(*second.begin());\n second.erase(second.begin());\n second.insert(temp);\n }\n\n if (j < k - 1) {\n ++j;\n continue;\n }\n\n if (k % 2)\n ans.push_back(*main.begin());\n else\n ans.push_back(((long long)*main.begin() + *(++main.begin())) /\n 2.0);\n \n auto it = main.find(nums.at(i));\n if (it != main.end()) \n main.erase(it);\n else\n second.erase(second.find(nums.at(i)));\n\n ++i;\n ++j;\n }\n\n return ans;\n }\n};", "memory": "53450" }
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>
2
{ "code": "class Solution {\npublic:\n vector<double> medianSlidingWindow(vector<int>& nums, int k) {\n multiset<int,greater<int>>l; multiset<int>r;\n int n=nums.size();\n vector<int>temp;\n for(int i=0;i<k;i++)\n temp.push_back(nums[i]);\n sort(temp.begin(),temp.end());\n int till=k/2;\n if(k%2==0) till--;\n for(int i=0;i<=till;i++) l.insert(temp[i]);\n for(int i=till+1;i<k;i++) r.insert(temp[i]);\n vector<double>ans;\n if(k%2==0)ans.push_back(((*l.begin())*1LL+(*r.begin())*1LL)/2.0);\n else ans.push_back((*l.begin())*1.0);\n cout<<ans[0]<<endl;\n for(int i=k;i<n;i++)\n {\n if(l.find(nums[i-k])!=l.end()){\n l.erase(l.find(nums[i-k]));\n l.insert(nums[i]);\n }\n else if(r.find(nums[i-k])!=r.end())\n {\n r.erase(r.find(nums[i-k]));\n r.insert(nums[i]);\n }\n if(k!=1 && *l.begin()>*r.begin())\n {\n int temp=*l.begin();\n int temp1=*r.begin();\n l.erase(l.find(temp));\n l.insert(temp1);\n r.erase(r.find(temp1));\n r.insert(temp);\n }\n if(k%2==0)ans.push_back(((*l.begin())*1LL+(*r.begin())*1LL)/2.0);\n else ans.push_back((*l.begin())*1.0);\n\n }\n return ans;\n }\n};", "memory": "53450" }
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>
2
{ "code": "class Solution {\npublic:\n \n void ins(int i , multiset<long long int> &low , multiset<long long int> &high , vector<double> &v1) { \n if ( low.empty() ) { low.insert(i) ; }\n else { \n if ( i <= *(low.rbegin()) ) { \n if ( low.size() == high.size()) { low.insert(i) ; }\n else { \n int k = *(low.rbegin()) ;\n high.insert(k) ;\n low.erase(low.find(k)) ;\n low.insert(i) ;\n\n }\n }\n\n else { \n if ( low.size() == high.size() ) { \n\n int k = *(high.begin()) ;\n if ( i <= k) { low.insert(i);}\n else { \n low.insert(k) ;\n high.erase(high.begin()) ;\n high.insert(i) ; \n\n } \n }\n else { \n high.insert(i) ;\n }\n\n }\n\n\n\n\n\n } \n\n }\n \n\n void remove(int i , multiset<long long int> &low , multiset<long long int> &high , vector<double> &v1) { \n\n if ( i <= *(low.rbegin())) { \n if ( low.size() == high.size()) { \n int k = *(high.begin()) ;\n high.erase(high.begin()) ;\n \n low.erase(low.find(i)) ;\n low.insert(k) ;\n }\n else { \n low.erase(low.find(i)) ;\n }\n\n }\n\n else { \n if ( low.size() == high.size()) {\n high.erase(high.find(i)) ;\n }\n else { \n high.erase(high.find(i)) ;\n int k = *(low.rbegin()) ; \n high.insert(k) ;\n low.erase(low.find(k)) ;\n\n }\n\n\n\n }\n \n }\n\n void put_median( multiset<long long int> &low , multiset<long long int> &high , vector<double> &v1) { \n if( low.size() == high.size() ) { \n v1.push_back( double( *(low.rbegin()) + *(high.begin()) )/2 ) ;\n // cout << \" Median put for the below is \" << double( *(low.rbegin()) + *(high.begin()) )/2 << endl ;\n }\n else { \n v1.push_back( *(low.rbegin()) ) ;\n // cout << \" Median put for the below is \" << *(low.rbegin()) << endl ;\n\n }\n \n }\n\n \n vector<double> medianSlidingWindow(vector<int>& nums, int k) {\n multiset<long long int> low ;\n multiset<long long int> high ;\n vector<double> v1 ;\n for ( int i = 0 ; i < nums.size() ; i++) { \n\n ins(nums[i] , low , high , v1) ;\n if ( i >= k-1 ) { \n put_median( low , high , v1) ;\n\n // for ( auto it = low.begin() ; it != low.end() ; it++) { \n // cout << *it << \" \" ;\n // }\n // cout << \" \" ;\n // for ( auto it = high.begin() ; it != high.end() ; it++) { \n // cout << *it << \" \" ;\n // }\n // cout << endl ; \n remove(nums[i-k+1] , low , high , v1) ; \n }\n }\n return v1 ; \n\n\n }\n};", "memory": "53830" }
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>
2
{ "code": "class Solution {\npublic:\n \n void ins(int i , multiset<long long int> &low , multiset<long long int> &high , vector<double> &v1) { \n if ( low.empty() ) { low.insert(i) ; }\n else { \n if ( i <= *(low.rbegin()) ) { \n if ( low.size() == high.size()) { low.insert(i) ; }\n else { \n int k = *(low.rbegin()) ;\n high.insert(k) ;\n low.erase(low.find(k)) ;\n low.insert(i) ;\n\n }\n }\n\n else { \n if ( low.size() == high.size() ) { \n\n int k = *(high.begin()) ;\n if ( i <= k) { low.insert(i);}\n else { \n low.insert(k) ;\n high.erase(high.begin()) ;\n high.insert(i) ; \n\n } \n }\n else { \n high.insert(i) ;\n }\n\n }\n\n\n\n\n\n } \n\n }\n \n\n void remove(int i , multiset<long long int> &low , multiset<long long int> &high , vector<double> &v1) { \n\n if ( i <= *(low.rbegin())) { \n if ( low.size() == high.size()) { \n int k = *(high.begin()) ;\n high.erase(high.begin()) ;\n \n low.erase(low.find(i)) ;\n low.insert(k) ;\n }\n else { \n low.erase(low.find(i)) ;\n }\n\n }\n\n else { \n if ( low.size() == high.size()) {\n high.erase(high.find(i)) ;\n }\n else { \n high.erase(high.find(i)) ;\n int k = *(low.rbegin()) ; \n high.insert(k) ;\n low.erase(low.find(k)) ;\n\n }\n\n\n\n }\n \n }\n\n void put_median( multiset<long long int> &low , multiset<long long int> &high , vector<double> &v1) { \n if( low.size() == high.size() ) { \n v1.push_back( double( *(low.rbegin()) + *(high.begin()) )/2 ) ;\n // cout << \" Median put for the below is \" << double( *(low.rbegin()) + *(high.begin()) )/2 << endl ;\n }\n else { \n v1.push_back( *(low.rbegin()) ) ;\n // cout << \" Median put for the below is \" << *(low.rbegin()) << endl ;\n\n }\n \n }\n\n \n vector<double> medianSlidingWindow(vector<int>& nums, int k) {\n multiset<long long int> low ;\n multiset<long long int> high ;\n vector<double> v1 ;\n for ( int i = 0 ; i < nums.size() ; i++) { \n\n ins(nums[i] , low , high , v1) ;\n if ( i >= k-1 ) { \n put_median( low , high , v1) ;\n\n // for ( auto it = low.begin() ; it != low.end() ; it++) { \n // cout << *it << \" \" ;\n // }\n // cout << \" \" ;\n // for ( auto it = high.begin() ; it != high.end() ; it++) { \n // cout << *it << \" \" ;\n // }\n // cout << endl ; \n remove(nums[i-k+1] , low , high , v1) ; \n }\n }\n return v1 ; \n\n\n }\n};", "memory": "53830" }
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>
2
{ "code": "class Solution {\n struct value_info {\n long long value{};\n int idx{};\n };\n\n struct value_info_less {\n bool operator()(value_info l, value_info r) {\n return l.value < r.value;\n }\n };\n\n struct value_info_greater {\n bool operator()(value_info l, value_info r) {\n return l.value > r.value;\n }\n };\n\n template <typename Comp> class heap {\n public:\n void push(value_info val) {\n container.push_back(val);\n index_tracker[val.idx] = container.size() - 1;\n heapify_up(container.size() - 1);\n }\n\n bool empty() const { return container.empty(); }\n\n value_info top() const { return container[0]; }\n\n int size() const { return container.size(); }\n\n bool exist(int original_index) {\n return index_tracker[original_index] != -1;\n }\n\n void pop() {\n swap(container.front(), container.back());\n index_tracker[container.front().idx] = 0;\n index_tracker[container.back().idx] = -1;\n container.pop_back();\n heapify_down(0);\n }\n\n void remove(int original_index) {\n int i = index_tracker[original_index];\n assert(i < container.size());\n to_root(i);\n pop();\n }\n\n private:\n void to_root(int i) {\n if (i == 0) {\n return;\n }\n\n int p = parent(i);\n if (p >= 0) {\n index_tracker[container[p].idx] = i;\n index_tracker[container[i].idx] = p;\n swap(container[p], container[i]);\n to_root(p);\n }\n }\n void heapify_up(int i) {\n int p = parent(i);\n if (p >= 0) {\n Comp cmp;\n if (cmp(container[p], container[i])) {\n index_tracker[container[p].idx] = i;\n index_tracker[container[i].idx] = p;\n swap(container[p], container[i]);\n heapify_up(p);\n }\n }\n }\n void heapify_down(int i) {\n if (container.empty()) {\n return;\n }\n\n int l = left(i);\n int r = right(i);\n\n int next = -1;\n Comp cmp;\n if (l < container.size() && cmp(container[i], container[l])) {\n next = l;\n } else {\n next = i;\n }\n\n if (r < container.size() && cmp(container[next], container[r])) {\n next = r;\n }\n\n if (next != i) {\n index_tracker[container[next].idx] = i;\n index_tracker[container[i].idx] = next;\n swap(container[next], container[i]);\n heapify_down(next);\n }\n }\n\n int parent(int i) { return (i - 1) / 2; }\n int left(int i) { return 2 * i + 1; }\n int right(int i) { return 2 * i + 2; }\n\n private:\n vector<value_info> container;\n unordered_map<int, int> index_tracker;\n };\n\n class MinMaxHeap {\n using MaxHeap = heap<value_info_less>;\n using MinHeap = heap<value_info_greater>;\n\n MaxHeap mMaxHeap;\n MinHeap mMinHeap;\n\n public:\n double median() const {\n assert(!mMaxHeap.empty());\n if (mMaxHeap.size() > mMinHeap.size()) {\n return mMaxHeap.top().value;\n }\n\n return (mMaxHeap.top().value + mMinHeap.top().value) / 2.0;\n }\n\n void remove(int value, int idx) {\n assert(!mMaxHeap.empty());\n\n if (mMaxHeap.exist(idx)) {\n mMaxHeap.remove(idx);\n } else {\n mMinHeap.remove(idx);\n }\n\n rebalance();\n }\n\n void add(int value, int idx) {\n mMaxHeap.push({value, idx});\n mMinHeap.push(mMaxHeap.top());\n mMaxHeap.pop();\n\n rebalance();\n }\n\n private:\n void rebalance() {\n while (mMaxHeap.size() < mMinHeap.size()) {\n mMaxHeap.push(mMinHeap.top());\n mMinHeap.pop();\n }\n }\n };\n\npublic:\n vector<double> medianSlidingWindow(vector<int>& nums, int k) {\n MinMaxHeap heap;\n\n for (int i = 0; i < k; ++i) {\n heap.add(nums[i], i);\n }\n\n vector<double> result;\n result.push_back(heap.median());\n for (int i = k; i < nums.size(); ++i) {\n heap.remove(nums[i - k], i - k);\n heap.add(nums[i], i);\n result.push_back(heap.median());\n }\n\n return result;\n }\n};", "memory": "54210" }
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>
2
{ "code": "class Solution {\n multiset<int> min_heap;\n multiset<int> max_heap;\n void balance() {\n while (min_heap.size() > max_heap.size()) {\n int val = *min_heap.rbegin();\n auto it = min_heap.rbegin();\n it++;\n min_heap.erase(it.base());\n max_heap.insert(val);\n }\n while (min_heap.size() < max_heap.size()) {\n int val = *max_heap.begin();\n max_heap.erase(max_heap.begin());\n min_heap.insert(val);\n }\n }\n void add_value(int val) {\n if (min_heap.empty() || (*min_heap.rbegin())>=val ) {\n min_heap.insert(val);\n } else {\n max_heap.insert(val);\n }\n balance();\n }\n\n void remove_value(int val) {\n if (min_heap.find(val) != min_heap.end()) {\n min_heap.erase(min_heap.find(val));\n } else if (max_heap.find(val) != max_heap.end()) {\n max_heap.erase(max_heap.find(val));\n }\n balance();\n }\n\n double get_median() {\n int n = min_heap.size() + max_heap.size();\n if (n % 2 == 1) {\n return *min_heap.rbegin();\n } else if (n==0) {\n return 0;\n } else {\n return ((double) *min_heap.rbegin() + *max_heap.begin()) / 2.0;\n }\n }\npublic:\n vector<double> medianSlidingWindow(vector<int>& nums, int k) {\n \n int left = 0;\n vector<double> median_vec;\n for (int right = 0; right<nums.size(); right++) {\n add_value(nums[right]);\n while (left<right && (right-left+1)>k) {\n remove_value(nums[left]);\n left++;\n }\n if (right-left+1 == k) {\n median_vec.push_back(get_median());\n }\n }\n return median_vec;\n }\n};", "memory": "54210" }
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>
2
{ "code": "class Solution {\npublic:\nset<pair<double,int>> minheap;\n\tset<pair<double,int>,greater<pair<double,int>>> maxheap;\n //int i=0;\n void addNum(double num,int i) {\n if(maxheap.empty()||maxheap.begin()->first>num)maxheap.insert({num,i});\n else minheap.insert({num,i});\n\n if(minheap.size()>maxheap.size()+1){\n\t\t\tmaxheap.insert(*minheap.begin());\n minheap.erase(*minheap.begin());\n\t\t}\n\t\telse if(maxheap.size()>minheap.size()+1){\n\t\t\tminheap.insert(*maxheap.begin());\n maxheap.erase(*maxheap.begin());\n\t\t}\n }\n \n double findMedian() {\n if(minheap.size()==maxheap.size() && !minheap.empty())\n return (double)(minheap.begin()->first+maxheap.begin()->first)/2.0;\n\t\telse if(minheap.size()>maxheap.size())\n return (double)minheap.begin()->first;\n\t\telse if(minheap.size()<maxheap.size())\n return (double)maxheap.begin()->first;\n return 0;\n }\n\n void eraser(pair<double,int> p){\n if(minheap.find(p)!=minheap.end())minheap.erase(p);\n else maxheap.erase(p);\n \n if(minheap.size()>maxheap.size()+1){\n\t\t\tmaxheap.insert(*minheap.begin());\n minheap.erase(*minheap.begin());\n\t\t}\n\t\telse if(maxheap.size()>minheap.size()+1){\n\t\t\tminheap.insert(*maxheap.begin());\n maxheap.erase(*maxheap.begin());\n\t\t}\n }\n vector<double> medianSlidingWindow(vector<int>& nm, int k) {\n //i=0;\n vector<double> nums;\n\n for(int i=0;i<nm.size();i++){\n double k=nm[i];\n nums.push_back(k);\n }\n vector<double> ans;\n int j=0;\n for(;j<k;j++){\n addNum(nums[j],j);\n }\n double mid=findMedian();\n ans.push_back(mid);\n for(;j<nums.size();j++){\n eraser({nums[j-k]*1.0,j-k});\n addNum(nums[j]*1.0,j);\n mid=findMedian();\n ans.push_back(mid);\n }\n return ans;\n }\n};", "memory": "54590" }
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>
2
{ "code": "class MyStructure{\n multiset<int,greater<int>> ms1;\n multiset<int> ms2;\n int window;\n\n public:\n MyStructure(int k){\n window=k;\n }\n\n double getMedian(){\n // cout<<\"gm\";\n double el1=*(ms1.begin());\n double el2=*(ms2.begin());\n // cout<<el1<<\" \"<<el2<<\"\\n\";\n if(window%2){\n return el2;\n }\n return (el1+el2)/2;\n }\n\n void addEl(int x){\n ms2.insert(x);\n if(ms2.size()-ms1.size()>1){\n ms1.insert(*(ms2.begin()));\n ms2.erase(ms2.begin());\n }\n if(ms1.size()>0){\n int el1=*(ms1.begin());\n int el2=*(ms2.begin());\n if(el1>el2){\n cout<<el1<<\" \"<<el2<<\"\\n\";\n ms1.erase(ms1.begin());\n ms2.erase(ms2.begin());\n ms1.insert(el2);\n ms2.insert(el1);\n }\n }\n // for(auto it:ms1) cout<<(it)<<\"msa\";\n // cout<<\"\\n\";\n // for(auto it:ms2) cout<<(it)<<\"msb \";\n // cout<<\"\\n\";\n }\n\n void removeEl(int x){\n if(ms2.find(x)!=ms2.end()){\n ms2.erase(ms2.find(x));\n return;\n }\n ms1.erase(ms1.find(x));\n if(ms2.size() - ms1.size()>1){\n ms1.insert(*(ms2.begin()));\n ms2.erase(ms2.begin());\n }\n }\n};\n\nclass Solution {\npublic:\n vector<double> medianSlidingWindow(vector<int>& nums, int k) {\n MyStructure mystr(k);\n vector<double> ans;\n for(int i=0;i<k-1;i++) mystr.addEl(nums[i]);\n for(int i=k-1;i<nums.size();i++){\n mystr.addEl(nums[i]);\n ans.push_back(mystr.getMedian());\n if(i-k+1 >=0) mystr.removeEl(nums[i-k+1]);\n }\n return ans;\n\n }\n};", "memory": "54590" }
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>
2
{ "code": "#include <bits/stdc++.h>\n#include <ext/pb_ds/assoc_container.hpp>\n#include <ext/pb_ds/tree_policy.hpp>\n\nusing namespace std;\nusing namespace __gnu_pbds;\ntemplate<typename T>\nstruct comp {\n bool operator()(const T& lhs, const T& rhs) const {\n return lhs <= rhs; \n }\n};\n// ordered set\ntypedef tree<int, null_type, comp<int>, rb_tree_tag,\n tree_order_statistics_node_update> pbds;\n\nclass Solution {\npublic:\n pbds set;\n\n vector<double> medianSlidingWindow(vector<int>& nums, int k) {\n int n = nums.size();\n vector<double> ans(n - k + 1);\n long double val;\n for (int i = 0; i < k; i++) \n {\n set.insert(nums[i]);\n }\n if ((k % 2) == 0) \n {\n val = ((long double)(*set.find_by_order(k / 2)) + (long double)(*set.find_by_order(k / 2 - 1))); \n val /= 2; \n } \n else \n {\n val = (*set.find_by_order(k / 2));\n }\n ans[0] = val;\n\n for (int i = 1; i <= n - k; i++) \n {\n int num = nums[i - 1];\n set.erase(set.upper_bound(num));\n num = nums[i + k - 1];\n set.insert(num);\n \n if ((k % 2) == 0) \n {\n val = ((long double)(*set.find_by_order(k / 2)) + (long double)(*set.find_by_order(k / 2 - 1))); \n val /= 2; \n } \n else \n {\n val = (*set.find_by_order(k / 2));\n }\n ans[i] = val;\n\n // cout << val << \" \";\n }\n\n return ans;\n }\n};", "memory": "54970" }
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>
2
{ "code": "#include <bits/stdc++.h>\n#include <ext/pb_ds/assoc_container.hpp>\n#include <ext/pb_ds/tree_policy.hpp>\n\nusing namespace std;\nusing namespace __gnu_pbds;\ntemplate<typename T>\nstruct comp {\n bool operator()(const T& lhs, const T& rhs) const {\n return lhs <= rhs; \n }\n};\n// ordered set\ntypedef tree<int, null_type, comp<int>, rb_tree_tag,\n tree_order_statistics_node_update> pbds;\n\nclass Solution {\npublic:\n pbds set;\n\n vector<double> medianSlidingWindow(vector<int>& nums, int k) {\n int n = nums.size();\n vector<double> ans(n - k + 1);\n long double val;\n for (int i = 0; i < k; i++) \n {\n set.insert(nums[i]);\n }\n if ((k % 2) == 0) \n {\n val = ((long double)(*set.find_by_order(k / 2)) + (long double)(*set.find_by_order(k / 2 - 1))); \n val /= 2; \n } \n else \n {\n val = (*set.find_by_order(k / 2));\n }\n ans[0] = val;\n\n for (int i = 1; i <= n - k; i++) \n {\n int num = nums[i - 1];\n set.erase(set.upper_bound(num));\n num = nums[i + k - 1];\n set.insert(num);\n \n if ((k % 2) == 0) \n {\n val = ((long double)(*set.find_by_order(k / 2)) + (long double)(*set.find_by_order(k / 2 - 1))); \n val /= 2; \n } \n else \n {\n val = (*set.find_by_order(k / 2));\n }\n ans[i] = val;\n\n // cout << val << \" \";\n }\n\n return ans;\n }\n};", "memory": "54970" }
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>
2
{ "code": "class Solution {\n multiset<int> left;\n multiset<int> right;\n double getmed(int k){\n if(k%2==0){\n // cout<<\",\"<<left.size()<<\" \"<<right.size()<<endl;\n return ( (double) (*(left.rbegin()) ) + (double) (*(right.begin()) ) ) / 2.0;\n }\n else{\n return *right.begin();\n }\n }\npublic:\n vector<double> medianSlidingWindow(vector<int>& nums, int k) {\n int lsize, rsize;\n if(k%2==0){\n lsize=rsize = k/2;\n }\n else{\n lsize = k/2;\n rsize = (k/2)+1;\n }\n for(int i=0;i<k;i++){\n left.insert(nums[i]);\n }\n // cout<<\",\"<<left.size()<<\" \"<<right.size()<<endl;\n for(int i=0;i<rsize;i++){\n int temp= *(left.rbegin());\n cout<<temp<<endl;\n right.insert( temp );\n left.erase( prev(left.end() ));\n }\n // cout<<\",\"<<left.size()<<\" \"<<right.size()<<endl;\n vector<double> ans;\n for(int i=k;i<=nums.size();i++){\n double median = getmed(k);\n ans.push_back(median);\n if(i==nums.size())break;\n // return ans;\n\n int previndex = i-k;\n int newele = i;\n\n if(left.find(nums[previndex]) != left.end() ){\n left.erase(left.find(nums[previndex]));\n left.insert(nums[newele]);\n if(right.size()>0)\n if( *left.rbegin() > *right.begin() ){\n int rval = *right.begin();\n int lval = *left.rbegin();\n auto rval_i = right.begin();\n auto lval_i = prev(left.end());\n left.erase(lval_i);\n right.erase(rval_i);\n left.insert(rval);\n right.insert(lval);\n }\n }\n else{\n right.erase(right.find(nums[previndex]));\n right.insert(nums[newele]);\n if(left.size()>0)\n if( *left.rbegin() > *right.begin() ){\n int rval = *right.begin();\n int lval = *left.rbegin();\n auto rval_i = right.begin();\n auto lval_i = prev(left.end());\n left.erase(lval_i);\n right.erase(rval_i);\n left.insert(rval);\n right.insert(lval);\n }\n }\n }\n \n return ans;\n \n }\n};", "memory": "55350" }
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>
2
{ "code": "class Solution {\n multiset<int> left;\n multiset<int> right;\n double getmed(int k){\n if(k%2==0){\n cout<<\",\"<<left.size()<<\" \"<<right.size()<<endl;\n return ( (double) (*(left.rbegin()) ) + (double) (*(right.begin()) ) ) / 2.0;\n }\n else{\n return *right.begin();\n }\n }\npublic:\n vector<double> medianSlidingWindow(vector<int>& nums, int k) {\n int lsize, rsize;\n if(k%2==0){\n lsize=rsize = k/2;\n }\n else{\n lsize = k/2;\n rsize = (k/2)+1;\n }\n for(int i=0;i<k;i++){\n left.insert(nums[i]);\n }\n cout<<\",\"<<left.size()<<\" \"<<right.size()<<endl;\n for(int i=0;i<rsize;i++){\n int temp= *(left.rbegin());\n cout<<temp<<endl;\n right.insert( temp );\n left.erase( prev(left.end() ));\n }\n cout<<\",\"<<left.size()<<\" \"<<right.size()<<endl;\n vector<double> ans;\n for(int i=k;i<=nums.size();i++){\n double median = getmed(k);\n ans.push_back(median);\n if(i==nums.size())break;\n // return ans;\n\n int previndex = i-k;\n int newele = i;\n\n if(left.find(nums[previndex]) != left.end() ){\n left.erase(left.find(nums[previndex]));\n left.insert(nums[newele]);\n if(right.size()>0)\n if( *left.rbegin() > *right.begin() ){\n int rval = *right.begin();\n int lval = *left.rbegin();\n auto rval_i = right.begin();\n auto lval_i = prev(left.end());\n left.erase(lval_i);\n right.erase(rval_i);\n left.insert(rval);\n right.insert(lval);\n }\n }\n else{\n right.erase(right.find(nums[previndex]));\n right.insert(nums[newele]);\n if(left.size()>0)\n if( *left.rbegin() > *right.begin() ){\n int rval = *right.begin();\n int lval = *left.rbegin();\n auto rval_i = right.begin();\n auto lval_i = prev(left.end());\n left.erase(lval_i);\n right.erase(rval_i);\n left.insert(rval);\n right.insert(lval);\n }\n }\n }\n \n return ans;\n \n }\n};", "memory": "55350" }
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>
2
{ "code": "class Solution {\npublic:\n vector<double> medianSlidingWindow(vector<int>& vec, int k) {\n multiset<double> big, low;\n vector<double> ans;\n\n for (int i=0; i<k; ++i) big.insert(vec[i]);\n for (int i=0; i<k - (k/2+1); ++i) {\n low.insert(*big.begin());\n big.erase(big.begin());\n }\n\n ans.push_back(k % 2 ? *big.begin() : (*big.begin() + *(++big.begin())) / 2);\n for (int i=k; i<vec.size(); ++i) {\n if (low.contains(vec[i-k])) low.erase(low.find(vec[i-k]));\n else big.erase(big.find(vec[i-k]));\n\n low.insert(vec[i]);\n \n while (big.size() < k/2 + 1) {\n big.insert(*low.rbegin());\n low.erase(low.find(*low.rbegin()));\n }\n\n if (!low.empty()) while (*low.rbegin() > *big.begin()) {\n int mnbig = *big.begin();\n big.erase(big.begin());\n big.insert(*low.rbegin());\n low.erase(low.find(*low.rbegin()));\n low.insert(mnbig);\n }\n\n ans.push_back(k % 2 ? *big.begin() : (*big.begin() + *(++big.begin())) / 2);\n }\n\n return ans;\n }\n};", "memory": "55730" }
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>
2
{ "code": "#include<ext/pb_ds/assoc_container.hpp>\n#include<ext/pb_ds/tree_policy.hpp>\nusing namespace __gnu_pbds;\n\ntemplate <typename T> using pbds = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;\n\nclass Solution {\npublic:\n vector<double> medianSlidingWindow(vector<int>& nums, int k) \n {\n int pos = k / 2, poss = pos;\n if(k%2==0) poss--;\n\n vector<double> ans;\n pbds<pair<int,int>>pbds;\n int i = 0, j = 0, n = nums.size();\n while(j<n)\n {\n pbds.insert({nums[j], j});\n if(j-i+1 == k)\n {\n auto it = pbds.find_by_order(pos);\n auto itt = pbds.find_by_order(poss);\n\n double val = (double(it->first) + double(itt->first)) / 2;\n ans.push_back(val);\n pbds.erase({nums[i], i});\n i++;\n }\n j++;\n }\n return ans;\n }\n};", "memory": "55730" }
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>
2
{ "code": "#include<bits/stdc++.h>\n\n#include<ext/pb_ds/assoc_container.hpp>\n#include<ext/pb_ds/tree_policy.hpp>\n\nusing namespace std;\nusing namespace __gnu_pbds;\n\ntypedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> pbds; // find_by_order, order_of_key\n\n\nclass Solution {\npublic:\n vector<double> medianSlidingWindow(vector<int>& nums, int k) {\n int n = nums.size();\n pbds s;\n\n vector<double> ans;\n for(int i=0; i<k; i++) {\n s.insert(nums[i]);\n }\n\n if(k%2) {\n ans.push_back(*s.find_by_order(k/2));\n } else {\n double med = (double)(1LL*(*s.find_by_order(k/2 - 1)) + *(s.find_by_order(k/2)))/2;\n ans.push_back(med);\n }\n\n\n for(int i=k; i<n; i++) {\n // for(auto i:s) {\n // cout<<i<<\" \";\n // }\n // cout<<endl;\n\n int pos = s.order_of_key(nums[i-k]);\n auto it = s.find_by_order(pos);\n s.erase(it);\n\n s.insert(nums[i]);\n\n\n if(k%2) {\n ans.push_back(*s.find_by_order(k/2));\n } else {\n double med = (double)(1LL*(*s.find_by_order(k/2 - 1)) + *(s.find_by_order(k/2)))/2;\n ans.push_back(med);\n }\n }\n return ans;\n }\n};", "memory": "56110" }
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>
2
{ "code": "#include<bits/stdc++.h>\n\n#include<ext/pb_ds/assoc_container.hpp>\n#include<ext/pb_ds/tree_policy.hpp>\n\nusing namespace std;\nusing namespace __gnu_pbds;\n\ntypedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> pbds; // find_by_order, order_of_key\n\nclass Solution {\npublic:\n void myerase(pbds &t, int v){\n int rank = t.order_of_key(v);//Number of elements that are less than v in t\n pbds::iterator it = t.find_by_order(rank); //Iterator that points to the (rank+1)th element in t\n t.erase(it);\n}\n vector<double> medianSlidingWindow(vector<int>& nums, int k) {\n pbds a;\n int i=0,j=0;\n vector<double>ans;\n while(j<nums.size()){\n a.insert(nums[j]);\n if(j-i+1==k){\n if(k%2==1) {\n int ind=k/2;\n double e= *a.find_by_order(ind); \n ans.push_back(e*1.00000);\n }\n else{\n int ind=k/2;\n double e=*a.find_by_order(ind);\n double prev=*a.find_by_order(ind-1);\n double final=((e+prev)*1.00000)/2;\n ans.push_back(final);\n }\n int num=nums[i];\n myerase(a,num); i++;\n }\n j++;\n }\n return ans;\n }\n};", "memory": "56110" }
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>
3
{ "code": "class Solution {\npublic:\n multiset<double> minH;\n multiset<double, greater<double>> maxH;\n \n void balanceHeaps()\n {\n if(maxH.size() > minH.size())\n {\n minH.insert(*maxH.begin()); \n maxH.erase(maxH.begin());\n }\n }\n \n double getMedian(int k)\n {\n if(k%2 == 0)\n return (*minH.begin() + *maxH.begin())*0.5;\n else return *minH.begin();\n }\n \n vector<double> medianSlidingWindow(vector<int>& nums, int k) \n {\n vector<double> ans;\n \n for(int i=0; i<nums.size(); i++)\n {\n if(i >= k) // Only delete element from one of the heaps\n {\n if(minH.find(nums[i-k]) != minH.end()) minH.erase(minH.find(nums[i-k]));\n else maxH.erase(maxH.find(nums[i-k]));\n }\n \n // The idea here is very simple: First we insert the current element in the min heap\n // The we insert the top of min heap to max heap\n // Then we balance the heaps i.e. move element from max to min heap if max heap has\n // greater size\n minH.insert(nums[i]);\n maxH.insert(*minH.begin());\n minH.erase(minH.begin());\n \n balanceHeaps(); \n \n if(i >= k-1) ans.push_back(getMedian(k)); \n }\n \n return ans;\n }\n};", "memory": "57630" }
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>
3
{ "code": "class Solution {\npublic:\n vector<double> medianSlidingWindow(vector<int>& nums, int k) {\n multiset<int> firstHalf,secondHalf;\n vector<double> ans;\n for(int i=0;i<nums.size();i++){\n if(i>=k){\n if(secondHalf.find(nums[i-k])!=secondHalf.end()){\n secondHalf.erase(secondHalf.find(nums[i-k]));\n }\n else if(firstHalf.find(nums[i-k])!=firstHalf.end()){\n firstHalf.erase(firstHalf.find(nums[i-k]));\n }\n }\n secondHalf.insert(nums[i]);\n firstHalf.insert(*secondHalf.begin());\n secondHalf.erase(secondHalf.find(*secondHalf.begin()));\n \n while(firstHalf.size()-1>secondHalf.size()){\n secondHalf.insert(*firstHalf.rbegin());\n firstHalf.erase(firstHalf.find(*firstHalf.rbegin()));\n }\n if(i>=k-1){\n if(k%2){\n ans.push_back(1.00*(*firstHalf.rbegin()));\n }\n else{\n double val=(1.00*(*firstHalf.rbegin())+1.00*(*secondHalf.begin()))/2.0;\n ans.push_back(val);\n }\n }\n }\n return ans;\n }\n};", "memory": "58010" }
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>
3
{ "code": "//VERY GOOD QUESTION\n//Intution is First put the elemet in minHEap\n//Then remove it and put the top element to maxHeap\n//If size of maxHeap is more than minHEap\n//Remove the top element of maxHeap and put it into minHeap\n//REmove the expired index\n//In minheap we put the higher value elemts.In maxHeap we put the low valued elements\n\n\n//MISTAKE\n//If u add 2 ints it will overflow\n\n\n//TELL IN INTERVIEWS\n//we use two heap structure\n//Weuse the balancing technique of heaps\n#define ll long long\n#define pb push_back\nclass Solution {\npublic:\n vector<double> medianSlidingWindow(vector<int>& nums, int k) {\n ll n = nums.size();\n set<pair<ll,ll>>maxHeap;\n set<pair<ll,ll>>minHeap;\n vector<double>ans;\n for(ll i=0;i<n;i++){\n if(i-k>=0){\n if(maxHeap.find({nums[i-k],i-k})!=maxHeap.end()){\n maxHeap.erase({nums[i-k],i-k});\n }\n if(minHeap.find({nums[i-k],i-k})!=minHeap.end()){\n minHeap.erase({nums[i-k],i-k});\n } \n }\n\n minHeap.insert({nums[i],i});\n auto it = minHeap.begin();\n ll node=(*it).second;\n minHeap.erase(it);\n\n maxHeap.insert({nums[node],node});\n if(maxHeap.size()>minHeap.size()){\n auto it2 = maxHeap.end();\n it2--;\n ll node2=(*it2).second;\n maxHeap.erase(it2);\n minHeap.insert({nums[node2],node2});\n }\n\n if(i-k+1>=0){\n auto it3 = minHeap.begin();\n ll node3=(*it3).second; \n if(k%2==1){\n ans.pb(nums[node3]);\n }\n else{\n auto it4 = maxHeap.end();\n it4--;\n ll node4=(*it4).second;\n//INTEGER OVERFLOWED runtime error\n double a = nums[node3];\n double b = nums[node4];\n ans.pb((a+b)*(1.0)/2);\n }\n }\n\n // cout<<minHeap.size()<<\" \"<<maxHeap.size()<<endl;\n }\n return ans;\n }\n};", "memory": "58010" }
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>
3
{ "code": "class Solution {\npublic:\n std::vector<double> medianSlidingWindow(std::vector<int>& nums, int k) {\n std::multiset<int> min;\n std::multiset<int, std::greater<int>> max;\n \n auto addNum = [&min, &max](int num) {\n if (min.empty()) {\n min.insert(num);\n if (max.empty())\n return;\n }\n else if(max.empty())\n max.insert(num);\n else\n min.size() < max.size() ? min.insert(num) : max.insert(num);\n int right = *min.begin();\n int left = *max.begin();\n if (right < left) {\n max.erase(max.begin());\n max.insert(right);\n min.erase(min.begin());\n min.insert(left);\n }\n };\n\n auto findMedian = [&min, &max]() {\n int right = *min.begin();\n int left = *max.begin();\n double med = max.size() < min.size() ? right :\n max.size() != min.size() ? left : (static_cast<double>(left) + right) / 2;\n return med;\n };\n\n auto erase = [&min, &max](int num) {\n auto left = max.find(num);\n auto right = min.find(num);\n if (left == max.end())\n min.erase(right);\n else if (right == min.end())\n max.erase(left);\n else\n min.size() < max.size() ? max.erase(left) : min.erase(right);\n };\n\n for (int i = 0; i < k; ++i)\n addNum(nums[i]);\n std::vector<double> ans;\n ans.reserve(nums.size() - k + 1);\n ans.push_back(findMedian());\n for (int i = k; i < nums.size(); ++i) {\n erase(nums[i - k]);\n addNum(nums[i]);\n ans.push_back(findMedian());\n }\n return ans;\n }\n};", "memory": "58390" }
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>
3
{ "code": "class Solution {\npublic:\n template <typename T>\n class Median{\n queue<T> q;\n multiset<T> s1, s2;\n\n public:\n\n int size(){\n return q.size();\n }\n\n void push_back(T x){\n q.push(x);\n s2.insert(x);\n rebalance();\n }\n\n void pop(){\n T fst = q.front();\n q.pop();\n\n auto it = s1.find(fst);\n if (it != s1.end()){\n s1.erase(it);\n } else {\n it = s2.find(fst);\n s2.erase(it);\n }\n rebalance();\n }\n\n double getMedian(){\n if (q.size() % 2 == 1)\n return *s1.rbegin();\n return (1.0 * *s1.rbegin() + *s2.begin()) / 2.0;\n }\n\n private:\n void rebalance(){\n int size1 = (q.size() + 1) / 2;\n // set 2 is always bigger because that's where we push things,\n // But actually no: we might remove from the first set\n int size2 = q.size() - size1;\n // Case 1 2 5 3 4 5\n // first make sure those containers all(s1) <= all(s2)\n\n while (!s1.empty() && !s2.empty() && *s1.rbegin() > *s2.begin()){\n T s1last = *s1.rbegin();\n T s2first = *s2.begin();\n s1.erase(s1.find(s1last));\n s2.erase(s2.begin());\n\n s1.insert(s2first);\n s2.insert(s1last);\n }\n // okay, now they might have different sizes\n while (s1.size() > size1){\n T s1last = *s1.rbegin();\n s1.erase(s1.find(s1last));\n s2.insert(s1last);\n }\n while (s2.size() > size2){\n T s2first = *s2.begin();\n s2.erase(s2.begin());\n s1.insert(s2first);\n }\n // now they're balanced\n }\n };\n vector<double> medianSlidingWindow(vector<int>& a, int k) {\n vector<double> ans;\n Median<int> x;\n for (int i = 0 ; i < k - 1; i++)\n x.push_back(a[i]);\n \n for (int i = k - 1; i < a.size(); i++){\n x.push_back(a[i]);\n ans.push_back(x.getMedian());\n x.pop();\n }\n return ans;\n }\n};", "memory": "58390" }
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>
3
{ "code": "class Solution {\npublic:\n std::vector<double> medianSlidingWindow(std::vector<int>& nums, int k) {\n std::multiset<int> min;\n std::multiset<int, std::greater<int>> max;\n \n auto addNum = [&min, &max](int num) {\n if (min.empty()) {\n min.insert(num);\n if (max.empty())\n return;\n }\n else if(max.empty())\n max.insert(num);\n else\n min.size() < max.size() ? min.insert(num) : max.insert(num);\n int right = *min.begin();\n int left = *max.begin();\n if (right < left) {\n max.erase(max.begin());\n max.insert(right);\n min.erase(min.begin());\n min.insert(left);\n }\n };\n\n auto findMedian = [&min, &max]() {\n int right = *min.begin();\n int left = *max.begin();\n double med = max.size() < min.size() ? right :\n max.size() != min.size() ? left : (static_cast<double>(left) + right) / 2;\n return med;\n };\n\n auto erase = [&min, &max](int num) {\n auto left = max.find(num);\n auto right = min.find(num);\n if (left == max.end())\n min.erase(right);\n else if (right == min.end())\n max.erase(left);\n else\n min.size() < max.size() ? max.erase(left) : min.erase(right);\n };\n\n for (int i = 0; i < k; ++i)\n addNum(nums[i]);\n std::vector<double> ans;\n ans.reserve(nums.size() - k + 1);\n ans.push_back(findMedian());\n for (int i = k; i < nums.size(); ++i) {\n erase(nums[i - k]);\n addNum(nums[i]);\n ans.push_back(findMedian());\n }\n return ans;\n }\n};", "memory": "58770" }
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>
3
{ "code": "class Solution {\npublic:\n std::vector<double> medianSlidingWindow(std::vector<int>& nums, int k) {\n std::multiset<int> min;\n std::multiset<int, std::greater<int>> max;\n \n auto addNum = [&min, &max](int num) {\n if (min.empty()) {\n min.insert(num);\n if (max.empty())\n return;\n }\n else if(max.empty())\n max.insert(num);\n else\n min.size() < max.size() ? min.insert(num) : max.insert(num);\n int right = *min.begin();\n int left = *max.begin();\n if (right < left) {\n max.erase(max.begin());\n max.insert(right);\n min.erase(min.begin());\n min.insert(left);\n }\n };\n\n auto findMedian = [&min, &max]() {\n int right = *min.begin();\n int left = *max.begin();\n double med = max.size() < min.size() ? right :\n max.size() != min.size() ? left : (static_cast<double>(left) + right) / 2;\n return med;\n };\n\n auto erase = [&min, &max](int num) {\n auto left = max.find(num);\n auto right = min.find(num);\n if (left == max.end())\n min.erase(right);\n else if (right == min.end())\n max.erase(left);\n else\n min.size() < max.size() ? max.erase(left) : min.erase(right);\n };\n\n for (int i = 0; i < k; ++i)\n addNum(nums[i]);\n std::vector<double> ans;\n ans.reserve(nums.size() - k + 1);\n ans.push_back(findMedian());\n for (int i = k; i < nums.size(); ++i) {\n erase(nums[i - k]);\n addNum(nums[i]);\n ans.push_back(findMedian());\n }\n return ans;\n }\n};", "memory": "58770" }
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>
3
{ "code": "class Solution {\npublic:\n vector<double> medianSlidingWindow(vector<int>& nums, int k) {\n multiset<int> s1, s2;\n for (int i = 0; i < k; i++) {\n if (s1.empty()) {\n s1.insert(nums[i]);\n continue;\n }\n if (s1.size() == s2.size()) {\n s2.insert(nums[i]);\n int small = *s2.begin();\n s1.insert(small);\n s2.erase(s2.find(small));\n } else {\n s1.insert(nums[i]);\n int large = *s1.rbegin();\n s2.insert(large);\n s1.erase(s1.find(large));\n }\n }\n vector<double> ans;\n if (k % 2 == 1) {\n ans.push_back(*s1.rbegin());\n } else {\n double med = (1.0*(*s1.rbegin()) + (*s2.begin())) / 2.0;\n ans.push_back(med);\n }\n for (int i = k; i < nums.size(); i++) {\n int del = nums[i - k];\n if (*s1.rbegin() >= del) {\n s1.erase(s1.find(del));\n if (s1.size() < s2.size()) {\n int small = *s2.begin();\n s1.insert(small);\n s2.erase(s2.find(small));\n }\n } else {\n s2.erase(s2.find(del));\n if (s1.size() - s2.size() == 2) {\n int large = *s1.rbegin();\n s2.insert(large);\n s1.erase(s1.find(large));\n }\n }\n if (s1.size() == s2.size()) {\n s2.insert(nums[i]);\n int small = *s2.begin();\n s1.insert(small);\n s2.erase(s2.find(small));\n } else {\n s1.insert(nums[i]);\n int large = *s1.rbegin();\n s2.insert(large);\n s1.erase(s1.find(large));\n }\n if (k % 2 == 1) {\n ans.push_back(*s1.rbegin());\n } else {\n double med = (1.0*(*s1.rbegin()) + (*s2.begin())) / 2.0;\n ans.push_back(med);\n }\n }\n return ans;\n }\n};", "memory": "59150" }
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>
3
{ "code": "class Solution {\npublic:\n void add_element(multiset<double>& mini, multiset<double, greater<double>>& maxi, int data){\n mini.insert(data);\n maxi.insert(*mini.begin());\n mini.erase(mini.begin());\n if(maxi.size() > mini.size()){\n mini.insert(*maxi.begin());\n maxi.erase(maxi.begin());\n }\n return ;\n }\n\n void del_element(multiset<double>& mini, multiset<double, greater<double>>& maxi, int data){\n if(mini.find(data) != mini.end())\n mini.erase(mini.find(data));\n else if(maxi.find(data) != maxi.end())\n maxi.erase(maxi.find(data));\n return ;\n }\n\n vector<double> medianSlidingWindow(vector<int>& nums, int k){\n int n = nums.size();\n vector<double> result;\n multiset<double> mini;\n multiset<double, greater<double>> maxi;\n for(int i=0;i<k;i++)\n add_element(mini, maxi, nums[i]);\n if(k % 2 == 0){\n double res = (double)(*mini.begin() + *maxi.begin())/(double)2;\n result.push_back(res);\n }\n else\n result.push_back(*mini.begin());\n for(int i=k;i<n;i++){\n del_element(mini, maxi, nums[i-k]);\n add_element(mini, maxi, nums[i]);\n if(k % 2 == 0){\n double res = (double)(*mini.begin() + *maxi.begin())/(double)2;\n result.push_back(res);\n }\n else\n result.push_back(*mini.begin());\n }\n return result;\n }\n};", "memory": "59150" }
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>
3
{ "code": "class Solution {\npublic:\n multiset<int> lo, hi;\n vector<double> medianSlidingWindow(vector<int>& a, int k) {\n vector<double> ret;\n for (int i=0; i<a.size(); ++i) {\n lo.insert(a[i]);\n if ((lo.size()-hi.size()) > 1) {\n hi.insert(*lo.rbegin());\n lo.erase(prev(lo.end()));\n }\n if (!hi.empty() && *lo.rbegin() > *hi.begin()) {\n int to_lo = *hi.begin(); \n hi.erase(hi.begin());\n int to_hi = *lo.rbegin(); \n lo.erase(prev(lo.end()));\n hi.insert(to_hi);\n lo.insert(to_lo);\n }\n if ((lo.size()+hi.size()) == k) {\n ret.push_back(k%2==1 ? *lo.rbegin() : ((double)*lo.rbegin() + *hi.begin()) * 0.5);\n\n // delete element at the head (i-(k-1))\n int elem = a[i-(k-1)];\n if (elem <= *lo.rbegin()) {\n lo.erase(lo.find(elem));\n } else hi.erase(hi.find(elem));\n }\n }\n return ret;\n }\n};", "memory": "59530" }
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>
3
{ "code": "\nclass Solution {\npublic:\n \n vector<double> medianSlidingWindow(vector<int>& v, int k) {\n multiset<int>ms1,ms2;\n vector<double>op;\n bool odd = k%2==1;\n //cout<<odd;\n for(int i=0;i<k;i++){\n cout<<i;\n ms1.insert(v[i]);\n \n if(ms1.size()-ms2.size()>1){\n auto toMoveI = ms1.find(*ms1.rbegin());\n ms2.insert(*toMoveI);\n ms1.erase(toMoveI);\n }\n while(ms1.size() && ms2.size() && *ms1.rbegin()>*ms2.begin()){\n //swap\n int v1 = *ms1.rbegin(), v2 = *ms2.begin();\n ms1.erase(ms1.find(v1));\n ms2.erase(ms2.find(v2));\n\n ms1.insert(v2);\n ms2.insert(v1);\n }\n }\n //return op;\n if(odd) op.push_back(*ms1.rbegin());\n else op.push_back((double)((double)*ms1.rbegin() + (double)*ms2.begin())/2);\n\n for(int i=k;i<v.size();i++){\n //slide \n int torem = v[i-k];\n if(torem <= *ms1.rbegin()){ //ele in 1st half\n ms1.erase(ms1.find(torem));\n }\n else{\n ms2.erase(ms2.find(torem));\n }\n ms1.insert(v[i]);\n \n while(ms1.size()-ms2.size()>1){\n auto toMoveI = ms1.find(*ms1.rbegin());\n ms2.insert(*toMoveI);\n ms1.erase(toMoveI);\n }\n while(ms1.size() && ms2.size() && *ms1.rbegin()>*ms2.begin()){\n //swap\n int v1 = *ms1.rbegin(), v2 = *ms2.begin();\n ms1.erase(ms1.find(v1));\n ms2.erase(ms2.find(v2));\n\n ms1.insert(v2);\n ms2.insert(v1);\n }\n \n if(odd) op.push_back(*ms1.rbegin());\n else op.push_back((double)((double)*ms1.rbegin() + (double)*ms2.begin())/2);\n }\n return op;\n}\n \n};", "memory": "59530" }
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>
3
{ "code": "class Solution {\n\n void insertAndBalanceSets(multiset<int> &topMinSet, multiset<int, greater<int>> &topMaxSet) {\n // printSet(topMinSet);\n // printSet(topMaxSet);\n // cout<<endl;\n if(topMaxSet.size() - topMinSet.size() == 2) {\n // cout<<\" size gr than 2\\n\";\n topMinSet.insert(*topMaxSet.begin());\n topMaxSet.erase(topMaxSet.begin()) ;\n }\n\n else if(topMinSet.size() > 0 && *topMaxSet.begin() > *topMinSet.begin()) {\n topMinSet.insert(*topMaxSet.begin());\n topMaxSet.insert(*topMinSet.begin()) ;\n topMaxSet.erase(topMaxSet.begin()) ;\n topMinSet.erase(topMinSet.begin()) ;\n }\n\n // printSet(topMinSet);\n // printSet(topMaxSet);\n // cout<<endl;\n \n }\n\n double getMedian(multiset<int> &topMinSet, multiset<int, greater<int>> &topMaxSet){\n if(topMaxSet.size() - topMinSet.size() == 1) {\n return *topMaxSet.begin();\n } else{\n return (*topMinSet.begin()*1.0 + *topMaxSet.begin()*1.0 )/2.0 ;\n }\n }\n \n void printSet(multiset<int> &x){\n cout<<\"min set\\n\";\n for(auto n: x){\n cout<<n<<\" \";\n }\n\n cout<<endl;\n }\n\n void printSet(multiset<int, greater<int>> x ){\n cout<<\"max set\\n\";\n for(auto n: x){\n cout<<n<<\" \";\n }\n\n cout<<endl;\n }\n\npublic:\n vector<double> medianSlidingWindow(vector<int>& nums, int k) {\n \n multiset<int> topMinSet;\n multiset<int, greater<int>> topMaxSet ;\n vector<double> ans ;\n for(int i=0; i<nums.size(); i++){\n // cout<<\"i \"<<i<<endl;\n topMaxSet.insert(nums[i]);\n insertAndBalanceSets(topMinSet , topMaxSet);\n\n // printSet(topMinSet);\n // printSet(topMaxSet);\n // cout<<endl;\n\n if(i >= k-1) {\n ans.push_back(getMedian(topMinSet , topMaxSet));\n if(topMinSet.find(nums[i-k+1]) != topMinSet.end()) {\n topMinSet.erase(topMinSet.find(nums[i-k+1])) ;\n }\n\n else if(topMaxSet.find(nums[i-k+1]) != topMaxSet.end()) {\n topMaxSet.erase(topMaxSet.find(nums[i-k+1])) ;\n }\n\n insertAndBalanceSets(topMinSet , topMaxSet);\n // printSet(topMinSet);\n // printSet(topMaxSet);\n // cout<<endl;\n }\n }\n return ans;\n }\n};", "memory": "59910" }
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>
3
{ "code": "class Solution {\npublic:\n\n multiset<long long> L;\n multiset<long long> R;\n\n void add(int x){\n if((int)L.size() < (int)R.size()){\n R.insert(x);\n L.insert(*R.begin());\n R.erase(R.begin());\n }else{\n L.insert(x);\n R.insert(*L.rbegin());\n L.erase(prev(L.end()));\n }\n }\n\n void remove(int x){\n auto t = L.find(x);\n if(t!=L.end()){\n // found here\n L.erase(t);\n if((int)R.size() - (int)L.size() > 1){\n L.insert(*R.begin());\n R.erase(R.begin());\n }\n }else{\n t = R.find(x);\n R.erase(t);\n\n if((int)L.size() > (int)R.size()){\n R.insert(*L.rbegin());\n L.erase(prev(L.end()));\n }\n }\n }\n\n double med(){\n if((int)L.size()==(int)R.size())\n return (*L.rbegin() + *R.begin())/2.0;\n else\n return *R.begin();\n }\n\n vector<double> medianSlidingWindow(vector<int>& a, int k) {\n vector<double> ans;\n int n = (int)a.size(), i;\n\n for(i=0;i<k;i++){\n add(a[i]);\n \n }\n\n ans.push_back(med());\n\n for(i=k;i<n;i++){\n add(a[i]);\n remove(a[i-k]);\n ans.push_back(med());\n }\n\n return ans;\n\n }\n};", "memory": "59910" }
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>
3
{ "code": "class Solution {\npublic:\n vector<double> medianSlidingWindow(vector<int>& vec, int k) {\n multiset<double> big, low;\n vector<double> ans;\n\n for (int i=0; i<vec.size(); ++i) {\n if (i >= k) {\n if (low.contains(vec[i-k])) low.erase(low.find(vec[i-k]));\n else big.erase(big.find(vec[i-k]));\n }\n low.insert(vec[i]);\n \n while (big.size() < k/2 + 1 and !low.empty()) {\n big.insert(*low.rbegin());\n low.erase(low.find(*low.rbegin()));\n }\n\n if (!low.empty()) while (*low.rbegin() > *big.begin()) {\n int mnbig = *big.begin();\n big.erase(big.begin());\n big.insert(*low.rbegin());\n low.erase(low.find(*low.rbegin()));\n low.insert(mnbig);\n }\n\n if (low.size() + big.size() == k) ans.push_back(k % 2 ? *big.begin() : (*big.begin() + *(++big.begin())) / 2);\n }\n\n return ans;\n }\n};", "memory": "60290" }
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>
3
{ "code": "class Solution {\npublic:\n void Balance(multiset<long long>&mst1,multiset<long long>&mst2)\n {\n if(mst1.size()==1&&mst2.empty())\n return;\n\n if(mst1.size()>1&&mst2.empty())\n { long long val= *(mst1.rbegin());\n mst2.insert(val);\n mst1.erase(mst1.find(val));\n }\n else{\n if(mst1.size()-mst2.size()==1)\n {\n if(*(mst1.rbegin()) <= *(mst2.begin()))\n return;\n else\n {\n auto val = *mst1.rbegin();\n\n mst1.erase(mst1.find(val));\n\n mst2.insert(val);\n\n mst1.insert(*(mst2.begin()));\n mst2.erase(mst2.begin());\n\n \n }\n\n }\n else if(mst1.size()-mst2.size()>=2){\n mst2.insert(*(mst1.rbegin()));\n\n mst1.erase(mst1.find(*(mst1.rbegin())));\n \n \n }\n else\n {\n while(*(mst1.rbegin())>*(mst2.begin()))\n {\n int val1= *(mst1.rbegin());\n int val2= *(mst2.begin());\n mst1.erase(mst1.find(val1));\n mst2.erase(mst2.find(val2));\n\n mst1.insert(val2);\n mst2.insert(val1);\n }\n }\n \n }\n }\n vector<double> medianSlidingWindow(vector<int>& nums, int k) {\n multiset<long long>mst1,mst2;\n\n vector<double>ans;\n\n for(int i=0;i<nums.size();i++)\n { \n mst1.insert(nums[i]);\n Balance(mst1,mst2);\n\n if(i+1-k<0)continue;\n\n long long val1= *(mst1.rbegin());\n if(k%2==0)\n {long long val2= *(mst2.begin());\n ans.push_back(((double)val2+(double)val1)/2.0);\n\n }\n else\n {\n ans.push_back(val1);\n }\n\n long long val= nums[i+1-k];\n\n if(mst1.find(val)!=mst1.end())\n {\n mst1.erase(mst1.find(val));\n }\n else\n {\n mst2.erase(mst2.find(val));\n }\n cout<<i<<\" \";\n }\n\n return ans;\n\n }\n};", "memory": "60670" }
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>
3
{ "code": "class Solution {\npublic:\n vector<double> medianSlidingWindow(vector<int>& nums, int k) {\n vector<double> medians(nums.size()-k+1, 0); \n for (int i = 0; i < nums.size(); i++) {\n // cout << \"insert \" << nums[i] << endl;\n insert(nums[i]);\n if (i < k-1) {\n rebalance();\n } else if (i == k-1) {\n rebalance();\n medians[i-k+1] = get_median(k);\n } else {\n // cout << \"remove \" << nums[i-k] << endl;\n remove(nums[i-k]);\n rebalance();\n medians[i-k+1] = get_median(k);\n }\n }\n return medians;\n }\n\n double get_median(int k) {\n if (k % 2 == 1) {\n return *min_heap.begin();\n }\n return (1.0) * ((double)*min_heap.begin() + (double)*max_heap.begin()) / 2;\n }\n\n void insert(int n) {\n max_heap.insert(n);\n min_heap.insert(*max_heap.begin());\n max_heap.erase(max_heap.begin());\n }\n\n void remove(int n) {\n // cout << \"remove method\" << endl;\n if (max_heap.size() == 0) { // special case\n min_heap.erase(min_heap.find(n));\n return;\n }\n if (*max_heap.begin() >= n) {\n max_heap.erase(max_heap.find(n));\n } else {\n min_heap.erase(min_heap.find(n));\n }\n }\n\n void rebalance() {\n // cout << \"enter rebalance \" << min_heap.size() << \" \" << max_heap.size() << endl;\n while (min_heap.size() - max_heap.size() >= 2) {\n max_heap.insert(*min_heap.begin());\n min_heap.erase(min_heap.begin());\n }\n }\n\n multiset<int> min_heap;\n multiset<int, greater<int>> max_heap;\n\n // maintain two heap?\n // max_heap maintains the first small half, min_heap maintain the second large half\n};", "memory": "60670" }
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>
3
{ "code": "class Solution {\npublic:\n vector<double> medianSlidingWindow(vector<int>& nums, int k) {\n std::multiset<int> left;\n std::multiset<int> right;\n int const n = nums.size();\n vector<double> meds(n-k+1,0);\n int i=0;\n bool odd = k %2;\n for(;i<n; ++i){\n if(i>=k){\n if(nums[i-k] <= *left.rbegin() ){\n left.erase(left.find(nums[i-k]));\n //left.erase()\n }\n else{\n right.erase(right.find(nums[i-k]));\n }\n }\n\n left.insert(nums[i]);\n\n right.insert(*left.rbegin());\n left.erase(prev(left.end()));\n\n if(left.size() < right.size()){\n left.insert(*right.begin());\n right.erase(right.begin());\n }\n\n if(i >=k-1){\n double x = odd ? *left.rbegin() : \n 0.5 * (double(*left.rbegin()) + *right.begin());\n meds[i+1-k] = x; \n }\n\n }\n\n return meds;\n\n \n \n }\n};", "memory": "61050" }
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>
3
{ "code": "class Solution {\npublic:\n vector<double> medianSlidingWindow(vector<int>& nums, int k) {\n std::multiset<int> left;\n std::multiset<int> right;\n int const n = nums.size();\n vector<double> meds(n-k+1,0);\n int i=0;\n bool odd = k %2;\n for(;i<n; ++i){\n if(i>=k){\n if(nums[i-k] <= *left.rbegin() ){\n left.erase(left.find(nums[i-k]));\n //left.erase()\n }\n else{\n right.erase(right.find(nums[i-k]));\n }\n }\n\n left.insert(nums[i]);\n\n right.insert(*left.rbegin());\n left.erase(prev(left.end()));\n\n if(left.size() < right.size()){\n left.insert(*right.begin());\n right.erase(right.begin());\n }\n\n if(i >=k-1){\n double x = odd ? *left.rbegin() : \n 0.5 * (double(*left.rbegin()) + *right.begin());\n meds[i+1-k] = x; \n }\n\n }\n\n return meds;\n\n \n \n }\n};", "memory": "61050" }
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>
3
{ "code": "class Solution {\npublic:\n vector<double> medianSlidingWindow(vector<int>& nums, int k) {\n vector<double> res;\n multiset<int> l, r;\n int n = nums.size();\n int left = 0, right = 0;\n auto remove = [&](int x) -> void {\n if (l.find(x) != l.end()) {\n l.erase(l.find(x));\n } else if (r.find(x) != r.end()) {\n r.erase(r.find(x));\n }\n while ((int)l.size() > (int)r.size() + 1) {\n int v = *l.rbegin();\n l.erase(l.find(v));\n r.insert(v);\n }\n while ((int)l.size() < (int)r.size()) {\n int v = *r.begin();\n r.erase(r.find(v));\n l.insert(v);\n }\n };\n auto add = [&](int x) -> void {\n l.insert(x);\n while(!l.empty() && !r.empty() && *l.rbegin() > *r.begin()){\n int v = *l.rbegin();\n l.erase(l.find(v));\n r.insert(v);\n }\n while ((int)l.size() > (int)r.size() + 1) {\n int v = *l.rbegin();\n l.erase(l.find(v));\n r.insert(v);\n }\n while ((int)l.size() < (int)r.size()) {\n int v = *r.begin();\n r.erase(r.find(v));\n l.insert(v);\n }\n };\n while (right < n) {\n add(nums[right]);\n if (right - left + 1 > k) {\n remove(nums[left]);\n left++;\n }\n if ((right - left + 1) == k) {\n if ((k % 2) == 1) {\n res.push_back(*l.rbegin());\n } else {\n res.push_back((long long)(*l.rbegin() + (long long)*r.begin())/2.0);\n }\n }\n right++;\n }\n\n return res;\n }\n};\n", "memory": "61430" }
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>
3
{ "code": "class MedianFinder {\npublic:\n multiset<double> ms1, ms2;\n MedianFinder() {}\n \n void addNum(int num) {\n ms1.insert(num);\n rebalance();\n }\n\n void deleteNum(int num) {\n if(ms2.find(num) != ms2.end()) {\n ms2.erase(ms2.find(num));\n rebalance();\n return;\n }\n ms1.erase(ms1.find(num));\n if(ms1.size() < ms2.size()) {\n ms1.insert(*ms2.begin());\n ms2.erase(ms2.begin());\n }\n }\n\n void rebalance() {\n if(ms1.size() > ms2.size() + 1) {\n ms2.insert(*ms1.rbegin());\n ms1.erase(ms1.find(*ms1.rbegin()));\n }\n if(ms2.size()) {\n if(*ms2.begin() < *ms1.rbegin()) {\n double a = *ms2.begin(), b = *ms1.rbegin();\n ms2.erase(ms2.begin());\n ms1.erase(ms1.find(b));\n ms1.insert(a);\n ms2.insert(b);\n }\n }\n }\n \n double findMedian() {\n if((ms1.size() + ms2.size())%2==0) {\n return (*ms1.rbegin() + *ms2.begin())/2;\n } else {\n return *ms1.rbegin();\n }\n }\n};\n\nclass Solution {\npublic:\n vector<double> medianSlidingWindow(vector<int>& nums, int k) {\n MedianFinder mf;\n for(int i = 0; i<k;++i) mf.addNum(nums[i]);\n vector<double> ans;\n ans.push_back(mf.findMedian());\n for(int i = k; i < nums.size();++i) {\n mf.addNum(nums[i]);\n mf.deleteNum(nums[i-k]);\n ans.push_back(mf.findMedian());\n }\n return ans;\n }\n};", "memory": "61430" }
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>
3
{ "code": "class Solution {\npublic:\n vector<double> medianSlidingWindow(vector<int>& arr, int k) {\n int n = arr.size();\n vector<double> ans;\n multiset<int> low,high;\n for(int i=0;i<n;i++){\n //insertion\n low.insert(arr[i]);\n high.insert(*low.rbegin());\n low.erase(low.find(*low.rbegin()));\n if(low.size()<high.size()){\n low.insert(*high.begin());\n high.erase(high.find(*high.begin()));\n }\n if(i>=k-1){\n if(k&1) ans.push_back(*low.rbegin());\n else ans.push_back(((double)*low.rbegin()+(double)*high.begin())/2.0);\n\n if(arr[i-k+1]<=*low.rbegin()) low.erase(low.find(arr[i-k+1]));\n else high.erase(high.find(arr[i-k+1]));\n }\n }\n return ans;\n }\n};", "memory": "61810" }
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>
3
{ "code": "/*\t\n\t\n\tconstraints: n == k == 10e5\n\t\n\tMy Approaches:\n\n\tA) Navie: sort every subarray of size k, and find middle and store as answer -> TLE\n\n\tTC: O(n * klogk) => O(10^5 * 10^5(log(10^5))) => will give TLE\n\n\tB) Sets: unique | sorted -> can't use as duplicates are possible in subarray\n\t\n\tC) Multisets: duplices | sorted | Balanced BST (logN)\n\t\t\n\t\tAs we move window, we will push elements to multiset, and it will come sorted, then to find middle for median,\n\t\twe would do iterator on BST (of k size), that will take extra TC of O(k) as we can't do [] indexing on BST\n\n\t\tSo it would have taken TC: O(nklogk), but we need TC lesser than O(nk)\n\n\t\tTo solve this:\n\n\tC.1) Two multisets:\n\n\t\tinstead of iterating through multiset of size k, we could have used multiset.begin() and multiset.rbegin() to find median\n\n\t\tTC: O(n * logk) i.e., < O(nk)\n\n\tD) Map: balanced BST -> has 'key-value' pair compared to multiset which has just 'key'\n\tWe can also use Map if we would want\n\n\n\t2 1 3 4\n\t{} {2} -> {2} {}\n\t{2} {1} -> {1 2} {}\n\t{1 2} {3}\n\n*/\n\nclass Solution {\npublic:\n #define vi vector<int> \n #define vd vector<double>\n vector<double> medianSlidingWindow(vector<int>& nums, int k) {\n int n = nums.size();\n\n multiset<int> low;\n multiset<int> high;\n\n vd result;\n\n for (int i=0;i<n;i++) {\n\n // outside window - remove one occurance of outside element from either low or high depending upon where it is\n if (i-k >= 0) { \n if (nums[i-k] <= *low.rbegin()) { // either nums[i] will be in low or high or both, let's find as it is sorted\n low.erase(low.find(nums[i-k]));\n }\n else {\n high.erase(high.find(nums[i-k]));\n }\n }\t\n\n // step 1: insert to low\n low.insert(nums[i]);\n\n // step 2: remove highest from low, and add it to high\n high.insert(*low.rbegin());\n low.erase(low.find(*low.rbegin()));\n\n // step 3: check conditon \n // low.size() == high.size() if even OR low.size() == high.size() + 1 if odd, i.e., high size can't be bigger than low size\n\n if (low.size() < high.size()) {\n low.insert(*high.begin());\n high.erase(high.begin());\n }\n\n /*\n Note:\n\n low.rbegin() will always be smaller than high.begin() because:\n after step 2, low.rbegin() <= high.begin()\n\n e.g., {1,3} {2,6}\n step 2: {1} {2,3,6}\n\n So we don't need to check if *low.rbegin() > *high.begin() & move high.begin() to low\n */\n\n if (i >= k-1) { // pushing median to result vector\n if (k & 1) { // k is odd\n result.push_back(*low.rbegin());\n }\n else { // k is even\n result.push_back(((double)*low.rbegin() + (double)*high.begin())/2.0);\n }\n }\n }\n\n return result;\n }\n};", "memory": "61810" }
482
<p>You are given a license key represented as a string <code>s</code> that consists of only alphanumeric characters and dashes. The string is separated into <code>n + 1</code> groups by <code>n</code> dashes. You are also given an integer <code>k</code>.</p> <p>We want to reformat the string <code>s</code> such that each group contains exactly <code>k</code> characters, except for the first group, which could be shorter than <code>k</code> but still must contain at least one character. Furthermore, there must be a dash inserted between two groups, and you should convert all lowercase letters to uppercase.</p> <p>Return <em>the reformatted license key</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;5F3Z-2e-9-w&quot;, k = 4 <strong>Output:</strong> &quot;5F3Z-2E9W&quot; <strong>Explanation:</strong> The string s has been split into two parts, each part has 4 characters. Note that the two extra dashes are not needed and can be removed. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;2-5g-3-J&quot;, k = 2 <strong>Output:</strong> &quot;2-5G-3J&quot; <strong>Explanation:</strong> The string s has been split into three parts, each part has 2 characters except the first part as it could be shorter as mentioned above. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists of English letters, digits, and dashes <code>&#39;-&#39;</code>.</li> <li><code>1 &lt;= k &lt;= 10<sup>4</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n string licenseKeyFormatting(string s, int k) {\n size_t pos;\n while((pos = s.find('-')) != string::npos){\n s.erase(pos, 1);\n }\n \n for(char& c: s){\n c = toupper(c);\n }\n \n if(s.size() < k){\n return s;\n }\n \n size_t first_part = s.size() % k;\n \n pos = 0;\n if(first_part != 0){\n s.insert(first_part, 1, '-');\n pos += (first_part + 1);\n }\n \n pos += k;\n while(pos < s.size()){\n s.insert(pos, 1, '-');\n pos += (k + 1);\n }\n \n return s;\n }\n};", "memory": "9100" }
482
<p>You are given a license key represented as a string <code>s</code> that consists of only alphanumeric characters and dashes. The string is separated into <code>n + 1</code> groups by <code>n</code> dashes. You are also given an integer <code>k</code>.</p> <p>We want to reformat the string <code>s</code> such that each group contains exactly <code>k</code> characters, except for the first group, which could be shorter than <code>k</code> but still must contain at least one character. Furthermore, there must be a dash inserted between two groups, and you should convert all lowercase letters to uppercase.</p> <p>Return <em>the reformatted license key</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;5F3Z-2e-9-w&quot;, k = 4 <strong>Output:</strong> &quot;5F3Z-2E9W&quot; <strong>Explanation:</strong> The string s has been split into two parts, each part has 4 characters. Note that the two extra dashes are not needed and can be removed. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;2-5g-3-J&quot;, k = 2 <strong>Output:</strong> &quot;2-5G-3J&quot; <strong>Explanation:</strong> The string s has been split into three parts, each part has 2 characters except the first part as it could be shorter as mentioned above. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists of English letters, digits, and dashes <code>&#39;-&#39;</code>.</li> <li><code>1 &lt;= k &lt;= 10<sup>4</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n string licenseKeyFormatting(string s, int k) {\n int n=s.length();\n int count=0;\n\n for(int i=0; i<n; i++) {\n if (s[i]>='a' && s[i]<='z') {\n s[i]=s[i]-'a'+'A';\n }\n }\n //transform(s.begin(), s.end(), s.begin(),::toupper);\n\n for(int i=n-1; i>=0; i--) {\n if (count==k && s[i]!='-') {\n count=0;\n s.insert(i+1,\"-\");\n i++;\n } else if ((s[i]>='A' && s[i]<='Z') || (s[i]>='0' && s[i]<='9')) {\n count++;\n } else if (s[i]=='-' && count!=k) {\n s.erase(i,1);\n } else {\n count=0;\n }\n }\n if(s[0]=='-') {\n s.erase(0,1);\n }\n return s;\n }\n};\n\n/*\nclass Solution {\npublic:\n string licenseKeyFormatting(string s, int k) {\n transform(s.begin(), s.end(), s.begin(),::toupper);\n\n int j=s.length(), count=0;\n for(int i=j-1; i>=0; i--) {\n if (count==k){\n count=0;\n if(s[i]!='-'){\n s.insert(i+1,\"-\");\n }\n cout << s;\n }\n else if((s[i]>='A' && s[i]<='Z') || (s[i]>='0' &&s[i]<='9')) {\n count++;\n } else {\n if (count==k) {\n count=0;\n if(s[i]!='-'){\n s.insert(i,\"-\");\n }\n } else {\n s.erase(i,1);\n }\n }\n }\n //cout << s;\n return s;\n }\n};\n*/", "memory": "9200" }
482
<p>You are given a license key represented as a string <code>s</code> that consists of only alphanumeric characters and dashes. The string is separated into <code>n + 1</code> groups by <code>n</code> dashes. You are also given an integer <code>k</code>.</p> <p>We want to reformat the string <code>s</code> such that each group contains exactly <code>k</code> characters, except for the first group, which could be shorter than <code>k</code> but still must contain at least one character. Furthermore, there must be a dash inserted between two groups, and you should convert all lowercase letters to uppercase.</p> <p>Return <em>the reformatted license key</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;5F3Z-2e-9-w&quot;, k = 4 <strong>Output:</strong> &quot;5F3Z-2E9W&quot; <strong>Explanation:</strong> The string s has been split into two parts, each part has 4 characters. Note that the two extra dashes are not needed and can be removed. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;2-5g-3-J&quot;, k = 2 <strong>Output:</strong> &quot;2-5G-3J&quot; <strong>Explanation:</strong> The string s has been split into three parts, each part has 2 characters except the first part as it could be shorter as mentioned above. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists of English letters, digits, and dashes <code>&#39;-&#39;</code>.</li> <li><code>1 &lt;= k &lt;= 10<sup>4</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n string licenseKeyFormatting(string s, int k) {\n int n=s.length();\n int count=0;\n\n transform(s.begin(), s.end(), s.begin(),::toupper);\n\n for(int i=n-1; i>=0; i--) {\n if (count==k && s[i]!='-') {\n count=0;\n s.insert(i+1,\"-\");\n i++;\n } else if ((s[i]>='A' && s[i]<='Z') || (s[i]>='0' && s[i]<='9')) {\n count++;\n } else if (s[i]=='-' && count!=k) {\n s.erase(i,1);\n } else {\n count=0;\n }\n }\n if(s[0]=='-') {\n s.erase(0,1);\n }\n return s;\n }\n};\n\n/*\nclass Solution {\npublic:\n string licenseKeyFormatting(string s, int k) {\n transform(s.begin(), s.end(), s.begin(),::toupper);\n\n int j=s.length(), count=0;\n for(int i=j-1; i>=0; i--) {\n if (count==k){\n count=0;\n if(s[i]!='-'){\n s.insert(i+1,\"-\");\n }\n cout << s;\n }\n else if((s[i]>='A' && s[i]<='Z') || (s[i]>='0' &&s[i]<='9')) {\n count++;\n } else {\n if (count==k) {\n count=0;\n if(s[i]!='-'){\n s.insert(i,\"-\");\n }\n } else {\n s.erase(i,1);\n }\n }\n }\n //cout << s;\n return s;\n }\n};\n*/", "memory": "9300" }
482
<p>You are given a license key represented as a string <code>s</code> that consists of only alphanumeric characters and dashes. The string is separated into <code>n + 1</code> groups by <code>n</code> dashes. You are also given an integer <code>k</code>.</p> <p>We want to reformat the string <code>s</code> such that each group contains exactly <code>k</code> characters, except for the first group, which could be shorter than <code>k</code> but still must contain at least one character. Furthermore, there must be a dash inserted between two groups, and you should convert all lowercase letters to uppercase.</p> <p>Return <em>the reformatted license key</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;5F3Z-2e-9-w&quot;, k = 4 <strong>Output:</strong> &quot;5F3Z-2E9W&quot; <strong>Explanation:</strong> The string s has been split into two parts, each part has 4 characters. Note that the two extra dashes are not needed and can be removed. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;2-5g-3-J&quot;, k = 2 <strong>Output:</strong> &quot;2-5G-3J&quot; <strong>Explanation:</strong> The string s has been split into three parts, each part has 2 characters except the first part as it could be shorter as mentioned above. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists of English letters, digits, and dashes <code>&#39;-&#39;</code>.</li> <li><code>1 &lt;= k &lt;= 10<sup>4</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n string licenseKeyFormatting(string s, int k) {\n int n=s.length();\n int count=0;\n\n transform(s.begin(), s.end(), s.begin(),::toupper);\n\n for(int i=n-1; i>=0; i--) {\n if (count==k && s[i]!='-') {\n count=0;\n s.insert(i+1,\"-\");\n i++;\n } else if ((s[i]>='A' && s[i]<='Z') || (s[i]>='0' && s[i]<='9')) {\n count++;\n } else if (s[i]=='-' && count!=k) {\n s.erase(i,1);\n } else {\n count=0;\n }\n }\n if(s[0]=='-') {\n s.erase(0,1);\n }\n return s;\n }\n};\n\n/*\nclass Solution {\npublic:\n string licenseKeyFormatting(string s, int k) {\n transform(s.begin(), s.end(), s.begin(),::toupper);\n\n int j=s.length(), count=0;\n for(int i=j-1; i>=0; i--) {\n if (count==k){\n count=0;\n if(s[i]!='-'){\n s.insert(i+1,\"-\");\n }\n cout << s;\n }\n else if((s[i]>='A' && s[i]<='Z') || (s[i]>='0' &&s[i]<='9')) {\n count++;\n } else {\n if (count==k) {\n count=0;\n if(s[i]!='-'){\n s.insert(i,\"-\");\n }\n } else {\n s.erase(i,1);\n }\n }\n }\n //cout << s;\n return s;\n }\n};\n*/", "memory": "9300" }
482
<p>You are given a license key represented as a string <code>s</code> that consists of only alphanumeric characters and dashes. The string is separated into <code>n + 1</code> groups by <code>n</code> dashes. You are also given an integer <code>k</code>.</p> <p>We want to reformat the string <code>s</code> such that each group contains exactly <code>k</code> characters, except for the first group, which could be shorter than <code>k</code> but still must contain at least one character. Furthermore, there must be a dash inserted between two groups, and you should convert all lowercase letters to uppercase.</p> <p>Return <em>the reformatted license key</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;5F3Z-2e-9-w&quot;, k = 4 <strong>Output:</strong> &quot;5F3Z-2E9W&quot; <strong>Explanation:</strong> The string s has been split into two parts, each part has 4 characters. Note that the two extra dashes are not needed and can be removed. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;2-5g-3-J&quot;, k = 2 <strong>Output:</strong> &quot;2-5G-3J&quot; <strong>Explanation:</strong> The string s has been split into three parts, each part has 2 characters except the first part as it could be shorter as mentioned above. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists of English letters, digits, and dashes <code>&#39;-&#39;</code>.</li> <li><code>1 &lt;= k &lt;= 10<sup>4</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n std::string licenseKeyFormatting(std::string s, int k) {\n std::string sigma = s;\n while (std::find(sigma.begin(), sigma.end(), '-') != sigma.end())\n {\n sigma.erase(std::find(sigma.begin(), sigma.end(), '-'));\n }\n if (sigma.size() == 0)\n {\n return sigma;\n }\n std::transform(sigma.begin(), sigma.end(), sigma.begin(), ::toupper);\n int x = 1;\n for (size_t i = sigma.size()-1; i > 0; --i)\n {\n if (x % k == 0)\n {\n sigma.insert(i, \"-\");\n }\n ++x;\n }\n return sigma;\n }\n};", "memory": "9400" }
482
<p>You are given a license key represented as a string <code>s</code> that consists of only alphanumeric characters and dashes. The string is separated into <code>n + 1</code> groups by <code>n</code> dashes. You are also given an integer <code>k</code>.</p> <p>We want to reformat the string <code>s</code> such that each group contains exactly <code>k</code> characters, except for the first group, which could be shorter than <code>k</code> but still must contain at least one character. Furthermore, there must be a dash inserted between two groups, and you should convert all lowercase letters to uppercase.</p> <p>Return <em>the reformatted license key</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;5F3Z-2e-9-w&quot;, k = 4 <strong>Output:</strong> &quot;5F3Z-2E9W&quot; <strong>Explanation:</strong> The string s has been split into two parts, each part has 4 characters. Note that the two extra dashes are not needed and can be removed. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;2-5g-3-J&quot;, k = 2 <strong>Output:</strong> &quot;2-5G-3J&quot; <strong>Explanation:</strong> The string s has been split into three parts, each part has 2 characters except the first part as it could be shorter as mentioned above. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists of English letters, digits, and dashes <code>&#39;-&#39;</code>.</li> <li><code>1 &lt;= k &lt;= 10<sup>4</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n\n string licenseKeyFormatting(string s, int k) {\n int n = s.size(), cnt=0;\n string ans = \"\";\n for(int i = n-1; i>=0;i--){\n if(s[i]== '-'){\n continue;\n }\n if(cnt>0 && cnt%k ==0){\n ans.push_back('-');\n }\n ans.push_back(toupper(s[i]));\n cnt++;\n }\n reverse(ans.begin(), ans.end());\n return ans;\n }\n};", "memory": "9500" }
482
<p>You are given a license key represented as a string <code>s</code> that consists of only alphanumeric characters and dashes. The string is separated into <code>n + 1</code> groups by <code>n</code> dashes. You are also given an integer <code>k</code>.</p> <p>We want to reformat the string <code>s</code> such that each group contains exactly <code>k</code> characters, except for the first group, which could be shorter than <code>k</code> but still must contain at least one character. Furthermore, there must be a dash inserted between two groups, and you should convert all lowercase letters to uppercase.</p> <p>Return <em>the reformatted license key</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;5F3Z-2e-9-w&quot;, k = 4 <strong>Output:</strong> &quot;5F3Z-2E9W&quot; <strong>Explanation:</strong> The string s has been split into two parts, each part has 4 characters. Note that the two extra dashes are not needed and can be removed. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;2-5g-3-J&quot;, k = 2 <strong>Output:</strong> &quot;2-5G-3J&quot; <strong>Explanation:</strong> The string s has been split into three parts, each part has 2 characters except the first part as it could be shorter as mentioned above. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists of English letters, digits, and dashes <code>&#39;-&#39;</code>.</li> <li><code>1 &lt;= k &lt;= 10<sup>4</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n string licenseKeyFormatting(string s, int k) {\n int j=0;\n string str=\"\";\n int i;\n for( i=s.length()-1;i>=0;i--)\n { \n if(isalnum(s[i])&& j<k)\n {\n str+=toupper(s[i]);\n j++;\n }\n if(j==k && i!=0)\n {\n str+='-';\n j=0;\n }\n } \n reverse(str.begin(),str.end());\n if(str[0]=='-')\n {\n str=str.substr(1,str.size()-1);\n }\n return str;\n }\n};", "memory": "9600" }
482
<p>You are given a license key represented as a string <code>s</code> that consists of only alphanumeric characters and dashes. The string is separated into <code>n + 1</code> groups by <code>n</code> dashes. You are also given an integer <code>k</code>.</p> <p>We want to reformat the string <code>s</code> such that each group contains exactly <code>k</code> characters, except for the first group, which could be shorter than <code>k</code> but still must contain at least one character. Furthermore, there must be a dash inserted between two groups, and you should convert all lowercase letters to uppercase.</p> <p>Return <em>the reformatted license key</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;5F3Z-2e-9-w&quot;, k = 4 <strong>Output:</strong> &quot;5F3Z-2E9W&quot; <strong>Explanation:</strong> The string s has been split into two parts, each part has 4 characters. Note that the two extra dashes are not needed and can be removed. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;2-5g-3-J&quot;, k = 2 <strong>Output:</strong> &quot;2-5G-3J&quot; <strong>Explanation:</strong> The string s has been split into three parts, each part has 2 characters except the first part as it could be shorter as mentioned above. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists of English letters, digits, and dashes <code>&#39;-&#39;</code>.</li> <li><code>1 &lt;= k &lt;= 10<sup>4</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n string licenseKeyFormatting(string s, int k) {\n string res = \"\";\n int c = 0, j = 0;\n \n for (int i = s.size() - 1; i >= 0; i--) {\n if (s[i] != '-') {\n res += toupper(s[i]);\n c++;\n \n if (c == k) {\n res += '-';\n c = 0;\n }\n }\n }\n \n if (!res.empty() && res.back() == '-') {\n res.pop_back();\n }\n \n reverse(res.begin(), res.end());\n \n return res;\n }\n};", "memory": "9600" }
482
<p>You are given a license key represented as a string <code>s</code> that consists of only alphanumeric characters and dashes. The string is separated into <code>n + 1</code> groups by <code>n</code> dashes. You are also given an integer <code>k</code>.</p> <p>We want to reformat the string <code>s</code> such that each group contains exactly <code>k</code> characters, except for the first group, which could be shorter than <code>k</code> but still must contain at least one character. Furthermore, there must be a dash inserted between two groups, and you should convert all lowercase letters to uppercase.</p> <p>Return <em>the reformatted license key</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;5F3Z-2e-9-w&quot;, k = 4 <strong>Output:</strong> &quot;5F3Z-2E9W&quot; <strong>Explanation:</strong> The string s has been split into two parts, each part has 4 characters. Note that the two extra dashes are not needed and can be removed. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;2-5g-3-J&quot;, k = 2 <strong>Output:</strong> &quot;2-5G-3J&quot; <strong>Explanation:</strong> The string s has been split into three parts, each part has 2 characters except the first part as it could be shorter as mentioned above. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists of English letters, digits, and dashes <code>&#39;-&#39;</code>.</li> <li><code>1 &lt;= k &lt;= 10<sup>4</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n string licenseKeyFormatting(string s, int k) {\n int n = s.length();\n int cnt = (n - count(s.begin(), s.end(), '-')) % k;\n if (cnt == 0) {\n cnt = k;\n }\n string ans;\n for (int i = 0; i < n; ++i) {\n char c = s[i];\n if (c == '-') {\n continue;\n }\n ans += toupper(c);\n if (--cnt == 0) {\n cnt = k;\n if (i != n - 1) {\n ans += '-';\n }\n }\n }\n if (!ans.empty() && ans.back() == '-') {\n ans.pop_back();\n }\n return ans;\n }\n};", "memory": "9700" }
482
<p>You are given a license key represented as a string <code>s</code> that consists of only alphanumeric characters and dashes. The string is separated into <code>n + 1</code> groups by <code>n</code> dashes. You are also given an integer <code>k</code>.</p> <p>We want to reformat the string <code>s</code> such that each group contains exactly <code>k</code> characters, except for the first group, which could be shorter than <code>k</code> but still must contain at least one character. Furthermore, there must be a dash inserted between two groups, and you should convert all lowercase letters to uppercase.</p> <p>Return <em>the reformatted license key</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;5F3Z-2e-9-w&quot;, k = 4 <strong>Output:</strong> &quot;5F3Z-2E9W&quot; <strong>Explanation:</strong> The string s has been split into two parts, each part has 4 characters. Note that the two extra dashes are not needed and can be removed. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;2-5g-3-J&quot;, k = 2 <strong>Output:</strong> &quot;2-5G-3J&quot; <strong>Explanation:</strong> The string s has been split into three parts, each part has 2 characters except the first part as it could be shorter as mentioned above. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists of English letters, digits, and dashes <code>&#39;-&#39;</code>.</li> <li><code>1 &lt;= k &lt;= 10<sup>4</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n string licenseKeyFormatting(string s, int k) {\n int c = 0;\n for (auto i : s) c+= i=='-';\n int mod = (s.size()-c)%k;\n string ans;\n int kk=mod;\n if (kk==0) kk =k;\n for (auto i : s){\n if (isalpha(i)||isdigit(i))ans.push_back(toupper(i)), kk--;\n if (kk==0) {\n ans.push_back('-');\n kk=k;\n }\n }\n if (!ans.empty())if (ans[ans.size()-1]=='-') ans.pop_back();\n return ans;\n }\n};", "memory": "9700" }
482
<p>You are given a license key represented as a string <code>s</code> that consists of only alphanumeric characters and dashes. The string is separated into <code>n + 1</code> groups by <code>n</code> dashes. You are also given an integer <code>k</code>.</p> <p>We want to reformat the string <code>s</code> such that each group contains exactly <code>k</code> characters, except for the first group, which could be shorter than <code>k</code> but still must contain at least one character. Furthermore, there must be a dash inserted between two groups, and you should convert all lowercase letters to uppercase.</p> <p>Return <em>the reformatted license key</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;5F3Z-2e-9-w&quot;, k = 4 <strong>Output:</strong> &quot;5F3Z-2E9W&quot; <strong>Explanation:</strong> The string s has been split into two parts, each part has 4 characters. Note that the two extra dashes are not needed and can be removed. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;2-5g-3-J&quot;, k = 2 <strong>Output:</strong> &quot;2-5G-3J&quot; <strong>Explanation:</strong> The string s has been split into three parts, each part has 2 characters except the first part as it could be shorter as mentioned above. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists of English letters, digits, and dashes <code>&#39;-&#39;</code>.</li> <li><code>1 &lt;= k &lt;= 10<sup>4</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n string licenseKeyFormatting(string s, int k)\n {\n string ans;\n int l = s.length();\n int dash = k;\n for(int i = l - 1; i >= 0; --i)\n {\n if(s[i] == '-')\n continue;\n \n if(0 == dash)\n {\n ans.push_back('-');\n dash = k;\n }\n \n ans.push_back(toupper(s[i]));\n --dash;\n }\n \n reverse(ans.begin(), ans.end());\n return ans;\n }\n};", "memory": "9800" }
482
<p>You are given a license key represented as a string <code>s</code> that consists of only alphanumeric characters and dashes. The string is separated into <code>n + 1</code> groups by <code>n</code> dashes. You are also given an integer <code>k</code>.</p> <p>We want to reformat the string <code>s</code> such that each group contains exactly <code>k</code> characters, except for the first group, which could be shorter than <code>k</code> but still must contain at least one character. Furthermore, there must be a dash inserted between two groups, and you should convert all lowercase letters to uppercase.</p> <p>Return <em>the reformatted license key</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;5F3Z-2e-9-w&quot;, k = 4 <strong>Output:</strong> &quot;5F3Z-2E9W&quot; <strong>Explanation:</strong> The string s has been split into two parts, each part has 4 characters. Note that the two extra dashes are not needed and can be removed. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;2-5g-3-J&quot;, k = 2 <strong>Output:</strong> &quot;2-5G-3J&quot; <strong>Explanation:</strong> The string s has been split into three parts, each part has 2 characters except the first part as it could be shorter as mentioned above. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists of English letters, digits, and dashes <code>&#39;-&#39;</code>.</li> <li><code>1 &lt;= k &lt;= 10<sup>4</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n string licenseKeyFormatting(string s, int k) {\n string str = \"\";\n int n = s.length();\n int cnt=0;\n for(int i = n-1; i>=0; i--){\n \n if(s[i] != '-' && cnt < k){\n str += toupper(s[i]);\n cnt++;\n }\n if(cnt == k && i!=0){\n str += '-';\n cnt = 0;\n }\n }\n\n reverse(str.begin(),str.end());\n if(str[0] == '-'){\n str = str.substr(1,n-1);\n } \n return str;\n }\n};", "memory": "9800" }
482
<p>You are given a license key represented as a string <code>s</code> that consists of only alphanumeric characters and dashes. The string is separated into <code>n + 1</code> groups by <code>n</code> dashes. You are also given an integer <code>k</code>.</p> <p>We want to reformat the string <code>s</code> such that each group contains exactly <code>k</code> characters, except for the first group, which could be shorter than <code>k</code> but still must contain at least one character. Furthermore, there must be a dash inserted between two groups, and you should convert all lowercase letters to uppercase.</p> <p>Return <em>the reformatted license key</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;5F3Z-2e-9-w&quot;, k = 4 <strong>Output:</strong> &quot;5F3Z-2E9W&quot; <strong>Explanation:</strong> The string s has been split into two parts, each part has 4 characters. Note that the two extra dashes are not needed and can be removed. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;2-5g-3-J&quot;, k = 2 <strong>Output:</strong> &quot;2-5G-3J&quot; <strong>Explanation:</strong> The string s has been split into three parts, each part has 2 characters except the first part as it could be shorter as mentioned above. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists of English letters, digits, and dashes <code>&#39;-&#39;</code>.</li> <li><code>1 &lt;= k &lt;= 10<sup>4</sup></code></li> </ul>
2
{ "code": "class Solution {\n public:\n string licenseKeyFormatting(string s, int k) {\n string ans;\n int length = 0;\n\n for (int i = s.length() - 1; i >= 0; --i) {\n if (s[i] == '-')\n continue;\n if (length > 0 && length % k == 0)\n ans += \"-\";\n ans += toupper(s[i]);\n ++length;\n }\n\n ranges::reverse(ans);\n return ans;\n }\n};", "memory": "9900" }
482
<p>You are given a license key represented as a string <code>s</code> that consists of only alphanumeric characters and dashes. The string is separated into <code>n + 1</code> groups by <code>n</code> dashes. You are also given an integer <code>k</code>.</p> <p>We want to reformat the string <code>s</code> such that each group contains exactly <code>k</code> characters, except for the first group, which could be shorter than <code>k</code> but still must contain at least one character. Furthermore, there must be a dash inserted between two groups, and you should convert all lowercase letters to uppercase.</p> <p>Return <em>the reformatted license key</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;5F3Z-2e-9-w&quot;, k = 4 <strong>Output:</strong> &quot;5F3Z-2E9W&quot; <strong>Explanation:</strong> The string s has been split into two parts, each part has 4 characters. Note that the two extra dashes are not needed and can be removed. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;2-5g-3-J&quot;, k = 2 <strong>Output:</strong> &quot;2-5G-3J&quot; <strong>Explanation:</strong> The string s has been split into three parts, each part has 2 characters except the first part as it could be shorter as mentioned above. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists of English letters, digits, and dashes <code>&#39;-&#39;</code>.</li> <li><code>1 &lt;= k &lt;= 10<sup>4</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n void reverseStr(string& str)\n {\n int n = str.length();\n for (int i = 0; i < n / 2; i++)\n swap(str[i], str[n - i - 1]);\n }\n \n string licenseKeyFormatting(string s, int k) {\n string result = \"\";\n int counter = 0;\n for(int i = s.length()-1; i >= 0; i--){\n if(s[i] != '-') \n {\n result += toupper(s[i]);\n counter++;\n if(counter == k){\n result += '-';\n counter = 0;\n }\n }\n }\n reverseStr(result);\n if(result[0] == '-') return result.substr(1);\n return result;\n }\n};", "memory": "9900" }
482
<p>You are given a license key represented as a string <code>s</code> that consists of only alphanumeric characters and dashes. The string is separated into <code>n + 1</code> groups by <code>n</code> dashes. You are also given an integer <code>k</code>.</p> <p>We want to reformat the string <code>s</code> such that each group contains exactly <code>k</code> characters, except for the first group, which could be shorter than <code>k</code> but still must contain at least one character. Furthermore, there must be a dash inserted between two groups, and you should convert all lowercase letters to uppercase.</p> <p>Return <em>the reformatted license key</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;5F3Z-2e-9-w&quot;, k = 4 <strong>Output:</strong> &quot;5F3Z-2E9W&quot; <strong>Explanation:</strong> The string s has been split into two parts, each part has 4 characters. Note that the two extra dashes are not needed and can be removed. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;2-5g-3-J&quot;, k = 2 <strong>Output:</strong> &quot;2-5G-3J&quot; <strong>Explanation:</strong> The string s has been split into three parts, each part has 2 characters except the first part as it could be shorter as mentioned above. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists of English letters, digits, and dashes <code>&#39;-&#39;</code>.</li> <li><code>1 &lt;= k &lt;= 10<sup>4</sup></code></li> </ul>
2
{ "code": "#include<string>\nclass Solution {\npublic:\n string licenseKeyFormatting(string s, int k) {\n \n int n = s.length() ;\n string result ;\n string temp ;\n \n for(int i=n-1 ; i>=0 ; i--){\n char ch = s[i] ;\n if(ch == '-'){\n continue ;\n }\n else{\n temp += toupper(ch) ;\n if(temp.length() == k){\n result += temp;\n result.push_back('-');\n temp = \"\" ;\n }\n }\n }\n // if temp has some characters left.\n result += temp ;\n reverse(result.begin(),result.end());\n if(result[0] == '-'){\n result.erase(result.begin());\n }\n return result ;\n }\n};", "memory": "10000" }
482
<p>You are given a license key represented as a string <code>s</code> that consists of only alphanumeric characters and dashes. The string is separated into <code>n + 1</code> groups by <code>n</code> dashes. You are also given an integer <code>k</code>.</p> <p>We want to reformat the string <code>s</code> such that each group contains exactly <code>k</code> characters, except for the first group, which could be shorter than <code>k</code> but still must contain at least one character. Furthermore, there must be a dash inserted between two groups, and you should convert all lowercase letters to uppercase.</p> <p>Return <em>the reformatted license key</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;5F3Z-2e-9-w&quot;, k = 4 <strong>Output:</strong> &quot;5F3Z-2E9W&quot; <strong>Explanation:</strong> The string s has been split into two parts, each part has 4 characters. Note that the two extra dashes are not needed and can be removed. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;2-5g-3-J&quot;, k = 2 <strong>Output:</strong> &quot;2-5G-3J&quot; <strong>Explanation:</strong> The string s has been split into three parts, each part has 2 characters except the first part as it could be shorter as mentioned above. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists of English letters, digits, and dashes <code>&#39;-&#39;</code>.</li> <li><code>1 &lt;= k &lt;= 10<sup>4</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n string licenseKeyFormatting(string s, int k) {\n string str = \"\";\n int ctr = 0;\n for(int i=s.size()-1;i>=0;i--)\n {\n if(s[i] != '-')\n {\n str += toupper(s[i]);\n ctr++;\n if(ctr == k)\n {\n str += '-';\n ctr = 0;\n }\n }\n }\n reverse(str.begin(),str.end());\n if(str[0] == '-')\n {\n return str.substr(1);\n }\n return str;\n }\n};", "memory": "10000" }
482
<p>You are given a license key represented as a string <code>s</code> that consists of only alphanumeric characters and dashes. The string is separated into <code>n + 1</code> groups by <code>n</code> dashes. You are also given an integer <code>k</code>.</p> <p>We want to reformat the string <code>s</code> such that each group contains exactly <code>k</code> characters, except for the first group, which could be shorter than <code>k</code> but still must contain at least one character. Furthermore, there must be a dash inserted between two groups, and you should convert all lowercase letters to uppercase.</p> <p>Return <em>the reformatted license key</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;5F3Z-2e-9-w&quot;, k = 4 <strong>Output:</strong> &quot;5F3Z-2E9W&quot; <strong>Explanation:</strong> The string s has been split into two parts, each part has 4 characters. Note that the two extra dashes are not needed and can be removed. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;2-5g-3-J&quot;, k = 2 <strong>Output:</strong> &quot;2-5G-3J&quot; <strong>Explanation:</strong> The string s has been split into three parts, each part has 2 characters except the first part as it could be shorter as mentioned above. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists of English letters, digits, and dashes <code>&#39;-&#39;</code>.</li> <li><code>1 &lt;= k &lt;= 10<sup>4</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n string licenseKeyFormatting(string s, int k) {\n\n string l;\n for(int i=0;i<s.length();i++)\n {\n if(s[i]!='-')\n {\n l.push_back(toupper(s[i]));\n }\n }\n reverse(l.begin(),l.end());\n int count=0;\n string ans;\n\n for(int i=0;i<l.length();i++)\n {\n count++;\n ans.push_back(l[i]);\n\n if(count==k&&i!=l.length()-1)\n {\n ans.push_back('-');\n count=0;\n }\n \n }\n reverse(ans.begin(),ans.end());\n return ans;\n }\n};", "memory": "10100" }
482
<p>You are given a license key represented as a string <code>s</code> that consists of only alphanumeric characters and dashes. The string is separated into <code>n + 1</code> groups by <code>n</code> dashes. You are also given an integer <code>k</code>.</p> <p>We want to reformat the string <code>s</code> such that each group contains exactly <code>k</code> characters, except for the first group, which could be shorter than <code>k</code> but still must contain at least one character. Furthermore, there must be a dash inserted between two groups, and you should convert all lowercase letters to uppercase.</p> <p>Return <em>the reformatted license key</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;5F3Z-2e-9-w&quot;, k = 4 <strong>Output:</strong> &quot;5F3Z-2E9W&quot; <strong>Explanation:</strong> The string s has been split into two parts, each part has 4 characters. Note that the two extra dashes are not needed and can be removed. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;2-5g-3-J&quot;, k = 2 <strong>Output:</strong> &quot;2-5G-3J&quot; <strong>Explanation:</strong> The string s has been split into three parts, each part has 2 characters except the first part as it could be shorter as mentioned above. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists of English letters, digits, and dashes <code>&#39;-&#39;</code>.</li> <li><code>1 &lt;= k &lt;= 10<sup>4</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n string licenseKeyFormatting(string s, int k) {\n\n string l;\n for(int i=0;i<s.length();i++)\n {\n if(s[i]!='-')\n {\n l.push_back(toupper(s[i]));\n }\n }\n reverse(l.begin(),l.end());\n int count=0;\n string ans;\n\n for(int i=0;i<l.length();i++)\n {\n count++;\n ans.push_back(l[i]);\n\n if(count==k&&i!=l.length()-1)\n {\n ans.push_back('-');\n count=0;\n }\n \n }\n reverse(ans.begin(),ans.end());\n return ans;\n }\n};", "memory": "10200" }