Skip to content
Merged
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 missing-number/parkhojeong.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.

🏷️ 알고리즘 패턴 분석

missing-number/parkhojeong.py
class Solution:
    def missingNumber(self, nums: List[int]) -> int:
        N = max(nums)
        num_set = set()

        for i in range(N + 1):
            num_set.add(i)

        for num in nums:
            num_set.remove(num)

        if len(num_set) == 0:
            return N + 1
        else:
            return num_set.pop()
  • 패턴: Hash Map / Hash Set, Greedy, Binary Search, Dynamic Programming, Two Pointers, Sliding Window, Backtracking, Divide and Conquer, Union Find, Trie, Bit Manipulation, DFS, BFS, Monotonic Stack, Heap / Priority Queue
  • 설명: 집합을 이용해 존재 여부를 체크하는 방식으로 누락된 수를 찾는다. 전체 가능한 숫자 세트를 만든 뒤 주어진 nums에서 제거하는 해시 셋 활용 패턴에 해당한다.

📊 시간/공간 복잡도 분석

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

피드백: 정수 집합을 만들어 가능한 모든 값을 저장한 뒤 남은 값을 찾는다.

개선 제안: 현재 구현은 최악의 경우 추가적 공간이 필요하므로, 가정된 수의 합이나 비트 연산 등을 이용한 상수 공간 방법을 고려해볼 수 있다.

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Solution:
def missingNumber(self, nums: List[int]) -> int:
N = max(nums)
num_set = set()
Comment on lines +3 to +4

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.

정답 범위는 항상 0~n이니까 max 대신 len을 쓰면 더 효율적일 것 같아요!


for i in range(N + 1):
num_set.add(i)
Comment on lines +4 to +7

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.

num_set = set(range(N + 1)) 로 한 줄에 됩니다! 그리고 크로드한테 물어보니까 저 코드는 파이썬 레벨 루프가 C 레벨로 내려가서 눈에 띄게 빨라진다고 합니다!!👍


for num in nums:
num_set.remove(num)

if len(num_set) == 0:
return N + 1
else:
return num_set.pop()
Loading