-
-
Notifications
You must be signed in to change notification settings - Fork 245
[hi-rachel] Week 06 Solutions #1418
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
6 commits
Select commit
Hold shift + click to select a range
5f63a86
valid-parentheses solution (py, ts)
hi-rachel ff5dad5
fix: 파일명 lint
hi-rachel ea0e395
container-with-most-water solution (ts, py)
hi-rachel 2706b2a
design-add-and-search-words-data-structure solution (py, ts)
hi-rachel d7bb9cb
spiral-matrix solution (py)
hi-rachel 9cf961f
longest-increasing-subsequence solution (py, ts)
hi-rachel 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,72 @@ | ||
// TC: O(N^2), SC: O(N) | ||
// function isValid(s: string): boolean { | ||
// let prevLength = -1; | ||
|
||
// while (s.length !== prevLength) { | ||
// prevLength = s.length; | ||
// s = s.replace("()", "").replace("[]", "").replace("{}", ""); | ||
// } | ||
|
||
// return s.length === 0; | ||
// } | ||
|
||
/** | ||
* STACK 풀이 | ||
* TC: O(N), SC: O(N) | ||
* 스택 방식 작동 순서 | ||
* for (const char of s) { | ||
if (여는 괄호) { | ||
스택에 push | ||
} else { | ||
스택에서 pop한 값이 char의 짝이 아니면 return false | ||
} | ||
} | ||
*/ | ||
function isValid(s: string): boolean { | ||
const stack: string[] = []; | ||
const parentheseMap: Record<string, string> = { | ||
")": "(", | ||
"]": "[", | ||
"}": "{", | ||
}; | ||
|
||
for (const char of s) { | ||
if (["(", "[", "{"].includes(char)) { | ||
stack.push(char); | ||
} else { | ||
if (stack.pop() !== parentheseMap[char]) return false; | ||
} | ||
} | ||
return stack.length === 0; | ||
} | ||
|
||
/** | ||
* Python과 JS에서의 pop() 메서드 차이 | ||
* JS 같은 로직을 Python으로 바꾸면 Python에서만 다음과 같은 테스트 케이스 오류가 발생 | ||
* TEST CASE: "]", "){", ")(){}" | ||
* | ||
* [원인] | ||
* if (stack.pop() !== parentheseMap[char]) return false; 비교시 | ||
* JavaScript에서는 빈 스택 pop() -> undefined 반환으로 비교 가능! | ||
* Python에서는 빈 스택 pop() -> IndexError -> 오류 발생 | ||
* | ||
* [해결책] - 예외 처리 필요 | ||
* pop 전에 not stack 체크 꼭 해주기 | ||
* not stack -> 스택이 비어 있다면 잘못된 닫는 괄호 먼저 나온 경우 | ||
*/ | ||
|
||
// PY 풀이 | ||
// class Solution: | ||
// def isValid(self, s: str) -> bool: | ||
// parentheseMap = {")" : "(", "]": "[", "}": "{"} | ||
// stack = [] | ||
// open_parenthese = "([{" | ||
|
||
// for char in s: | ||
// if char in open_parenthese: | ||
// stack.append(char) | ||
// else: | ||
// if (not stack or stack.pop() != parentheseMap[char]): | ||
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. 파이썬에서는 스택에 pop하기 전 반드시 비어있는지 확인해주어야 한다는 걸 저도 이번 문제 풀면서 새롭게 알게 되었습니다. 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. @river20s 두 가지 언어의 차이를 느낄 수 있었던 풀이였습니다. |
||
// return False | ||
|
||
// 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.
파이썬 풀이도 함께 적어주셔서 TS를 사용해본 적 없음에도 알고리즘을 잘 이해할 수 있었습니다.👍