-
-
Notifications
You must be signed in to change notification settings - Fork 245
[hu6r1s] WEEK 06 Solutions #1869
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
Changes from all commits
8d08322
7345f01
16044ef
ca90209
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
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 |
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
from bisect import bisect_left | ||
|
||
class Solution: | ||
""" | ||
뭔가 bfs 풀이 방식 | ||
메모리 초과 | ||
""" | ||
# def lengthOfLIS(self, nums: List[int]) -> int: | ||
# n = len(nums) | ||
# q = deque() | ||
# q.append((-1, float('-inf'), 0)) # (idx, lastValue, length) | ||
# answer = 0 | ||
|
||
# while q: | ||
# idx, last, length = q.popleft() | ||
# answer = max(answer, length) | ||
|
||
# for nxt in range(idx + 1, n): | ||
# if nums[nxt] > last: | ||
# q.append((nxt, nums[nxt], length + 1)) | ||
|
||
# return answer | ||
|
||
# def lengthOfLIS(self, nums: List[int]) -> int: | ||
# dp = [1] * len(nums) | ||
# for cur in range(1, len(nums)): | ||
# for pre in range(cur): | ||
# if nums[pre] < nums[cur]: | ||
# dp[cur] = max(1 + dp[pre], dp[cur]) | ||
# return max(dp) | ||
|
||
def lengthOfLIS(self, nums: List[int]) -> int: | ||
sub = [] | ||
for num in nums: | ||
index = bisect_left(sub, num) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. index 변수에는 어떤 값이 들어가게 되는 걸까요? sub 배열에서 num 값보다 작거나 같들 중 가장 큰 값이 담기는 걸까요? 제가 파이썬이 주 언어가 아니어서 궁금해서 질문 남깁니다! |
||
if index == len(sub): | ||
sub.append(num) | ||
else: | ||
sub[index] = num | ||
return len(sub) |
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 == "[": | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 지금 괄호 종류를 if문으로 분기 처리하신 부분을 딕셔너리 자료구조로 매핑하면 더 깔끔하게 작성할 수도 있을 것 같아요! There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
주석으로 문제 분석해주셔서 쉽게 코드를 파악할 수 있었습니다. 투포인터로 문제 풀이를 하셨는데 저도 비슷하게 풀이를 해서 바로 이해했습니다.