티스토리 뷰
1150번과 매우 유사한 문제다. 다만 다른 점은 해당 문제에선 Majority Element가 반드시 있다는 가정하에 이를 반환하는 것이고, 1150은 input으로 주어진 숫자가 Majority Element가 맞는지 틀린지를 반환하는 것이다.
Given an array nums of size n, return the majority element.
The majority element is the element that appears more than ⌊n / 2⌋ times. You may assume that the majority element always exists in the array.
Example 1:
Input: nums = [3,2,3]
Output: 3
Example 2:
Input: nums = [2,2,1,1,1,2,2]
Output: 2
class Solution:
def majorityElement(self, nums: List[int]) -> int:
dic = defaultdict(int)
l = len(nums) // 2
for i, n in enumerate(nums):
dic[n] += 1
if dic[n] > l:
return n
참고:
반응형
'기술(Tech, IT) > 리트코드(LeetCode)' 카테고리의 다른 글
| [LeetCode] 392. Is Subsequence (0) | 2023.07.14 |
|---|---|
| [LeetCode] 229. Majority Element II (0) | 2023.07.13 |
| [LeetCode] 70. Climbing Stairs (0) | 2023.07.11 |
| [LeetCode] 1150. Check If a Number Is Majority Element in a Sorted Array (0) | 2023.07.10 |
| [LeetCode] 1057. Campus Bikes (0) | 2023.06.15 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 리트코드
- machine learning
- java
- vertex shader
- ml
- leetcode
- The Economist Espresso
- 딕셔너리
- C++
- Python
- tf-idf
- 이코노미스트 에스프레소
- 소켓 프로그래밍
- I2C
- The Economist
- 투 포인터
- Hash Map
- Computer Graphics
- min heap
- 오블완
- socket programming
- join
- DICTIONARY
- 티스토리챌린지
- 머신 러닝
- 안드로이드
- 파이썬
- 이코노미스트
- Android
- defaultdict
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5 | 6 | 7 |
| 8 | 9 | 10 | 11 | 12 | 13 | 14 |
| 15 | 16 | 17 | 18 | 19 | 20 | 21 |
| 22 | 23 | 24 | 25 | 26 | 27 | 28 |
| 29 | 30 | 31 |
글 보관함
반응형
