-
Notifications
You must be signed in to change notification settings - Fork 0
【Grind75Hard】12問目 4. Median of Two Sorted Arrays #71
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
Open
shining-ai
wants to merge
1
commit into
main
Choose a base branch
from
grind75_12
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
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 |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| # 2つのリストをマージしたものを作成していく(マージソートの要領) | ||
| # O(m + n) | ||
| class Solution: | ||
| def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float: | ||
| marged_list = [] | ||
| i = 0 | ||
| j = 0 | ||
| while i < len(nums1) and j < len(nums2): | ||
| if (len(nums1) + len(nums2)) // 2 < i + j: | ||
| break | ||
| if nums1[i] <= nums2[j]: | ||
| marged_list.append(nums1[i]) | ||
| i += 1 | ||
| else: | ||
| marged_list.append(nums2[j]) | ||
| j += 1 | ||
| else: | ||
| if len(nums1) <= i: | ||
| marged_list += nums2[j : (len(nums1) + len(nums2)) // 2 - i + 1] | ||
| else: | ||
| marged_list += nums1[i : (len(nums1) + len(nums2)) // 2 - j + 1] | ||
|
|
||
| if (len(nums1) + len(nums2)) % 2 == 1: | ||
| return marged_list[-1] | ||
| else: | ||
| return (marged_list[-2] + marged_list[-1]) / 2 | ||
|
|
||
|
|
||
| # 2分探索 | ||
| # k番目に小さい要素を求める関数を作成 | ||
| # 小さい方からk-1個を削除すればk番目に小さいものは求められる。 | ||
| # nums1: [1, 4, 5, 7, 9] nums2: [2, 3, 6, 8] | ||
| # nums1[3]=5 > nums2[1]=3 なので、nums2[0], nums2[1]は中央値にならないから捨てる | ||
| # というように、小さい方からk-1個を削除していく | ||
| # O(log(min(m, n))) | ||
| class Solution: | ||
| def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float: | ||
| # k=0のとき最小値を返す | ||
| def get_kth_small(k, a_start, a_end, b_start, b_end): | ||
| if a_start >= a_end: | ||
| return nums2[k - a_start] | ||
| if b_start >= b_end: | ||
| return nums1[k - b_start] | ||
| a_middle = (a_start + a_end) // 2 | ||
| b_middle = (b_start + b_end) // 2 | ||
| if a_middle + b_middle < k: | ||
| if nums1[a_middle] < nums2[b_middle]: | ||
| return get_kth_small(k, a_middle + 1, a_end, b_start, b_end) | ||
| return get_kth_small(k, a_start, a_end, b_middle + 1, b_end) | ||
| if nums1[a_middle] < nums2[b_middle]: | ||
| return get_kth_small(k, a_start, a_end, b_start, b_middle) | ||
| return get_kth_small(k, a_start, a_middle, b_start, b_end) | ||
|
|
||
| nums_length = len(nums1) + len(nums2) | ||
| if nums_length % 2 == 1: | ||
| return get_kth_small(nums_length // 2, 0, len(nums1), 0, len(nums2)) | ||
| return ( | ||
| get_kth_small(nums_length // 2 - 1, 0, len(nums1), 0, len(nums2)) | ||
| + get_kth_small(nums_length // 2, 0, len(nums1), 0, len(nums2)) | ||
| ) / 2 |
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 |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| # 2分探索 | ||
| # インデックスではなく、配列を渡すように修正 | ||
| class Solution: | ||
| def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float: | ||
| def get_kth_small(k, nums1, nums2): | ||
| if not nums1: | ||
| return nums2[k] | ||
| if not nums2: | ||
| return nums1[k] | ||
| middle_1 = len(nums1) // 2 | ||
| middle_2 = len(nums2) // 2 | ||
| if middle_1 + middle_2 < k: | ||
| if nums1[middle_1] < nums2[middle_2]: | ||
| return get_kth_small(k - middle_1 - 1, nums1[middle_1 + 1 :], nums2) | ||
| return get_kth_small(k - middle_2 - 1, nums1, nums2[middle_2 + 1 :]) | ||
| if nums1[middle_1] < nums2[middle_2]: | ||
| return get_kth_small(k, nums1, nums2[:middle_2]) | ||
| return get_kth_small(k, nums1[:middle_1], nums2) | ||
|
|
||
| nums_length = len(nums1) + len(nums2) | ||
| if nums_length % 2 == 1: | ||
| return get_kth_small(nums_length // 2, nums1, nums2) | ||
| return ( | ||
| get_kth_small(nums_length // 2 - 1, nums1, nums2) | ||
| + get_kth_small(nums_length // 2, nums1, nums2) | ||
| ) / 2 |
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 |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| class Solution: | ||
| def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float: | ||
| def get_kth_small(k, nums1, nums2): | ||
| if not nums1: | ||
| return nums2[k] | ||
| if not nums2: | ||
| return nums1[k] | ||
| middle_1 = len(nums1) // 2 | ||
| middle_2 = len(nums2) // 2 | ||
| if middle_1 + middle_2 < k: | ||
| if nums1[middle_1] < nums2[middle_2]: | ||
| return get_kth_small(k - middle_1 - 1, nums1[middle_1 + 1 :], nums2) | ||
| return get_kth_small(k - middle_2 - 1, nums1, nums2[middle_2 + 1 :]) | ||
| if nums1[middle_1] < nums2[middle_2]: | ||
| return get_kth_small(k, nums1, nums2[:middle_2]) | ||
| return get_kth_small(k, nums1[:middle_1], nums2) | ||
|
|
||
| nums_length = len(nums1) + len(nums2) | ||
| if nums_length % 2 == 1: | ||
| return get_kth_small(nums_length // 2, nums1, nums2) | ||
| return ( | ||
| get_kth_small(nums_length // 2 - 1, nums1, nums2) | ||
| + get_kth_small(nums_length // 2, nums1, nums2) | ||
| ) / 2 | ||
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.
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.
これはここでスライスを取っているからコピーされていませんか。
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.
level1のようにindexで管理したほうが良さそうですね。
引数減らせて読みやすいかと思いましたが、遅くなるデメリットのほうが大きいですね。