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
14 changes: 14 additions & 0 deletions longest-substring-without-repeating-characters/Chanz82.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.

🏷️ 알고리즘 패턴 분석

longest-substring-without-repeating-characters/Chanz82.py
class Solution:
    def lengthOfLongestSubstring(self, s: str) -> int:
        left = 0
        max_len = 0
        visited = {}

        for right, ch in enumerate(s):
            if ch in visited and visited[ch] >= left: 
                left = visited[ch] + 1 # 현재 윈도우 상에서 중복 문자가 발견되었기 때문에 윈도우를 중복 문자 이후로 옮김.
            
            visited[ch] = right
            max_len = max(max_len, right - left + 1)

        return max_len
  • 패턴: Two Pointers, Hash Map / Hash Set, Sliding Window
  • 설명: 왼쪽 포인터와 오른쪽 포인터로 창을 유지하며, 각 문자 위치를 해시맵에 저장하고 중복 시 left를 이동시키는 슬라이딩 윈도우 패턴입니다. 해시 맵으로 문자 위치를 추적해 빠르게 중복 여부를 판단합니다.

📊 시간/공간 복잡도 분석

복잡도
Time O(n)
Space O(min(n, k))

피드백: 각 문자의 마지막 위치를 저장하고 현재 인덱스와 비교해 왼쪽 포인터를 이동시키는 표준 슬라이딩 윈도우 방식이다.

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

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
left = 0
max_len = 0
visited = {}

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.

visited는 단순히 방문했던 것만 담는 것처럼 느껴지는데요. 인덱스를 담는다는 걸 나타내는 건 어떨까요?


for right, ch in enumerate(s):
if ch in visited and visited[ch] >= left:
left = visited[ch] + 1 # 현재 윈도우 상에서 중복 문자가 발견되었기 때문에 윈도우를 중복 문자 이후로 옮김.

visited[ch] = right
max_len = max(max_len, right - left + 1)

return max_len
19 changes: 19 additions & 0 deletions reverse-linked-list/Chanz82.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.

🏷️ 알고리즘 패턴 분석

reverse-linked-list/Chanz82.py
# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
        dummy = ListNode()
        dummy = head
        prev = None

        while dummy:
            nextNode = dummy.next
            dummy.next = prev
            prev = dummy
            dummy = nextNode

        return prev
  • 패턴: Linked List, Two Pointers, Reverse Linked List
  • 설명: 주어진 코드는 연결 리스트를 역순으로 뒤집는 문제로, 포인터 두 개를 이용해 노드를 차례로 뒤집는 일반적인 Two Pointers 패턴을 사용합니다. 흐름은 현재 노드와 이전 노드를 유지하며 next를 임시로 보관하고 링크를 역전시키는 방식입니다.

📊 시간/공간 복잡도 분석

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

피드백: 반전 과정에서 prev, nextNode를 이용해 노드의 연결을 뒤집는다. 루프 종료 후 prev가 새 head가 된다.

개선 제안: dummy 변수 할당이 불필요해 보이나 현재 로직은 올바르게 동작한다.

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
dummy = ListNode()
dummy = head
Comment on lines +8 to +9

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.

8라인이 필요할까요? 바로 아래 코드에서 head로 덮어써지네요

prev = None

while dummy:

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.

dummy는 어떤 값이 담기는지 알기가 어려운데요. 의미에 맞는 네이밍을 하시는 건 어떨까요?

nextNode = dummy.next
dummy.next = prev
prev = dummy
dummy = nextNode

return prev

Loading