Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions grind75_hard/04_295. Find Median from Data Stream/level_1.py
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
17 changes: 17 additions & 0 deletions grind75_hard/04_295. Find Median from Data Stream/level_2.py
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)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


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
16 changes: 16 additions & 0 deletions grind75_hard/04_295. Find Median from Data Stream/level_3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class MedianFinder:
Copy link
Copy Markdown

@liquo-rice liquo-rice May 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Level 3でsorted arrayに挿入の解法を選んだ理由はありますか?

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

アルゴリズムの分かりやすさと実装の容易さで選びました。
level1のheapの解法の方が高速ですが、分かりづらいかと思いやめました。


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:
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

私は、これは == 1 を書きますが、これも趣味です。

Copy link
Copy Markdown
Owner Author

@shining-ai shining-ai May 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

一瞬どっちか分からなくなるので、趣味の範囲なら==1書くようにします。

return float(self.nums[len(self.nums) // 2])
else:
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

私はインデントを落としますが、これは趣味の範囲でしょう。

Copy link
Copy Markdown
Owner Author

@shining-ai shining-ai May 10, 2024

Choose a reason for hiding this comment

The 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
15 changes: 15 additions & 0 deletions grind75_hard/04_295. Find Median from Data Stream/level_4.py
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
14 changes: 14 additions & 0 deletions grind75_hard/04_295. Find Median from Data Stream/level_5.py
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