diff --git a/combination-sum/ekgns33.java b/combination-sum/ekgns33.java new file mode 100644 index 000000000..1e037722b --- /dev/null +++ b/combination-sum/ekgns33.java @@ -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> combinationSum(int[] candidates, int target) { + List> answer = new ArrayList<>(); + List prev = new ArrayList<>(); + backTrackingHelper(answer, prev, candidates, target, 0, 0); + return answer; + } + private void backTrackingHelper(List> ans, List 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); + } + } + } +} diff --git a/maximum-subarray/ekgns33.java b/maximum-subarray/ekgns33.java new file mode 100644 index 000000000..f87957c53 --- /dev/null +++ b/maximum-subarray/ekgns33.java @@ -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; + } +} diff --git a/product-of-array-except-self/ekgns33.java b/product-of-array-except-self/ekgns33.java new file mode 100644 index 000000000..14e313f2a --- /dev/null +++ b/product-of-array-except-self/ekgns33.java @@ -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; + } +} diff --git a/reverse-bits/ekgns33.java b/reverse-bits/ekgns33.java new file mode 100644 index 000000000..b78c5b73e --- /dev/null +++ b/reverse-bits/ekgns33.java @@ -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< 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; + } +}