Conversation
| if index1 + 1 < len(nums1 and (index1 + 1, index2)) not in processed: | ||
| heapq.heappush( | ||
| candidates_heap, | ||
| (nums1[index1 + 1] + nums2[index2], index1 + 1, index2) | ||
| ) | ||
| processed.add((index1 + 1, index2)) | ||
|
|
||
| if index2 + 1 < len(nums2) and (index1, index2 + 1) not in processed: | ||
| heapq.heappush( | ||
| candidates_heap, | ||
| (nums1[index1] + nums2[index2 + 1], index1, index2 + 1) | ||
| ) | ||
| processed.add(index1, index2 + 1) |
There was a problem hiding this comment.
よく書けていると思います。
私はここがちょっと冗長な感じを受けるので内部関数にしてみたらと思います。
There was a problem hiding this comment.
class Solution:
def kSmallestPairs(self, nums1: List[int], nums2: List[int], k: int) -> List[List[int]]:
def heapPush(index1: int, index2: int):
if index1 + 1 < len(nums1) and index2 == next_index2[index1 + 1]:
heapq.heappush(
candidates_heap,
(nums1[index1 + 1] + nums2[index2], index1 + 1, index2)
)
if index2 + 1 < len(nums2) and index1 == next_index1[index2 + 1]:
heapq.heappush(
candidates_heap,
(nums1[index1] + nums2[index2 + 1], index1, index2 + 1)
)
k_smallest_pairs = []
candidates_heap = [(nums1[0] + nums2[0], 0, 0)]
next_index1 = [0] * len(nums2)
next_index2 = [0] * len(nums1)
while len(k_smallest_pairs) < k and candidates_heap:
_, index1, index2 = heapq.heappop(candidates_heap)
k_smallest_pairs.append([nums1[index1], nums2[index2]])
next_index1[index2] += 1
next_index2[index1] += 1
heapPush(index1, index2)
return k_smallest_pairsこういうことですかね?
|
|
||
| ### 1回目 | ||
| - 失敗した | ||
| - Memory Limit |
There was a problem hiding this comment.
Constraints に 1 <= nums1.length, nums2.length <= 10^5 とあります。これより、全てを dict に入れると 10^10 個の要素が入ることになります。 10^10 = 10 G ですので、要素の型が何であってもメモリーが足りなくなるということが分かります。
解法を考える際、空間計算量を求め、データの最大サイズを代入し、使用可能なメモリー量を推定し、メモリー制限を超えないかどうかを確認することをお勧めいたします。
There was a problem hiding this comment.
ありがとうございます。
下のコメントにも共通いたしますが、私は時間・空間計算量への考慮をせずにコードを書いてしまっているので今後より意識的になろうと思います。
| return top_k_nums | ||
| ``` | ||
| ### 2回目 | ||
| - 失敗 Time Limit Error |
There was a problem hiding this comment.
こちらについても似たようなことが言えます。時間計算量は O(N^2 log N) となります。データサイズの最大値である N を代入すると、 4 * 10^10 となります。 Python は 1 秒間に 100 万~1000 万ステップ程度しか処理できないため、時間が足りないことが分かります。
Python の 1 秒間の計算ステップ数については
ichika0615/arai60#8 (comment)
をご覧ください。
| heapq.heappush(heap, (nums1[i] + nums2[0], i, 0)) | ||
|
|
||
| while heap and len(result) < k: | ||
| curr_sum, i, j = heapq.heappop(heap) |
There was a problem hiding this comment.
読むにあたり認知負荷が高くなるため、変数中の単語は、文字を削って省略しないことをお勧めいたします。
https://google.github.io/styleguide/pyguide.html#316-naming
do not abbreviate by deleting letters within a word.
There was a problem hiding this comment.
ご指摘ありがとうございます。
解法を見て写しただけになってしまい、読みやすさなどについての配慮を失念しておりました。
| _, index1, index2 = heapq.heappop(candidates_heap) | ||
| k_smallest_pairs.append([nums1[index1], nums2[index2]]) | ||
|
|
||
| if index1 + 1 < len(nums1 and (index1 + 1, index2)) not in processed: |
There was a problem hiding this comment.
if index1 + 1 < len(nums1) and (index1 + 1, index2) not in processed:
でしょうか。
| def kSmallestPirs(self, nums1: List[int], nums2: List[int], k: int) -> List[List[int]]: | ||
| processed = set() | ||
| candidates_heap = [] | ||
| k_smallest_pair = [] |
|
|
||
| while heap and len(result) < k: | ||
| curr_sum, i, j = heapq.heappop(heap) | ||
| result.append((nums1[i], nums2[j])) |
There was a problem hiding this comment.
返り値がList[List[int]]のはずなので, result.append([nums1[i], nums2[j]])の方が良いかなと思います
問題 https://leetcode.com/problems/find-k-pairs-with-smallest-sums/description/