Skip to content

Commit

Permalink
Merge pull request #405 from KyrieJ95/main
Browse files Browse the repository at this point in the history
[joon] Week3 solutions
  • Loading branch information
leokim0922 authored Sep 3, 2024
2 parents 0266b08 + c80e2b7 commit d561e58
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 0 deletions.
24 changes: 24 additions & 0 deletions climbing-stairs/joon.py
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
34 changes: 34 additions & 0 deletions combination-sum/joon.py
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
28 changes: 28 additions & 0 deletions product-of-array-except-self/joon.py
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))
28 changes: 28 additions & 0 deletions two-sum/joon.py
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

0 comments on commit d561e58

Please sign in to comment.