-
-
Notifications
You must be signed in to change notification settings - Fork 195
[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
yyyyyyyyyKim
wants to merge
7
commits into
DaleStudy:main
Choose a base branch
from
yyyyyyyyyKim:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
bba4367
longest-substring-without-repeating-characters solution
yyyyyyyyyKim fbffd5e
longest-substring-without-repeating-characters solution
yyyyyyyyyKim 32f43a4
number-of-islands solution
yyyyyyyyyKim 60d0ed2
unique-paths solution
yyyyyyyyyKim f73c2ce
set-matrix-zeroes solution
yyyyyyyyyKim da62e13
set-matrix-zeroes solution
yyyyyyyyyKim 629c00c
reverse-linked-list solution
yyyyyyyyyKim File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
21 changes: 21 additions & 0 deletions
21
longest-substring-without-repeating-characters/yyyyyyyyyKim.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
시간복잡도, 공간복잡도와 Follow up 할 수 있는 포인트, 풀이에 대한 전체적인 설명이 꼼꼼하게 적혀 있어서 문제 풀이 흐름을 쉽게 이해할 수 있는 점이 좋은 것 같습니다.