Skip to content

Commit

Permalink
Merge pull request #897 from Chaedie/main
Browse files Browse the repository at this point in the history
[Chaedie] Week 6
  • Loading branch information
Chaedie authored Jan 19, 2025
2 parents 132b69e + 6eccf0f commit 16e6492
Show file tree
Hide file tree
Showing 6 changed files with 361 additions and 0 deletions.
43 changes: 43 additions & 0 deletions container-with-most-water/Chaedie.py
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
112 changes: 112 additions & 0 deletions design-add-and-search-words-data-structure/Chaedie.py
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)
49 changes: 49 additions & 0 deletions encode-and-decode-strings/Chaedie.py
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))
44 changes: 44 additions & 0 deletions longest-increasing-subsequence/Chaedie.py
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)
46 changes: 46 additions & 0 deletions spiral-matrix/Chaedie.py
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
67 changes: 67 additions & 0 deletions valid-parentheses/Chaedie.py
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

0 comments on commit 16e6492

Please sign in to comment.