-
-
Notifications
You must be signed in to change notification settings - Fork 9.1k
feat: add solutions to lc problem: No.0295 #3166
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
7f9eeaf
feat: add 2nd ts solution to lc problem: No.0295
rain84 3534e19
Update README.md
yanglbme 59cd174
Update README_EN.md
yanglbme 93af280
Delete solution/0200-0299/0295.Find Median from Data Stream/Solution2.ts
yanglbme 6e38e48
Update Solution.py
yanglbme 816af1a
Update Solution.java
yanglbme c563638
Update Solution.cpp
yanglbme 05745f6
Update Solution.go
yanglbme 3d377e3
Update Solution.js
yanglbme b2a5247
Update Solution.ts
yanglbme dcc257a
Update Solution.rs
yanglbme 5458b9e
Update Solution.cs
yanglbme 9b5a2be
Create Solution.swift
yanglbme 84274e8
Update Solution.java
yanglbme 05caeb0
Update README.md
yanglbme f94c8cb
Update README_EN.md
yanglbme File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
374 changes: 220 additions & 154 deletions
solution/0200-0299/0295.Find Median from Data Stream/README.md
Oops, something went wrong.
372 changes: 224 additions & 148 deletions
solution/0200-0299/0295.Find Median from Data Stream/README_EN.md
Oops, something went wrong.
25 changes: 11 additions & 14 deletions
solution/0200-0299/0295.Find Median from Data Stream/Solution.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,31 @@ | ||
class MedianFinder { | ||
public: | ||
/** initialize your data structure here. */ | ||
MedianFinder() { | ||
} | ||
|
||
void addNum(int num) { | ||
q1.push(num); | ||
q2.push(q1.top()); | ||
q1.pop(); | ||
if (q2.size() - q1.size() > 1) { | ||
q1.push(q2.top()); | ||
q2.pop(); | ||
maxQ.push(num); | ||
minQ.push(maxQ.top()); | ||
maxQ.pop(); | ||
|
||
if (minQ.size() > maxQ.size() + 1) { | ||
maxQ.push(minQ.top()); | ||
minQ.pop(); | ||
} | ||
} | ||
|
||
double findMedian() { | ||
if (q2.size() > q1.size()) { | ||
return q2.top(); | ||
} | ||
return (double) (q1.top() + q2.top()) / 2; | ||
return minQ.size() == maxQ.size() ? (minQ.top() + maxQ.top()) / 2.0 : minQ.top(); | ||
} | ||
|
||
private: | ||
priority_queue<int, vector<int>, greater<int>> q1; | ||
priority_queue<int> q2; | ||
priority_queue<int> maxQ; | ||
priority_queue<int, vector<int>, greater<int>> minQ; | ||
}; | ||
|
||
/** | ||
* Your MedianFinder object will be instantiated and called as such: | ||
* MedianFinder* obj = new MedianFinder(); | ||
* obj->addNum(num); | ||
* double param_2 = obj->findMedian(); | ||
*/ | ||
*/ |
43 changes: 7 additions & 36 deletions
solution/0200-0299/0295.Find Median from Data Stream/Solution.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 17 additions & 16 deletions
solution/0200-0299/0295.Find Median from Data Stream/Solution.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,33 @@ | ||
/** | ||
* initialize your data structure here. | ||
*/ | ||
var MedianFinder = function () { | ||
this.val = []; | ||
this.minQ = new MinPriorityQueue(); | ||
this.maxQ = new MaxPriorityQueue(); | ||
}; | ||
|
||
/** | ||
* @param {number} num | ||
* @return {void} | ||
*/ | ||
MedianFinder.prototype.addNum = function (num) { | ||
let left = 0; | ||
let right = this.val.length; | ||
while (left < right) { | ||
let mid = left + ~~((right - left) / 2); | ||
if (num > this.val[mid]) { | ||
left = mid + 1; | ||
} else { | ||
right = mid; | ||
} | ||
this.maxQ.enqueue(num); | ||
this.minQ.enqueue(this.maxQ.dequeue().element); | ||
if (this.minQ.size() - this.maxQ.size() > 1) { | ||
this.maxQ.enqueue(this.minQ.dequeue().element); | ||
} | ||
this.val.splice(left, 0, num); | ||
}; | ||
|
||
/** | ||
* @return {number} | ||
*/ | ||
MedianFinder.prototype.findMedian = function () { | ||
let mid = ~~(this.val.length / 2); | ||
return this.val.length % 2 ? this.val[mid] : (this.val[mid - 1] + this.val[mid]) / 2; | ||
if (this.minQ.size() === this.maxQ.size()) { | ||
return (this.minQ.front().element + this.maxQ.front().element) / 2; | ||
} | ||
return this.minQ.front().element; | ||
}; | ||
|
||
/** | ||
* Your MedianFinder object will be instantiated and called as such: | ||
* var obj = new MedianFinder() | ||
* obj.addNum(num) | ||
* var param_2 = obj.findMedian() | ||
*/ |
21 changes: 9 additions & 12 deletions
solution/0200-0299/0295.Find Median from Data Stream/Solution.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 20 additions & 21 deletions
solution/0200-0299/0295.Find Median from Data Stream/Solution.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,35 @@ | ||
use std::cmp::Reverse; | ||
use std::collections::BinaryHeap; | ||
|
||
struct MedianFinder { | ||
nums: Vec<i32>, | ||
minQ: BinaryHeap<Reverse<i32>>, | ||
maxQ: BinaryHeap<i32>, | ||
} | ||
|
||
/** | ||
* `&self` means the method takes an immutable reference. | ||
* If you need a mutable reference, change it to `&mut self` instead. | ||
*/ | ||
impl MedianFinder { | ||
/** initialize your data structure here. */ | ||
fn new() -> Self { | ||
Self { nums: Vec::new() } | ||
MedianFinder { | ||
minQ: BinaryHeap::new(), | ||
maxQ: BinaryHeap::new(), | ||
} | ||
} | ||
|
||
fn add_num(&mut self, num: i32) { | ||
let mut l = 0; | ||
let mut r = self.nums.len(); | ||
while l < r { | ||
let mid = (l + r) >> 1; | ||
if self.nums[mid] < num { | ||
l = mid + 1; | ||
} else { | ||
r = mid; | ||
} | ||
self.maxQ.push(num); | ||
self.minQ.push(Reverse(self.maxQ.pop().unwrap())); | ||
|
||
if self.minQ.len() > self.maxQ.len() + 1 { | ||
self.maxQ.push(self.minQ.pop().unwrap().0); | ||
} | ||
self.nums.insert(l, num); | ||
} | ||
|
||
fn find_median(&self) -> f64 { | ||
let n = self.nums.len(); | ||
if (n & 1) == 1 { | ||
return f64::from(self.nums[n >> 1]); | ||
if self.minQ.len() == self.maxQ.len() { | ||
let min_top = self.minQ.peek().unwrap().0; | ||
let max_top = *self.maxQ.peek().unwrap(); | ||
(min_top + max_top) as f64 / 2.0 | ||
} else { | ||
self.minQ.peek().unwrap().0 as f64 | ||
} | ||
f64::from(self.nums[n >> 1] + self.nums[(n >> 1) - 1]) / 2.0 | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.