-
Notifications
You must be signed in to change notification settings - Fork 126
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 #914 from sungjinwi/main
[suwi] Week06
- Loading branch information
Showing
4 changed files
with
123 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,26 @@ | ||
""" | ||
ํ์ด : | ||
left, right ์ปค์๋ฅผ ์ ๋์ ๋๊ณ ์กฐ๊ฑด์ ๋ฐ๋ผ ๋ฐ๋ํธ์ผ๋ก ์ด๋์ํค๋ฉฐ ๋ฌผ ์ ๋น๊ต | ||
๋ ์ค ๋ ๋์ ๋์ด๋ฅผ ์ด๋์ํค๋ฉด ๋ฌด์กฐ๊ฑด ๋ฌผ์ ์์ด ์ค์ด๋ค๊ธฐ ๋๋ฌธ์ ๋ ๋ฎ๊ฑฐ๋ ๊ฐ์ ์ปค์๋ฅผ ์ด๋์ํค๋ฉฐ ์ ๋ฐ์ดํธ | ||
๋์ด ๋ง๋๋ฉด ๋น๊ต ์ข ๋ฃ | ||
len(height) : N | ||
TC : O(N) | ||
l, r์ ์ด๋์ด ์ ์ฒด height ๊ฐ์๋งํผ ์ผ์ด๋๋ฏ๋ก | ||
SC : O(1) | ||
""" | ||
|
||
class Solution: | ||
def maxArea(self, height: List[int]) -> int: | ||
l = 0 | ||
r = len(height) - 1 | ||
max_area = 0 | ||
while (l != r) : | ||
cur_area = (r - l) * min(height[l], height[r]) | ||
max_area = max(cur_area, max_area) | ||
if (height[l] >= height[r]) : | ||
r -= 1 | ||
else : | ||
l += 1 | ||
return max_area |
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,25 @@ | ||
""" | ||
ํ์ด : | ||
ํด๋น ์ธ๋ฑ์ค์ ์ซ์๋ก ๋๋๋ LIS์ ๊ธธ์ด๋ฅผ dp๋ฐฐ์ด์ ์ ์ฅ | ||
nums[i] ์ ๊ทธ ์ดํ์ ์ค๋ nums[j]๋ฅผ ๋น๊ตํด nums[j]๊ฐ ํฌ๊ณ | ||
i๊น์ง์ LIS ๊ธธ์ด + 1 > j๊น์ง์ LIS๊ธธ์ด์ด๋ฉด j๊น์ง์ LIS๊ธธ์ด ์ ๋ฐ์ดํธ | ||
์ ๋ฐ์ดํธ ํ ๋๋ง๋ค max๊ธธ์ด์ ๋น๊ตํด์ ์ต๋๊ธธ์ด ์ ๋ฐ์ดํธ | ||
nums์ ๊ธธ์ด N | ||
TC : O(N^2) | ||
for๋ฌธ์ ๊ฐ์๋ฅผ ์ดํด๋ณด๋ฉด N-1 + N-2 + ... + 1 = (N-1)N/2 ์ด๋ฏ๋ก N^2/2 -> O(N^2) | ||
SC : O(N) | ||
dp์ ๊ธธ์ด๋ nums์ ๊ธธ์ด์ ๋น๋กํ๋ฏ๋ก | ||
""" | ||
|
||
class Solution: | ||
def lengthOfLIS(self, nums: List[int]) -> int: | ||
dp = [1] * len(nums) | ||
max_LIS = 1 | ||
for i in range(len(nums) - 1) : | ||
for j in range(i + 1, len(nums)) : | ||
if nums[i] < nums[j] and dp[i] + 1 > dp[j] : | ||
dp[j] = dp[i] + 1 | ||
max_LIS = max(max_LIS, dp[j]) | ||
return max_LIS |
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,42 @@ | ||
""" | ||
ํ์ด : | ||
์ค๋ฅธ์ชฝ, ์๋์ชฝ์ผ๋ก ์ด๋ํ ๋ ๊ฐ๊ฐ column๊ณผ row๊ฐ ์ฆ๊ฐํ๋ฏ๋ก direction 1 | ||
์ผ์ชฝ, ์์ชฝ์ ๊ฐ์ํ๋ฏ๋ก direction -1 | ||
๋ก ์ค์ ํ๊ณ n_row ๋๋ n_col๋งํผ direction์ ์ด๋ํ๋ฉด์ ans์ ๋ด์์ค๋ค | ||
๊ฐ for๋ฌธ์ด ๋๋ ๋๋ง๋ค n_row ๋๋ n_col์ ๊ฐ์์์ผ ์ฃผ๊ณ | ||
๋ ์ค ํ๋๊ฐ 0์ด ๋ ๋๊น์ง ๊ณ์ ์งํ | ||
- col์ด -1๋ถํฐ ์์ํด์ matrix[0][0]๋ถํฐ appendํ ์ ์๋๋ก ์ ์ | ||
- n_cols, n_rows ๋ ์ค ํ๋๋ง 0์ด๋๋ ๋๋จ์ ์ ์ | ||
TC : O(M * N) | ||
matrix ์ ์ฒด๋ฅผ ํ๋ฒ์ฉ ์ํํด์ผํ๋ฏ๋ก | ||
SC : O(1) | ||
returnํ ๋ฆฌ์คํธ ์ธ์๋ ์ถ๊ฐ์ ์ธ ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ์ด ์์๊ฐ(5๊ฐ)์ด๋ฏ๋ก | ||
""" | ||
|
||
class Solution: | ||
def spiralOrder(self, matrix: List[List[int]]) -> List[int]: | ||
ans = [] | ||
n_rows = len(matrix) | ||
n_cols = len(matrix[0]) | ||
|
||
row = 0 | ||
col = -1 | ||
direction = 1 | ||
|
||
while n_cols and n_rows : | ||
for _ in range(n_cols): | ||
col += direction | ||
ans.append(matrix[row][col]) | ||
n_rows -= 1 | ||
|
||
for _ in range(n_rows): | ||
row += direction | ||
ans.append(matrix[row][col]) | ||
n_cols -= 1 | ||
|
||
direction *= -1 | ||
|
||
return ans |
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,30 @@ | ||
""" | ||
ํ์ด : | ||
stack๊ตฌ์กฐ๋ฅผ ์ด์ฉํด์ ๊ตฌํ | ||
๊ดํธ์ ํ์ด๋ฅผ ๋์ ๋๋ฆฌ์ key, value๋ฅผ ์ด์ฉํด ๋งค์น์ํจ๋ค | ||
์ฌ๋ ๊ดํธ๋ฅผ ๋ง๋๋ฉด stack์ push, ๋ซ๋ ๊ดํธ๋ฅผ ๋ง๋๋ฉด stack์ ๋ง์ง๋ง ๊ดํธ์ pair์ผ ๋ pop, ์๋๋ฉด return False | ||
๋ฌธ์์ด ์ ์ฒด๋ฅผ ์ํํ์ ๋ stack์ ๋จ์์์ผ๋ฉด ์ ๋ซํ ๊ดํธ๊ฐ ์์ผ๋ฏ๋ก return False | ||
s์ ๊ธธ์ด N | ||
TC : O(N) | ||
s์ ๋ํด ํ๋ฒ for๋ฌธ | ||
SC : O(N) | ||
stack์ ์ต๋ ํฌ๊ธฐ๋ s์ ๊ธธ์ด์ ๋น๋ก | ||
""" | ||
|
||
class Solution: | ||
def isValid(self, s: str) -> bool: | ||
stack = [] | ||
pair = dict(zip('({[',')}]')) | ||
for paren in s : | ||
if paren in pair : | ||
stack.append(paren) | ||
elif not stack : | ||
return False | ||
elif pair[stack.pop()] != paren : | ||
return False | ||
if not stack : | ||
return True | ||
else : | ||
return False |