Skip to content

Commit

Permalink
Merge pull request #888 from jungsiroo/main
Browse files Browse the repository at this point in the history
[jungsiroo] Week 06
  • Loading branch information
jungsiroo authored Jan 17, 2025
2 parents 817f6f0 + 508129a commit a41c907
Show file tree
Hide file tree
Showing 4 changed files with 180 additions and 0 deletions.
44 changes: 44 additions & 0 deletions container-with-most-water/jungsiroo.py
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


60 changes: 60 additions & 0 deletions design-add-and-search-words-data-structure/jungsiroo.py
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)

44 changes: 44 additions & 0 deletions spiral-matrix/jungsiroo.py
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


32 changes: 32 additions & 0 deletions valid-parentheses/jungsiroo.py
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


0 comments on commit a41c907

Please sign in to comment.