-
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 #897 from Chaedie/main
[Chaedie] Week 6
- Loading branch information
Showing
6 changed files
with
361 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,43 @@ | ||
""" | ||
Solution: Brute Force | ||
Time: O(n^2) | ||
Space: O(1) | ||
""" | ||
|
||
|
||
class Solution: | ||
def maxArea(self, height: List[int]) -> int: | ||
max_water = 0 | ||
for i in range(len(height) - 1): | ||
for j in range(i + 1, len(height)): | ||
h = min(height[i], height[j]) | ||
w = j - i | ||
max_water = max(h * w, max_water) | ||
|
||
return max_water | ||
|
||
|
||
""" | ||
Solution: Two Pointer | ||
1) ํฌ์ธํฐ ์ด๋์ left height, right height ์ค ์์ value๋ฅผ ๊ฐ์ง ๊ฒฝ์ฐ ์ด๋ | ||
Time: O(n) | ||
Space: O(1) | ||
""" | ||
|
||
|
||
class Solution: | ||
def maxArea(self, height: List[int]) -> int: | ||
l = 0 | ||
r = len(height) - 1 | ||
max_water = 0 | ||
|
||
while l < r: | ||
h = min(height[l], height[r]) | ||
w = r - l | ||
max_water = max(h * w, max_water) | ||
|
||
if height[l] < height[r]: | ||
l += 1 | ||
else: | ||
r -= 1 | ||
return max_water |
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,112 @@ | ||
""" | ||
ํ์ด๋ฅผ ๋ณด๊ณ ๊ณต๋ถํ์ต๋๋ค. | ||
Solution: | ||
set์ ์ด์ฉ, | ||
1) addWord ์์ add | ||
2) search ์์ .์ ํฌํจํ ๋ชจ๋ ๊ธ์๊ฐ ๋์ผํ ๋จ์ด๊ฐ ์๋์ง ํ์ธ | ||
search | ||
Time: O(n * w) | ||
Space: O(1) | ||
""" | ||
|
||
|
||
class WordDictionary: | ||
|
||
def __init__(self): | ||
self.root = set() | ||
|
||
def addWord(self, word: str) -> None: | ||
self.root.add(word) | ||
|
||
def search(self, word: str) -> bool: | ||
for candidate in self.root: | ||
if len(candidate) != len(word): | ||
continue | ||
if all(w == c or w == "." for w, c in zip(word, candidate)): | ||
return True | ||
return False | ||
|
||
|
||
""" | ||
ํ์ด๋ฅผ ๋ณด๊ณ ๊ณต๋ถํ์ต๋๋ค. | ||
Solution: Trie - ๋ฌ๋ ์ ์ฝ๋ | ||
""" | ||
|
||
|
||
class WordDictionary: | ||
|
||
def __init__(self): | ||
self.root = {"$": True} | ||
|
||
def addWord(self, word: str) -> None: | ||
node = self.root | ||
for ch in word: | ||
if ch not in node: | ||
node[ch] = {"$": False} | ||
node = node[ch] | ||
node["$"] = True | ||
|
||
def search(self, word: str) -> bool: | ||
def dfs(node, idx): | ||
if idx == len(word): | ||
return node["$"] | ||
|
||
ch = word[idx] | ||
if ch in node: | ||
return dfs(node[ch], idx + 1) | ||
if ch == ".": | ||
if any(dfs(node[k], idx + 1) for k in node if k != "$"): | ||
return True | ||
return False | ||
|
||
return dfs(self.root, 0) | ||
|
||
|
||
""" | ||
ํ์ด๋ฅผ ๋ณด๊ณ ๊ณต๋ถํ์ต๋๋ค. | ||
Solution: Trie with TrieNode - NeetCode | ||
""" | ||
|
||
|
||
class TrieNode: | ||
def __init__(self): | ||
self.children = {} # a: TrieNode | ||
self.word = False | ||
|
||
|
||
class WordDictionary: | ||
|
||
def __init__(self): | ||
self.root = TrieNode() | ||
|
||
def addWord(self, word: str) -> None: | ||
cur = self.root | ||
|
||
for c in word: | ||
if c not in cur.children: | ||
cur.children[c] = TrieNode() | ||
cur = cur.children[c] | ||
cur.word = True | ||
|
||
def search(self, word: str) -> bool: | ||
def dfs(j, root): | ||
cur = root | ||
for i in range(j, len(word)): | ||
c = word[i] | ||
|
||
if c == ".": | ||
for child in cur.children.values(): | ||
if dfs(i + 1, child): | ||
return True | ||
return False | ||
else: | ||
if c not in cur.children: | ||
return False | ||
cur = cur.children[c] | ||
return cur.word | ||
|
||
return dfs(0, self.root) |
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,49 @@ | ||
""" | ||
Solution: | ||
1) encode: ๊ฐ ๊ธ์์ ์์ ๊ธ์์์ # ๋ผ๋ delimiter ๋ฅผ ๋ถ์ฌ stringify ํ๋ค. | ||
2) decode: length ๋ฅผ ์ฐธ๊ณ ์ผ์ word๋ฅผ ๋ฐ๋ด์ด result ๋ฐฐ์ด์ ๋ง๋ ๋ค. | ||
encode: | ||
Time: O(n) (n: strs ๋ฐฐ์ด์ ๊ธธ์ด๋งํผ ์ฐ์ฐ) | ||
Space: O(1) | ||
decode: | ||
Time: O(n) (n: s ์ ๊ธธ์ด๋งํผ ์ฐ์ฐ) | ||
Space: O(m) (m: decode ์ดํ ๋ฐฐ์ด์ ๊ธธ์ด) | ||
""" | ||
|
||
|
||
class Codec: | ||
|
||
def encode(self, strs: List[str]) -> str: | ||
"""Encodes a list of strings to a single string.""" | ||
result = "" | ||
for word in strs: | ||
result += str(len(word)) | ||
result += "#" | ||
result += word | ||
return result | ||
|
||
def decode(self, s: str) -> List[str]: | ||
"""Decodes a single string to a list of strings.""" | ||
i = 0 | ||
result = [] | ||
length = "" | ||
while i < len(s): | ||
# find number | ||
length = "" | ||
while s[i] is not "#": | ||
length += s[i] | ||
i += 1 | ||
# find # | ||
i += 1 | ||
# find word | ||
result.append(s[i : i + int(length)]) | ||
i += int(length) | ||
|
||
return result | ||
|
||
|
||
# Your Codec object will be instantiated and called as such: | ||
# codec = Codec() | ||
# codec.decode(codec.encode(strs)) |
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 @@ | ||
""" | ||
Solution: 1) DFS Brute Force -> TLE | ||
Time: O(2^n * nlogn) | ||
""" | ||
|
||
|
||
class Solution: | ||
def lengthOfLIS(self, nums: List[int]) -> int: | ||
|
||
sub = [] | ||
max_len = 0 | ||
|
||
def dfs(i, length): | ||
nonlocal max_len | ||
if i == len(nums): | ||
if sub == sorted(list(set(sub))): | ||
max_len = max(len(sub), max_len) | ||
return | ||
|
||
dfs(i + 1, length) | ||
sub.append(nums[i]) | ||
dfs(i + 1, length) | ||
sub.pop() | ||
|
||
dfs(0, 0) | ||
return max_len | ||
|
||
|
||
""" | ||
ํ์ด๋ฅผ ๋ณด๊ณ ์ ์์ผ๋ฉฐ, ์๋ฒฝํ ์ดํด ๋์ง๋ ์์ต๋๋ค. | ||
Time: O(n^2) | ||
Space: O(n) | ||
""" | ||
|
||
|
||
class Solution: | ||
def lengthOfLIS(self, nums: List[int]) -> int: | ||
LIS = [1] * len(nums) | ||
|
||
for i in range(len(nums) - 1, -1, -1): | ||
for j in range(i + 1, len(nums)): | ||
if nums[i] < nums[j]: | ||
LIS[i] = max(LIS[i], 1 + LIS[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,46 @@ | ||
""" | ||
Solution: | ||
1) ์ข์ฐ ์ํ์ ๊ฒฝ๊ณ๋ฅผ ์ขํ๊ฐ๋ฉฐ ์ํํ๋ค. | ||
2) ํ์ถ ์กฐ๊ฑด์ผ๋ก result ์ ์ฌ์ด์ฆ๊ฐ matrix ์ ์์ ๊ฐฏ์๊ฐ ๊ฐ์์ง๋์ง ํ์ธํ๋ค. | ||
(ํ์ถ ์กฐ๊ฑด์ ์ข์ฐ ์ํ ๊ฒฝ๊ณ๊ฐ ๋์ด๊ฐ๋ ์ํ์ ๊ธฐ์ค์ผ๋ก ์ผ์ ์๋ฃจ์ ์ ๋ดค์ง๋ง, ์ ํํ ์ดํด๋ ๋์ง ์์ ์ฌ์ด์ฆ ๋น๊ต๋ก ์งํํ์ต๋๋ค.) | ||
Time: O(m * n) | ||
Space: O(1) | ||
""" | ||
|
||
|
||
class Solution: | ||
def spiralOrder(self, matrix: List[List[int]]) -> List[int]: | ||
result = [] | ||
left, right = 0, len(matrix[0]) | ||
top, bottom = 0, len(matrix) | ||
|
||
while left < right and top < bottom: | ||
for i in range(left, right): | ||
result.append(matrix[top][i]) | ||
top += 1 | ||
|
||
if len(result) == len(matrix[0]) * len(matrix): | ||
break | ||
|
||
for i in range(top, bottom): | ||
result.append(matrix[i][right - 1]) | ||
right -= 1 | ||
|
||
if len(result) == len(matrix[0]) * len(matrix): | ||
break | ||
|
||
for i in range(right - 1, left - 1, -1): | ||
result.append(matrix[bottom - 1][i]) | ||
bottom -= 1 | ||
|
||
if len(result) == len(matrix[0]) * len(matrix): | ||
break | ||
|
||
for i in range(bottom - 1, top - 1, -1): | ||
result.append(matrix[i][left]) | ||
left += 1 | ||
|
||
if len(result) == len(matrix[0]) * len(matrix): | ||
break | ||
|
||
return result |
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,67 @@ | ||
""" | ||
Solution: | ||
1) s ๋ฅผ ์ํํ๋ฉด์ ์ฌ๋ ๊ดํธ๋ฉด stack ์ push ํ๋ค. | ||
2) ๋ซ๋ ๊ดํธ์ผ ๊ฒฝ์ฐ ๊ฐ case ๋ณ๋ก stack ์ ์ต์๋จ์ด ์๋ง๋ ์ฌ๋ ๊ดํธ์ด๋ฉด stack ์ pop ํ๊ณ ์๋๋ฉด False ๋ฅผ return ํ๋ค. | ||
Time: O(n) | ||
Space: O(n) | ||
""" | ||
|
||
|
||
class Solution: | ||
def isValid(self, s: str) -> bool: | ||
stack = [] | ||
left = ["(", "{", "["] | ||
for char in s: | ||
if char in left: | ||
stack.append(char) | ||
continue | ||
|
||
if not stack: | ||
return False | ||
|
||
if char == ")": | ||
if stack[-1] == "(": | ||
stack.pop() | ||
else: | ||
return False | ||
if char == "}": | ||
if stack[-1] == "{": | ||
stack.pop() | ||
else: | ||
return False | ||
if char == "]": | ||
if stack[-1] == "[": | ||
stack.pop() | ||
else: | ||
return False | ||
|
||
return not stack | ||
|
||
|
||
""" | ||
Solution: | ||
1) stack๊ณผ hash map์ ์ฌ์ฉํ๋ค. | ||
2) s๋ฅผ ์ํํ๋ฉด์ ์ฌ๋ ๊ดํธ์ผ ๊ฒฝ์ฐ stack ์ push | ||
3) ๋ซ๋ ๊ดํธ์ผ ๊ฒฝ์ฐ stack ์ด ๋น์ง ์์ผ๋ฉด์ ์ต์๋จ ์์๊ฐ ์๋ง์ ์ฌ๋ ๊ดํธ์ด๋ฉด stack pop, ์๋ ๊ฒฝ์ฐ return False | ||
Time: O(n) | ||
Space: O(n) | ||
""" | ||
|
||
|
||
class Solution: | ||
def isValid(self, s: str) -> bool: | ||
opening = "({[" | ||
closing = ")}]" | ||
closeToOpen = dict(zip(closing, opening)) | ||
|
||
stack = [] | ||
for char in s: | ||
if char in opening: | ||
stack.append(char) | ||
else: | ||
if stack and stack[-1] == closeToOpen[char]: | ||
stack.pop() | ||
else: | ||
return False | ||
return not stack |