Skip to content

Commit

Permalink
Merge pull request #756 from GangBean/main
Browse files Browse the repository at this point in the history
[GangBean] Week 3
  • Loading branch information
GangBean authored Dec 28, 2024
2 parents fd0241e + cd96d8b commit 97813a9
Show file tree
Hide file tree
Showing 5 changed files with 186 additions and 0 deletions.
43 changes: 43 additions & 0 deletions combination-sum/GangBean.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
class Solution {
public List<List<Integer>> combinationSum(int[] candidates, int target) {
/**
1. understanding
- find all combinations, which sum to target
- can use same number multiple times
2. strategy
- dp[target]: all combination, which sum to target
- dp[n + 1] = dp[n] | dp[1]
- [2,3,6,7], target = 7
- dp[0] = [[]]
- dp[1] = [[]]
- dp[2] = [[2]]
- dp[3] = [[3]]
- dp[4] = dp[2] | dp[2] = [[2,2]]
- dp[5] = dp[2] | dp[3] = [[2,3]]
- dp[6] = dp[2] | dp[4] , dp[3] | dp[3] = [[2,2,2], [3,3]]
- dp[7] = dp[2] | dp[5], dp[3] | dp[4], dp[6] | dp[1], dp[7] = [[2,2,3],]
3. complexity
- time: O(target * N) where N is length of candidates
- space: O(target * N)
*/
List<List<Integer>>[] dp = new List[target + 1];
for (int i = 0; i <= target; i++) {
dp[i] = new ArrayList<>();
}

dp[0].add(new ArrayList<>());

for (int candidate : candidates) {
for (int i = candidate; i <= target; i++) {
for (List<Integer> combination : dp[i - candidate]) {
List<Integer> newCombination = new ArrayList<>(combination);
newCombination.add(candidate);
dp[i].add(newCombination);
}
}
}

return dp[target];
}
}

28 changes: 28 additions & 0 deletions maximum-subarray/GangBean.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
class Solution {
public int maxSubArray(int[] nums) {
/**
1. understanding
- integer array nums
- find largest subarray sum
2. starategy
- calculate cumulative sum
- mem[i+1] = num[i+1] + mem[i] if (num[i+1] + mem[i] >= 0) else num[i+1]
3. complexity
- time: O(N)
- space: O(1)
*/
int prev = 0;
int curr = 0;
int max = Integer.MIN_VALUE;
for (int i = 0 ; i < nums.length; i++) {
curr = nums[i];
if (prev >= 0) {
curr += prev;
}
max = Math.max(max, curr);
prev = curr;
}
return max;
}
}

56 changes: 56 additions & 0 deletions product-of-array-except-self/GangBean.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
class Solution {
public int[] productExceptSelf(int[] nums) {
/**
1. understanding
- given integer array nums
- product of which's all elements except that element
- should be under O(N) time complexity
- should not use division operation
2. strategy
- brute force: O(n^2)
- for every elements, calculate product of other elements
- 1: 2 * [3 * 4]
- 2: 1 * [3 * 4]
- 3: [1 * 2] * 4
- 4: [1 * 2] * 3
- dynamic programming
- assign array mem to memorize product till that idx
- mem memorize in ascending order product value and reverse order product value
3. complexity
- time: O(N)
- space: O(N)
*/
// 1. assign array variable mem
int[][] mem = new int[nums.length][];
for (int i = 0 ; i < nums.length; i++) {
mem[i] = new int[2];
}

// 2. calculate product values
for (int i = 0 ; i < nums.length; i++) { // O(N)
if (i == 0) {
mem[i][0] = nums[i];
continue;
}
mem[i][0] = nums[i] * mem[i-1][0];
}

for (int i = nums.length - 1; i >= 0; i--) { // O(N)
if (i == nums.length - 1) {
mem[i][1] = nums[i];
continue;
}
mem[i][1] = nums[i] * mem[i+1][1];
}

int[] ret = new int[nums.length];
for (int i = 0; i < nums.length; i++) { // O(N)
int left = (i - 1) >= 0 ? mem[i-1][0] : 1;
int right = (i + 1) < nums.length ? mem[i+1][1] : 1;
ret[i] = left * right;
}

return ret;
}
}

24 changes: 24 additions & 0 deletions reverse-bits/GangBean.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
public class Solution {
// you need treat n as an unsigned value
public int reverseBits(int n) {
/**
1. understanding
- return the reversed 32 bit input num
2. strategy
- assign stack
- iterate until stack.size is 32
- push value % 2, and value /= 2
3. complexity
- time: O(1)
- space: O(1)
*/
int reversed = 0;
for (int i = 0; i < 32; i++) {
reversed <<= 1;
reversed |= n & 1;
n >>= 1;
}
return reversed;
}
}

35 changes: 35 additions & 0 deletions two-sum/GangBean.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
class Solution {
public int[] twoSum(int[] nums, int target) {
/**
1. understanding
- find the indices where the numbers of that index pair sum upto the target number.
- exactly one solution
- use each element only once
- return in any order
2. strategy
- brute force:
- validate for all pair sum
- hashtable:
- assing hashtable variable to save the nums and index
- while iterate over the nums,
- calculate the diff, and check if the diff in the hashtable.
- or save new entry.
3. complexity
- time: O(N)
- space: O(N)
*/

Map<Integer, Integer> map = new HashMap<>();

for (int i = 0; i < nums.length; i++) {
int diff = target - nums[i];
if (map.containsKey(diff)) {
return new int[] {map.get(diff), i};
}
map.put(nums[i], i);
}

return new int[] {};
}
}

0 comments on commit 97813a9

Please sign in to comment.