Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
20 changes: 20 additions & 0 deletions container-with-most-water/hu6r1s.py
Copy link
Contributor

Choose a reason for hiding this comment

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

주석으로 문제 분석해주셔서 쉽게 코드를 파악할 수 있었습니다. 투포인터로 문제 풀이를 하셨는데 저도 비슷하게 풀이를 해서 바로 이해했습니다.

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

Copy link
Contributor

Choose a reason for hiding this comment

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

from typing import List를 추가하면 함수의 타입 힌트 관련 에러를 제거할 수 있습니다!

class Solution:
"""
1. 브루트포스와 같이 전부 길이를 대조해보고 하면 시간복잡도가 터질 것.
투포인터 방식을 활용하면 됨. 투포인터에 대한 문제가 코딩테스트로 많이 나올 것 같음.
확실하게 공부 필요.
"""
def maxArea(self, height: List[int]) -> int:
max_area = 0
start, end = 0, len(height) - 1
while start < end:
area = (end - start) * min(height[start], height[end])
max_area = max(area, max_area)

if height[start] <= height[end]:
start += 1
else:
end -= 1

return max_area
35 changes: 35 additions & 0 deletions design-add-and-search-words-data-structure/hu6r1s,py
Copy link
Contributor

Choose a reason for hiding this comment

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

파일명에 오타가 있는 거 같습니다! ",py"를 ".py" 바꿔야할 거 같습니다.

Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
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}
Copy link
Contributor

Choose a reason for hiding this comment

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

단어의 마지막 단어라는 것을 $ 키로 표시를 하셨는데 이게 끝을 의미하는 특수문자여서 그런걸까요?

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)


# Your WordDictionary object will be instantiated and called as such:
# obj = WordDictionary()
# obj.addWord(word)
# param_2 = obj.search(word)
23 changes: 23 additions & 0 deletions valid-parentheses/hu6r1s.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class Solution:
"""
1. 스택을 활용한 분기 처리
여는 괄호일 때는 스택에 무조건 넣어준다.
닫는 괄호일 때는 대, 중, 소 괄호에 맞춰서 분기를 해줘야 한다.
스택이 있고, 스택의 마지막이 해당 괄호의 여는 괄호이면 빼내준다.
이외는 닫힌 괄호가 먼저 나오는 것이기 때문에 False를 반환해준다.
전형적인 스택 문제로 O(n)의 시간복잡도를 가진다.
"""
def isValid(self, s: str) -> bool:
stack = []
for word in s:
if word == "(" or word == "{" or word == "[":
Copy link
Contributor

Choose a reason for hiding this comment

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

지금 괄호 종류를 if문으로 분기 처리하신 부분을 딕셔너리 자료구조로 매핑하면 더 깔끔하게 작성할 수도 있을 것 같아요!
예를 들어 brackets = {')': '(', ']': '[', '}': '{'}
이런 식으로 매핑해두면, 조건문을 여러 개 쓰지 않고도 brackets[word] == stack[-1] 식으로 바로 비교할 수 있습니다.
한 번 참고해보시면 좋을 것 같아요 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

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

그런 방법이 있겠네요! 감사합니다~

stack.append(word)
elif stack and word == ")" and stack[-1] == "(":
stack.pop()
elif stack and word == "]" and stack[-1] == "[":
stack.pop()
elif stack and word == "}" and stack[-1] == "{":
stack.pop()
else:
return False
return True if not stack else False