Leetcode Find Median from Data Stream

Source: Internet
Author: User

Topic Connection

https://leetcode.com/problems/find-median-from-data-stream/

Find Median from Data streamdescription

Median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. The median is the mean of the middle value.

Examples:
[2,3,4], the median is 3

[2,3], the median is (2 + 3)/2 = 2.5

Design a data structure that supports the following and the operations:

void addnum (int num)-Add A integer number from the data stream to the data structure.
Double Findmedian ()-Return The median of all elements so far.
For example:

Add (1)
Add (2)
Findmedian () 1.5
Add (3)
Findmedian () 2

balance tree bare title.

Class Medianfinder {public:const int INF = ~0u >> 1; Medianfinder () {null = new Node (INF, 0, null); root = null;} ~medianfinder () {Clear (root), delete null;} void addnum (int num) {insert (root, num);} Double Findmedian () {int t = root->s;if (T & 1) {return (double) kth ((t >> 1) + 1);} else {int v1 = KTH (T > > 1); int v2 = KTH ((t >> 1) + 1); return (v1 + v2)/2.0;}} private:struct Node {int V, S, c; Node *ch[2]; Node () = default; Node (int _v_, int _s_, node *p) {v = _v_, s = c = _s_;ch[0] = ch[1] = p;} inline void Push_up () {s = ch[0]->s + Ch[1]->s + C;} inline int cmp (int x) const {return x = = v? -1:x > v;}} *root, *null;inline node *newnode (int v) {node *x = new Node (V, 1, null); return x;} inline void Rotate (node *&x, int D) {node *k = x->ch[!d]; X->ch[!d] = K->ch[d], k->ch[d] = X;k->s = x-& Gt;s; X->push_up (); x = k;} inline void Maintain (Node *&x, int d) {if (!x->ch[d]->s) return;if (X->ch[d]->ch[d]->s > x->ch[!d]->s) rotate (x,!d), else if (X->ch[d]->ch[!d]->s > X->ch[!d]->s) rotate (x->ch[d], D), Rotate (x,!d); else return; Maintain (x, 0), maintain (x, 1);} inline void Insert (Node *&x, int v) {if (!x->s) {x = NewNode (v); return;} X->s++;int d = x->cmp (v); if ( -1 = = d) {x->c++; return;} Insert (X->ch[d], V); X->push_up (); Maintain (x, d);} inline void Clear (Node *&x) {if (x->s) clear (x->ch[0]), clear (x->ch[1]), delete x;} inline int kth (int k) {Node *x = root;for (int t = 0; x->s;) {T = x->ch[0]->s;if (k <= t) x = X->ch[0];else if (t + 1 <= k && k <= t + x->c) Break;else K -= t + x->c, x = x->ch[1];} Return x->v;}};

Leetcode Find Median from Data Stream

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.