-
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 #405 from KyrieJ95/main
[joon] Week3 solutions
- Loading branch information
Showing
4 changed files
with
114 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,24 @@ | ||
from typing import Dict | ||
|
||
|
||
class Solution: | ||
# Time: O(n) | ||
# Space: O(n) | ||
# --- | ||
# 1. Prepare a table for caching. Initially put trivial answers for n = 1, 2. | ||
# Space: O(n) | ||
cacheTableOfAnswerByN: Dict[int, int] = {1: 1, 2: 2} | ||
|
||
def climbStairs(self, n: int) -> int: | ||
# 2. Use caching. | ||
# Time: O(1) | ||
try: return self.cacheTableOfAnswerByN[n] | ||
except KeyError: | ||
# 3. Simply, the answers follow Fibonacci sequence. Use recursion. | ||
# O(n) | ||
answerBeforeTwoSteps = self.climbStairs(n - 2) | ||
# 4. Cache the answer during recursion. | ||
self.cacheTableOfAnswerByN[n - 2] = answerBeforeTwoSteps | ||
answerBeforeOneStep = self.climbStairs(n - 1) | ||
self.cacheTableOfAnswerByN[n - 1] = answerBeforeOneStep | ||
return answerBeforeTwoSteps + answerBeforeOneStep |
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 List | ||
from typing import Set | ||
from typing import Tuple | ||
|
||
|
||
class Solution: | ||
target: int | ||
candidates: List[int] | ||
answers: Set[Tuple[int]] | ||
|
||
# Time: O(k^N) | ||
# Space: O(N^2) | ||
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]: | ||
self.target = target | ||
self.candidates = candidates | ||
self.answers = set() | ||
self.findAnswers(0, []) | ||
return list(self.answers) | ||
|
||
def findAnswers(self, currentSum: int, nums: List[int]): | ||
assert currentSum < self.target, "Given sum should be smaller than the target." | ||
for num in self.candidates: | ||
if currentSum + num < self.target: | ||
# 1. Continue finding. | ||
# Time: O(k^N) | ||
# Space: O(N^2). Max total size of all "nums" = 1 + 2 + ... + N. | ||
self.findAnswers(currentSum + num, nums + [num]) | ||
if currentSum + num > self.target: | ||
# 2. Stop finding. | ||
continue | ||
if currentSum + num == self.target: | ||
# 3. Add the answer. | ||
self.answers.add(tuple(sorted(list(nums + [num])))) | ||
return |
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,28 @@ | ||
from typing import List | ||
from functools import reduce | ||
|
||
|
||
class Solution: | ||
# Time: O(n) | ||
# Space: O(n) | ||
def productExceptSelf(self, nums: List[int]) -> List[int]: | ||
size: int = len(nums) | ||
if size == 1: | ||
return [1] | ||
# 1. Cut the array at the middle. | ||
# Time: O(1) | ||
# Space: O(n) | ||
indexToCut: int = len(nums) // 2 | ||
left: List[int] = nums[0:indexToCut] | ||
right: List[int] = nums[indexToCut:size] | ||
|
||
# 2. Divide, conquer, and merge. But no benefit in complexity. | ||
# Time: O(n) | ||
# Space: O(n) | ||
return self.multiplyToAll(self.productExceptSelf(left), self.calculateProductAll(right)) + self.multiplyToAll(self.productExceptSelf(right), self.calculateProductAll(left)) | ||
|
||
def calculateProductAll(self, nums: List[int]) -> int: | ||
return reduce(lambda x, y: x * y, nums) | ||
|
||
def multiplyToAll(self, nums: List[int], multiplier: int) -> List[int]: | ||
return list(map(lambda num: multiplier * num, nums)) |
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,28 @@ | ||
from typing import List | ||
from typing import Dict | ||
|
||
|
||
class Solution: | ||
# Time: O(n) | ||
# Space: O(n) | ||
def twoSum(self, nums: List[int], target: int) -> List[int]: | ||
# 1. Make a map {target - num : index of num} with the list nums. | ||
# Time: O(n) | ||
# Space: O(n) | ||
mapOfSubtractedValueToIndex: Dict[int, int] = ({ | ||
target - num: index for index, num in enumerate(nums) | ||
}) | ||
|
||
# 2. ForEach num in nums, get value from the map. | ||
# Time: O(n) | ||
# Space: O(n) | ||
result: List[int] = None | ||
for indexOfNum, num in enumerate(nums): | ||
try: | ||
indexOfSubtractedValue = mapOfSubtractedValueToIndex[num] | ||
if indexOfSubtractedValue == indexOfNum: | ||
continue | ||
# 3. Return the index of num in nums and the value from the map. | ||
return [indexOfNum, indexOfSubtractedValue] | ||
except KeyError: | ||
continue |