diff --git a/combination-sum/GangBean.java b/combination-sum/GangBean.java new file mode 100644 index 000000000..04d9b46c1 --- /dev/null +++ b/combination-sum/GangBean.java @@ -0,0 +1,43 @@ +class Solution { + public List> 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>[] 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 combination : dp[i - candidate]) { + List newCombination = new ArrayList<>(combination); + newCombination.add(candidate); + dp[i].add(newCombination); + } + } + } + + return dp[target]; + } +} + diff --git a/maximum-subarray/GangBean.java b/maximum-subarray/GangBean.java new file mode 100644 index 000000000..1eebca0ab --- /dev/null +++ b/maximum-subarray/GangBean.java @@ -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; + } +} + diff --git a/product-of-array-except-self/GangBean.java b/product-of-array-except-self/GangBean.java new file mode 100644 index 000000000..cf53f48c1 --- /dev/null +++ b/product-of-array-except-self/GangBean.java @@ -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; + } +} + diff --git a/reverse-bits/GangBean.java b/reverse-bits/GangBean.java new file mode 100644 index 000000000..93f7ad6c1 --- /dev/null +++ b/reverse-bits/GangBean.java @@ -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; + } +} + diff --git a/two-sum/GangBean.java b/two-sum/GangBean.java new file mode 100644 index 000000000..cd5abf382 --- /dev/null +++ b/two-sum/GangBean.java @@ -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 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[] {}; + } +} +