From 2c5bf5fb95253cfb4b431d1497262c57b1549e28 Mon Sep 17 00:00:00 2001 From: ekgns33 Date: Tue, 24 Dec 2024 14:57:45 +0900 Subject: [PATCH 1/6] feat : combination-sum --- combination-sum/ekgns33.java | 42 ++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 combination-sum/ekgns33.java diff --git a/combination-sum/ekgns33.java b/combination-sum/ekgns33.java new file mode 100644 index 000000000..2288d70e8 --- /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); + } + } + } +} \ No newline at end of file From 98dfafb3ead29673a2c1fb7e63dea7cc64ac279c Mon Sep 17 00:00:00 2001 From: ekgns33 Date: Tue, 24 Dec 2024 14:58:12 +0900 Subject: [PATCH 2/6] feat : product-of-arrray-except-self --- product-of-array-except-self/ekgns33.java | 72 +++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 product-of-array-except-self/ekgns33.java diff --git a/product-of-array-except-self/ekgns33.java b/product-of-array-except-self/ekgns33.java new file mode 100644 index 000000000..b53937cd3 --- /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; + } +} \ No newline at end of file From 22b7af4b121725c0b7df0fcd25364d6f61050d27 Mon Sep 17 00:00:00 2001 From: ekgns33 Date: Tue, 24 Dec 2024 14:58:28 +0900 Subject: [PATCH 3/6] feat : reverse-bits --- reverse-bits/ekgns33.java | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 reverse-bits/ekgns33.java 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< Date: Tue, 24 Dec 2024 14:58:36 +0900 Subject: [PATCH 4/6] feat : two-sum --- two-sum/ekgns33.java | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 two-sum/ekgns33.java diff --git a/two-sum/ekgns33.java b/two-sum/ekgns33.java new file mode 100644 index 000000000..097903bb7 --- /dev/null +++ b/two-sum/ekgns33.java @@ -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 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; + } +} From 27ea13d63b20750d9c9216e6577844658a4068b6 Mon Sep 17 00:00:00 2001 From: ekgns33 Date: Tue, 24 Dec 2024 15:00:25 +0900 Subject: [PATCH 5/6] fix : add empty line --- combination-sum/ekgns33.java | 2 +- product-of-array-except-self/ekgns33.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/combination-sum/ekgns33.java b/combination-sum/ekgns33.java index 2288d70e8..1e037722b 100644 --- a/combination-sum/ekgns33.java +++ b/combination-sum/ekgns33.java @@ -39,4 +39,4 @@ private void backTrackingHelper(List> ans, List prev, int } } } -} \ No newline at end of file +} diff --git a/product-of-array-except-self/ekgns33.java b/product-of-array-except-self/ekgns33.java index b53937cd3..14e313f2a 100644 --- a/product-of-array-except-self/ekgns33.java +++ b/product-of-array-except-self/ekgns33.java @@ -69,4 +69,4 @@ public int[] productExceptSelf(int[] nums) { } return answer; } -} \ No newline at end of file +} From 9f5f0a4f8c8bdd1b74476315e99d4650bfadac64 Mon Sep 17 00:00:00 2001 From: ekgns33 Date: Thu, 26 Dec 2024 12:47:56 +0900 Subject: [PATCH 6/6] feat : solve maximum-subarray --- maximum-subarray/ekgns33.java | 41 +++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 maximum-subarray/ekgns33.java 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; + } +}