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
24 changes: 24 additions & 0 deletions longest-substring-without-repeating-characters/okyungjin.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/okyungjin.py
"""
N: `s`의 길이, M: `s`에서 중복을 제외한 문자의 개수
Time: O(N)
Space: O(min(N,M))
"""
class Solution:
    def lengthOfLongestSubstring(self, s: str) -> int:
         # 문자의 최근 인덱스를 저장
        char_map = {}

        left = 0
        max_len = 0

        for right, char in enumerate(s):
            if char in char_map and char_map[char] >= left:
                left = char_map[char] + 1

            char_map[char] = right
            
            curr_len = right - left + 1
            if curr_len > max_len:
                max_len = curr_len

        return max_len
  • 패턴: Two Pointers, Hash Map / Hash Set
  • 설명: 이 코드는 왼쪽 포인터와 오른쪽 포인터를 이용해 문자열을 한 번 순회하며, 중복 문자를 추적하기 위해 해시 맵을 사용합니다. 중복이 나타나면 왼쪽 포인터를 갱신하고, 현재 구간의 길이를 갱신해 최장값을 구합니다.

📊 시간/공간 복잡도 분석

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

피드백: 문자의 최근 위치를 해시 맵에 저장하고, 창(left..right)에서 중복이 발견되면 left를 중복 문자 바로 뒤로 이동시켜 중복 없이 길이를 확장한다.

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

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""
N: `s`의 길이, M: `s`에서 중복을 제외한 문자의 개수
Time: O(N)
Space: O(min(N,M))
"""
class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
# 문자의 최근 인덱스를 저장
char_map = {}

left = 0
max_len = 0

for right, char in enumerate(s):
if char in char_map and char_map[char] >= left:
left = char_map[char] + 1

char_map[char] = right

curr_len = right - left + 1
if curr_len > max_len:
max_len = curr_len

return max_len
50 changes: 50 additions & 0 deletions reverse-linked-list/okyungjin.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/okyungjin.py
# https://leetcode.com/problems/reverse-linked-list/

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next

"""
Time: O(N)
Space O(N)
"""
class Solution:
    def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
        if not head:
            return None
            
        stack = []

        while head:
            stack.append(head)
            head = head.next

        dummy_head = ListNode()
        curr = dummy_head
        
        while stack:
            curr.next = stack.pop()
            curr = curr.next

        curr.next = None

        return dummy_head.next


"""
Time: O(N)
Space O(1)
"""
class Solution:
    def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
        prev = None
        curr = head

        while curr:
            temp = curr.next
            curr.next = prev
            prev, curr = curr, temp

        return prev
  • 패턴: Two Pointers, Stack / Hash Map / Hash Set, Linked List
  • 설명: 링크드 리스트를 역순으로 뒤집는 문제로, 첫 번째 풀이는 스택을 이용한 역순 배치(스택 기반 순회), 두 번째 풀이는 포인터를 앞뒤로 엮어 연결을 역전하는 방식으로 해결한다. 두 방법 모두 연결 리스트의 노드를 순회하며 방향을 바꾼다.

📊 시간/공간 복잡도 분석

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

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

피드백: 스택에 노드를 차례대로 저장한 뒤 역순으로 재연결하여 역순 리스트를 만듭니다.

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

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

피드백: 링크드 리스트를 한 번 순회하며 포인터를 역전시켜 공간 복잡도를 상수로 줄였습니다.

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

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# https://leetcode.com/problems/reverse-linked-list/

# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next

"""
Time: O(N)
Space O(N)
"""
class Solution:
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
if not head:
return None

stack = []

while head:
stack.append(head)
head = head.next

dummy_head = ListNode()
curr = dummy_head

while stack:
curr.next = stack.pop()
curr = curr.next

curr.next = None

return dummy_head.next


"""
Time: O(N)
Space O(1)
"""
class Solution:
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
prev = None
curr = head

while curr:
temp = curr.next
curr.next = prev
prev, curr = curr, temp

return prev
Loading