-
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 #888 from jungsiroo/main
[jungsiroo] Week 06
- Loading branch information
Showing
4 changed files
with
180 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,44 @@ | ||
class Solution: | ||
def maxArea(self, height: List[int]) -> int: | ||
# Naive - ์์ ํ์ | ||
n = len(height) | ||
|
||
""" | ||
๋ชจ๋ ์กฐํฉ์ ๊ณ์ฐํ ํ ์ต๋๊ฐ ๋ฆฌํด | ||
Tc : O(n^2) | ||
Sc : O(1) | ||
ret = 0 | ||
for i in range(n-1): | ||
for j in range(i+1, n): | ||
w = j - i | ||
h = min(height[i], height[j]) | ||
ret = max(ret, w*h) | ||
return ret | ||
""" | ||
|
||
# Better Solution | ||
""" | ||
ํฌ ํฌ์ธํฐ๋ฅผ ํ์ฉํ ๋ฐฉ๋ฒ | ||
ํฌ์ธํฐ๋ฅผ ์์ง์ผ ๋๋ ๋ ์์ ๊ฐ์ ๊ฐ์ง ์ชฝ์ด ํ์นธ ์์ง์ฌ ๊ทธ ๋์ด๋ฅผ ๋์ด๋ ๋ฐฉํฅ ์ชฝ์ผ๋ก ์งํ | ||
Tc : O(n) | ||
Sc : O(1) | ||
""" | ||
left, right = 0, n-1 | ||
ret = 0 | ||
|
||
while left < right: | ||
w = right - left | ||
h = min(height[left], height[right]) | ||
ret = max(ret, h*w) | ||
|
||
if height[left] <= height[right]: | ||
left += 1 | ||
else: | ||
right -= 1 | ||
return ret | ||
|
||
|
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,60 @@ | ||
class Node: | ||
def __init__(self, char=None): | ||
self.is_end = False | ||
self.children = {} | ||
|
||
class WordDictionary: | ||
""" | ||
ํธ๋ผ์ด๋ฅผ ํ์ฉ | ||
๊ธฐ๋ณธ ๋ฌธ์ ์ ๋ค๋ฅธ ์ ์ search ํ ๋ "." ์ด ํฌํจ๋์ด์๋ ๊ฒ | ||
๋ฐ๋ผ์ . ์ด๋ผ๋ฉด ํด๋น ๋ ธ๋์ ๋ชจ๋ ์์์ ๋ชจ๋ ์กฐํํด์ผ๋จ | ||
addWord | ||
w = len(word) | ||
Tc : O(w) | ||
Sc : O(w) | ||
search | ||
์ต์ ์ ๊ฒฝ์ฐ case : xa, xb, xc, xd, .... xz ๋ก x์ ๋ค์ ๊ธ์๊ฐ a~z์ผ ๋ | ||
search("x.") ๋ผ๋ฉด 26๊ฐ ๋ชจ๋๋ฅผ ์ฐพ์๋ด์ผ๋จ | ||
Tc : O(26^w) | ||
Sc : O(w) (๊ธ์ ๊ธธ์ด๋งํผ ์ฌ๊ท๋ฅผ ํธ์ถํ๊ธฐ์) | ||
""" | ||
def __init__(self): | ||
self.root = Node() | ||
|
||
def addWord(self, word: str) -> None: | ||
now = self.root | ||
|
||
for ch in word: | ||
if ch not in now.children: | ||
now.children[ch] = Node(ch) | ||
now = now.children[ch] | ||
now.is_end = True | ||
|
||
def search(self, word: str) -> bool: | ||
def _recur_search(node, index): | ||
if index == len(word): | ||
return node.is_end | ||
|
||
if word[index] == ".": | ||
for ch in node.children: | ||
if _recur_search(node.children[ch], index+1): | ||
return True | ||
|
||
if word[index] in node.children: | ||
return _recur_search(node.children[word[index]], index+1) | ||
return False | ||
|
||
return _recur_search(self.root, 0) | ||
|
||
|
||
|
||
|
||
# Your WordDictionary object will be instantiated and called as such: | ||
# obj = WordDictionary() | ||
# obj.addWord(word) | ||
# param_2 = obj.search(word) | ||
|
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,44 @@ | ||
from collections import deque | ||
|
||
""" | ||
BFS์ ์์ด๋์ด๋ฅผ ์ฐจ์ฉํ์ฌ ํ์ด | ||
๋์์๋ถํฐ ์์ชฝ์ผ๋ก ๋์๊ฐ์ผํ๊ธฐ์ ๋ฏธ๋ฆฌ ๋์๊ฐ ๋ฐฉํฅ ์ง์ : (dx, dy) | ||
ํ์นธ์ฉ ์ด๋ํด๊ฐ๋ฉด์ ๋ฒ์ ๋ฐ์ ๋์ด๊ฐ๊ฑฐ๋ ์ด๋ฏธ visitํ ๋ฐ์ดํฐ๊ฐ ๋ฐ๊ฒฌ๋๋ฉด ๋ฐฉํฅ์ ๊บพ์ | ||
Tc : O(m*n) | ||
Sc : O(m*n) | ||
""" | ||
|
||
# r, d, l ,u | ||
dx = [0,1,0,-1] | ||
dy = [1,0,-1,0] | ||
|
||
class Solution: | ||
def __init__(self): | ||
self.m = 0 | ||
self.n = 0 | ||
|
||
def in_range(self, r, c): | ||
if r<0 or r>=self.m or c<0 or c>=self.n: | ||
return False | ||
return True | ||
|
||
def spiralOrder(self, matrix: List[List[int]]) -> List[int]: | ||
INF = int(1e9) | ||
d = 0 | ||
self.m, self.n = len(matrix), len(matrix[0]) | ||
r, c = 0, 0 | ||
|
||
ret = [] | ||
|
||
for _ in range(self.m * self.n): | ||
ret.append(matrix[r][c]) | ||
if not self.in_range(r+dx[d], c+dy[d]) or matrix[r+dx[d]][c+dy[d]] == INF: | ||
d = (d+1)%4 | ||
matrix[r][c] = INF | ||
r, c = r+dx[d], c+dy[d] | ||
|
||
return ret | ||
|
||
|
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,32 @@ | ||
class Solution: | ||
def isValid(self, s: str) -> bool: | ||
""" | ||
์คํ์ ์ด์ฉํ ๊ฐ๋จํ ํ์ด | ||
s์ ํฌํจ๋ ๊ดํธ๋ค์ ์ด๋ฏธ ์ ํด์ ธ์๋ ์ํ์ด๊ธฐ์ ๋์ ๋๋ฆฌ ์ด์ฉ | ||
์ด๋ฅผ ์ด์ฉํด stack์ ๋ง์ง๋ง ์์๊ฐ ํ์ฌ ๊ดํธ์ ๋งค์น๋๋์ง๋ฅผ ์ ์๋ฅผ ํตํด ์ ์ ์์ | ||
๋ง์ง๋ง์ stack์ด ์ ๋ถ ๋น์์ ๋ True๋ฅผ ๋ฆฌํด | ||
Time Complexity : O(n) | ||
Space Complexity : O(n) (stack ๋ณ์) | ||
""" | ||
|
||
|
||
chars = {'(':1, '{':2, '[':3, ')':-1, '}':-2, ']':-3} | ||
stack = [] | ||
|
||
for char in s: | ||
if chars[char] > 0: | ||
stack.append(char) | ||
else: | ||
if not stack : return False | ||
|
||
if chars[stack[-1]] == -chars[char]: | ||
stack.pop() | ||
else: | ||
return False | ||
|
||
return not stack | ||
|
||
|