-
-
Notifications
You must be signed in to change notification settings - Fork 245
[TONY] WEEK 06 Solutions #465
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
Changes from 1 commit
Commits
Show all changes
5 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 hidden or 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,18 @@ | ||
// TC: O(n) | ||
// -> n = s.length | ||
// SC: O(n) | ||
// -> n = s.length / 2 | ||
class Solution { | ||
public boolean isValid(String s) { | ||
Stack<Character> stack = new Stack<>(); | ||
|
||
for (char c : s.toCharArray()) { | ||
if (c == '(') stack.add(')'); | ||
else if (c == '{') stack.add('}'); | ||
else if (c == '[') stack.add(']'); | ||
else if (stack.isEmpty() || stack.pop() != c) return false; | ||
} | ||
|
||
return stack.isEmpty(); | ||
} | ||
} |
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.
stack에 데이터가 들어가는 경우는 현재 값이 (, {, [ 인 경우 뿐이고, 최악의 경우
(((({{{[[[
같은 값이 주어지더라도, 여전히 상수값이기에 공간 복잡도는 O(n)이라고 생각합니다!Uh oh!
There was an error while loading. Please reload this page.
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.
답변 감사합니다 토니님 :)
O(N)의 형태로 사용 공간이 증가한다는 것은 이해 및 동의합니다
여전히 제가 이해를 하지 못한 부분이 있어서 더 질문 드리고 싶습니다
n = s.length / 2
이 부분에 대해서 더 자세히 설명 부탁 드려도 될까요?최악의 경우(((({{{[[[ 같은 값이 주어지더라도, 여전히 상수값
에서상수값
이라는 표현에 대해 잘 이해하지 못했습니다.1 <= s.length <= 10^4
이라는 입력값 제한 조건이 있는데, 이 중에서10^4
를 상수라고 표현하신 걸까요?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.
@obzva 스택은 열린 괄호가 들어올 때마다 저장되고 닫힌 괄호가 들어올 때 제거되므로, 스택의 최대 크기는 열린 괄호의 개수만큼이 될겁니다. 따라서, 공간 복잡도는 최악의 경우 O( s.length() / 2) 라고 생각되지만 공간 복잡도계산시 보통 상수를 무시하므로 O(n) 이라고 생각합니다.
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.
아하 감사합니다 ㅎㅎㅎ
그치만
s
자체가((((((((((((((((((((((((((((((
같은 값이 주어진다면 최악의 경우엔 공간 사용량이s.length() / 2
가 아닌s.length()
아닌가요? 결국 아무 char도 pop되지 않고 스택에 쌓이기만 할테니까요