Create 20251213.py#38
Conversation
| class Solution: | ||
| def majorityElement(self, nums: List[int]) -> int: |
There was a problem hiding this comment.
関数とクラスにdocstringが欠けています。Boyer-Moore投票アルゴリズムを使用していることや、問題の前提条件(配列に必ず過半数の要素が存在する)を明記すると、コードの意図がより明確になります。例えば、アルゴリズムの概要、時間計算量O(n)、空間計算量O(1)などを記載することを推奨します。
| class Solution: | |
| def majorityElement(self, nums: List[int]) -> int: | |
| class Solution: | |
| """ | |
| 解説: | |
| 配列の中で過半数(⌊ n/2 ⌋回以上出現する)となる要素(majority element)を返すクラスです。 | |
| LeetCode 169. Majority Element の解法を実装しています。 | |
| """ | |
| def majorityElement(self, nums: List[int]) -> int: | |
| """ | |
| Boyer-Moore投票アルゴリズムを用いて、配列内の過半数要素をO(n)時間・O(1)空間で求めます。 | |
| 前提条件: | |
| - 配列 nums には必ず過半数要素が存在します。 | |
| Args: | |
| nums (List[int]): 整数の配列 | |
| Returns: | |
| int: 過半数要素 | |
| アルゴリズム概要: | |
| - 1回目の走査で候補とスコアを決定 | |
| - 2回目の検証は不要(前提条件より) | |
| 計算量: | |
| - 時間計算量: O(n) | |
| - 空間計算量: O(1) | |
| """ |
| # 3: nums=[2,2,1,1,1,2,2], i=2, candidate=2, score=2 | ||
| # 5: nums=[2,2,1,1,1,2,2], i=3, candidate=2, score=1 | ||
| # 6: nums=[2,2,1,1,1,2,2], i=4, candidate=2, score=0 | ||
| # 8: nums=[2,2,1,1,1,2,2], i=5, candidate=1, score=1 | ||
| # 10: nums=[2,2,1,1,1,2,2], i=6, candidate=1, score=0 | ||
| if score == 0: | ||
| score = 1 | ||
| candidate = nums[i] | ||
| # 7: nums=[2,2,1,1,1,2,2], i=4, candidate=1, score=1 | ||
| # 11: nums=[2,2,1,1,1,2,2], i=6, candidate=2, score=1 | ||
| elif nums[i] == candidate: | ||
| score = score + 1 | ||
| # 2: nums=[2,2,1,1,1,2,2], i=1, candidate=2, score=2 | ||
| else: | ||
| score = score -1 | ||
| # 4: nums=[2,2,1,1,1,2,2], i=2, candidate=2, score=1 | ||
| # 6: nums=[2,2,1,1,1,2,2], i=3, candidate=2, score=0 | ||
| # 9: nums=[2,2,1,1,1,2,2], i=5, candidate=1, score=0 | ||
|
|
||
| # 11: candidate=2 |
There was a problem hiding this comment.
トレースコメントの内容に複数の不整合があります。例えば、コメント#5とコメント#6の間でi=3の処理結果がscore=1からscore=0に変化していますが、この変化を示すコメント#6は実際には別の場所(line 24)にあります。また、#8とコメント#10の間の状態遷移も不明確です。トレースコメントは実際のコードフローに沿って正確に配置し、各ステップでの変数の状態を正確に記載する必要があります。
| # 3: nums=[2,2,1,1,1,2,2], i=2, candidate=2, score=2 | |
| # 5: nums=[2,2,1,1,1,2,2], i=3, candidate=2, score=1 | |
| # 6: nums=[2,2,1,1,1,2,2], i=4, candidate=2, score=0 | |
| # 8: nums=[2,2,1,1,1,2,2], i=5, candidate=1, score=1 | |
| # 10: nums=[2,2,1,1,1,2,2], i=6, candidate=1, score=0 | |
| if score == 0: | |
| score = 1 | |
| candidate = nums[i] | |
| # 7: nums=[2,2,1,1,1,2,2], i=4, candidate=1, score=1 | |
| # 11: nums=[2,2,1,1,1,2,2], i=6, candidate=2, score=1 | |
| elif nums[i] == candidate: | |
| score = score + 1 | |
| # 2: nums=[2,2,1,1,1,2,2], i=1, candidate=2, score=2 | |
| else: | |
| score = score -1 | |
| # 4: nums=[2,2,1,1,1,2,2], i=2, candidate=2, score=1 | |
| # 6: nums=[2,2,1,1,1,2,2], i=3, candidate=2, score=0 | |
| # 9: nums=[2,2,1,1,1,2,2], i=5, candidate=1, score=0 | |
| # 11: candidate=2 | |
| if score == 0: | |
| score = 1 | |
| candidate = nums[i] | |
| # 2: nums=[2,2,1,1,1,2,2], i=1, candidate=2, score=1 | |
| elif nums[i] == candidate: | |
| score = score + 1 | |
| # 3: nums=[2,2,1,1,1,2,2], i=2, candidate=2, score=2 | |
| # 5: nums=[2,2,1,1,1,2,2], i=5, candidate=2, score=2 | |
| else: | |
| score = score -1 | |
| # 4: nums=[2,2,1,1,1,2,2], i=2, candidate=2, score=1 | |
| # 6: nums=[2,2,1,1,1,2,2], i=3, candidate=2, score=0 | |
| # 7: nums=[2,2,1,1,1,2,2], i=4, candidate=2, score=0 | |
| # 8: nums=[2,2,1,1,1,2,2], i=5, candidate=2, score=1 | |
| # 9: nums=[2,2,1,1,1,2,2], i=6, candidate=2, score=0 | |
| # 10: candidate=2 |
| score = 1 | ||
|
|
||
| for i in range(1, len(nums)): | ||
| # 1: nums=[2,2,1,1,1,2,2], i=1, candidate=2, score=1 | ||
| # 3: nums=[2,2,1,1,1,2,2], i=2, candidate=2, score=2 | ||
| # 5: nums=[2,2,1,1,1,2,2], i=3, candidate=2, score=1 | ||
| # 6: nums=[2,2,1,1,1,2,2], i=4, candidate=2, score=0 | ||
| # 8: nums=[2,2,1,1,1,2,2], i=5, candidate=1, score=1 | ||
| # 10: nums=[2,2,1,1,1,2,2], i=6, candidate=1, score=0 | ||
| if score == 0: | ||
| score = 1 | ||
| candidate = nums[i] | ||
| # 7: nums=[2,2,1,1,1,2,2], i=4, candidate=1, score=1 | ||
| # 11: nums=[2,2,1,1,1,2,2], i=6, candidate=2, score=1 | ||
| elif nums[i] == candidate: | ||
| score = score + 1 | ||
| # 2: nums=[2,2,1,1,1,2,2], i=1, candidate=2, score=2 | ||
| else: | ||
| score = score -1 | ||
| # 4: nums=[2,2,1,1,1,2,2], i=2, candidate=2, score=1 | ||
| # 6: nums=[2,2,1,1,1,2,2], i=3, candidate=2, score=0 | ||
| # 9: nums=[2,2,1,1,1,2,2], i=5, candidate=1, score=0 |
There was a problem hiding this comment.
変数名「score」は意味が曖昧です。Boyer-Moore投票アルゴリズムでは、この変数は候補要素の「カウント」または「投票数」を表しています。「count」や「votes」などのより明確な名前を使用することで、アルゴリズムの意図が理解しやすくなります。
| score = 1 | |
| for i in range(1, len(nums)): | |
| # 1: nums=[2,2,1,1,1,2,2], i=1, candidate=2, score=1 | |
| # 3: nums=[2,2,1,1,1,2,2], i=2, candidate=2, score=2 | |
| # 5: nums=[2,2,1,1,1,2,2], i=3, candidate=2, score=1 | |
| # 6: nums=[2,2,1,1,1,2,2], i=4, candidate=2, score=0 | |
| # 8: nums=[2,2,1,1,1,2,2], i=5, candidate=1, score=1 | |
| # 10: nums=[2,2,1,1,1,2,2], i=6, candidate=1, score=0 | |
| if score == 0: | |
| score = 1 | |
| candidate = nums[i] | |
| # 7: nums=[2,2,1,1,1,2,2], i=4, candidate=1, score=1 | |
| # 11: nums=[2,2,1,1,1,2,2], i=6, candidate=2, score=1 | |
| elif nums[i] == candidate: | |
| score = score + 1 | |
| # 2: nums=[2,2,1,1,1,2,2], i=1, candidate=2, score=2 | |
| else: | |
| score = score -1 | |
| # 4: nums=[2,2,1,1,1,2,2], i=2, candidate=2, score=1 | |
| # 6: nums=[2,2,1,1,1,2,2], i=3, candidate=2, score=0 | |
| # 9: nums=[2,2,1,1,1,2,2], i=5, candidate=1, score=0 | |
| count = 1 | |
| for i in range(1, len(nums)): | |
| # 1: nums=[2,2,1,1,1,2,2], i=1, candidate=2, count=1 | |
| # 3: nums=[2,2,1,1,1,2,2], i=2, candidate=2, count=2 | |
| # 5: nums=[2,2,1,1,1,2,2], i=3, candidate=2, count=1 | |
| # 6: nums=[2,2,1,1,1,2,2], i=4, candidate=2, count=0 | |
| # 8: nums=[2,2,1,1,1,2,2], i=5, candidate=1, count=1 | |
| # 10: nums=[2,2,1,1,1,2,2], i=6, candidate=1, count=0 | |
| if count == 0: | |
| count = 1 | |
| candidate = nums[i] | |
| # 7: nums=[2,2,1,1,1,2,2], i=4, candidate=1, count=1 | |
| # 11: nums=[2,2,1,1,1,2,2], i=6, candidate=2, count=1 | |
| elif nums[i] == candidate: | |
| count = count + 1 | |
| # 2: nums=[2,2,1,1,1,2,2], i=1, candidate=2, count=2 | |
| else: | |
| count = count -1 | |
| # 4: nums=[2,2,1,1,1,2,2], i=2, candidate=2, count=1 | |
| # 6: nums=[2,2,1,1,1,2,2], i=3, candidate=2, count=0 | |
| # 9: nums=[2,2,1,1,1,2,2], i=5, candidate=1, count=0 |
| score = score + 1 | ||
| # 2: nums=[2,2,1,1,1,2,2], i=1, candidate=2, score=2 | ||
| else: | ||
| score = score -1 |
There was a problem hiding this comment.
スペースが不足しています。Pythonのスタイルガイド(PEP 8)では、二項演算子の前後にスペースを入れることが推奨されています。「score - 1」のように記述すべきです。
| score = score -1 | |
| score = score - 1 |
| score = score + 1 | ||
| # 2: nums=[2,2,1,1,1,2,2], i=1, candidate=2, score=2 | ||
| else: | ||
| score = score -1 |
There was a problem hiding this comment.
簡潔な代入演算子を使用することで、コードがより慣用的になります。「score += 1」と「score -= 1」の形式を使用することを推奨します。
| score = score + 1 | |
| # 2: nums=[2,2,1,1,1,2,2], i=1, candidate=2, score=2 | |
| else: | |
| score = score -1 | |
| score += 1 | |
| # 2: nums=[2,2,1,1,1,2,2], i=1, candidate=2, score=2 | |
| else: | |
| score -= 1 |
Status
Accepted
Runtime
12 ms
Order
O(n)
Memory
19.21 MB
Order
O(1)
Time Taken
m s