Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
15 changes: 15 additions & 0 deletions contains-duplicate/ehlung.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
"""
n = nums의 길이
시간 복잡도: O(n) - 각 숫자를 한 번씩 확인 (set 조회/추가는 O(1))
공간 복잡도: O(n) - 지금까지 본 숫자들을 담는 set
"""


class Solution:
def containsDuplicate(self, nums: list[int]) -> bool:
seen = set()
for num in nums:
if num in seen:
return True
seen.add(num)
return False
Comment on lines +10 to +15

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

순회 중 중복을 발견하는 즉시 반환해서 불필요한 탐색을 줄인 점이 좋은 것 같습니다 😆

14 changes: 14 additions & 0 deletions house-robber/ehlung.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
"""
n = nums의 길이
시간 복잡도: O(n) - 각 집을 한 번씩 확인
공간 복잡도: O(1) - 배열 없이 변수 두 개만 사용
"""


class Solution:
def rob(self, nums: list[int]) -> int:
# prev2: 두 집 전까지의 최대 금액, prev1: 바로 전 집까지의 최대 금액
prev2, prev1 = 0, 0
for money in nums:
prev2, prev1 = prev1, max(prev2 + money, prev1)
return prev1
Comment on lines +9 to +14

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

DP 배열 없이 변수 2개만 사용해 공간 복잡도를 최적화해서 잘 풀어주신 것 같습니다👍

21 changes: 21 additions & 0 deletions longest-consecutive-sequence/ehlung.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
"""
n = nums의 길이
시간 복잡도: O(n) - 시작점에서만 세어 나가서 각 숫자는 최대 두 번만 확인
공간 복잡도: O(n) - 모든 숫자를 담는 set
"""


class Solution:
def longestConsecutive(self, nums: list[int]) -> int:
num_set = set(nums)
longest = 0

for num in num_set:
# 수열의 시작점에서만 길이를 센다
if num - 1 not in num_set:
length = 1
while num + length in num_set:
length += 1
longest = max(longest, length)

return longest
15 changes: 15 additions & 0 deletions top-k-frequent-elements/ehlung.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
"""
n = nums의 길이, m = 서로 다른 숫자의 개수
시간 복잡도: O(n + m log m) - 빈도 세기 O(n) + 서로 다른 숫자 m개를 빈도순 정렬 O(m log m)
공간 복잡도: O(m) - 숫자별 빈도를 담는 딕셔너리와 정렬 결과
"""


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

sorted_nums = sorted(count, key=lambda num: count[num], reverse=True)
return sorted_nums[:k]
Comment on lines +10 to +15

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

저는 Counter.most_common(k)를 사용해서 비슷한 방식으로 풀었는데, collections 라이브러리 없이 잘 구현해주신 것 같습니다 !
알고달레 풀이를 확인해보니 배열의 인덱스로 빈도를 저장하는 방식으로 풀면 문제 Follow-up 요구사항을 만족하는 O(n)로 풀 수 있어서 함께 참고해보면 좋을 것 같습니다👍

16 changes: 16 additions & 0 deletions two-sum/ehlung.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""
n = nums의 길이
시간 복잡도: O(n) - 각 숫자를 한 번씩 확인 (딕셔너리 조회/저장은 O(1))
공간 복잡도: O(n) - 숫자와 인덱스를 담는 딕셔너리
"""


class Solution:
def twoSum(self, nums: list[int], target: int) -> list[int]:
index_of = {}
for i, num in enumerate(nums):
need = target - num
if need in index_of:
return [index_of[need], i]
index_of[num] = i
return []
Comment on lines +2 to +16

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

한 번의 순회로 필요한 값을 조회하고, 현재 숫자 인덱스까지 저장해 O(n)으로 효율적으로 푸셔서 배워갑니다! 😆

Loading