-
-
Notifications
You must be signed in to change notification settings - Fork 195
[byteho0n] Week 03 #773
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
Merged
Merged
[byteho0n] Week 03 #773
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2c5bf5f
feat : combination-sum
ekgns33 98dfafb
feat : product-of-arrray-except-self
ekgns33 22b7af4
feat : reverse-bits
ekgns33 af75666
feat : two-sum
ekgns33 27ea13d
fix : add empty line
ekgns33 9f5f0a4
feat : solve maximum-subarray
ekgns33 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 hidden or 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,42 @@ | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
input : array of distinct integers, single integer target | ||
output : all unique combinations of candidates where chosen ones sum is target | ||
constraints: | ||
1) can we use same integer multiple times? | ||
yes | ||
2) input array can be empty? | ||
no. [1, 30] | ||
|
||
|
||
solution 1) | ||
combination >> back-tracking | ||
O(2^n) at most 2^30 << (2^10)^3 ~= 10^9 | ||
|
||
tc : O(2^n) sc : O(n) call stack | ||
*/ | ||
class Solution { | ||
public List<List<Integer>> combinationSum(int[] candidates, int target) { | ||
List<List<Integer>> answer = new ArrayList<>(); | ||
List<Integer> prev = new ArrayList<>(); | ||
backTrackingHelper(answer, prev, candidates, target, 0, 0); | ||
return answer; | ||
} | ||
private void backTrackingHelper(List<List<Integer>> ans, List<Integer> prev, int[] cands, int target, int curSum, int p) { | ||
if(curSum == target) { | ||
ans.add(new ArrayList(prev)); | ||
return; | ||
} | ||
if(p >= cands.length) return; | ||
|
||
for(int i = p; i< cands.length; i++) { | ||
if((curSum + cands[i]) <= target) { | ||
prev.add(cands[i]); | ||
backTrackingHelper(ans, prev, cands, target, curSum + cands[i],i); | ||
prev.remove(prev.size() - 1); | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or 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 @@ | ||
/* | ||
input : array of integer | ||
output : largest sum of subarray | ||
constraints : | ||
1) is the input array not empty? | ||
yes. at least one el | ||
2) range of integers | ||
[-10^4, 10^4] | ||
3) maximum lenght of input | ||
[10^5] | ||
>> maximum sum = 10^5 * 10^4 = 10 ^ 9 < INTEGER | ||
|
||
sol1) brute force | ||
nested for loop : O(n^2) | ||
tc : O(n^2), sc : O(1) | ||
|
||
sol2) dp? | ||
Subarray elements are continuous in the original array, so we can use dp. | ||
let dp[i] represent the largest sum of a subarray where the ith element is the last element of the subarray. | ||
|
||
if dp[i-1] + curval < cur val : take curval | ||
if dp[i-1] + cur val >= curval : take dp[i-1] + curval | ||
tc : O(n) sc : O(n) | ||
*/ | ||
class Solution { | ||
public int maxSubArray(int[] nums) { | ||
int n = nums.length; | ||
int[] dp = new int[n]; | ||
int maxSum = nums[0]; | ||
dp[0] = nums[0]; | ||
for(int i = 1; i < n; i++) { | ||
if(dp[i-1] + nums[i] < nums[i]) { | ||
dp[i] = nums[i]; | ||
} else { | ||
dp[i] = nums[i] + dp[i-1]; | ||
} | ||
maxSum = Math.max(maxSum, dp[i]); | ||
} | ||
return maxSum; | ||
} | ||
} |
This file contains hidden or 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,72 @@ | ||
/* | ||
input : array of integer | ||
output : array of integer that each element | ||
is product of all the elements except itself | ||
constraint | ||
1) is the result of product also in range of integer? | ||
yes product of nums is guaranteed to fit 32-bit int | ||
2) how about zero? | ||
doesn't matter if the prduct result is zero | ||
3) is input array non-empty? | ||
yes. length of array is in range [2, 10^5] | ||
|
||
solution1) brute force | ||
|
||
calc all the product except current index element | ||
|
||
tc : O(n^2) sc : O(n) << for the result. when n is the length of input array | ||
|
||
solution 2) better? | ||
ds : array | ||
algo : hmmm we can reuse the product using prefix sum | ||
|
||
1) get prefix sum from left to right and vice versa : 2-O(n) loop + 2-O(n) space | ||
2) for i = 0 to n-1 when n is the length of input | ||
get product of leftPrfex[i-1] * rightPrefix[i+1] | ||
// edge : i == 0, i == n-1 | ||
|
||
tc : O(n) sc : O(n) | ||
|
||
solution 3) optimal? | ||
can we reduce space? | ||
1) product of all elements. divide by current element. | ||
|
||
> edge : what if current element is zero? | ||
2) if there exists only one zero: | ||
all the elements except zero index will be zero | ||
3) if there exist multiple zeros: | ||
all the elements are zero | ||
4) if there is no zero | ||
do 1) | ||
*/ | ||
class Solution { | ||
public int[] productExceptSelf(int[] nums) { | ||
int n = nums.length, product = 1, zeroCount = 0; | ||
for(int num : nums) { | ||
if(num == 0) { | ||
zeroCount ++; | ||
if(zeroCount > 1) break; | ||
} else { | ||
product *= num; | ||
} | ||
} | ||
|
||
int[] answer = new int[n]; | ||
if(zeroCount > 1) { | ||
return answer; | ||
} else if (zeroCount == 1) { | ||
for(int i = 0; i < n; i++) { | ||
if(nums[i] != 0) { | ||
answer[i] = 0; | ||
continue; | ||
} | ||
answer[i] = product; | ||
} | ||
} else { | ||
for(int i = 0; i < n; i++) { | ||
answer[i] = product / nums[i]; | ||
} | ||
} | ||
return answer; | ||
} | ||
} |
This file contains hidden or 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 @@ | ||
/* | ||
input : 32 bit unsigned integer n | ||
output : unsigned integer representation of reversed bit | ||
constraint : | ||
1) input is always 32 bit unsigned integer | ||
2) implementation should not be affected by programming language | ||
|
||
solution 1) | ||
|
||
get unsigned integer bit representation | ||
|
||
build string s O(n) | ||
reverse O(n) | ||
get integer O(n) | ||
|
||
tc : O(n) sc : O(n) | ||
|
||
solution 2) one loop | ||
|
||
nth bit indicates (1<<n) if reverse (1<< (32 - n)) | ||
add up in one loop | ||
return | ||
|
||
tc : O(n), sc: O(1) | ||
|
||
*/ | ||
public class Solution { | ||
// you need treat n as an unsigned value | ||
static final int LENGTH = 32; | ||
public int reverseBits(int n) { | ||
int answer = 0; | ||
for(int i = 0; i < LENGTH; i++) { | ||
if(((1<<i) & n) != 0) { | ||
answer += (1 << (LENGTH - i - 1)); | ||
} | ||
} | ||
return answer; | ||
} | ||
} |
This file contains hidden or 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 @@ | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
/* | ||
input : array of integers, single integer target | ||
output : indices of the two numbers that they add up to target | ||
constraint: | ||
1) is integer positive? | ||
no [-10^9, 10^9] | ||
2) is there any duplicates? | ||
yes. but only one valid answer exists | ||
3) can i reuse elem? | ||
no | ||
|
||
sol1) brute force | ||
nested for loop. tc: O(n^2), sc: O(1) when n is the length of input | ||
|
||
sol2) better solution with hash map | ||
iterate through the array | ||
check if target - current elem exists | ||
if return pair of indices | ||
else save current elem and continue | ||
|
||
tc : O(n), sc: O(n) when n is the length of input | ||
|
||
*/ | ||
class Solution { | ||
public int[] twoSum(int[] nums, int target) { | ||
Map<Integer, Integer> prev = new HashMap<>(); | ||
for(int i = 0; i < nums.length; i++) { | ||
int key = target - nums[i]; | ||
if(prev.containsKey(key)) { | ||
return new int[] {prev.get(key), i}; | ||
} | ||
prev.put(nums[i], i); | ||
} | ||
return null; | ||
} | ||
} |
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.
이런식으로 솔루션을 2가지로 제출 할 수 있다는걸 제시하는 방법, 실제 인터뷰에서도 유용하게 사용될 수 있을 것 같습니다 :) 좋은 유즈케이스 공유 감사드립니다