Skip to content

Commit

Permalink
Merge pull request #770 from dusunax/main
Browse files Browse the repository at this point in the history
[SunaDu] Week 3
  • Loading branch information
dusunax authored Dec 28, 2024
2 parents e23e1cd + d6a33dd commit 2fec6cf
Show file tree
Hide file tree
Showing 5 changed files with 181 additions and 0 deletions.
39 changes: 39 additions & 0 deletions combination-sum/dusunax.py
Original file line number Diff line number Diff line change
@@ -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
34 changes: 34 additions & 0 deletions maximum-subarray/dusunax.py
Original file line number Diff line number Diff line change
@@ -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
34 changes: 34 additions & 0 deletions product-of-array-except-self/dusunax.py
Original file line number Diff line number Diff line change
@@ -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
41 changes: 41 additions & 0 deletions reverse-bits/dusunax.py
Original file line number Diff line number Diff line change
@@ -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
33 changes: 33 additions & 0 deletions two-sum/dusunax.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 2fec6cf

Please sign in to comment.