-
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
[SunaDu] Week 6 #901
Merged
+258
−0
Merged
[SunaDu] Week 6 #901
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c0204ce
add solution: valid-parentheses
dusunax 85c2967
add solution: container-with-most-water
dusunax ee6f6e4
add solution: design-add-and-search-words-data-structure
dusunax 0bdc50c
add solution: longest-increasing-subsequence
dusunax b8a39d6
add solution: spiral-matrix
dusunax 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
Next
Next commit
Loading status checks…
add solution: valid-parentheses
commit c0204ce8fd5b813fa55f3420ca449ff45de6c4e0
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,41 @@ | ||
''' | ||
# 20. Valid Parentheses | ||
use stack data structure to perform as a LIFO | ||
## Time and Space Complexity | ||
``` | ||
TC: O(n) | ||
SC: O(n) | ||
``` | ||
#### TC is O(n): | ||
- iterating through the string just once to check if the parentheses are valid. = O(n) | ||
#### SC is O(n): | ||
- using a stack to store the parentheses. = the worst case is O(n) | ||
- using a map to store the parentheses. = O(1) | ||
> for space complexity, fixed space is O(1). | ||
> 👉 parentheses_map is fixed and its size doesn't grow with the input size. | ||
> 👉 if the map has much larger size? the space complexity is still O(1). | ||
''' | ||
|
||
class Solution: | ||
def isValid(self, s: str) -> bool: | ||
stack = [] # SC: O(n) | ||
parentheses_map = { # SC: O(1) | ||
"(": ")", | ||
"{": "}", | ||
"[": "]" | ||
} | ||
|
||
for char in s: # TC: O(n) | ||
if char in parentheses_map: | ||
stack.append(char) | ||
else: | ||
if len(stack) == 0 or parentheses_map[stack.pop()] != char: | ||
return False | ||
|
||
return not stack |
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.
map 을 사용하지 않고 푸는 방법도 도전해도시면 재미있을것 같아요 :)