Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions contains-duplicate/hj4645.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Solution:
# 리스트 안에 동일한 숫자가 2개 이상 존재하면 true를 반환해야 하는 문제
# Set으로 변환 시 중복이 제거되므로 List와 Set의 크기를 비교해 답을 구할 수 있음
def containsDuplicate(self, nums: List[int]) -> bool:
return len(nums) != len(set(nums))
Copy link
Contributor

Choose a reason for hiding this comment

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

저도 이런 로직으로 풀이해서 바로 이해가 되었습니다.


# Time Complexity
# - set(nums) → O(n)
# - len(nums), len(set(nums)) → O(1)
# - Total: O(n) (n = number of list elements)

12 changes: 12 additions & 0 deletions top-k-frequent-elements/hj4645.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Solution:
# nums 에서 가장 빈도가 높은 k개의 요소를 찾는 문제
# 딕셔너리와 정렬을 사용해 해결
# 시간복잡도: O(n log n), 공간복잡도: O(n)
Copy link
Contributor

Choose a reason for hiding this comment

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

heapq나 bucket sort를 쓰면 O(n) 또는 O(n log k)로 더 빠르게 가능합니다. 물론 가독성과 정확성으로는 이 방법이 가장 좋아보이긴 합니다.

def topKFrequent(self, nums: List[int], k: int) -> List[int]:
freq_map = {}
for num in nums:
freq_map[num] = freq_map.get(num, 0) + 1

sorted_nums = sorted(freq_map.items(), key=lambda x:x[1], reverse=True)
return [num for num, _ in sorted_nums[:k]]

11 changes: 11 additions & 0 deletions two-sum/hj4645.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Solution:
# 2개의 수를 합해 target이 되는 경우를 찾는 문제
# 순서가 보장되는 python dictionary를 사용해서,
# 요소 x에 대해서 target-x 가 딕셔너리 내에 있는지를 찾는다.
def twoSum(self, nums: List[int], target: int) -> List[int]:
dict = {}
Copy link
Contributor

Choose a reason for hiding this comment

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

dict는 파이썬 내장 타입이라 변수명으로 사용하면 좋지 않습니다. 참고 부탁드립니다

for i, num in enumerate(nums):
remain = target - num
if remain in dict:
return [dict[remain], i]
dict[num] = i