-
-
Notifications
You must be signed in to change notification settings - Fork 245
[prograsshopper] week 7 solutions #1891
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
2 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
38 changes: 38 additions & 0 deletions
38
longest-substring-without-repeating-characters/prograsshopper.py
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,38 @@ | ||
class Solution: | ||
def lengthOfLongestSubstring(self, s: str) -> int: | ||
# sol 1 | ||
# time complexity: O(n^2) / memory complexity: O(n) | ||
if len(s) in [0, 1]: | ||
return len(s) | ||
|
||
from collections import defaultdict | ||
strings = [] | ||
for i in range(0, len(s) - 1): | ||
char_dict = defaultdict(int) | ||
char_dict[s[i]] += 1 | ||
for j in range(i + 1, len(s)): | ||
char_dict[s[j]] += 1 | ||
if char_dict[s[j]] > 1: | ||
strings.append(s[i:j]) | ||
break | ||
else: | ||
strings.append(s[i:]) | ||
|
||
max_len = len(strings[0]) | ||
for elem in strings: | ||
max_len = max(max_len, len(elem)) | ||
return max_len | ||
|
||
# sol 2 | ||
# time complexity: O(n) / memory complexity: O(n) | ||
str_set = set() | ||
left = 0 | ||
max_len = 0 | ||
|
||
for right in range(len(s)): | ||
while s[right] in str_set: | ||
str_set.remove(s[left]) | ||
left += 1 | ||
str_set.add(s[right]) | ||
max_len = max(max_len, right - left + 1) | ||
return max_len |
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,20 @@ | ||
# Definition for singly-linked list. | ||
# class ListNode: | ||
# def __init__(self, val=0, next=None): | ||
# self.val = val | ||
# self.next = next | ||
class Solution: | ||
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]: | ||
# time complexity: O(n) / memory complexity: O(n) | ||
stack = [] | ||
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. 스택을 활용한 방식도 좋지만, 추가 메모리 없이 in-place로 처리하는 방법도 많이 사용되니 참고 바랍니다. |
||
current = head | ||
while current: | ||
stack.append(current.val) | ||
current = current.next | ||
|
||
dummy_head = ListNode() | ||
current = dummy_head | ||
while stack: | ||
current.next = ListNode(val=stack.pop(), next=None) | ||
current = current.next | ||
return dummy_head.next |
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.
주석으로 시간/공간 복잡도를 잘 명시해주셔서 이해에 도움이 되네요!