{"id":2304,"date":"2016-10-23T19:51:21","date_gmt":"2016-10-23T11:51:21","guid":{"rendered":"http:\/\/boweihe.me\/?p=2304"},"modified":"2016-10-23T19:51:21","modified_gmt":"2016-10-23T11:51:21","slug":"leetcode-295-find-median-from-data-stream","status":"publish","type":"post","link":"https:\/\/dayandcarrot.space\/?p=2304","title":{"rendered":"LeetCode 295. Find Median from Data Stream"},"content":{"rendered":"<p>\u9898\u76ee\u5927\u610f\u662f\u7ed9\u5b9a\u4e00\u4e2aint\u6570\u636e\u6d41\uff0c\u5224\u65ad\u5f53\u524d\u6574\u4e2a\u6570\u7ec4\u7684\u4e2d\u4f4d\u6570\u503c\u3002\u7531\u4e8e\u6570\u636e\u6d41\u662f\u53ef\u4ee5\u4e0d\u65ad\u589e\u52a0\u7684\uff0c\u56e0\u6b64\u91c7\u7528\u4e24\u4e2a\u5806\uff08\u6700\u5927\u5806*1+\u6700\u5c0f\u5806*1\uff09\u6765\u5b9e\u73b0\u3002<br \/>\n\u8fd9\u4e1c\u897f\u601d\u8def\u7f51\u4e0a\u90fd\u6709\uff0c\u6bcf\u6b21\u65b0\u8fdb\u4e00\u4e2a\u6570\uff0c\u7ef4\u62a4\u5806\u5c31\u884c\u4e86\u3002\u7ef4\u62a4\u4e00\u6b21\u7684\u590d\u6742\u5ea6\u662fO(logN)\uff0c\u5bf9\u5e94\u6574\u4e2a\u6570\u7ec4\u7684\u590d\u6742\u5ea6\u662fO(N logN)<\/p>\n<h3>STL \u5806\u76f8\u5173\u7684\u4e09\u4e2a\u51fd\u6570 make_heap(), push_heap(), pop_heap()<\/h3>\n<p>STL\u662f\u4e2a\u597d\u4e1c\u897f\uff0c\u5728&lt;algorithm&gt;\u5e93\u91cc\u9762\u5c31\u6709\u4e09\u4e2a\u4e0e\u5806\u6709\u5173\u7684\u51fd\u6570\uff0c\u5229\u7528vector\u5b58\u50a8\u5806\u5143\u7d20\u5c31\u53ef\u4ee5\u8c03\u7528\u8fd9\u4e9b\u51fd\u6570\u6765\u5efa\u7acb\u3001\u66f4\u65b0\u5806\u3002<br \/>\nmake_heap(first, last, comp)\u662f\u5efa\u7acb\u5806\u7684\u51fd\u6570\uff0c\u8f93\u5165\u7684first, last\u5bf9\u5e94begin()\u548cend()\u7684iterator\uff0ccomp\u51fd\u6570\u53ef\u9009\uff0c\u4e0e\u5e93\u4e2dsort()\u7528\u5230\u7684\u65b9\u6cd5\u76f8\u540c\uff0c\u81ea\u5b9a\u4e49\u7684\u65f6\u5019\u6bd4\u8f83\u4e24\u4e2a\u5143\u7d20x, y\u7684\u504f\u5e8f\u5173\u7cfb\uff0c\u8fd4\u56detrue\u8bf4\u660ex\u5e94\u8be5\u6392\u5728\u524d\u9762\u3002\u76f8\u5173\u7684\u6709\u4e24\u4e2a\u4eff\u51fd\u6570\u53ebless&lt;T&gt;() \u548cgreater&lt;T&gt;()\uff0c\u76f4\u63a5\u7528\u6765\u6bd4\u8f83&lt;\u548c&gt;\u5173\u7cfb\u7684\u3002<br \/>\npush_heap(first, last, comp)\u4f7f\u7528\u65f6\uff0c\u5c06\u65b0\u5143\u7d20\u653e\u5728\u6570\u7ec4\u672b\u5c3e(\u4ee3\u8868\u5806\u7684\u672b\u53f6\u8282\u70b9)\uff0c\u7136\u540e\u8c03\u7528\u8fd9\u4e2a\u51fd\u6570\u66f4\u65b0\u5806\uff0c\u662f\u4e00\u4e2asift-up\u7684\u8fc7\u7a0b\u3002<br \/>\npop_heap(first, last, comp)\u4f1a\u5c06\u5806\u9876\u5143\u7d20\u8c03\u6574\u5230\u6570\u7ec4\u672b\u5c3e\u7528\u4e8epop\uff0c\u540c\u65f6\u8c03\u6574\u66f4\u65b0\u5806\u3002<\/p>\n<h3>\u4ee3\u7801<\/h3>\n<pre class=\"lang:c++ decode:true \">class MedianFinder {\nprivate:\n    vector&lt;int&gt; maxHeap; \/\/ store values less than median\n    vector&lt;int&gt; minHeap; \/\/ store values greater than median\npublic:\n    \/\/ Adds a number into the data structure.\n    void addNum(int num) {\n        int minSize = minHeap.size();\n        int maxSize = maxHeap.size();\n        if(maxSize == 0){\n            maxHeap.push_back(num);\n            return;\n        }\n        if(num &lt; maxHeap[0]){\n            maxHeap.push_back(num);\n            push_heap(maxHeap.begin(), maxHeap.end());\n            if(maxSize &gt; minSize) {\n                pop_heap(maxHeap.begin(), maxHeap.end());\n                int popVal = maxHeap[maxHeap.size()-1];\n                maxHeap.pop_back();\n                minHeap.push_back(popVal);\n                push_heap(minHeap.begin(), minHeap.end(), greater&lt;int&gt;());\n            }\n        } else {\n            minHeap.push_back(num);\n            push_heap(minHeap.begin(), minHeap.end(), greater&lt;int&gt;());\n            if(minSize &gt;= maxSize) {\n                pop_heap(minHeap.begin(), minHeap.end(), greater&lt;int&gt;());\n                int popVal = minHeap[minHeap.size()-1];\n                minHeap.pop_back();\n                maxHeap.push_back(popVal);\n                push_heap(maxHeap.begin(), maxHeap.end());\n            }\n        }\n    }\n    \/\/ Returns the median of current data stream\n    double findMedian() {\n        if((maxHeap.size() + minHeap.size())%2 == 0){\n            \/\/even num.\n            if(maxHeap.empty() &amp;&amp; minHeap.empty())\n                return 0.0;\n            double sum = maxHeap[0] + minHeap[0];\n            return sum\/2.0;\n        } else {\n            if(maxHeap.empty())\n                return 0.0;\n            return (double)maxHeap[0];\n        }\n    }\n};\n\/\/ Your MedianFinder object will be instantiated and called as such:\n\/\/ MedianFinder mf;\n\/\/ mf.addNum(1);\n\/\/ mf.findMedian();<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee\u5927\u610f\u662f\u7ed9\u5b9a\u4e00\u4e2aint\u6570\u636e\u6d41\uff0c\u5224\u65ad\u5f53\u524d\u6574\u4e2a\u6570\u7ec4\u7684\u4e2d\u4f4d\u6570\u503c\u3002\u7531\u4e8e\u6570\u636e\u6d41\u662f\u53ef\u4ee5\u4e0d\u65ad\u589e\u52a0\u7684\uff0c\u56e0\u6b64\u91c7\u7528\u4e24\u4e2a\u5806\uff08\u6700\u5927\u5806*1+\u6700\u5c0f\u5806*1\uff09\u6765\u5b9e\u73b0\u3002 \u8fd9\u4e1c\u897f\u601d\u8def\u7f51\u4e0a\u90fd\u6709\uff0c\u6bcf\u6b21\u65b0\u8fdb\u4e00\u4e2a\u6570\uff0c\u7ef4\u62a4\u5806\u5c31\u884c\u4e86\u3002\u7ef4\u62a4\u4e00\u6b21\u7684\u590d\u6742\u5ea6\u662fO(logN)\uff0c\u5bf9\u5e94\u6574\u4e2a\u6570\u7ec4\u7684\u590d\u6742\u5ea6\u662fO(N logN) STL \u5806\u76f8\u5173\u7684\u4e09\u4e2a\u51fd\u6570 make_heap(), push_heap(), pop_heap() STL\u662f\u4e2a\u597d\u4e1c\u897f\uff0c\u5728&lt;algorithm&gt;\u5e93\u91cc\u9762\u5c31\u6709\u4e09\u4e2a\u4e0e\u5806\u6709\u5173\u7684\u51fd\u6570\uff0c\u5229\u7528vector\u5b58\u50a8\u5806\u5143\u7d20\u5c31\u53ef\u4ee5\u8c03\u7528\u8fd9\u4e9b\u51fd\u6570\u6765\u5efa\u7acb\u3001\u66f4\u65b0\u5806\u3002 make_heap(first, last, comp)\u662f\u5efa\u7acb\u5806\u7684\u51fd\u6570\uff0c\u8f93\u5165\u7684first, last\u5bf9\u5e94begin()\u548cend()\u7684iterator\uff0ccomp\u51fd\u6570\u53ef\u9009\uff0c\u4e0e\u5e93\u4e2dsort()\u7528\u5230\u7684\u65b9\u6cd5\u76f8\u540c\uff0c\u81ea\u5b9a\u4e49\u7684\u65f6\u5019\u6bd4\u8f83\u4e24\u4e2a\u5143\u7d20x, y\u7684\u504f\u5e8f\u5173\u7cfb\uff0c\u8fd4\u56detrue\u8bf4\u660ex\u5e94\u8be5\u6392\u5728\u524d\u9762\u3002\u76f8\u5173\u7684\u6709\u4e24\u4e2a\u4eff\u51fd\u6570\u53ebless&lt;T&gt;() \u548cgreater&lt;T&gt;()\uff0c\u76f4\u63a5\u7528\u6765\u6bd4\u8f83&lt;\u548c&gt;\u5173\u7cfb\u7684\u3002 push_heap(first, last, comp)\u4f7f\u7528\u65f6\uff0c\u5c06\u65b0\u5143\u7d20\u653e\u5728\u6570\u7ec4\u672b\u5c3e(\u4ee3\u8868\u5806\u7684\u672b\u53f6\u8282\u70b9)\uff0c\u7136\u540e\u8c03\u7528\u8fd9\u4e2a\u51fd\u6570\u66f4\u65b0\u5806\uff0c\u662f\u4e00\u4e2asift-up\u7684\u8fc7\u7a0b\u3002 pop_heap(first, last, comp)\u4f1a\u5c06\u5806\u9876\u5143\u7d20\u8c03\u6574\u5230\u6570\u7ec4\u672b\u5c3e\u7528\u4e8epop\uff0c\u540c\u65f6\u8c03\u6574\u66f4\u65b0\u5806\u3002 \u4ee3\u7801 class MedianFinder { private: vector&lt;int&gt; maxHeap; \/\/ store values less than median vector&lt;int&gt; minHeap; \/\/ store values greater than median public: \/\/ Adds a number into the data structure. void addNum(int num) { [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[66,146],"class_list":["post-2304","post","type-post","status-publish","format-standard","hentry","category-study","tag-leetcode-oj","tag-146"],"_links":{"self":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/2304","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2304"}],"version-history":[{"count":0,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/2304\/revisions"}],"wp:attachment":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2304"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2304"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2304"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}