From ab85227a70e46f1f72cb0ebff5c09a56cb3b2bd0 Mon Sep 17 00:00:00 2001 From: Dusuna <94776135+dusunax@users.noreply.github.com> Date: Mon, 23 Dec 2024 22:25:10 +0900 Subject: [PATCH 1/6] add solution: two-sum --- two-sum/dusunax.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 two-sum/dusunax.py diff --git a/two-sum/dusunax.py b/two-sum/dusunax.py new file mode 100644 index 000000000..fa188e8d9 --- /dev/null +++ b/two-sum/dusunax.py @@ -0,0 +1,33 @@ +''' +# 1. Two Sum + +use a hash map to store the numbers and their indices. +iterate through the list and check if the complement of the current number (target - nums[i]) is in the hash map. + +(assume that each input would have exactly one solution) +- if it is a pairNum, return the indices of the two numbers. +- if it is not, add the current number and its index to the hash map. + + +## Time and Space Complexity + +``` +TC: O(n) +SC: O(n) +``` + +#### TC is O(n): +- iterating through the list just once to find the two numbers. = O(n) + +#### SC is O(n): +- using a hash map to store the numbers and their indices. = O(n) +''' + +class Solution: + def twoSum(self, nums: List[int], target: int) -> List[int]: + map = {} + for i in range(len(nums)): + pairNum = target - nums[i] + if pairNum in map: + return [map.get(pairNum), i] + map[nums[i]] = i \ No newline at end of file From 8c1043eb8eef5caf4a093ae7d63dba24a20add28 Mon Sep 17 00:00:00 2001 From: Dusuna <94776135+dusunax@users.noreply.github.com> Date: Tue, 24 Dec 2024 01:10:46 +0900 Subject: [PATCH 2/6] fix: add line breaks --- two-sum/dusunax.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/two-sum/dusunax.py b/two-sum/dusunax.py index fa188e8d9..2377329c2 100644 --- a/two-sum/dusunax.py +++ b/two-sum/dusunax.py @@ -30,4 +30,4 @@ def twoSum(self, nums: List[int], target: int) -> List[int]: pairNum = target - nums[i] if pairNum in map: return [map.get(pairNum), i] - map[nums[i]] = i \ No newline at end of file + map[nums[i]] = i From 1580f7193b1b644acabf6768e3f3e2517b3a0f54 Mon Sep 17 00:00:00 2001 From: Dusuna <94776135+dusunax@users.noreply.github.com> Date: Wed, 25 Dec 2024 20:03:52 +0900 Subject: [PATCH 3/6] add solution: reverse-bits --- reverse-bits/dusunax.py | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 reverse-bits/dusunax.py diff --git a/reverse-bits/dusunax.py b/reverse-bits/dusunax.py new file mode 100644 index 000000000..808d3efe4 --- /dev/null +++ b/reverse-bits/dusunax.py @@ -0,0 +1,41 @@ +''' +# 190. Reverse Bits + +SolutionA: using bin() and int() to convert the types. +SolutionB: using bitwise operations to reverse the bits. + +## Time and Space Complexity + +### SolutionA +``` +TC: O(32) -> O(1) +SC: O(1) +``` + +### SolutionB +``` +TC: O(32) -> O(1) +SC: O(1) +``` +''' +class Solution: + ''' + SolutionA + - using bin() and int() to convert the number to binary and back to integer. + - use .zfill(32) ensures that the binary string is always 32 bits long. + ''' + def reverseBitsA(self, n: int) -> int: + bit = bin(n)[2:].zfill(32) + return int(bit[::-1], 2) + + ''' + SolutionB + - using bitwise operations to reverse the bits. + - iterate through the bits and reverse them. + ''' + def reverseBitsB(self, n: int) -> int: + result = 0 + for i in range(32): + result = (result << 1) | (n & 1) # shift the result to the left & add LSB of n + n >>= 1 # shift n to the right & remove previous LSB + return result From 78d9149346256ecbe04ef09a777447bf87c0aeb8 Mon Sep 17 00:00:00 2001 From: Dusuna <94776135+dusunax@users.noreply.github.com> Date: Thu, 26 Dec 2024 23:14:53 +0900 Subject: [PATCH 4/6] add solution: product-of-array-except-self --- product-of-array-except-self/dusunax.py | 34 +++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 product-of-array-except-self/dusunax.py diff --git a/product-of-array-except-self/dusunax.py b/product-of-array-except-self/dusunax.py new file mode 100644 index 000000000..27c13e828 --- /dev/null +++ b/product-of-array-except-self/dusunax.py @@ -0,0 +1,34 @@ +''' +# 238. Product of Array Except Self + +use prefix and suffix to calculate the product of the array, except self. + +## Time and Space Complexity + +``` +TC: O(n) +SC: O(n) +``` + +### TC is O(n): +- iterating through the list twice, to calculate both prefix and suffix products. = O(n) + +### SC is O(n): +- storing the prefix and suffix in the answer list. = O(n) +''' +class Solution: + def productExceptSelf(self, nums: List[int]) -> List[int]: + n = len(nums) + answer = [1] * n + + # prefix + for i in range(1, n): + answer[i] *= nums[i - 1] * answer[i - 1] + + # suffix + suffix_product = 1 + for i in range(n - 1, -1, -1): + answer[i] *= suffix_product + suffix_product *= nums[i] + + return answer From 23966d57e7d4e44aa9815ef3445f5d3f1e720009 Mon Sep 17 00:00:00 2001 From: Dusuna <94776135+dusunax@users.noreply.github.com> Date: Fri, 27 Dec 2024 23:52:33 +0900 Subject: [PATCH 5/6] add solution: combination-sum --- combination-sum/dusunax.py | 39 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 combination-sum/dusunax.py diff --git a/combination-sum/dusunax.py b/combination-sum/dusunax.py new file mode 100644 index 000000000..9126e5924 --- /dev/null +++ b/combination-sum/dusunax.py @@ -0,0 +1,39 @@ +''' +# 39. Combination Sum + +Backtracking for find combinations. + +## Time and Space Complexity + +``` +TC: O(n^2) +SC: O(n) +``` + +#### TC is O(n^2): +- iterating through the list in backtracking recursion to find combinations. = O(n^2) + +#### SC is O(n): +- using a list to store the combinations. = O(n) +''' + +class Solution: + # Backtracking = find combination + # candidate is distinct & can use multiple times. + def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]: + result = [] + + def backtrack(currIdx, remain, combination): + if(remain == 0): + result.append(combination[:]) + return + if(remain < 0): + return + + for i in range(currIdx, len(candidates)): + combination.append(candidates[i]) + backtrack(i, remain - candidates[i], combination) + combination.pop() + + backtrack(0, target, [permutations]) + return result From d6a33dd1ebc4520cb7b58774a7d950cc58235cb2 Mon Sep 17 00:00:00 2001 From: Dusuna <94776135+dusunax@users.noreply.github.com> Date: Sat, 28 Dec 2024 07:31:09 +0900 Subject: [PATCH 6/6] add solution: maximum-subarray --- maximum-subarray/dusunax.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 maximum-subarray/dusunax.py diff --git a/maximum-subarray/dusunax.py b/maximum-subarray/dusunax.py new file mode 100644 index 000000000..01022cdcc --- /dev/null +++ b/maximum-subarray/dusunax.py @@ -0,0 +1,34 @@ +''' +# 53. Maximum Subarray + +- use Kadane's Algorithm for efficiently finding the maximum subarray sum. + +## Time and Space Complexity + +``` +TC: O(n) +SC: O(1) +``` + +#### TC is O(n): +- iterating through the list just once to find the maximum subarray sum. = O(n) + +#### SC is O(1): +- using a constant amount of extra space to store the current sum and the maximum sum. = O(1) +''' + +class Solution: + def maxSubArray(self, nums: List[int]) -> int: + if len(nums) == 1: + return nums[0] + + currentSum = 0 # SC: O(1) + maxSum = nums[0] # SC: O(1) + + for i in range(len(nums)): # TC: O(n) + currentSum = max(currentSum + nums[i], nums[i]) # TC: O(1) + + if currentSum > maxSum: # TC: O(1) + maxSum = currentSum + + return maxSum