-
-
Notifications
You must be signed in to change notification settings - Fork 339
[gyeo-ri] WEEK 04 Solutions #2486
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 all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a5769ca
feat: 속도는 빠르지만 복잡한 로직
gyeo-ri 2ffbb37
fix: 로직은 유지한 채로 가독성 / 메모리 사용을 개선
gyeo-ri 55c4df4
fix: 최적 알고리즘 사용하여 가독성 개선
gyeo-ri 5f97cdc
Merge pull request #4 from gyeo-ri/valid-parentheses
gyeo-ri 467eb00
Merge branch 'DaleStudy:main' into main
gyeo-ri 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,63 @@ | ||
| """ | ||
| [결과 요약] | ||
| # 재시도횟수: 3회 | ||
| 1. 조건에는 맞지만 메모리를 많이 사용 / 빈번한 False 리턴을 사용한 방법:: O(n)/O(n) | ||
| 2. 1의 로직의 가독성과 메모리 사용량 개선하기: O(n)/O(n) | ||
| 3. 실제로는 if/else문으로 풀어도 최적의 성능이 나오면서 가독성 측면에서도 유리: : O(n)/O(n) | ||
| """ | ||
|
|
||
|
|
||
| class Solution: | ||
| def isValid(self, s: str) -> bool: | ||
| character_map = {"(": ")", "{": "}", "[": "]"} | ||
| s_list = [] | ||
|
|
||
| for c in s: | ||
| if c in character_map: | ||
| s_list.append(character_map[c]) | ||
|
|
||
| else: | ||
| if len(s_list) > 0: | ||
| if s_list.pop() == c: | ||
| continue | ||
| return False | ||
|
|
||
| return len(s_list) == 0 | ||
|
|
||
|
|
||
| """ | ||
| # 실제로는 복잡한 로직 없이 if/else로 직접 비교하는 쪽이 성능과 코드 가독성 면에서 모두 유리 | ||
| class Solution: | ||
| def isValid(self, s: str) -> bool: | ||
| s_list = [] | ||
|
|
||
| for c in s: | ||
| if c == '(': | ||
| s_list.append(')') | ||
| elif c == '{': | ||
| s_list.append('}') | ||
| elif c == '[': | ||
| s_list.append(']') | ||
| else: | ||
| if not s_list or s_list.pop() != c: | ||
| return False | ||
| """ | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| test_cases = [ | ||
| ("{{{[[[(([{{}}]))]]]}}}", True), | ||
| ("{{{]}}", False), | ||
| ("{", False), | ||
| ("}}]", False), | ||
| ("{}[]()", True), | ||
| ("{[]}[()]", True), | ||
| ] | ||
|
|
||
| solution = Solution() | ||
| for idx, case_ in enumerate(test_cases): | ||
| s, answer = case_ | ||
| result = solution.isValid(s) | ||
| assert ( | ||
| answer == result | ||
| ), f"Test Case {idx} Failed: Expected {answer}, Got {result}" | ||
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.
Uh oh!
There was an error while loading. Please reload this page.