Skip to content

Create 169. Majority Element.md#19

Open
Kitaken0107 wants to merge 2 commits intomainfrom
Kitaken0107-patch-21
Open

Create 169. Majority Element.md#19
Kitaken0107 wants to merge 2 commits intomainfrom
Kitaken0107-patch-21

Conversation

@Kitaken0107
Copy link
Owner

```python
class Solution:
def majorityElement(self, nums: List[int]) -> int:
cnt_nums = {}
Copy link

Choose a reason for hiding this comment

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

https://discord.com/channels/1084280443945353267/1192728121644945439/1244281685818740737
をご覧ください。

今回の場合は、 num_to_count あたりがよいと思います。

class Solution:
def majorityElement(self, nums: List[int]) -> int:
cnt_nums = {}
nums_len = len(nums)
Copy link

Choose a reason for hiding this comment

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

nums_len ですと len(nums) からあまり変わっていないように思いますので、変数に代入する必要はないと思います。

cnt_nums = {}
nums_len = len(nums)
for num in nums:
cnt_nums[num] = cnt_nums.get(num,0) + 1
Copy link

Choose a reason for hiding this comment

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

nums_len = len(nums)
for num in nums:
cnt_nums[num] = cnt_nums.get(num,0) + 1
if cnt_nums[num] > nums_len//2:
Copy link

Choose a reason for hiding this comment

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

// の左右にスペースを空けましょう。
https://peps.python.org/pep-0008/#other-recommendations

class Solution:
def majorityElement(self, nums: List[int]) -> int:
cnt_nums = defaultdict(int)
num_len = len(nums)
Copy link

Choose a reason for hiding this comment

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

私はこの変数は作らないかしら。
趣味の問題ですが登場人物を増やして増える複雑性と名前をつけることによって減る複雑性が釣り合っていないのではないかと思います。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants