-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #508 from mangodm-web/main
[mangodm-web] Week 08 Solutions
- Loading branch information
Showing
2 changed files
with
60 additions
and
0 deletions.
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 |