-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #770 from dusunax/main
[SunaDu] Week 3
- Loading branch information
Showing
5 changed files
with
181 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |