Skip to content

Week3 #404

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed

Week3 #404

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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