-
Notifications
You must be signed in to change notification settings - Fork 0
【Grind75Hard】8問目 23. Merge k Sorted Lists #67
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?
Conversation
| for i, node in enumerate(lists): | ||
| if not node: | ||
| continue | ||
| heapq.heappush(min_heap, (node.val, i, node)) |
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.
これがないと、定義されていないListNodeの比較になって問題が出るのですかね。
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.
問題文上は不要です。
これがないと、定義されていないListNodeの比較になって問題が出るのですかね。
おっしゃる通りで、同値の比較のために入れていました。
この旨はコメントで書いておかないと、疑問を持たれてしまいますね。
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.
同じ値があったときに、オブジェクトの < が定義されていないからエラーということですね。
| for i, node in enumerate(lists): | ||
| if not node: | ||
| continue | ||
| heapq.heappush(min_heap, (node.val, i, node)) |
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.
これがないと、定義されていないListNodeの比較になって問題が出るのですかね。
| node = sentinel | ||
| while min_heap: | ||
| val, index, smallest_head = heapq.heappop(min_heap) | ||
| node.next = ListNode(val) |
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.
こういうことですかね。
val, index, smallest_head = heapq.heappop(min_heap)
node.next = smallest_head
node = node.next既存のノードのnextは必ず上書きされるので、新しく作った方が分かりやすいかなと考えてました。
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.
in-placeなのか新しく作るのかは、要件によるかと思います。
問題
https://leetcode.com/problems/merge-k-sorted-lists/