-
Notifications
You must be signed in to change notification settings - Fork 126
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
[KwonNayeon] Week 6 #894
Merged
Merged
[KwonNayeon] Week 6 #894
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
""" | ||
Constraints: | ||
1. n == height.length | ||
2. 2 <= n <= 10^5 | ||
3. 0 <= height[i] <= 10^4 | ||
|
||
Time Complexity: | ||
- | ||
|
||
Space Complexity: | ||
- | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
""" | ||
Constraints: | ||
1. 1 <= s.length <= 10^4 | ||
2. s consists of parentheses only '()[]{}' | ||
|
||
Time Complexity: O(n) | ||
- 문자열의 각 문자를 한 번씩만 순회하므로 O(n) | ||
- 각 문자에 대한 연산(push, pop)은 O(1) | ||
|
||
Space Complexity: O(n) | ||
- 최악의 경우 모든 문자가 여는 괄호일 때 스택에 모두 저장 | ||
- 따라서 입력 크기에 비례하는 O(n) 공간 필요 | ||
|
||
풀이방법: | ||
1. 스택을 사용하여 여는 괄호('(', '{', '[')를 저장 | ||
2. Dictionary를 사용해 닫는 괄호와 여는 괄호의 쌍을 O(1)로 매칭 | ||
3. 문자열을 순회하면서: | ||
- 여는 괄호는 스택에 추가 | ||
- 닫는 괄호가 나오면: | ||
a) 스택이 비어있거나 | ||
b) 스택 최상단의 괄호가 현재 닫는 괄호와 매칭되지 않으면 | ||
-> 잘못된 괄호 문자열 | ||
- 매칭되는 경우 스택에서 pop | ||
4. 모든 순회가 끝난 후 스택이 비어있어야 올바른 괄호 문자열 | ||
""" | ||
class Solution: | ||
def isValid(self, s: str) -> bool: | ||
stack = [] | ||
pairs = {')': '(', '}': '{', ']': '['} | ||
|
||
for char in s: | ||
if char in '({[': | ||
stack.append(char) | ||
else: | ||
if not stack or stack[-1] != pairs[char]: | ||
return False | ||
stack.pop() | ||
|
||
return len(stack) == 0 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
혹시 문제 풀이가 올라오지 않은 걸까요 :)?
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.
@TonyKim9401 리더님 안녕하세요! 이번 주는 너무 신경을 못 썼네요... 다음 주부터는 분발하겠습니다 댓글 감사합니다!