-
Notifications
You must be signed in to change notification settings - Fork 0
【Grind75Hard】4問目295. Find Median from Data Stream #64
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| # 毎回ソートする | ||
| # TimeOut | ||
| class MedianFinder: | ||
| def __init__(self): | ||
| self.nums = [] | ||
|
|
||
| def addNum(self, num: int) -> None: | ||
| self.nums.append(num) | ||
| self.nums.sort() | ||
|
|
||
| def findMedian(self) -> float: | ||
| if len(self.nums) % 2: | ||
| index = len(self.nums) // 2 | ||
| return float(self.nums[index]) | ||
|
|
||
| else: | ||
| index = len(self.nums) // 2 | ||
| return (self.nums[index - 1] + self.nums[index]) / 2 | ||
|
|
||
|
|
||
| # heapを2つ使う | ||
| # 半分より大きい値をいれるheapと小さい値をいれるheapの2種類を使って真ん中の値を取得する | ||
| class MedianFinder: | ||
|
|
||
| def __init__(self): | ||
| self.low_heap = [] | ||
| self.high_heap = [] | ||
| self.count = 0 | ||
|
|
||
| def addNum(self, num: int) -> None: | ||
| heapq.heappush(self.high_heap, num) | ||
| high_min = heapq.heappop(self.high_heap) | ||
| heapq.heappush(self.low_heap, -high_min) # low_heapは降順にしたいので-1をかける | ||
| if len(self.high_heap) < len(self.low_heap): | ||
| low_max = -heapq.heappop(self.low_heap) | ||
| heapq.heappush(self.high_heap, low_max) | ||
|
|
||
| def findMedian(self) -> float: | ||
| if (len(self.high_heap) + len(self.low_heap)) % 2: | ||
| return float(self.high_heap[0]) | ||
| else: | ||
| middle_low = -self.low_heap[0] | ||
| middle_high = self.high_heap[0] | ||
| return (middle_low + middle_high) / 2 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| # 挿入ソート的に解く | ||
| class MedianFinder: | ||
|
|
||
| def __init__(self): | ||
| self.nums = [] | ||
|
|
||
| def addNum(self, num: int) -> None: | ||
| index = bisect_left(self.nums, num) | ||
| self.nums.insert(index, num) | ||
|
|
||
| def findMedian(self) -> float: | ||
| if len(self.nums) % 2: | ||
| return float(self.nums[len(self.nums) // 2]) | ||
| else: | ||
| high_index = len(self.nums) // 2 | ||
| low_index = high_index - 1 | ||
| return (self.nums[low_index] + self.nums[high_index]) / 2 | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| class MedianFinder: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Level 3でsorted arrayに挿入の解法を選んだ理由はありますか?
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. アルゴリズムの分かりやすさと実装の容易さで選びました。 |
||
|
|
||
| def __init__(self): | ||
| self.nums = [] | ||
|
|
||
| def addNum(self, num: int) -> None: | ||
| index = bisect_left(self.nums, num) | ||
| self.nums.insert(index, num) | ||
|
|
||
| def findMedian(self) -> float: | ||
| if len(self.nums) % 2: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 私は、これは == 1 を書きますが、これも趣味です。
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 一瞬どっちか分からなくなるので、趣味の範囲なら==1書くようにします。 |
||
| return float(self.nums[len(self.nums) // 2]) | ||
| else: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 私はインデントを落としますが、これは趣味の範囲でしょう。
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. あ、else不要でしたね。 |
||
| high_index = len(self.nums) // 2 | ||
| low_index = high_index - 1 | ||
| return (self.nums[high_index] + self.nums[low_index]) / 2 | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| class MedianFinder: | ||
|
|
||
| def __init__(self): | ||
| self.nums = [] | ||
|
|
||
| def addNum(self, num: int) -> None: | ||
| index = bisect_left(self.nums, num) | ||
| self.nums.insert(index, num) | ||
|
|
||
| def findMedian(self) -> float: | ||
| if len(self.nums) % 2 == 1: | ||
| return float(self.nums[len(self.nums) // 2]) | ||
| high_index = len(self.nums) // 2 | ||
| low_index = high_index - 1 | ||
| return (self.nums[high_index] + self.nums[low_index]) / 2 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| class MedianFinder: | ||
|
|
||
| def __init__(self): | ||
| self.nums = [] | ||
|
|
||
| def addNum(self, num: int) -> None: | ||
| index = insort_left(self.nums, num) | ||
|
|
||
| def findMedian(self) -> float: | ||
| if len(self.nums) % 2 == 1: | ||
| return float(self.nums[len(self.nums) // 2]) | ||
| high_index = len(self.nums) // 2 | ||
| low_index = high_index - 1 | ||
| return (self.nums[high_index] + self.nums[low_index]) / 2 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://docs.python.org/3/library/bisect.html#bisect.insort_left