-
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
[mangodm-web] Week 08 Solutions #508
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
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,26 @@ | ||
from collections import defaultdict | ||
|
||
|
||
class Solution: | ||
def characterReplacement(self, s: str, k: int) -> int: | ||
""" | ||
- Idea: 슬라이딩 윈도우를 이용해 현재 윈도우 내에서 가장 자주 나타나는 문자의 개수를 추적한다. | ||
최대 k개의 문자를 바꿨을 때, 모든 문자가 같은 부분 문자열의 최대 길이를 계산한다. | ||
윈도우가 유효하지 않으면, 왼쪽 포인터를 이동시켜 윈도우 크기를 조정한다. | ||
- Time Complexity: O(n), n은 문자열의 길이다. 각 문자를 한번씩 순회하고, 슬라이딩 윈도우를 조정한다. | ||
- Space Complexity: O(26) = O(1), 슬라이딩 윈도우 내에 포함된 문자의 개수를 세기 위한 공간으로, 최대 알파벳 문자의 개수(26개)만큼 늘어날 수 있다. | ||
""" | ||
result = 0 | ||
counter = defaultdict(int) | ||
left = 0 | ||
|
||
for right in range(len(s)): | ||
counter[s[right]] = counter[s[right]] + 1 | ||
|
||
while (right - left + 1) - max(counter.values()) > k: | ||
counter[s[left]] -= 1 | ||
left += 1 | ||
|
||
result = max(result, right - left + 1) | ||
|
||
return result |
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,34 @@ | ||
from typing import Optional | ||
|
||
|
||
class ListNode: | ||
def __init__(self, val=0, next=None): | ||
self.val = val | ||
self.next = next | ||
|
||
|
||
class Solution: | ||
def mergeTwoLists( | ||
self, list1: Optional[ListNode], list2: Optional[ListNode] | ||
) -> Optional[ListNode]: | ||
""" | ||
- Idea: dummy node를 하나 만들고, 두 리스트를 순회하면서 값을 비교하여 더 작은 노드를 dummy node에 이어 붙인다. | ||
둘 중 하나가 먼저 순회가 끝났다면, 나머지 리스트의 남은 노드들을 그대로 이어 붙인다. (리스트 내에서는 순서가 정렬되어 있음이 보장되어 있기 때문에 가능하다.) | ||
- Time Complexity: O(n), n은 m + k, m과 k은 각각 list1, list2의 길이이다. | ||
- Space Complexity: O(1), 추가적인 공간은 사용하지 않고, 기존 노드를 재사용하여 연결한다. | ||
""" | ||
merged = ListNode() | ||
cur = merged | ||
|
||
while list1 and list2: | ||
if list1.val > list2.val: | ||
cur.next = list2 | ||
list2 = list2.next | ||
else: | ||
cur.next = list1 | ||
list1 = list1.next | ||
cur = cur.next | ||
|
||
cur.next = list1 or list2 | ||
|
||
return merged.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.
이렇게 하면 둘 중에 비어있지 않은 리스트를 쉽게 찾을 수 있군요! 좋은 구현 배워갑니다.