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

[GangBean] Week 3 #756

Merged
merged 5 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
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;
Comment on lines +22 to +23
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dp 배열 없어도 해결이 가능하군요! 덕분에 공간 효율이 우수한 풀이법 배워갑니다.

}
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[] {};
}
}

Loading