From d7ac18fc6499632ada1b311981c259e1e608cd8a Mon Sep 17 00:00:00 2001 From: KyrieJ95 Date: Sun, 1 Sep 2024 02:24:45 +0900 Subject: [PATCH 1/4] Add a solution to two-sum problem. --- two-sum/joon.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 two-sum/joon.py diff --git a/two-sum/joon.py b/two-sum/joon.py new file mode 100644 index 000000000..ce06d9b3b --- /dev/null +++ b/two-sum/joon.py @@ -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 From a70a9fe1a44235471946fcdda34665e1eb0966cb Mon Sep 17 00:00:00 2001 From: KyrieJ95 Date: Sun, 1 Sep 2024 02:57:27 +0900 Subject: [PATCH 2/4] Add a solution to climbing-stairs problem. --- climbing-stairs/joon.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 climbing-stairs/joon.py diff --git a/climbing-stairs/joon.py b/climbing-stairs/joon.py new file mode 100644 index 000000000..a8687220d --- /dev/null +++ b/climbing-stairs/joon.py @@ -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 From 4e2753da859c48129ce6cea92f4c09bdbe80ce99 Mon Sep 17 00:00:00 2001 From: KyrieJ95 Date: Sun, 1 Sep 2024 03:47:24 +0900 Subject: [PATCH 3/4] Add a solution to product-of-array-except-self problem. --- product-of-array-except-self/joon.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 product-of-array-except-self/joon.py diff --git a/product-of-array-except-self/joon.py b/product-of-array-except-self/joon.py new file mode 100644 index 000000000..dc9281d26 --- /dev/null +++ b/product-of-array-except-self/joon.py @@ -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)) From e8a268e121a03c5b62ddf41eea311c86e50c7cf2 Mon Sep 17 00:00:00 2001 From: KyrieJ95 Date: Sun, 1 Sep 2024 04:30:11 +0900 Subject: [PATCH 4/4] Add a solution to combination-sum problem. --- combination-sum/joon.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 combination-sum/joon.py diff --git a/combination-sum/joon.py b/combination-sum/joon.py new file mode 100644 index 000000000..c588d5291 --- /dev/null +++ b/combination-sum/joon.py @@ -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