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
11 changes: 11 additions & 0 deletions contains-duplicate/njngwn.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.

🏷️ 알고리즘 패턴 분석

  • 패턴: Binary Search, Two Pointers
  • 설명: 정렬을 이용해 인접한 중복을 확인하는 방식으로, 인접 비교를 통해 중복 여부를 판단한다. 두 포인터처럼 연속 원소를 비교하는 흐름이 핵심이다.

📊 시간/공간 복잡도 분석

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

풀이 1: Solution.containsDuplicate — Time: ✅ O(nlogn) → O(n log n) / Space: ✅ O(1) → O(1)
유저 분석 실제 분석 결과
Time O(nlogn) O(n log n)
Space O(1) O(1)
풀이 2: unknown — Time: ? / Space: ?
복잡도
Time ?
Space ?
풀이 3: unknown — Time: ? / Space: ?
복잡도
Time ?
Space ?

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Solution:
# Time Complexity: O(nlogn), n: len(nums)
# Space Complexity: O(1)
def containsDuplicate(self, nums: List[int]) -> bool:
nums.sort() # O(nlogn)

for i in range(len(nums)):
if i > 0 and nums[i] == nums[i - 1]:
return True

return False
Comment on lines +1 to +11

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.

안녕하세요!
공통적으로 같이 푼 문제들부터 순서대로 리뷰를 드리겠습니다!

저는 다른 방식으로 풀었는데, 이 방식으로 정렬 후 인접 숫자끼리 같을 경우를 따져도 되겠다는 생각이 듭니다.
제 방식은 정렬없이 각 숫자를 순회하면서 set에 있는지 확인한 후 없으면 set에 집어넣고 있으면 true를 반환하는 식으로 풀이했습니다.

이러면 시간복잡도 O(n), 공간복잡도 O(n)이 나오는데
njngwn님의 풀이와 이렇게 비교해보니 trade-off가 명확한것 같네요

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.

시간복잡도를 고려했을 때, set을 이용한 것도 좋은 방법이네요! 감사합니다!

17 changes: 17 additions & 0 deletions house-robber/njngwn.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, Greedy
  • 설명: 코드는 각 집의 금액을 스킵하며 최대 이익을 누적해 최댓값을 구하는 전형적인 DP 방식이다. 연속된 두 지점의 상태를 비교해 최적해를 갱신하는 점에서 DP의 공식을 따른다.

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Solution:
# Time Complexity: O(n), n: len(nums)
# Space Complextiy: O(1)
def rob(self, nums: List[int]) -> int:
if not nums:
return 0
if len(nums) == 1:
return nums[0]

two_ago = nums[0]
one_ago = max(nums[0], nums[1])

for i in range(2, len(nums)):
current = max(one_ago, two_ago + nums[i])
two_ago, one_ago = one_ago, current

return one_ago
19 changes: 19 additions & 0 deletions longest-consecutive-sequence/njngwn.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.

🏷️ 알고리즘 패턴 분석

  • 패턴: Sorting
  • 설명: 코드에서 nums를 정렬하고 중복 제거 후 연속된 수의 길이를 스캔하여 최댓값을 구하므로 정렬 기반의 접근으로 분류됩니다. 패턴 목록에 정확히 매칭되는 항목은 'Sorting'이지만 목록에 있지 않으면 빈 배열이 될 수 있습니다.

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Solution:
# Time Complexity: O(nlogn), n: len(nums)
# Space Complexity: O(n), n: len(nums)
def longestConsecutive(self, nums: List[int]) -> int:
if not nums:
return 0

nums = sorted(set(nums)) # O(nlogn)
longest = 1
length = 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.

length 보다 좀 더 명확하게 변수명을 설정하는건 어떨까요?


for i in range(1, len(nums)):
if nums[i] - nums[i-1] == 1:
length += 1
longest = max(longest, length)
else:
length = 1

return longest
28 changes: 28 additions & 0 deletions top-k-frequent-elements/njngwn.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, Bucket / Counting Sort, Greedy
  • 설명: 두 솔루션 모두 원소의 빈도수를 해시 맵으로 세고(해시 맵), Solution 2에서는 카운트별 버킷 배열을 만들어 빈도에 따른 분류를 수행한다. 이를 통해 Top-K를 얻는 방식은 해시 맵 + 버킷 정렬의 아이디어에 해당한다.

Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from collections import Counter


class Solution:
# Solution 1
# Time Complexity: O(nlogn), n: len(nums)
# Space Complexity: O(n), n:len(nums)
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
counter = {}
for num in nums:
counter[num] = counter.get(num, 0) + 1
sorted_nums = sorted(counter, key=lambda num: counter[num])

return sorted_nums[-k:]
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.

dict의 value순 정렬하고, k개 뽑을때 파이썬 문법을 사용해서 깔끔하게 잘 마무리하신것 같습니다!


# Solution 2
# Time Complexity: O(n), n: len(nums)
# Space Complexity: O(n), n: len(nums)
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
counter = Counter(nums)
buckets = [[] for _ in range(len(nums) + 1)]

for num, cnt in counter.items():
buckets[cnt].append(num)

sorted_nums = [item for bucket in buckets for item in bucket]

return sorted_nums[-k:]
11 changes: 11 additions & 0 deletions two-sum/njngwn.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.

🏷️ 알고리즘 패턴 분석

  • 패턴: Sort
  • 설명: 배열 정렬 후 인접한 중복 여부를 확인하는 방식으로, 직접적인 패턴 목록에는 없지만 배열 정렬을 이용한 중복 탐지는 흔한 기법이다. 이 코드에선 정렬 후 인접 비교로 중복 여부를 판단한다.

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Solution:
# Time Complexity: O(nlogn), n: len(nums)
# Space Complexity: O(1)
def containsDuplicate(self, nums: List[int]) -> bool:
nums.sort() # O(nlogn)

for i in range(len(nums)):
if i > 0 and nums[i] == nums[i - 1]:
return True

return False
Loading