Skip to content
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

[HodaeSsi] week 06 #924

Merged
merged 5 commits into from
Jan 19, 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/HodaeSsi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# 시간 복잡도 : O(n)
# 공간 복잡도 : O(1)
# 문제 유형 : Two Pointer
class Solution:
def maxArea(self, height: List[int]) -> int:
Copy link
Contributor

Choose a reason for hiding this comment

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

가능하면 4 space indent로 맞춰주시면 좋을 것 같습니다. 파이썬의 PEP8에서 권고하는 사항입니다. https://peps.python.org/pep-0008/

Copy link
Contributor Author

Choose a reason for hiding this comment

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

오오 자세한 리뷰 감사합니다!

left, right = 0, len(height) - 1
max_area = 0

while left < right:
max_area = max(max_area, (right - left) * min(height[left], height[right]))
if height[left] < height[right]:
left += 1
else:
right -= 1

return max_area

30 changes: 30 additions & 0 deletions design-add-and-search-words-data-structure/HodaeSsi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# 문제유형 : Trie
class WordDictionary:
def __init__(self):
self.trie = {}

# 시간 복잡도 : O(N)
# 공간 복잡도 : O(N)
def addWord(self, word: str) -> None:
node = self.trie
for char in word:
if char not in node:
node[char] = {}
node = node[char]
node['$'] = True
Copy link
Contributor

Choose a reason for hiding this comment

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

저 라면 '$' 처럼 클래스의 다른 메서드에서도 사용하는 상수는 클래스 레벨에서 선언해서 공유할 것 같습니다


# 시간 복잡도 : O(m^N) (m : 철자의 종류, N : 문자열의 길이)
def search(self, word: str) -> bool:
return self.dfs(0, self.trie, word)

def dfs(self, i, node, word):
if i == len(word):
return '$' in node
if word[i] == '.':
for child in node.values():
if isinstance(child, dict) and self.dfs(i + 1, child, word):
Copy link
Contributor

Choose a reason for hiding this comment

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

isinstance를 사용하기 보다, boolean이나 None을 활용하는 방식으로 Trie를 개선해 볼 수 있을 것 같습니다.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

처음엔 그렇게 했다가
dict의 자식 원소로 dict 타입과 boolean 타입이 혼재되어서
반복, iterate에서 에러가 발생하더라구요?🤔

return True
if word[i] in node:
return self.dfs(i + 1, node[word[i]], word)
return False
Comment on lines +20 to +29
Copy link
Contributor

Choose a reason for hiding this comment

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

dfs 메서드는 search 메서드에서만 사용하므로 search 메서드 내부 레벨에서 선언하면 좋을 것 같습니다

Copy link
Contributor Author

Choose a reason for hiding this comment

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

그런 코드들을 보긴했는데,
제가 자바쟁이라 함수안에 함수 정의가 어색하긴 하네요😅


20 changes: 20 additions & 0 deletions longest-increasing-subsequence/HodaeSsi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# 시간 복잡도 : O(nlogn)
# 공간 복잡도 : O(n)
# 문제 유형 : DP, Binary Search
class Solution:
def lengthOfLIS(self, nums: List[int]) -> int:
dp = []
for num in nums:
if not dp or num > dp[-1]:
dp.append(num)
else:
left, right = 0, len(dp) - 1
while left < right:
mid = left + (right - left) // 2
if dp[mid] < num:
left = mid + 1
else:
right = mid
dp[right] = num
return len(dp)

31 changes: 31 additions & 0 deletions spiral-matrix/HodaeSsi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# 시간 복잡도 : O(m * n)
# 공간 복잡도 : O(1)
# 문제 유형 : 구현
class Solution:
def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
row, col = len(matrix), len(matrix[0])
up, down, left, right = 0, row - 1, 0, col - 1
result = []
while True:
for i in range(left, right + 1):
result.append(matrix[up][i])
up += 1
if up > down:
break
for i in range(up, down + 1):
result.append(matrix[i][right])
right -= 1
if right < left:
break
for i in range(right, left - 1, -1):
result.append(matrix[down][i])
down -= 1
if down < up:
break
for i in range(down, up - 1, -1):
result.append(matrix[i][left])
left += 1
if left > right:
break
return result

16 changes: 16 additions & 0 deletions valid-parentheses/HodaeSsi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# 시간 복잡도 : O(n)
# 공간 복잡도 : O(n)
# 문제 유형 : Stack
class Solution:
def isValid(self, s: str) -> bool:
stack = []

for char in s:
if char in (')', '}', ']'):
if not stack or stack.pop() != {')': '(', '}': '{', ']': '['}[char]:
Copy link
Contributor

Choose a reason for hiding this comment

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

이 라인은 가독성도 그렇고 임시 변수가 for문을 조회하며 계속 생성될 텐데, 저라면 외부 scope에서 변수로 선언해두고 사용할 것 같습니다

return False
else:
stack.append(char)

return not stack

Loading