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

🏷️ 알고리즘 패턴 분석

  • 패턴: Sliding Window
  • 설명: 이 코드는 두 포인터를 이용해 연속된 문자열 구간을 탐색하며, 중복을 방지하기 위해 set을 활용하는 슬라이딩 윈도우 패턴을 사용합니다.

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"""
Time Complexity: O(n^2)
Space Complexity: O(n)

과정:
1. 문자열을 순회하면서 중복되지 않는 가장 긴 부분 문자열을 찾음
2. 중복되지 않는 부분 문자열을 찾으면 그 길이를 최대 길이와 비교하여 최대 길이를 업데이트함
3. 중복되는 부분 문자열을 찾으면 그 부분 문자열을 초기화함
4. 중복되지 않는 부분 문자열을 찾으면 그 부분 문자열을 최대 길이와 비교하여 최대 길이를 업데이트함
"""


class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
ans = 0
for left in range(len(s)):
tmp = set()
for right in range(left, len(s)):
if s[right] in tmp:
break
tmp.add(s[right])
ans = max(right - left + 1, ans)
return ans
32 changes: 32 additions & 0 deletions number-of-islands/ohkingtaek.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.

🏷️ 알고리즘 패턴 분석

  • 패턴: DFS
  • 설명: 이 코드는 2차원 배열을 순회하며 섬을 찾고, DFS를 통해 연결된 섬의 모든 부분을 탐색하는 방식으로 섬의 개수를 셉니다. DFS는 재귀 또는 스택을 이용한 깊이 우선 탐색 패턴입니다.

Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"""
Time Complexity: O(m * n)
Space Complexity: O(m * n)

과정:
1. 2차원 배열을 순회하면서 1을 만나면 섬의 개수를 증가시킴
2. DFS를 통해 해당 섬을 탐색함
3. 해당 섬을 탐색하면서 0을 만나면 탐색을 종료함
"""


class Solution:
def numIslands(self, grid: List[List[str]]) -> int:
ans = 0
m, n = len(grid[0]), len(grid)
visited = [[0] * m for _ in range(n)]
dx, dy = [-1, 0, 1, 0], [0, -1, 0, 1]
for y in range(n):
for x in range(m):
if int(grid[y][x]) == 1 and visited[y][x] == 0:
ans += 1
dfs = [(x, y)]
visited[y][x] = 1
while dfs:
cx, cy = dfs.pop(0)
for _dx, _dy in zip(dx, dy):
gy = cy + _dy
gx = cx + _dx
if gx >= 0 and gy >= 0 and gx < m and gy < n and int(grid[gy][gx]) == 1 and visited[gy][gx] == 0:
visited[gy][gx] = 1
dfs.append((gx, gy))
return ans
25 changes: 25 additions & 0 deletions reverse-linked-list/ohkingtaek.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.

🏷️ 알고리즘 패턴 분석

  • 패턴: Fast & Slow Pointers
  • 설명: 이 코드는 두 포인터를 활용하여 리스트를 역순으로 뒤집는 과정에서, 현재 노드와 이전 노드를 가리키는 포인터를 사용하므로 Fast & Slow Pointers 패턴에 속합니다.

Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"""
Time Complexity: O(n)
Space Complexity: O(1)

과정:
1. 두 개의 포인터를 사용하여 리스트를 순회하면서 뒤집음
2. 첫 번째 포인터는 이전 노드를 가리키고, 두 번째 포인터는 현재 노드를 가리킴
3. 현재 노드의 next를 이전 노드로 변경하고, 두 포인터를 한 칸씩 이동시킴
4. 마지막 노드까지 반복하면 리스트가 뒤집어짐
"""


# 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]:
left, now = None, head
while now:
right = now.next
now.next = left
left, now = now, right
return left
31 changes: 31 additions & 0 deletions set-matrix-zeroes/ohkingtaek.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.

🏷️ 알고리즘 패턴 분석

  • 패턴: Two Pointers
  • 설명: 이 코드는 행과 열을 기준으로 0의 위치를 기록하고, 해당 위치를 0으로 변경하는 과정에서 두 포인터처럼 행과 열을 반복 탐색하는 방식을 사용합니다.

Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"""
Time Complexity: O(m * n)
Space Complexity: O(m + n)

과정:
1. 0이 있는 행과 열을 따로 집합에 기록해둠
2. 만들어진 행과 열을 기반으로 0으로 바꿔줌
"""


class Solution:
def setZeroes(self, matrix: List[List[int]]) -> None:
"""
Do not return anything, modify matrix in-place instead.
"""
m, n = len(matrix[0]), len(matrix)
array = []
for x in range(m):
for y in range(n):
if matrix[y][x] == 0:
array.append((x, y))
a, b = set(), set()
for x, y in array:
a.add(x)
b.add(y)
for x in range(m):
for i in b:
matrix[i][x] = 0
for y in range(n):
for i in a:
matrix[y][i] = 0
Loading