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/chapse57.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
  • 설명: 이 코드는 중복 체크를 위해 이중 루프로 비교하는 방식이지만, 효율성을 위해 Hash Set을 활용하는 패턴이 일반적입니다. 현재는 명시적 해시 구조를 사용하지 않지만, 중복 검사를 위해 Hash Map 또는 Hash Set이 적합합니다.

📊 시간/공간 복잡도 분석

복잡도
Time O(n^2)
Space O(1)

피드백: 이중 루프를 사용하여 모든 쌍을 비교하므로 시간 복잡도는 제곱에 비례하며, 공간은 상수입니다.

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

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Solution(object):
def containsDuplicate(self, nums):
"""
:type nums: List[int]
:rtype: bool
"""
for i in range(len(nums)):
for j in range(i+1,len(nums)):
if nums[i] == nums[j]:
return True
return False
Comment on lines +7 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을 활용할 수 있습니다. O(n) 최적화도 생각해보시면 좋을 것 같습니다.

22 changes: 22 additions & 0 deletions top-k-frequent-elements/chapse57.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
  • 설명: 이 코드는 숫자 빈도 수를 Hash Map에 저장하고, 정렬하여 상위 k개를 선택하는 방식으로 문제를 해결합니다. Hash Map과 정렬이 핵심 패턴입니다.

📊 시간/공간 복잡도 분석

복잡도
Time O(n log n)
Space O(n)

피드백: 해시맵으로 빈도수 저장 후 정렬하는 과정이 시간 복잡도를 결정하며, 정렬은 O(n log n)입니다. 공간은 빈도수 저장을 위한 해시맵과 정렬 결과를 위한 리스트입니다.

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

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class Solution(object):
def topKFrequent(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: List[int]
"""
count = {}
for n in nums:
if n in count:
count[n] +=1
else:
count[n] =1
# count.items()를 횟수 큰 순으로 정렬
freq = sorted(count.items(), key=lambda x: x[1], reverse=True)

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.

AI가 말한 것처럼 구현은 좋습니다. 다만 정렬 없이 최적화 하는 방법도 생각해보면 좋을 것 같네요. (e.g. 빈도 값을 배열 인덱스로 사용)


result = []
for x in freq[:k]:
result.append(x[0])
return result


14 changes: 14 additions & 0 deletions two-sum/chapse57.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
  • 설명: 이 코드는 해시 맵을 이용해 각 숫자의 인덱스를 저장하며, 목표값과 현재 숫자의 차이를 빠르게 찾는 방식으로 해결합니다. 이를 통해 시간 복잡도를 낮추는 해시 기반 패턴이 사용됩니다.

📊 시간/공간 복잡도 분석

복잡도
Time O(n)
Space O(n)

피드백: 해시맵을 사용하여 각 원소를 한 번씩만 처리하므로 시간 복잡도는 선형이며, 추가 공간으로 해시맵이 필요합니다.

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

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Solution(object):
def twoSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
seen = {}
for i in range(len(nums)):
if (target - nums[i]) in seen:
return [seen[target - nums[i]],i]
seen[nums[i]] =i


Loading