-
-
Notifications
You must be signed in to change notification settings - Fork 245
[Helena] Week 7 #936
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
[Helena] Week 7 #936
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5ca7bd6
solve: reverseLinkedList
GaggleHelena d656073
solve: longestSubstringWithoutRepeatingCharacters
GaggleHelena c793db9
solve: numberOfIslands
GaggleHelena 80a4854
solve: uniquePaths
GaggleHelena 529ab2e
solve: setMatrixZeroes
GaggleHelena 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,24 @@ | ||
# Time Complexity: O(n): both pointers (left and right) traverse the string once. | ||
# Space Complexity: O(n): worst case, all characters are unique, so the set will store all characters. | ||
|
||
class Solution: | ||
def lengthOfLongestSubstring(self, s: str) -> int: | ||
# to store characters in the current window | ||
window_set = set() | ||
# to store the max length of a substring | ||
max_length = 0 | ||
# left pointer for the sliding window | ||
left = 0 | ||
|
||
# iterate through each char in the string using the right pointer | ||
for right in range(len(s)): | ||
# if the char is already in the window, shrink the window | ||
while s[right] in window_set: | ||
window_set.remove(s[left]) # remove the leftmost char | ||
left += 1 # move the left pointer to the right | ||
# add the new char | ||
window_set.add(s[right]) | ||
# update the max length if the current window is longer | ||
max_length = max(max_length, right - left + 1) | ||
|
||
return max_length |
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,14 @@ | ||
# Time Complexity: O(n) | ||
# Space Complexity: O(1) | ||
|
||
class Solution: | ||
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]: | ||
prev = None | ||
# to traverse the original list, starting from head | ||
current = head | ||
while current: | ||
# reverse the link and move to the next node | ||
prev, prev.next, current = current, prev, current.next | ||
|
||
# prev is now the head of the reversed list | ||
return prev |
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.
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.
고생하셨습니다!
Set 자료구조를 사용하면 worst case시 공간 복잡도가 O(n)이 되는데
ASCII 배열을 사용해서 O(1)으로 개선하는 방향에 대해서 @ekgns33님께 리뷰를 받아서 함께 공유 드립니다🙂