-
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 #787 from pmjuu/main
[Lyla] WEEK 03
- Loading branch information
Showing
5 changed files
with
142 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,33 @@ | ||
from typing import List | ||
|
||
|
||
class Solution: | ||
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]: | ||
result = [] | ||
|
||
def backtrack(start, target, current_combination): | ||
# ์ข ๋ฃ ์กฐ๊ฑด | ||
if target == 0: | ||
result.append(list(current_combination)) | ||
return | ||
if target < 0: | ||
return | ||
|
||
# ๋ฐฑํธ๋ํน | ||
for i in range(start, len(candidates)): | ||
current_combination.append(candidates[i]) | ||
backtrack(i, target - candidates[i], current_combination) # ๊ฐ์ ์์๋ฅผ ์ฌ๋ฌ ๋ฒ ์ธ ์ ์๋๋ก i๋ฅผ ๊ทธ๋๋ก ๋ก๋๋ค. | ||
current_combination.pop() # ๋์๊ฐ์ ๋ค์ ์๋ํ ์ ์๋๋ก ์์๋ฅผ ์ ๊ฑฐํฉ๋๋ค. | ||
|
||
backtrack(0, target, []) | ||
|
||
return result | ||
|
||
|
||
# ์๊ฐ ๋ณต์ก๋: O(n^t) | ||
# - ํ๋ณด ๋ฆฌ์คํธ์์ ๊ฐ ์ซ์๋ฅผ ์ ํํ ์ ์๊ธฐ ๋๋ฌธ์, n๊ฐ์ ํ๋ณด๋ฅผ ์ฌ์ฉํด t๋ฒ์ ํ์์ ํ ์ ์์ต๋๋ค. | ||
# - ๋ฐ๋ผ์ ์ต์ ์ ๊ฒฝ์ฐ ํ์ ํ์๋ O(n^t)๋ก ๋ณผ ์ ์์ต๋๋ค. | ||
# | ||
# ๊ณต๊ฐ ๋ณต์ก๋: O(t) | ||
# - ์ฌ๊ท ํธ์ถ ์คํ์ ๊น์ด๋ ์ต๋ target ๊ฐ์ธ t์ ๋น๋กํ๋ฏ๋ก, ๊ณต๊ฐ ๋ณต์ก๋๋ O(t)์ ๋๋ค. | ||
# - ๋ํ, ํ์ฌ๊น์ง ์ ํ๋ ์ซ์๋ค์ ์กฐํฉ์ ์ ์ฅํ๋ ๊ณต๊ฐ๋ ์ต๋ t๊ฐ๊น์ง ์ ์ฅํ๋ฏ๋ก, ๊ณต๊ฐ ๋ณต์ก๋๋ O(t)์ ๋๋ค. |
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,20 @@ | ||
from typing import List | ||
|
||
|
||
class Solution: | ||
def maxSubArray(self, nums: List[int]) -> int: | ||
largest_sum = -float('inf') | ||
current_sum = -float('inf') | ||
|
||
for num in nums: | ||
current_sum = max(current_sum + num, num) | ||
largest_sum = max(largest_sum, current_sum) | ||
|
||
return largest_sum | ||
|
||
|
||
# ์๊ฐ ๋ณต์ก๋: O(n) | ||
# - nums ๋ฐฐ์ด์ ํ ๋ฒ ์ํํ๋ฉฐ ๊ฐ ์์์ ๋ํด ์ต๋ ๋ถ๋ถ ๋ฐฐ์ด ํฉ์ ๊ณ์ฐํ๋ฏ๋ก ์๊ฐ ๋ณต์ก๋๋ O(n)์ ๋๋ค. | ||
# | ||
# ๊ณต๊ฐ ๋ณต์ก๋: O(1) | ||
# - ์ถ๊ฐ๋ก ์ฌ์ฉํ๋ ๋ณ์๋ largest_sum๊ณผ current_sum ๋ ๊ฐ๋ฟ์ด๋ฏ๋ก ๊ณต๊ฐ ๋ณต์ก๋๋ O(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,27 @@ | ||
from typing import List | ||
|
||
|
||
class Solution: | ||
def productExceptSelf(self, nums: List[int]) -> List[int]: | ||
n = len(nums) | ||
result = [1] * n | ||
|
||
prefix = 1 | ||
for i in range(n): | ||
result[i] *= prefix | ||
prefix *= nums[i] | ||
|
||
suffix = 1 | ||
for i in range(-1, -n-1, -1): | ||
result[i] *= suffix | ||
suffix *= nums[i] | ||
|
||
return result | ||
|
||
|
||
# ์๊ฐ ๋ณต์ก๋: O(n) | ||
# - ์ ๋ ฅ ๋ฐฐ์ด nums๋ฅผ ๋ ๋ฒ ์ํํฉ๋๋ค. | ||
# | ||
# ๊ณต๊ฐ ๋ณต์ก๋: O(1) | ||
# - ์ถ๊ฐ ๊ณต๊ฐ์ผ๋ก ์ฌ์ฉํ๋ ๋ณ์๋ prefix์ suffix๋ฟ์ด๋ฉฐ, | ||
# ์ถ๋ ฅ ๋ฐฐ์ด(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,42 @@ | ||
class Solution: | ||
def reverseBits(self, n: int) -> int: | ||
# ์ด์ง์๋ก ๋ณํํ ํ '0b' ์ ๊ฑฐ | ||
binary = bin(n)[2:] | ||
# 32๋นํธ ๊ธธ์ด์ ๋ง๊ฒ ์์ชฝ์ 0์ ์ฑ์ | ||
binary = binary.zfill(32) | ||
# ์ด์ง์๋ฅผ ๋ค์ง์ | ||
reversed_binary = binary[::-1] | ||
# ๋ค์งํ ์ด์ง์๋ฅผ ์ ์๋ก ๋ณํํ์ฌ ๋ฐํ | ||
return int(reversed_binary, 2) | ||
|
||
|
||
# ์๊ฐ ๋ณต์ก๋: O(32) | ||
# - bin(): O(32) | ||
# - zfill(32): O(32) | ||
# - ๋ฌธ์์ด ๋ค์ง๊ธฐ [::-1]: O(32) | ||
# - int(๋ฌธ์์ด, 2): O(32) | ||
# ์ดํฉ: O(32) (์์ ์๊ฐ์ผ๋ก ๊ฐ์ฃผ ๊ฐ๋ฅ) | ||
|
||
# ๊ณต๊ฐ ๋ณต์ก๋: O(32) | ||
# - ์ด์ง ๋ฌธ์์ด(binary)์ ๋ค์งํ ๋ฌธ์์ด(reversed_binary)์ ์ ์ฅํ๋ฏ๋ก O(32). | ||
|
||
|
||
class Solution: | ||
def reverseBits(self, n: int) -> int: | ||
result = 0 | ||
|
||
for i in range(32): | ||
# result๋ฅผ ์ผ์ชฝ์ผ๋ก 1๋นํธ ์ด๋ํ๊ณ n์ ๋ง์ง๋ง ๋นํธ๋ฅผ ์ถ๊ฐ | ||
result = (result << 1) | n & 1 | ||
# n์ ์ค๋ฅธ์ชฝ์ผ๋ก 1๋นํธ ์ด๋ | ||
n >>= 1 | ||
|
||
return result | ||
|
||
|
||
# ์๊ฐ ๋ณต์ก๋: O(32) | ||
# - ๋ฐ๋ณต๋ฌธ์ด 32๋ฒ ์คํ๋๋ฉฐ ๊ฐ ์์ (๋นํธ ์ด๋ ๋ฐ OR ์ฐ์ฐ)์ O(1). | ||
# ์ดํฉ: O(32) (์์ ์๊ฐ์ผ๋ก ๊ฐ์ฃผ ๊ฐ๋ฅ) | ||
|
||
# ๊ณต๊ฐ ๋ณต์ก๋: O(1) | ||
# - ์ถ๊ฐ๋ก ์ฌ์ฉํ๋ ๋ณ์ result์ n๋ง ์ ์ฅํ๋ฏ๋ก ์์ ๊ณต๊ฐ. |
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,20 @@ | ||
from typing import List | ||
|
||
|
||
class Solution: | ||
def twoSum(self, nums: List[int], target: int) -> List[int]: | ||
subtract_map = {} | ||
|
||
for i, num in enumerate(nums): | ||
if num in subtract_map: | ||
return [i, subtract_map[num]] | ||
else: | ||
subtract_map[target - num] = i | ||
|
||
|
||
# ์๊ฐ ๋ณต์ก๋: O(n) | ||
# - nums ๋ฐฐ์ด์ ํ ๋ฒ ์ํํ๋ฉฐ ๊ฐ ์์๋ฅผ ํ์ธํ๋ฏ๋ก ์๊ฐ ๋ณต์ก๋๋ O(n)์ ๋๋ค. | ||
# | ||
# ๊ณต๊ฐ ๋ณต์ก๋: O(n) | ||
# - ์ถ๊ฐ๋ก ์ฌ์ฉํ๋ subtract_map ๋์ ๋๋ฆฌ์๋ ์ต์ ์ ๊ฒฝ์ฐ nums ๋ฐฐ์ด์ ๋ชจ๋ ์์๊ฐ ์ ์ฅ๋๋ฏ๋ก | ||
# ๊ณต๊ฐ ๋ณต์ก๋๋ O(n)์ ๋๋ค. |