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
7 changes: 7 additions & 0 deletions contains-duplicate/wlghsp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"""
* Time Complexity: O(N)
* Space Complexity: O(N)
"""
class Solution:
def containsDuplicate(self, nums: list[int]) -> bool:
return len(set(nums)) != len(nums)
20 changes: 20 additions & 0 deletions house-robber/wlghsp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"""
* Time Complexity: O(N)
* Space Complexity: O(N)
"""
class Solution:
def rob(self, nums: list[int]) -> int:
n = len(nums)

if n == 1:
return nums[0]

# dp[i]: 0번 부터 i번째 집까지 고려했을 때 얻을 수 있는 최대 금액
dp = [0] * n
dp[0] = nums[0]
dp[1] = max(nums[0], nums[1])

for i in range(2, n):
dp[i] = max(dp[i - 2] + nums[i], dp[i - 1])

return dp[-1]
15 changes: 15 additions & 0 deletions longest-consecutive-sequence/wlghsp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
"""
* Time Complexity: O(N)
* Space Complexity: O(N)
"""
class Solution:
def longestConsecutive(self, nums: list[int]) -> int:
nums_set = set(nums)
ans = 0
for n in nums_set:
if (n - 1) not in nums_set:
length = 1
while n + length in nums_set:
length += 1
ans = max(length, ans)
return ans
24 changes: 24 additions & 0 deletions top-k-frequent-elements/wlghsp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""
* Time Complexity: O(n)
* Space Complexity: O(n)
"""
from collections import Counter
class Solution:
def topKFrequent(self, nums: list[int], k: int) -> list[int]:
# 1. 빈도 계산: O(N)
count = Counter(nums)

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가 있군요. 배워갑니다. 감사합니다!

@wlghsp wlghsp Nov 14, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Counter가 있지만 너무 만들어주는 느낌이라 잘 안 쓸려고 하는데 깔끔해 보여서 썼습니다. 리뷰 감사합니다! 😊


# 2. 빈도를 인덱스로 사용하는 버킷 배열 생성: O(N)
# 최대 빈도는 len(nums)이므로 크기는 len(nums) + 1
buckets = [[] for _ in range(len(nums) + 1)]

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.

저도 문제봤을땐 처음푸신 방법 비슷하게 생각했는데 더 효율적인 방법도 있었네요. 참고하겠습니다.
전체적으로 풀이와 파일정리도 모두 깔끔하고 코멘트도 필요한곳에 적절히 주신 것 같습니다. 수고많으셨습니다

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

처음에는 heap으로 풀이했는데 O(N)으로 가능한 버킷 정렬 풀이가 있길래 한번 학습해보고 있습니다. 리뷰 감사합니다!😊

for num, freq in count.items():
buckets[freq].append(num)

# 3. 높은 빈도(뒤쪽 인덱스)부터 k개 추출: O(N)
res = []
for freq in range(len(buckets) - 1, 0, -1):
for num in buckets[freq]:
res.append(num)
if len(res) == k:
return res
return res
13 changes: 13 additions & 0 deletions two-sum/wlghsp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
"""
* Time Complexity: O(N)
* Space Complexity: O(N)
"""
class Solution:
def twoSum(self, nums: list[int], target: int) -> list[int]:
num_dict = {}
for i, e in enumerate(nums):
other = target - e
if other in num_dict:
return [num_dict[other], i]
num_dict[e] = i
return []
Loading