-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #938 from aa601/main
[yeoju] Week 7
- Loading branch information
Showing
5 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 @@ | ||
# TC:O(n^2) SC:O(1) | ||
class Solution: | ||
def lengthOfLongestSubstring(self, s: str) -> int: | ||
max_size = 0 | ||
for start in range(len(s)): | ||
saw = set() | ||
for end in range(start, len(s)): | ||
if s[end] in saw: | ||
break | ||
else: | ||
saw.add(s[end]) | ||
max_size = max(max_size, end - start + 1) # ๋ถ๋ถ ๋ฌธ์์ด์ ๊ธธ์ด์ ํ์ฌ๊น์ง์ ์ต๋๊ธธ์ด๊ฐ ๋น๊ต | ||
return max_size |
This file contains 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,20 @@ | ||
# TC:O(n * m), SC:O(n * m) | ||
class Solution: | ||
def numIslands(self, grid: List[List[str]]) -> int: | ||
cnt = 0 | ||
row = len(grid) | ||
col = len(grid[0]) | ||
def dfs(r: int, c: int) : | ||
grid[r][c] = "0" | ||
for y, x in [(r, c + 1), (r + 1, c), (r - 1, c), (r, c - 1)]: # ํ์ฌ r,c ์ขํ์ ๋ํด ์ํ์ข์ฐ ํ์ | ||
if 0 <= y < row and 0 <= x < col: | ||
if grid[y][x] == "1": | ||
dfs(y, x) | ||
return | ||
|
||
for r in range(row): | ||
for c in range(col): | ||
if grid[r][c] == "1": #๋ ๋ฐ๊ฒฌ ์ cnt ์ฆ๊ฐํ๊ณ , ๋ฐ๊ฒฌ๋ ๋ ๊ณผ ์ฐ๊ฒฐ๋ ๋ ๋ค์ ์ ๊ฑฐ | ||
cnt += 1 | ||
dfs(r, c) | ||
return cnt |
This file contains 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,11 @@ | ||
# TC:O(n), SC:O(1) | ||
class Solution: | ||
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]: | ||
cur = head | ||
prv = None | ||
while cur != None: | ||
tmp = cur.next # ๋ค์ ๋ ธ๋์ ์ฃผ์๋ฅผ tmp์ ์ ์ฅ | ||
cur.next = prv # ํ์ฌ ๋ ธ๋๊ฐ ๊ฐ๋ฆฌํค๋ ์ฃผ์๋ฅผ prv๋ก ์ค์ | ||
prv = cur # prv๋ฅผ ํ์ฌ ๋ ธ๋๋ก ์ค์ | ||
cur = tmp # ํ์ฌ ๋ ธ๋๋ฅผ ๊ทธ ๋ค์ ๋ ธ๋๋ก ๋ณ๊ฒฝ | ||
return prv |
This file contains 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,33 @@ | ||
# TC:O(n^2), SC:O(1) | ||
class Solution: | ||
def setZeroes(self, matrix: List[List[int]]) -> None: | ||
first_row = False | ||
first_col = False | ||
|
||
#์ฒซ๋ฒ์งธ ํ, ์ด flag | ||
for r in range(len(matrix)): | ||
if matrix[r][0] == 0: | ||
first_row = True | ||
for c in range(len(matrix[0])): | ||
if matrix[0][c] == 0: | ||
first_col = True | ||
|
||
#๊ทธ ์ด์ธ์ ํ, ์ด flag | ||
for r in range(1, len(matrix)): | ||
for c in range(1, len(matrix[0])): | ||
if matrix[r][c] == 0: | ||
matrix[r][0] = 0 | ||
matrix[0][c] = 0 | ||
|
||
# 0์ผ๋ก ์ค์ | ||
for r in range(1, len(matrix)): | ||
for c in range(1, len(matrix[0])): | ||
if matrix[r][0] == 0 or matrix[0][c] == 0: | ||
matrix[r][c] = 0 | ||
# ์ฒซ๋ฒ์งธ ํ๊ณผ ์ด์ ๋ํด ๊ฐ๊ฐ 0์ผ๋ก ์ค์ | ||
if first_row: | ||
for r in range(len(matrix)): | ||
matrix[r][0] = 0 | ||
if first_col: | ||
for c in range(len(matrix[0])): | ||
matrix[0][c] = 0 |
This file contains 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,12 @@ | ||
# TC:O(n*m), SC:O(1) | ||
# ์ผ์ชฝ์๋๋ก ๊ฐ๋ ๊ฒฝ๋ก๋ ์ผ์ชฝ์ผ๋ก ๊ฐ๋ ๊ฒฝ๋ก์ ๊ฒฝ์ฐ์ ์ + ์๋๋ก ๊ฐ๋ ๊ฒฝ๋ก์ ์ | ||
# ๊ฐ์ฅ์๋ฆฌ๋ก ๊ฐ๋ ๊ฒฝ๋ก์ ๊ฒฝ์ฐ์ ์๋ ๋ชจ๋ 1 | ||
# nํฌ๊ธฐ์ ๋ฆฌ์คํธ ๊ฐ์ ๋งค ๋ฒ ๋ฎ์ด์จ์ ๊ณ์ฐํจ | ||
class Solution: | ||
def uniquePaths(self, m: int, n: int) -> int: | ||
l = [1 for _ in range(n)] | ||
for row in range(1, m): | ||
for col in range(1, n): | ||
l[col] += l[col - 1] | ||
|
||
return l[-1] |