Skip to content

Commit 3b92814

Browse files
authored
Merge pull request #773 from ekgns33/main
[ekgns33] Week 03
2 parents ba86e4c + 9f5f0a4 commit 3b92814

File tree

5 files changed

+233
-0
lines changed

5 files changed

+233
-0
lines changed

combination-sum/ekgns33.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import java.util.ArrayList;
2+
import java.util.List;
3+
4+
/**
5+
input : array of distinct integers, single integer target
6+
output : all unique combinations of candidates where chosen ones sum is target
7+
constraints:
8+
1) can we use same integer multiple times?
9+
yes
10+
2) input array can be empty?
11+
no. [1, 30]
12+
13+
14+
solution 1)
15+
combination >> back-tracking
16+
O(2^n) at most 2^30 << (2^10)^3 ~= 10^9
17+
18+
tc : O(2^n) sc : O(n) call stack
19+
*/
20+
class Solution {
21+
public List<List<Integer>> combinationSum(int[] candidates, int target) {
22+
List<List<Integer>> answer = new ArrayList<>();
23+
List<Integer> prev = new ArrayList<>();
24+
backTrackingHelper(answer, prev, candidates, target, 0, 0);
25+
return answer;
26+
}
27+
private void backTrackingHelper(List<List<Integer>> ans, List<Integer> prev, int[] cands, int target, int curSum, int p) {
28+
if(curSum == target) {
29+
ans.add(new ArrayList(prev));
30+
return;
31+
}
32+
if(p >= cands.length) return;
33+
34+
for(int i = p; i< cands.length; i++) {
35+
if((curSum + cands[i]) <= target) {
36+
prev.add(cands[i]);
37+
backTrackingHelper(ans, prev, cands, target, curSum + cands[i],i);
38+
prev.remove(prev.size() - 1);
39+
}
40+
}
41+
}
42+
}

maximum-subarray/ekgns33.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
input : array of integer
3+
output : largest sum of subarray
4+
constraints :
5+
1) is the input array not empty?
6+
yes. at least one el
7+
2) range of integers
8+
[-10^4, 10^4]
9+
3) maximum lenght of input
10+
[10^5]
11+
>> maximum sum = 10^5 * 10^4 = 10 ^ 9 < INTEGER
12+
13+
sol1) brute force
14+
nested for loop : O(n^2)
15+
tc : O(n^2), sc : O(1)
16+
17+
sol2) dp?
18+
Subarray elements are continuous in the original array, so we can use dp.
19+
let dp[i] represent the largest sum of a subarray where the ith element is the last element of the subarray.
20+
21+
if dp[i-1] + curval < cur val : take curval
22+
if dp[i-1] + cur val >= curval : take dp[i-1] + curval
23+
tc : O(n) sc : O(n)
24+
*/
25+
class Solution {
26+
public int maxSubArray(int[] nums) {
27+
int n = nums.length;
28+
int[] dp = new int[n];
29+
int maxSum = nums[0];
30+
dp[0] = nums[0];
31+
for(int i = 1; i < n; i++) {
32+
if(dp[i-1] + nums[i] < nums[i]) {
33+
dp[i] = nums[i];
34+
} else {
35+
dp[i] = nums[i] + dp[i-1];
36+
}
37+
maxSum = Math.max(maxSum, dp[i]);
38+
}
39+
return maxSum;
40+
}
41+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
input : array of integer
3+
output : array of integer that each element
4+
is product of all the elements except itself
5+
constraint
6+
1) is the result of product also in range of integer?
7+
yes product of nums is guaranteed to fit 32-bit int
8+
2) how about zero?
9+
doesn't matter if the prduct result is zero
10+
3) is input array non-empty?
11+
yes. length of array is in range [2, 10^5]
12+
13+
solution1) brute force
14+
15+
calc all the product except current index element
16+
17+
tc : O(n^2) sc : O(n) << for the result. when n is the length of input array
18+
19+
solution 2) better?
20+
ds : array
21+
algo : hmmm we can reuse the product using prefix sum
22+
23+
1) get prefix sum from left to right and vice versa : 2-O(n) loop + 2-O(n) space
24+
2) for i = 0 to n-1 when n is the length of input
25+
get product of leftPrfex[i-1] * rightPrefix[i+1]
26+
// edge : i == 0, i == n-1
27+
28+
tc : O(n) sc : O(n)
29+
30+
solution 3) optimal?
31+
can we reduce space?
32+
1) product of all elements. divide by current element.
33+
34+
> edge : what if current element is zero?
35+
2) if there exists only one zero:
36+
all the elements except zero index will be zero
37+
3) if there exist multiple zeros:
38+
all the elements are zero
39+
4) if there is no zero
40+
do 1)
41+
*/
42+
class Solution {
43+
public int[] productExceptSelf(int[] nums) {
44+
int n = nums.length, product = 1, zeroCount = 0;
45+
for(int num : nums) {
46+
if(num == 0) {
47+
zeroCount ++;
48+
if(zeroCount > 1) break;
49+
} else {
50+
product *= num;
51+
}
52+
}
53+
54+
int[] answer = new int[n];
55+
if(zeroCount > 1) {
56+
return answer;
57+
} else if (zeroCount == 1) {
58+
for(int i = 0; i < n; i++) {
59+
if(nums[i] != 0) {
60+
answer[i] = 0;
61+
continue;
62+
}
63+
answer[i] = product;
64+
}
65+
} else {
66+
for(int i = 0; i < n; i++) {
67+
answer[i] = product / nums[i];
68+
}
69+
}
70+
return answer;
71+
}
72+
}

reverse-bits/ekgns33.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
input : 32 bit unsigned integer n
3+
output : unsigned integer representation of reversed bit
4+
constraint :
5+
1) input is always 32 bit unsigned integer
6+
2) implementation should not be affected by programming language
7+
8+
solution 1)
9+
10+
get unsigned integer bit representation
11+
12+
build string s O(n)
13+
reverse O(n)
14+
get integer O(n)
15+
16+
tc : O(n) sc : O(n)
17+
18+
solution 2) one loop
19+
20+
nth bit indicates (1<<n) if reverse (1<< (32 - n))
21+
add up in one loop
22+
return
23+
24+
tc : O(n), sc: O(1)
25+
26+
*/
27+
public class Solution {
28+
// you need treat n as an unsigned value
29+
static final int LENGTH = 32;
30+
public int reverseBits(int n) {
31+
int answer = 0;
32+
for(int i = 0; i < LENGTH; i++) {
33+
if(((1<<i) & n) != 0) {
34+
answer += (1 << (LENGTH - i - 1));
35+
}
36+
}
37+
return answer;
38+
}
39+
}

two-sum/ekgns33.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import java.util.HashMap;
2+
import java.util.Map;
3+
4+
/*
5+
input : array of integers, single integer target
6+
output : indices of the two numbers that they add up to target
7+
constraint:
8+
1) is integer positive?
9+
no [-10^9, 10^9]
10+
2) is there any duplicates?
11+
yes. but only one valid answer exists
12+
3) can i reuse elem?
13+
no
14+
15+
sol1) brute force
16+
nested for loop. tc: O(n^2), sc: O(1) when n is the length of input
17+
18+
sol2) better solution with hash map
19+
iterate through the array
20+
check if target - current elem exists
21+
if return pair of indices
22+
else save current elem and continue
23+
24+
tc : O(n), sc: O(n) when n is the length of input
25+
26+
*/
27+
class Solution {
28+
public int[] twoSum(int[] nums, int target) {
29+
Map<Integer, Integer> prev = new HashMap<>();
30+
for(int i = 0; i < nums.length; i++) {
31+
int key = target - nums[i];
32+
if(prev.containsKey(key)) {
33+
return new int[] {prev.get(key), i};
34+
}
35+
prev.put(nums[i], i);
36+
}
37+
return null;
38+
}
39+
}

0 commit comments

Comments
 (0)