Skip to content

[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 6 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
42 changes: 42 additions & 0 deletions combination-sum/ekgns33.java
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);
}
}
}
}
41 changes: 41 additions & 0 deletions maximum-subarray/ekgns33.java
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;
}
}
72 changes: 72 additions & 0 deletions product-of-array-except-self/ekgns33.java
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;
}
}
39 changes: 39 additions & 0 deletions reverse-bits/ekgns33.java
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;
}
}
39 changes: 39 additions & 0 deletions two-sum/ekgns33.java
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
Copy link
Contributor

Choose a reason for hiding this comment

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

이런식으로 솔루션을 2가지로 제출 할 수 있다는걸 제시하는 방법, 실제 인터뷰에서도 유용하게 사용될 수 있을 것 같습니다 :) 좋은 유즈케이스 공유 감사드립니다

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;
}
}
Loading