-
Notifications
You must be signed in to change notification settings - Fork 126
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
+186
−0
Merged
[GangBean] Week 3 #756
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,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]; | ||
} | ||
} | ||
|
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,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; | ||
} | ||
} | ||
|
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,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; | ||
} | ||
} | ||
|
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,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; | ||
} | ||
} | ||
|
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,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[] {}; | ||
} | ||
} | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
dp 배열 없어도 해결이 가능하군요! 덕분에 공간 효율이 우수한 풀이법 배워갑니다.