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
26 changes: 26 additions & 0 deletions house-robber/sangbeenmoon.py

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.

🏷️ 알고리즘 패턴 분석

  • 패턴: Dynamic Programming
  • 설명: 각 집의 최댓값을 누적 비교해 선택 여부를 결정하는 최적 부분구조와 중복계산 제거를 통해 최댓값을 구하는 DP 패턴의 전형적 예시입니다.

📊 시간/공간 복잡도 분석

ℹ️ 이 파일에는 2가지 풀이가 포함되어 있어 각각 분석합니다.

풀이 1: Solution.rob — Time: ✅ O(n) → O(n) / Space: ✅ O(n) → O(n)
유저 분석 실제 분석 결과
Time O(n) O(n)
Space O(n) O(n)

피드백: 두 가지 경우를 비교하며 현재 위치를 포함하는지 여부에 따라 최댓값을 갱신한다. 배열 크기에 따라 선형 시간 복잡도와 선형 공간 복잡도가 나타난다.

개선 제안: 현재 구현이 적절해 보입니다.

풀이 2: Solution.rob — Time: O(n) / Space: O(n)
복잡도
Time O(n)
Space O(n)

피드백: 초기화와 루프 흐름이 명확하며 같은 DP 아이디어를 사용하지만 배열 크기와 반환 부분에서 차이가 있다.

개선 제안: 현재 구현이 적절해 보입니다.

💡 풀이에 시간/공간 복잡도를 주석으로 남겨보세요!

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.

@sangbeenmoon 공간 복잡도를 O(1)로 최적화할 수 있는 풀이도 있는 것 같습니다!

Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,29 @@ def rob(self, nums: List[int]) -> int:
dp[i] = max(dp[i-2] + nums[i] , dp[i-1])

return dp[len(nums) - 1]





# dp[i] = max(dp[i-1], dp[i-2] + num[i])
# TC : O(n)
# SC : O(n)

class Solution:
def rob(self, nums: List[int]) -> int:
dp = [0] * len(nums)

dp[0] = nums[0]

if len(nums) == 1:
return dp[0]

dp[1] = max(nums[0],nums[1])

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

return max(dp)

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.

솔루션이 2개 있는데 마지막 라인만 다르네요. 혹시 max로 값을 추출하신 이유가 있을까요?



33 changes: 33 additions & 0 deletions longest-consecutive-sequence/sangbeenmoon.py

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.

🏷️ 알고리즘 패턴 분석

  • 패턴: Hash Map / Hash Set, Binary Search, Greedy
  • 설명: 이 코드는 해시 셋으로 방문 여부를 빠르게 확인하고, 시작점(이전 수가 없는 수)에서 연속 구간의 길이를 확장하는 방식으로 최댓값을 찾는다. 일반적으로 해시 맵/셋과 그리디성 시작점에서의 확장 아이디어를 활용한다.

📊 시간/공간 복잡도 분석

ℹ️ 이 파일에는 2가지 풀이가 포함되어 있어 각각 분석합니다.

풀이 1: Solution.longestConsecutive — Time: ✅ O(n) → O(n) / Space: ✅ O(n) → O(n)
유저 분석 실제 분석 결과
Time O(n) O(n)
Space O(n) O(n)

피드백: 중복 제거를 통해 각 원소에 대해 한 번씩만 확장 탐색한다. 해시셋으로 조회가 빠르다.

개선 제안: 현재 구현이 적절해 보입니다.

풀이 2: Solution.longestConsecutive — Time: O(n) / Space: O(n)
복잡도
Time O(n)
Space O(n)

피드백: 시작점만 확장하는 방식으로 중복 탐색을 피한다. 시간 복잡도는 최상으로 유지된다.

개선 제안: 현재 구현이 적절해 보입니다.

💡 풀이에 시간/공간 복잡도를 주석으로 남겨보세요!

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.

깔끔하네요 배워갑니다👍

Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,36 @@ def longestConsecutive(self, nums: List[int]) -> int:
answer = max(answer , length)

return answer



# 정렬하면 쉬운데 O(n)이 필요하네 → set으로 조회를 O(1)로 → 근데 중복으로 세네 → 시작점에서만 세자

# TC : O(n)
# SC : O(n)

class Solution:
def longestConsecutive(self, nums: List[int]) -> int:
s = set()

for num in nums:
s.add(num)

ans = 0

for num in s:
if num - 1 in s:
continue

sub_ans = 1
next_num = num + 1
while next_num in s:
sub_ans = sub_ans + 1
next_num = next_num + 1
ans = max(ans, sub_ans)

return ans




27 changes: 27 additions & 0 deletions top-k-frequent-elements/sangbeenmoon.py

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.

🏷️ 알고리즘 패턴 분석

  • 패턴: Hash Map / Hash Set, Sorting
  • 설명: 빈도수를 해시 맵으로 세고, 이를 내림차순으로 정렬한 뒤 상위 k개를 뽑는 방식으로 문제를 풀이한다. 해시 맵과 정렬 패턴이 핵심이다.

📊 시간/공간 복잡도 분석

ℹ️ 이 파일에는 2가지 풀이가 포함되어 있어 각각 분석합니다.

풀이 1: Solution.topKFrequent — Time: ✅ O(nlogn) → O(n log n) / Space: ✅ O(n) → O(n)
유저 분석 실제 분석 결과
Time O(nlogn) O(n log n)
Space O(n) O(n)

피드백: 해시 맵으로 빈도수를 수집하고, 정렬으로 상위 k개를 얻는다. 구현이 명확하지만 정렬은 비용이 더해진다.

개선 제안: 현재 구현이 적절해 보입니다.

풀이 2: Solution.topKFrequent — Time: O(n log n) / Space: O(n)
복잡도
Time O(n log n)
Space O(n)

피드백: 정렬 기반 방법으로 간결하지만 최악의 경우 비용이 큼.

개선 제안: 현재 구현이 적절해 보입니다.

💡 풀이에 시간/공간 복잡도를 주석으로 남겨보세요!

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.

문제에 시간 복잡도 n log n보다 낮게 풀어보라는 코멘트가 있던 것 같아요! 시간 복잡도 최적화 고민해보시면 좋을 것 같습니다.

이미 아실 수도 있지만, 파이썬 collections 모듈에서 Counter를 제공하고 있어서 숫자 갯수 셀때 사용하시면 편리합니다.
https://wikidocs.net/233689

Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,30 @@ def topKFrequent(self, nums: List[int], k: int) -> List[int]:



# TC: O(nlogn)
# SC: O(n)

class Solution:
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
count_map = {}

for num in nums:
if num in count_map:
count_map[num] = count_map[num] + 1
else:
count_map[num] = 1

tuple_list = []

for item in count_map.items():
tuple_list.append(item)

tuple_list.sort(key=lambda x: x[1], reverse=True)

answer = []
for i in range(k):
answer.append(tuple_list[i][0])


return answer

Loading