Skip to content

[sejineer] Week 06 solutions #1415

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions container-with-most-water/sejineer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
"""
시간 복잡도: O(N)
공간 복잡도: O(1)
"""
class Solution:
def maxArea(self, height: List[int]) -> int:
result = 0
start, end = 0, len(height) - 1

while start < end:
area = (end - start) * min(height[start], height[end])
result = max(result, area)
if height[start] < height[end]:
start += 1
else:
end -= 1
return result
26 changes: 26 additions & 0 deletions design-add-and-search-words-data-structure/sejineer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
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)
elif ch == ".":
return any(dfs(node[k], idx + 1) for k in node if k != "$")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any() 로 pythonic 하게 처리한 점 좋아요!

else:
return False
return dfs(self.root, 0)
16 changes: 16 additions & 0 deletions longest-increasing-subsequence/sejineer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""
시간 복잡도: O(Nlog(N))
공간 복잡도: O(N)
"""
from bisect import bisect_left

class Solution:
def lengthOfLIS(self, nums: List[int]) -> int:
sub = []
for num in nums:
index = bisect_left(sub, num)
if index == len(sub):
sub.append(num)
else:
sub[index] = num
return len(sub)
29 changes: 29 additions & 0 deletions spiral-matrix/sejineer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
"""
시간 복잡도: O(n * m)
공간 복잡도: O(n * m)
"""
class Solution:
def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
n, m = len(matrix), len(matrix[0])
dx = [1, 0, -1, 0]
dy = [0, 1, 0, -1]
state = 0
vis = [[False] * m for _ in range(n)]

result = [matrix[0][0]]
vis[0][0] = True
x, y = 0, 0

while len(result) < n * m:
nx = x + dx[state % 4]
ny = y + dy[state % 4]

if not (0 <= nx < m) or not (0 <= ny < n) or vis[ny][nx]:
state += 1
continue

vis[ny][nx] = True
result.append(matrix[ny][nx])
x, y = nx, ny

return result
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

vis 배열 없는 풀이도 참고하시면 좋을 것 같아요!

class Solution:
    def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
        if not matrix or not matrix[0]:
            return []

        result = []
        top, bottom = 0, len(matrix) - 1
        left, right = 0, len(matrix[0]) - 1

        while left <= right and top <= bottom:
            result += matrix[top][left:right + 1]
            top += 1

            result += [matrix[i][right] for i in range(top, bottom + 1)]
            right -= 1

            if top <= bottom:
                result += matrix[bottom][right:left - 1:-1] if right >= left else []
                bottom -= 1

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

        return result

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

감사합니다! 도움 많이 됐습니다 ㅎㅎ

21 changes: 21 additions & 0 deletions valid-parentheses/sejineer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
"""
시간 복잡도: O(N)
공간 복잡도: O(N)
"""
class Solution:
def isValid(self, s: str) -> bool:
pair = {'(': ')', '{': '}', '[': ']'}
stack = []
for c in s:
if c in ('(', '{', '['):
stack.append(c)
else:
if stack:
cur = stack.pop()
if pair[cur] == c:
continue
else:
return False
else:
return False
return not stack