-
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 #506 from haklee/main
[haklee] week 8
- Loading branch information
Showing
5 changed files
with
377 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,79 @@ | ||
"""TC: O(n + e), SC: - | ||
λ Έλ κ°μ nκ°, μ£μ§ κ°μ eκ° | ||
μμ΄λμ΄: | ||
λ¬Έμ μ€λͺ λΆν°κ° deepcopyλ₯Ό νλΌλ κ²μ΄λ λ΄μ₯ν¨μλ₯Ό μ¨μ deepcopyλ₯Ό ν΄μ£Όμ. | ||
SC: | ||
- λ΄μ₯ν¨μκ° νμν 곡κ°λ€μ λ°λ‘ μ κ΄λ¦¬ν΄μ£Όμ§ μμκΉ? μλ§ λ³μλ₯Ό μ½κ³ κ·Έλλ‘ λ¦¬ν΄κ°μΌλ‘ λ°κΏμ€λ―. | ||
- κ·Έλ λ€λ©΄ μΆκ°μ μΌλ‘ κ΄λ¦¬νλ 곡κ°μ νμ μλ€. | ||
TC: | ||
- deepcopyλ νμν μ 보λ₯Ό κ·Έλλ‘ λ€ deepcopy λ° λΏμ΄λ€. μλ§ node κ°μ + edge κ°μμ λΉλ‘ν΄μ μκ°μ΄ | ||
κ±Έλ¦΄κ² κ°λ€. O(n + e). | ||
""" | ||
|
||
""" | ||
# Definition for a Node. | ||
class Node: | ||
def __init__(self, val = 0, neighbors = None): | ||
self.val = val | ||
self.neighbors = neighbors if neighbors is not None else [] | ||
""" | ||
|
||
import copy | ||
from typing import Optional | ||
|
||
|
||
class Solution: | ||
def cloneGraph(self, node: Optional["Node"]) -> Optional["Node"]: | ||
return copy.deepcopy(node) | ||
|
||
|
||
"""TC: O(e), SC: O(e) | ||
λ Έλ κ°μ nκ°, μ£μ§ κ°μ eκ° | ||
μμ΄λμ΄: | ||
dfs λλ©΄μ λ Έλλ€μ λ©λͺ¨ν΄λμ. neighborsμ νΉμ λ Έλλ₯Ό μΆκ°ν΄μΌ ν λ λ©λͺ¨μ μμΌλ©΄ λ°λ‘ κ°μ Έλ€ | ||
μ°κ³ , μμΌλ©΄ μλ‘ λ§λ€μ΄μ λ©λͺ¨μ λ Έλλ₯Ό μΆκ°νλ€. | ||
SC: | ||
- λ Έλ μ΄ nκ°κ° memoμ μ¬λΌκ°λ€. O(n). | ||
- κ° λ Έλλ§λ€ neighborκ° μλ€. κ° edgeλ§λ€ neighbor 리μ€νΈλ€μ μ΄ μμ΄ν κ°μμ 2κ°μ© κΈ°μ¬νλ€. O(e). | ||
- λνλ©΄ O(n + e). μ¦, λ μ€ λ ν° κ°μ΄ 곡κ°λ³΅μ‘λλ₯Ό μ§λ°°νλ€. | ||
...κ³ μκ°νλ κ²μ΄ μΌμ°¨μ μΈ λΆμμΈλ°, μ¬κΈ°μ λ λμκ° μ μλ€. | ||
- μ£Όμ΄μ§ 쑰건μ λ°λ₯΄λ©΄ μ°λ¦¬μκ² μ£Όμ΄μ§ κ·Έλνλ connected graphλ€. μ¦, μ£μ§ κ°μκ° n-1κ° μ΄μμ΄λ€. | ||
- μ¦, O(n) < O(e)κ° λ¬΄μ‘°κ±΄ μ±λ¦½νλ―λ‘, O(e) < O(n + e) < O(e + e) = O(e). μ¦, O(e). | ||
TC: | ||
- SCμ λΉμ·ν λ°©μμΌλ‘ λΆμ κ°λ₯. O(e). | ||
""" | ||
|
||
""" | ||
# Definition for a Node. | ||
class Node: | ||
def __init__(self, val = 0, neighbors = None): | ||
self.val = val | ||
self.neighbors = neighbors if neighbors is not None else [] | ||
""" | ||
|
||
from typing import Optional | ||
|
||
|
||
class Solution: | ||
def cloneGraph(self, node: Optional["Node"]) -> Optional["Node"]: | ||
if node is None: | ||
return node | ||
|
||
memo = {} | ||
|
||
def dfs(node): | ||
if node not in memo: | ||
new_node = Node(node.val, []) | ||
memo[node] = new_node | ||
new_node.neighbors = [dfs(i) for i in node.neighbors] | ||
return memo[node] | ||
|
||
return dfs(node) |
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,64 @@ | ||
"""TC: O(m * n), SC: O(m * n) | ||
μ£Όμ΄μ§ λ¬Έμμ΄μ κΈΈμ΄λ₯Ό κ°κ° m, nμ΄λΌκ³ νμ. | ||
μμ΄λμ΄: | ||
- λ λ¬Έμμ΄μ΄ μ£Όμ΄μ‘λλ° λμ΄ κ°μ λ¬ΈμλΌκ³ νμ. μ΄ κ²½μ° lcsμ κΈΈμ΄λ κ°κ°μ λ¬Έμμ΄μμ | ||
λ λ¬Έμλ₯Ό μ κ±°ν λ¬Έμμ΄λ‘ lcsμ κΈΈμ΄λ₯Ό ꡬν κ°μ 1μ λν κ°μ΄λ€. | ||
- e.g.) abcz, bcdefzμ lcs κΈΈμ΄λ₯Ό `x`λΌκ³ νλ€λ©΄, | ||
abc/z, bcdef/zμμ λμ zκ° κ°μ λ¬ΈμλκΉ μ΄κ² lcsμ λ€μ΄κ°λ€ μΉ μ μμΌλ―λ‘, | ||
abc, bcdefμ lcs κΈΈμ΄λ `x - 1`μ΄ λλ€. | ||
- λ λ¬Έμμ΄μ λ λ¬Έμκ° λ€λ₯Ό κ²½μ°, 첫 λ²μ§Έ λ¬Έμμ΄μ λ λ¬Έμλ₯Ό μ κ±°νκ³ κ΅¬ν lcsμ κΈΈμ΄λ | ||
λ λ²μ§Έ λ¬Έμμ΄μ λ λ¬Έμλ₯Ό μ κ³ νκ³ κ΅¬ν lcsμ κΈΈμ΄ λ μ€ ν° κ°μ΄ μλ λ¬Έμμ΄λ‘ ꡬν lcs | ||
μ κΈΈμ΄λ€. | ||
- e.g.) abcz, bcdefyμ lcs κΈΈμ΄λ₯Ό `x`λΌκ³ νλ€λ©΄, | ||
abc, bcdefyμ lcs κΈΈμ΄μ | ||
abcz, bcdefμ lcs κΈΈμ΄ | ||
λ μ€ λ ν° κ°μ μ·¨νλ©΄ λλ€. | ||
- LCSλ μ λͺ ν μκ³ λ¦¬μ¦μ΄λ―λ‘ μμ μ€λͺ μ μκ°μ μΌλ‘ μ ννν μμλ€μ μ¨λΌμΈμμμ μ½κ² | ||
μ°Ύμ μ μλ€. | ||
- μμ μμ΄λμ΄λ₯Ό μ νμμΌλ‘ λ°κΎΈλ©΄ | ||
- 첫 λ²μ§Έ λ¬Έμμ΄μ μ iκΈμλ‘ λ§λ λ¬Έμμ΄κ³Ό λ λ²μ§Έ λ¬Έμμ΄μ μ jκΈμλ‘ λ§λ λ¬Έμμ΄μ | ||
lcsμ κΈΈμ΄λ₯Ό lcs(i, j)λΌκ³ νμ. | ||
- 첫 λ²μ§Έ λ¬Έμμ΄μ iλ²μ§Έ κΈμμ λ λ²μ§Έ λ¬Έμμ΄μ jλ²μ§Έ κΈμκ° κ°μ κ²½μ° λ€μμ μμ΄ μ±λ¦½. | ||
- lcs(i, j) = lcs(i-1, j-1) + 1 | ||
- λ€λ₯Ό κ²½μ°, λ€μμ μμ΄ μ±λ¦½. | ||
- lcs(i, j) = max(lcs(i-1, j), lcs(i, j-1)) | ||
- μμ μμ΄λμ΄λ₯Ό memoizeλ₯Ό νλ dpλ₯Ό ν΅ν΄ ꡬνν μ μλ€. μμΈν λ΄μ©μ μ½λ μ°Έμ‘°. | ||
SC: | ||
- 첫 λ²μ§Έ λ¬Έμμ΄μ μ iκΈμλ‘ λ§λ λ¬Έμμ΄κ³Ό λ λ²μ§Έ λ¬Έμμ΄μ μ jκΈμλ‘ λ§λ λ¬Έμμ΄μ lcsμ | ||
κΈΈμ΄λ₯Ό κ΄λ¦¬. | ||
- κ·Έλ°λ° μμ΄λμ΄μ μ μλ μ νμμ 보면 i, jκ°μ λν μ 체 λ°°μ΄μ μ μ₯ν νμ μμ΄ i=kμΌλ | ||
κ°μ ꡬνλ € νλ€λ©΄ i=k-1μΌλ ꡬν lcsκ°λ§ μκ³ μμΌλ©΄ μΆ©λΆνλ€. | ||
- μ¦, λ°°μ΄μ νμ¬ κ΅¬νκ³ μ νλ iκ°μ λν jκ°μ μμ΄ν κ³Ό μ§μ μ ꡬν jκ°μ μμ΄ν λ§ μ μ₯νλ©΄ | ||
μΆ©λΆνλ€. μ¦, text2μ κΈΈμ΄μΈ O(n)μ΄λΌκ³ λ³Ό μ μλ€. | ||
- κ·Έλ°λ° λ§μ½ text2μ κΈΈμ΄κ° text1λ³΄λ€ κΈΈλ©΄ μ΄ λμ λ°κΏμΉκΈ°ν΄μ μμ 곡κ°λ³΅μ‘λλ₯Ό O(m)μ΄λΌκ³ | ||
λ΄λ μμ΄λμ΄ μ체λ λκ°μ΄ μλνμ§ μλκ°? | ||
- μ¦, O(min(m, n)) | ||
TC: | ||
- dp λ°°μ΄μ μ±μ°λ λ°μ λ§μ§λ§ λ¬Έμκ° κ°μ κ²½μ° λ¨μ λ§μ , λ€λ₯Ό κ²½μ° λ κ° λΉκ΅. λ λ€ O(1). | ||
- λ°°μ΄ μ±μ°λ κ²μ m * nν λ°λ³΅νλ―λ‘ μ΄ O(m * n). | ||
""" | ||
|
||
|
||
class Solution: | ||
def longestCommonSubsequence(self, text1: str, text2: str) -> int: | ||
if len(text2) > len(text1): | ||
# μ΄ μ΅μ νκΉμ§ ν΄μ£Όλ©΄ μ¬μ©νλ λ©λͺ¨λ¦¬ ν¬κΈ°κ° λ§μ΄ μ€μ΄λ€ μ μλ€. | ||
text1, text2 = text2, text1 | ||
|
||
dp = [[0 for _ in range(len(text2) + 1)] for _ in range(2)] | ||
|
||
for i in range(1, len(text1) + 1): | ||
i_prev = (i + 1) % 2 | ||
i_cur = i % 2 | ||
for j in range(1, len(text2) + 1): | ||
dp[i_cur][j] = ( | ||
dp[i_prev][j - 1] + 1 | ||
if text1[i - 1] == text2[j - 1] | ||
else max(dp[i_prev][j], dp[i_cur][j - 1]) | ||
) | ||
|
||
return dp[i_cur][-1] |
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,36 @@ | ||
"""TC: O(n), SC: O(1) | ||
nμ μ£Όμ΄μ§ λ¬Έμμ΄μ κΈΈμ΄ | ||
μμ΄λμ΄: | ||
- ν¬ ν¬μΈν°λ₯Ό μ¨μ λ¬Έμμ΄μ μμ, λμ κ΄λ¦¬νλ©΄μ λΆλΆ λ¬Έμμ΄μ λ§λ λ€. | ||
- λΆλΆ λ¬Έμμ΄μ λ€μ΄μλ λ¬Έμ μ€ κ°μ₯ λ§μ λ¬Έμμ kμ ν©μ΄ λ¬Έμμ΄μ κΈΈμ΄ λ³΄λ€ ν¬λ©΄ 쑰건 λ§μ‘±. | ||
- λΆλΆ λ¬Έμμ΄μ λ€μ΄μλ λ¬Έμ κ°μλ₯Ό dictλ₯Ό μ¨μ κ΄λ¦¬νμ. | ||
SC: | ||
- λΆλΆ λ¬Έμμ΄μ λ€μ΄μλ λ¬Έμ κ°μλ₯Ό κ΄λ¦¬νλ dictμμ O(1). | ||
- λΆλΆ λ¬Έμμ΄μ μμ, λ μΈλ±μ€ κ΄λ¦¬ O(1). | ||
- μ’ ν©νλ©΄ O(1). | ||
TC: | ||
- λΆλΆ λ¬Έμμ΄μ λ μΈλ±μ€λ₯Ό νλ λ릴 λλ§λ€ λ°νν κ° μ λ°μ΄νΈ. O(1)μ nλ² μννλ―λ‘ O(n). | ||
- μμ, λ μΈλ±μ€ μμ ν λλ§λ€ λΆλΆ λ¬Έμμ΄μ λ€μ΄μλ λ¬Έμ κ°μ μ λ°μ΄νΈ. μμ, λ μΈλ±μ€λ | ||
λ§μ΄ μμ ν΄λ΄μΌ ν©μ³μ 2*nλ². μ¦, O(1)μ 2*nλ² μννλ―λ‘ O(n). | ||
- μμ, λ μΈλ±μ€μ 1μ λνλ μν. O(1)μ 2*nλ² μννλ―λ‘ O(n). | ||
- μ’ ν©νλ©΄ O(n). | ||
""" | ||
|
||
|
||
class Solution: | ||
def characterReplacement(self, string: str, k: int) -> int: | ||
char_cnt = {c: 0 for c in set(string)} | ||
s = e = 0 | ||
sol = -1 | ||
while e < len(string): | ||
char_cnt[string[e]] += 1 | ||
while e - s + 1 > max(char_cnt.values()) + k: | ||
char_cnt[string[s]] -= 1 | ||
s += 1 | ||
sol = max(e - s + 1, sol) | ||
e += 1 | ||
return sol |
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,65 @@ | ||
"""TC: O(n), SC: - | ||
nμ μ£Όμ΄μ§ λ 리μ€νΈμ κΈΈμ΄ μ€ ν° κ° | ||
μμ΄λμ΄: | ||
- μ£Όμ΄μ§ 쑰건μ μν΄ λ 리μ€νΈμ λ€μ΄μλ κ°λ€μ non-decreasingμ΄λ―λ‘, μλ‘μ΄ λ¦¬μ€νΈλ₯Ό λ§λ€κ³ | ||
λ 리μ€νΈμ μμ μλ κ° μ€ μμ κ°μ νλμ© λ½μμ λν΄μ£Όλ©΄ λλ€. | ||
- λΉ λ¦¬μ€νΈκ° μ£Όμ΄μ§ μ μλ κ²λ§ μ μνμ. | ||
SC: | ||
- νΉλ³ν κ΄λ¦¬νλ κ°μ΄ μλ€. | ||
TC: | ||
- λͺ¨λ λ Έλμ ν λ²μ© μ κ·Όν΄μ 리ν΄ν κ°μ μ΄μ΄μ€λ€. μ΄μ΄μ£Όλ μνλ§λ€ O(1). | ||
- 리ν΄ν κ°μ μ λ Έλλ₯Ό μΆκ°ν λλ§λ€ κ° λΉκ΅λ₯Ό ν λ²μ© νλ€. O(1). | ||
- nμ΄ λ 리μ€νΈ κΈΈμ΄ μ€ ν° κ°μ΄λ―λ‘ μ΄μ΄μ£Όλ μνμ xλ n <= x <= 2*n λ§μ‘±. | ||
- μ¦, μ΄ O(n). | ||
""" | ||
|
||
# Definition for singly-linked list. | ||
# 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]: | ||
# 1. init head | ||
# - λ 리μ€νΈλ₯Ό λ³΄κ³ νΉμ νλλΌλ λΉμ΄μμΌλ©΄ λ€λ₯Έ 리μ€νΈλ₯Ό 리ν΄νλ€. | ||
# - λ λ€ λΉμ΄μμ§ μμ κ²½μ° μ²« μμ΄ν μ λ³΄κ³ λ μ€ μμ κ°μ κ²°κ³Όλ¬Όμ 첫 μμ΄ν μΌλ‘ μ. | ||
if list1 is None: | ||
return list2 | ||
if list2 is None: | ||
return list1 | ||
# μ¬κΈ° λλ¬νμΌλ©΄ λ λ€ μ΅μν ν μμ΄ν μ©μ μ‘΄μ¬. | ||
sol = None | ||
if list1.val < list2.val: | ||
sol = ListNode(list1.val) | ||
list1 = list1.next | ||
else: | ||
sol = ListNode(list2.val) | ||
list2 = list2.next | ||
|
||
sol_head = sol | ||
|
||
# 2. add item | ||
# - μμ κ³Όμ μ λΉμ·νκ² λ°λ³΅νλ€. | ||
while True: | ||
# μΈμ κ° λ μ€ ν 리μ€νΈλ λΉκ² λλ―λ‘ λ¬΄ν 루νλ₯Ό λμ§ μλλ€. | ||
if list1 is None: | ||
sol_head.next = list2 | ||
return sol | ||
if list2 is None: | ||
sol_head.next = list1 | ||
return sol | ||
|
||
if list1.val < list2.val: | ||
sol_head.next = ListNode(list1.val) | ||
list1 = list1.next | ||
else: | ||
sol_head.next = ListNode(list2.val) | ||
list2 = list2.next | ||
|
||
sol_head = sol_head.next |
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,133 @@ | ||
"""TC: O(1), SC: O(n^2) | ||
-n <= a, b <= n κ°μ . | ||
λ¬Έμ μμλ nμ΄ 1000μΌλ‘ μ£Όμ΄μ Έμλ€κ³ λ³Ό μ μλ€. | ||
μμ΄λμ΄: | ||
λ§μ μ λͺ» μ°κ² νλ€λ©΄ μ μ²λ¦¬λ₯Ό ν΅ν΄ λͺ¨λ a, b μ‘°ν©μ λν λ§μ κ°μ λ§λ€μ΄λκ³ a, bμ κ°μ λ°λΌμ | ||
νμν κ°μ μΆλ ₯νλλ‘ νμ. pythonμμλ μμλ‘ λ μΈλ±μ€λ₯Ό μ§μνλ―λ‘ μ΄λ₯Ό νμ©νμ. | ||
- μλμ μ½λλ₯Ό ν΅ν΄μ μ μ²λ¦¬λ κ°μ μ€λΉνλ€. μ΄ μ½λλ leetcodeμμ μ€νλμ§ μμΌλ―λ‘ λνκΈ°λ₯Ό | ||
μ¨λ μκ΄ μλ€. | ||
```py | ||
n = 3 | ||
with open("foo.txt", "w") as f: | ||
a = [ | ||
[ | ||
(i if i <= n else i - 2 * n - 1) + (j if j <= n else j - 2 * n - 1) | ||
for j in range(0, 2 * n + 1) | ||
] | ||
for i in range(0, 2 * n + 1) | ||
] | ||
f.write(str(a)) | ||
``` | ||
SC: | ||
- O(n^2). μ ννλ, (2*n+1)^2 κ°μ μ μλ₯Ό λ°°μ΄μ μ μ₯νλ€. | ||
TC: | ||
- μΈλ±μ€λ₯Ό ν΅ν΄ λ°λ‘ μ κ·Ό. O(1). | ||
""" | ||
|
||
|
||
class Solution: | ||
# n = 3μΌλ μμ. | ||
def getSum(self, a: int, b: int) -> int: | ||
x = [ | ||
[0, 1, 2, 3, -3, -2, -1], | ||
[1, 2, 3, 4, -2, -1, 0], | ||
[2, 3, 4, 5, -1, 0, 1], | ||
[3, 4, 5, 6, 0, 1, 2], | ||
[-3, -2, -1, 0, -6, -5, -4], | ||
[-2, -1, 0, 1, -5, -4, -3], | ||
[-1, 0, 1, 2, -4, -3, -2], | ||
] | ||
return x[a][b] | ||
|
||
|
||
# λ¨, n = 1000μΌλ μ΄λ° μμΌλ‘ μ½λλ₯Ό μ§λ €κ³ νλ©΄ | ||
# `For performance reasons, the number of characters per line is limited to 10,000.` | ||
# νλ 문ꡬμ ν¨κ» 리μ€νΈλ₯Ό 볡λΆν μκ° μλ€... | ||
|
||
|
||
"""TC: O(n), SC: O(n) | ||
-n <= a, b <= n κ°μ . | ||
λ¬Έμ μμλ nμ΄ 1000μΌλ‘ μ£Όμ΄μ Έμλ€κ³ λ³Ό μ μλ€. | ||
μμ΄λμ΄: | ||
μ μ²λ¦¬ ν κ²μ κ°μ Έμ€λ λ°©λ²μ λͺ» μ°κ² λμμ§λ§, μΈλ±μ€λ₯Ό ν΅ν μ κ·Όμ μμ§ κ·Έλλ‘ μ°κ³ μΆλ€. | ||
- λ¬Έμ μ 쑰건μ λ°κΏμ 0 <= a, b <= nλΌκ³ ν΄λ³΄μ. κ·Έλ¦¬κ³ nμ΄ 3μ΄λΌκ³ ν΄λ³΄μ. | ||
- aκ° 0μΌλ‘ κ³ μ λμ΄ μλ€λ©΄, λ€μκ³Ό κ°μ λ°°μ΄μ΄ μ£Όμ΄μ‘μλ a+bμ κ°μ μΈλ±μ€λ‘ μ κ·Όν μ μλ€. | ||
- v = [0, 1, 2, 3] μΌλ a + b κ°μ v[b] | ||
- aκ° 1λ‘ κ³ μ λμ΄ μλ€λ©΄, | ||
- v = [1, 2, 3, 4] μΌλ a + b κ°μ v[b] | ||
- aκ° 2λ‘ κ³ μ λμ΄ μλ€λ©΄, | ||
- v = [2, 3, 4, 5] μΌλ a + b κ°μ v[b] | ||
- aκ° 3μΌλ‘ κ³ μ λμ΄ μλ€λ©΄, | ||
- v = [3, 4, 5, 6] μΌλ a + b κ°μ v[b] | ||
- μμ λ°°μ΄μ 보면 κ²ΉμΉλ μ«μλ€μ΄ λ§λ€. κ·Έλ λ€λ©΄ 0~6κΉμ§ μ«μλ€μ΄ λ€μ΄μλ λ°°μ΄μ slicingν΄μ | ||
μ°λ©΄ λμ§ μμκΉ? | ||
- aκ° 0μΌλ v = [0, 1, 2, 3, 4, 5, 6] μ€ | ||
[0, 1, 2, 3] μ¬μ©. | ||
μ¦, v[0:4] μ¬μ©. | ||
- aκ° 1μΌλ v = [0, 1, 2, 3, 4, 5, 6] μ€ | ||
[1, 2, 3, 4] μ¬μ©. | ||
μ¦, v[1:5] μ¬μ©. | ||
... | ||
- μΌλ°ννλ©΄, v[a:a+n+1] μ¬μ©. μ΄λ a+b κ°μ v = list(range(0, 2 * n + 1))μΌλ v[a:a+n+1][b]. | ||
- κ·Έλ°λ° v[a:a+n+1][b]λ₯Ό 보면 μ¬λΌμ΄μ± νλ λΆλΆμμ + κΈ°νΈλ₯Ό μ¬μ©νλ€. | ||
- κ·Έλ λ€λ©΄ μ κΈ°μ λνκΈ° κΈ°νΈλ₯Ό μ¬μ©ν νμ μμ΄ μ¬λΌμ΄μ±μ μμ κ°κ³Ό λ κ°λ 미리 리μ€νΈλ‘ λ§λ€κ³ , | ||
μ΄ λ¦¬μ€νΈμμ aλ²μ§Έ μμ΄ν μ κ°μ Έμ€λ λ°©μμ νμ©ν΄λ³΄μ. | ||
- s = [0, 1, 2, 3], e = [4, 5, 6, 7]μΌλ, v[a:a+n+1][b] = v[s[a]:e[a]][b]κ° λλ€. | ||
- μΌλ°ννλ©΄, s = list(range(0, n)), e = list(range(n+1, 2*n+1))μ΄λ©΄ λλ€. | ||
- eλ₯Ό λ§λ€λ©΄μ λνκΈ°λ₯Ό μ΄ κ²μ²λΌ 보μ΄μ§λ§, μ€μ λ‘λ nμ΄ μ£Όμ΄μ§ μμμ΄λ―λ‘ κ°μ κ³μ°ν΄μ λ£μΌλ©΄ λλ€. | ||
- μλ₯Ό λ€μ΄, n=3μΌλ e = list(range(4, 7))μ΄λ€. | ||
ν° μμ΄λμ΄λ μμ λ°©μμΌλ‘ μ€λͺ μ΄ λλ¬λ€. μ΄μ λ¬Έμ λ 0 <= a, b <= nμ΄ μλλΌ -n <= a, b <= n λ²μμμλ | ||
μμ λ°©μμ΄ μλνλλ‘ νλ κ²μΈλ°, λ¨Όμ aκ°μ μμ λ²μμ λκ³ bκ°λ§ μμλ‘ νμ₯ν μνμμ vλ₯Ό ꡬν΄λ³΄κ³ , | ||
κ·Έ λ€μ aλ μμκΉμ§ νμ₯νλ μμΌλ‘ μ κ·Όνλ©΄ λλ€. μμΈν μ€λͺ μ μλ΅νκ³ , λ λ€ μμ λ²μκΉμ§ νμ₯ν λ€ | ||
μ€μ λ‘ μλνλ κ²°κ³Όλ¬Όμ μ€λͺ νλ κ²μΌλ‘ λμ νκ² λ€. | ||
- nμ 3μ΄λΌκ³ κ°μ νκ² λ€. | ||
- v = [0, 1, 2, 3, 4, 5, 6, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, -6, -5, -4, -3, -2, -1] | ||
- list(range(0, n+1))μ list(range(-n, 0))μ μ΄μ΄λΆμΈ κ²μ λ λ² λ°λ³΅νλ€. | ||
- λ λ² λ°λ³΅ν μ΄μ λ aκ°μ΄ μμκ° λ μν©μ λμνκΈ° μν¨μ΄λ€. μλμ μ€λͺ μ μ΄μ΄μ 보λλ‘ νμ. | ||
- s = list(range(0, 4 * n + 1)) = list(range(0, 13))μ΄λ€. | ||
- μ΄λ κ² νλ©΄ aκ° μμκ° λμμλλ slicingμ μμν μΈλ±μ€λ μμλ‘ μ μ§ν μ μλ€. | ||
- bλ₯Ό 0μΌλ‘ κ³ μ νλ©΄ slicingμ μμνλ μΈλ±μ€μ μλ μμ΄ν μ΄ a+bμ κ°μ΄ λμ΄μΌ νλ€. | ||
- μ΄λ₯Ό μμ vμ κ°μ΄ μκ°νλ©΄ vμ μ 13κ° μμ΄ν μ μ·¨ν [0, 1, 2, 3, 4, 5, 6, -6, -5, -4, -3, -2, -1] | ||
λ°°μ΄μ΄ μμλ aκ° μ·¨ν μ μλ κ°μ λ²μλ -3~3μ΄λ―λ‘ [0, 1, 2, 3, ..., -3, -2, -1] μ€ νλλΆν° | ||
slicingμ΄ μμλλ€κ³ 보면 λλ€. | ||
- κ·Έλ¬λκΉ μ½κ² λ§ν΄μ aμ λ²μλ‘ μΈν΄ slicingμ΄ μμ΄ν 4λΆν° μμν΄μ [4, 5, 6, -6, ...] νλ μΌμ΄ | ||
μΌμ΄λμ§λ μλλ€λ λ». | ||
- slicingν λ°°μ΄μ ν¬κΈ°λ 4*n+1μ΄μ΄μΌ νλ€. eλ sμ κ° μμ΄ν μ 4*n+1μ λν κ°μ΄λ©΄ λλ€. | ||
- 4*n+1μ κ΄μ°°μ ν΅ν΄ μ»μ μ μλ κ°μ΄λ€.a, bμ ν©μ μ΅μκ° -2*n, μ΅λκ° 2*nμ΄μ΄μ κ·Έ μ¬μ΄μ μλ | ||
μ«μλ€μ΄ μ΄ 4*n+1κ° μλ€λ κ²μμ λΉλ‘―λ μ«μλ€. | ||
- λμ μμλ₯Ό 보면 μ΄ν΄κ° μ’ λ νΈνλ€. | ||
- μ 리νλ©΄, e = list(range(4*n+1, 8*n+2)) = list(range(13, 26))μ΄λ€. | ||
- a+b κ°μ v[s[a] : e[a]][b] λ‘ κ΅¬ν μ μλ€. | ||
- μλ₯Ό λ€μ΄, a=2, b=-3μ΄λΌκ³ ν λ | ||
- v[s[a] : e[a]] = v[2:15] = [2, 3, 4, 5, 6, -6, -5, -4, -3, -2, -1, 0, 1]λ€. | ||
- bκ° -3μ΄λ―λ‘ μμ slicingλ λ°°μ΄μμ λ€μμ μΈ λ²μ§Έ μμ΄ν μ μ°ΎμΌλ©΄ λλ€. μ¦, -1μ΄λ€. | ||
- μ κ΄μ°°νλ©΄ λ§μ μ κ²°κ³Όκ° λ μ μλ κ°μ [2, 3, 4, 5, ..., -1, 0, 1] λ°μ μλ€. μ¬μ΄μ μλ μ«μλ | ||
bμ λ²μκ° μ νλμ΄ μμ΄μ μ κ·Ό λΆκ°λ₯ν, μ¦, νμ μλ μ«μλ€μ΄λΌκ³ 보면 λλ€. | ||
SC: | ||
- μ½λ μ°Έμ‘°. O(n). | ||
TC: | ||
- μΈλ±μ€λ₯Ό ν΅ν΄ λ°λ‘ μ κ·Ό. O(1). | ||
""" | ||
|
||
|
||
class Solution: | ||
def getSum(self, a: int, b: int) -> int: | ||
x = list(range(0, 2001)) | ||
x.extend(list(range(-2000, 0))) | ||
v = x * 2 # SC: O(n) | ||
s = list(range(0, 4001)) # SC: O(n) | ||
e = list(range(4001, 8002)) # SC: O(n) | ||
return v[s[a] : e[a]][b] |