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 4 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
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]