Skip to content
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

[SunaDu] Week 3 #770

Merged
merged 6 commits into from
Dec 28, 2024
Merged
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
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
Loading