Skip to content

[yyyyyyyyyKim] WEEK 07 solutions #1462

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
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
21 changes: 21 additions & 0 deletions longest-substring-without-repeating-characters/yyyyyyyyyKim.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:

# 브루트포스(시간복잡도 : O(n^2))
answer = 0

for i in range(len(s)):
# 중복없는 문자열을 저장할 집합
substring = set()

for j in range(i,len(s)):

# 중복 문자를 만나면 break
if s[j] in substring:
break

# 중복 아니면 문자 추가하고 긴 문자열 길이 비교해서 업데이트
substring.add(s[j])
answer = max(answer, len(substring))

return answer
31 changes: 31 additions & 0 deletions number-of-islands/yyyyyyyyyKim.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
class Solution:
def numIslands(self, grid: List[List[str]]) -> int:

# DFS (시간복잡도 O(m*n), 공간복잡도 O(m*n))
answer = 0 # 섬의 수
m = len(grid)
n = len(grid[0])

# 하나의 섬 처리(연결된 땅 모두 방문)
def dfs(x,y):
# 범위를 벗어나거나, 이미 방문했거나, 땅이 아니면 종료
if x < 0 or y < 0 or x >= m or y >= n or grid[x][y] != "1":
return

# 현재 땅 방문처리
grid[x][y] = "*"

# 상하좌우 탐색
dfs(x+1, y)
dfs(x-1, y)
dfs(x, y+1)
dfs(x, y-1)

for i in range(m):
for j in range(n):
# 땅 발견시 dfs로 연결되어 있는 모든 땅 방문하고 섬+1 처리
if grid[i][j] == "1":
dfs(i,j)
answer += 1

return answer
19 changes: 19 additions & 0 deletions reverse-linked-list/yyyyyyyyyKim.py
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]:

# 시간복잡도 O(n), 공간복잡도 O(1)
answer = None

while head:
next_node = head.next # 다음 노드 저장
head.next = answer # 현재 노드의 next를 이전 노드로 변경
answer = head # answer를 현재 노드로 업데이트
head = next_node # head를 다음 노드로 이동

# answer = 역순 리스트의 head
return answer
27 changes: 27 additions & 0 deletions set-matrix-zeroes/yyyyyyyyyKim.py
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

시간복잡도, 공간복잡도와 Follow up 할 수 있는 포인트, 풀이에 대한 전체적인 설명이 꼼꼼하게 적혀 있어서 문제 풀이 흐름을 쉽게 이해할 수 있는 점이 좋은 것 같습니다.

Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class Solution:
def setZeroes(self, matrix: List[List[int]]) -> None:
"""
Do not return anything, modify matrix in-place instead.
"""
# 0이 있는 행, 열을 기록해두고 그 기록을 토대로 바꾸기(시간복잡도 O(m*n), 공간복잡도 O(m+n))
# Follow up : 공간복잡도 O(1) 생각해볼것
m, n = len(matrix), len(matrix[0])
# rows, cols = [] , [] -> 중복되어 들어갈 수 있음. 불필요함. 중복제거를 위해 set 사용.
rows, cols = set(), set()

# 0이 있는 행, 열 기록
for i in range(m):
for j in range(n):
if matrix[i][j] == 0:
rows.add(i)
cols.add(j)

# 기록된 행 0으로 바꾸기
for i in rows:
for j in range(n):
matrix[i][j] = 0

# 기록된 열 0으로 바꾸기
for j in cols:
for i in range(m):
matrix[i][j] = 0
13 changes: 13 additions & 0 deletions unique-paths/yyyyyyyyyKim.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Solution:
def uniquePaths(self, m: int, n: int) -> int:

# DP (시간복잡도 O(m*n), 공간복잡도 O(m*n))
# 모든 1행과 1열은 경로가 1개이므로 1로 배열 초기화.
dp = [[1]*n for _ in range(m)]

for i in range(1,m):
for j in range(1,n):
# 현재위치 경로 경우의 수 = 위쪽 + 왼쪽
dp[i][j] = dp[i-1][j] + dp[i][j-1]

return dp[m-1][n-1]