Skip to content

Commit

Permalink
Merge pull request #911 from pmjuu/main
Browse files Browse the repository at this point in the history
[Lyla] Week 06
  • Loading branch information
pmjuu authored Jan 19, 2025
2 parents 16e6492 + 9d995c6 commit 50a6e9e
Show file tree
Hide file tree
Showing 5 changed files with 180 additions and 0 deletions.
26 changes: 26 additions & 0 deletions container-with-most-water/pmjuu.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'''
์‹œ๊ฐ„ ๋ณต์žก๋„: O(n)
- ๋‘ ํฌ์ธํ„ฐ๋ฅผ ์ด๋™ํ•˜๋ฉด์„œ ๋ฐฐ์—ด์„ ํ•œ ๋ฒˆ๋งŒ ์ˆœํšŒํ•˜๋ฏ€๋กœ ์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” O(n)์ž…๋‹ˆ๋‹ค.
๊ณต๊ฐ„ ๋ณต์žก๋„: O(1)
- ์ถ”๊ฐ€ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ๋ณ€์ˆ˜๋งŒ ์‚ฌ์šฉํ•˜๋ฏ€๋กœ O(1)์ž…๋‹ˆ๋‹ค.
'''

from typing import List


class Solution:
def maxArea(self, height: List[int]) -> int:
max_area = 0
left, right = 0, len(height) - 1

while left < right:
current_area = (right - left) * min(height[left], height[right])
max_area = max(current_area, max_area)

if height[left] < height[right]:
left += 1
else:
right -= 1

return max_area
58 changes: 58 additions & 0 deletions design-add-and-search-words-data-structure/pmjuu.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
'''
* L: ๋‹จ์–ด์˜ ๊ธธ์ด
์‹œ๊ฐ„๋ณต์žก๋„: O(1)
- addWord(word): O(L), ์ตœ๋Œ€ ๋‹จ์–ด ๊ธธ์ด๊ฐ€ 25๋กœ ์ œํ•œ๋˜๋ฏ€๋กœ ์ด ์ž‘์—…์€ ์ƒ์ˆ˜ ์‹œ๊ฐ„์— ๊ฐ€๊น์Šต๋‹ˆ๋‹ค.
- search(word): O(L * 26^d), ์—ฌ๊ธฐ์„œ 26์€ ์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž์˜ ๊ฐœ์ˆ˜๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. d๋Š” ๋‹จ์–ด ๋‚ด '.'์˜ ๊ฐœ์ˆ˜์ธ๋ฐ, 2๋กœ ์ œํ•œ๋˜๋ฏ€๋กœ ์ƒ์ˆ˜ ์‹œ๊ฐ„์— ๊ฐ€๊น์Šต๋‹ˆ๋‹ค.
๊ณต๊ฐ„๋ณต์žก๋„:
- Trie ๊ตฌ์กฐ์— ์ €์žฅ๋˜๋Š” ๋ฌธ์ž์˜ ์ˆ˜์— ๋น„๋ก€ํ•ฉ๋‹ˆ๋‹ค. ๋‹จ์–ด์˜ ์ด ๊ธธ์ด๋ฅผ T๋ผ๊ณ  ํ•˜๋ฉด ๊ณต๊ฐ„๋ณต์žก๋„๋Š” O(T) ์ž…๋‹ˆ๋‹ค.
'''


class Trie:
def __init__(self):
self.children = {}
self.is_end_of_word = False

class WordDictionary:

def __init__(self):
# Trie์˜ ๋ฃจํŠธ ๋…ธ๋“œ ์ดˆ๊ธฐํ™”
self.root = Trie()

def addWord(self, word: str) -> None:
current = self.root

for char in word:
if char not in current.children:
current.children[char] = Trie()

current = current.children[char]

current.is_end_of_word = True

def search(self, word: str) -> bool:
def dfs(node, i):
if i == len(word):
return node.is_end_of_word

char = word[i]
if char == '.':
# '.'์ธ ๊ฒฝ์šฐ ๋ชจ๋“  ์ž์‹ ๋…ธ๋“œ์— ๋Œ€ํ•ด ํƒ์ƒ‰
for child in node.children.values():
if dfs(child, i + 1):
return True
return False

if char not in node.children:
return False

return dfs(node.children[char], i + 1)

return dfs(self.root, 0)



# Your WordDictionary object will be instantiated and called as such:
# obj = WordDictionary()
# obj.addWord(word)
# param_2 = obj.search(word)
40 changes: 40 additions & 0 deletions longest-increasing-subsequence/pmjuu.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
'''
Dynamic programming ํ™œ์šฉ
์‹œ๊ฐ„๋ณต์žก๋„: O(n^2) - ๋‘ ๊ฐœ์˜ ์ค‘์ฒฉ๋œ ๋ฐ˜๋ณต๋ฌธ์ด nums ๋ฐฐ์—ด์„ ํƒ์ƒ‰ํ•จ
๊ณต๊ฐ„๋ณต์žก๋„: O(n) - dp ๋ฐฐ์—ด์— ์ˆซ์ž ๊ฐœ์ˆ˜(n)๋งŒํผ ๊ณต๊ฐ„์ด ํ•„์š”ํ•จ
'''

def lengthOfLIS_n2(nums):
n = len(nums)
dp = [1] * n

for i in range(1, n):
for j in range(i):
if nums[j] < nums[i]:
dp[i] = max(dp[i], dp[j] + 1) # ์ด์ „ LIS ๊ธธ์ด์— 1 ์ถ”๊ฐ€

return max(dp) # dp ๋ฐฐ์—ด์˜ ์ตœ๋Œ€๊ฐ’์ด ์ตœ์žฅ ๊ธธ์ด


'''
์ด์ง„ํƒ์ƒ‰ ํ™œ์šฉ
์‹œ๊ฐ„๋ณต์žก๋„: O(n log n) - ๊ฐ ์ˆซ์ž์— ๋Œ€ํ•ด ์ด์ง„ ํƒ์ƒ‰(bisect_left)์„ ์ˆ˜ํ–‰ํ•จ
๊ณต๊ฐ„๋ณต์žก๋„: O(n) - sub ๋ฆฌ์ŠคํŠธ์— ์ตœ๋Œ€ n๊ฐœ์˜ ์ˆซ์ž๊ฐ€ ์ €์žฅ๋  ์ˆ˜ ์žˆ์Œ
'''

from bisect import bisect_left

def lengthOfLIS_nlogn(nums):
sub = [] # ํ˜„์žฌ๊นŒ์ง€ ์ฐพ์€ LIS์˜ ์ˆซ์ž๋“ค์„ ์ €์žฅ

for num in nums:
pos = bisect_left(sub, num) # ์‚ฝ์ž… ์œ„์น˜๋ฅผ ์ด์ง„ ํƒ์ƒ‰์œผ๋กœ ์ฐพ์Œ

if pos == len(sub):
sub.append(num) # ์‚ฝ์ž… ์œ„์น˜๊ฐ€ sub์˜ ๋ฒ”์œ„ ๋ฐ–์ด๋ฉด ์ˆซ์ž ์ถ”๊ฐ€
else:
sub[pos] = num # ์‚ฝ์ž… ์œ„์น˜๊ฐ€ ๋ฒ”์œ„ ์•ˆ์ด๋ฉด ํ•ด๋‹น ์œ„์น˜์˜ ์ˆซ์ž๋ฅผ ํ˜„์žฌ ์ˆซ์ž๋กœ ๊ต์ฒด

return len(sub)
33 changes: 33 additions & 0 deletions spiral-matrix/pmjuu.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'''
์‹œ๊ฐ„๋ณต์žก๋„: O(m * n) - ๋ชจ๋“  ํ–‰๋ ฌ์˜ ์š”์†Œ๋ฅผ ํ•œ ๋ฒˆ์”ฉ ๋ฐฉ๋ฌธ
๊ณต๊ฐ„๋ณต์žก๋„: O(1) - ์ถ”๊ฐ€ ๊ณต๊ฐ„ ์—†์ด ๊ฒฐ๊ณผ๋ฅผ ์ €์žฅ
'''
from typing import List


class Solution:
def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
result = []
top, bottom = 0, len(matrix) - 1
left, right = 0, len(matrix[0]) - 1

while top <= bottom and left <= right:
for col in range(left, right + 1):
result.append(matrix[top][col])
top += 1

for row in range(top, bottom + 1):
result.append(matrix[row][right])
right -= 1

if top <= bottom:
for col in range(right, left - 1, -1):
result.append(matrix[bottom][col])
bottom -= 1

if left <= right:
for row in range(bottom, top - 1, -1):
result.append(matrix[row][left])
left += 1

return result
23 changes: 23 additions & 0 deletions valid-parentheses/pmjuu.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'''
์‹œ๊ฐ„ ๋ณต์žก๋„: O(n)
- ๋ฌธ์ž์—ด s์˜ ๊ธธ์ด๋ฅผ n์ด๋ผ๊ณ  ํ•  ๋•Œ, ๋ฌธ์ž์—ด์˜ ๊ฐ ๋ฌธ์ž๋ฅผ ํ•œ ๋ฒˆ์”ฉ ์ˆœํšŒํ•˜๋ฉฐ ์ฒ˜๋ฆฌํ•˜๋ฏ€๋กœ O(n)์ž…๋‹ˆ๋‹ค.
๊ณต๊ฐ„ ๋ณต์žก๋„: O(n)
- ์Šคํƒ์— ์—ด๋ฆฐ ๊ด„ํ˜ธ๋ฅผ ์ €์žฅํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋˜๋Š” ๊ณต๊ฐ„์ด ์ตœ์•…์˜ ๊ฒฝ์šฐ ๋ฌธ์ž์—ด s์˜ ๊ธธ์ด n๊ณผ ๊ฐ™์„ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ O(n)์ž…๋‹ˆ๋‹ค.
'''

class Solution:
def isValid(self, s: str) -> bool:
stack = []
bracket_map = {")": "(", "}": "{", "]": "["}

for bracket in s:
if bracket in bracket_map:
if stack and stack[-1] == bracket_map[bracket]:
stack.pop()
else:
return False
else:
stack.append(bracket)

return len(stack) == 0

0 comments on commit 50a6e9e

Please sign in to comment.