-
-
Notifications
You must be signed in to change notification settings - Fork 361
[okyungjin] WEEK 07 Solutions #2804
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
+74
−0
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
24 changes: 24 additions & 0 deletions
24
longest-substring-without-repeating-characters/okyungjin.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,24 @@ | ||
| """ | ||
| N: `s`의 길이, M: `s`에서 중복을 제외한 문자의 개수 | ||
| Time: O(N) | ||
| Space: O(min(N,M)) | ||
| """ | ||
| class Solution: | ||
| def lengthOfLongestSubstring(self, s: str) -> int: | ||
| # 문자의 최근 인덱스를 저장 | ||
| char_map = {} | ||
|
|
||
| left = 0 | ||
| max_len = 0 | ||
|
|
||
| for right, char in enumerate(s): | ||
| if char in char_map and char_map[char] >= left: | ||
| left = char_map[char] + 1 | ||
|
|
||
| char_map[char] = right | ||
|
|
||
| curr_len = right - left + 1 | ||
| if curr_len > max_len: | ||
| max_len = curr_len | ||
|
|
||
| return max_len |
|
Contributor
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. 🏷️ 알고리즘 패턴 분석reverse-linked-list/okyungjin.py# https://leetcode.com/problems/reverse-linked-list/
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
"""
Time: O(N)
Space O(N)
"""
class Solution:
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
if not head:
return None
stack = []
while head:
stack.append(head)
head = head.next
dummy_head = ListNode()
curr = dummy_head
while stack:
curr.next = stack.pop()
curr = curr.next
curr.next = None
return dummy_head.next
"""
Time: O(N)
Space O(1)
"""
class Solution:
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
prev = None
curr = head
while curr:
temp = curr.next
curr.next = prev
prev, curr = curr, temp
return prev
📊 시간/공간 복잡도 분석
풀이 1:
|
| 복잡도 | |
|---|---|
| Time | O(n) |
| Space | O(n) |
피드백: 스택에 노드를 차례대로 저장한 뒤 역순으로 재연결하여 역순 리스트를 만듭니다.
개선 제안: 현재 구현이 적절해 보입니다.
풀이 2: Solution.reverseList — Time: O(n) / Space: O(1)
| 복잡도 | |
|---|---|
| Time | O(n) |
| Space | O(1) |
피드백: 링크드 리스트를 한 번 순회하며 포인터를 역전시켜 공간 복잡도를 상수로 줄였습니다.
개선 제안: 현재 구현이 적절해 보입니다.
💡 풀이에 시간/공간 복잡도를 주석으로 남겨보세요!
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,50 @@ | ||
| # https://leetcode.com/problems/reverse-linked-list/ | ||
|
|
||
| # Definition for singly-linked list. | ||
| # class ListNode: | ||
| # def __init__(self, val=0, next=None): | ||
| # self.val = val | ||
| # self.next = next | ||
|
|
||
| """ | ||
| Time: O(N) | ||
| Space O(N) | ||
| """ | ||
| class Solution: | ||
| def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]: | ||
| if not head: | ||
| return None | ||
|
|
||
| stack = [] | ||
|
|
||
| while head: | ||
| stack.append(head) | ||
| head = head.next | ||
|
|
||
| dummy_head = ListNode() | ||
| curr = dummy_head | ||
|
|
||
| while stack: | ||
| curr.next = stack.pop() | ||
| curr = curr.next | ||
|
|
||
| curr.next = None | ||
|
|
||
| return dummy_head.next | ||
|
|
||
|
|
||
| """ | ||
| Time: O(N) | ||
| Space O(1) | ||
| """ | ||
| class Solution: | ||
| def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]: | ||
| prev = None | ||
| curr = head | ||
|
|
||
| while curr: | ||
| temp = curr.next | ||
| curr.next = prev | ||
| prev, curr = curr, temp | ||
|
|
||
| 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.
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.
🏷️ 알고리즘 패턴 분석
longest-substring-without-repeating-characters/okyungjin.py
📊 시간/공간 복잡도 분석
피드백: 문자의 최근 위치를 해시 맵에 저장하고, 창(left..right)에서 중복이 발견되면 left를 중복 문자 바로 뒤로 이동시켜 중복 없이 길이를 확장한다.
개선 제안: 현재 구현이 적절해 보입니다.