diff --git a/contains-duplicate/samcho0608.java b/contains-duplicate/samcho0608.java new file mode 100644 index 0000000000..1a4486c9e4 --- /dev/null +++ b/contains-duplicate/samcho0608.java @@ -0,0 +1,22 @@ +import java.util.HashSet; + +// link: https://leetcode.com/problems/contains-duplicate/description/ +// difficulty: Easy +class Solution { + // Problem: + // * return: does any value appear more than once in the array + // Solution: + // * Time Complexity: O(N) + // * Space Complexity: O(N) + public boolean containsDuplicate(int[] nums) { + // Space Complexity: O(N) + HashSet uniqNums = new HashSet<>(); + + // Time Complexity: O(N) + for(int num : nums) { + if(!uniqNums.add(num)) return true; + } + + return false; + } +} diff --git a/house-robber/samcho0608.java b/house-robber/samcho0608.java new file mode 100644 index 0000000000..cb580c1c25 --- /dev/null +++ b/house-robber/samcho0608.java @@ -0,0 +1,35 @@ +// link: https://leetcode.com/problems/house-robber/description/ +// difficulty: Medium +class Solution { + // Problem: + // * can't rob two adj houses in the same night + // * return: max amount of money robbale in one night + // Solution: + // * Time Complexity: O(N) + // * Space Complexity: O(N) + public int rob(int[] nums) { + if(nums.length == 1) return nums[0]; + if(nums.length == 2) return Math.max(nums[0], nums[1]); + + // maxSum[i] = max sum possible with nums[i] + int[] maxSum = new int[nums.length]; + maxSum[0] = nums[0]; + maxSum[1] = nums[1]; + + for(int i = 2; i < nums.length; i++) { + if(i == 2) { + maxSum[i] = nums[i] + maxSum[i-2]; + continue; + } + + // adj houses(i-1) can't be robbed + // choices are: + // 1. i-2 th house (choosing without skipping a house) + // 2. i-3 th house (choosing with skipping a house) + // * choosing < i-4 th houses wouldn't be optimal because it'll be missing either of 1. or 2. + maxSum[i] = nums[i] + Math.max(maxSum[i-2], maxSum[i-3]); + } + + return Math.max(maxSum[nums.length-2], maxSum[nums.length-1]); + } +} diff --git a/longest-consecutive-sequence/samcho0608.java b/longest-consecutive-sequence/samcho0608.java new file mode 100644 index 0000000000..c80c32bb57 --- /dev/null +++ b/longest-consecutive-sequence/samcho0608.java @@ -0,0 +1,40 @@ +import java.util.HashSet; +import java.util.Set; + +// link: https://leetcode.com/problems/longest-consecutive-sequence/ +// difficulty: Medium +class Solution { + // Problem: + // * nums is unsorted + // * return: length of longest consecutive elements sequence + // * req: O(N) time + // Solution: + // * Time Complexity: O(N) + // * Space Complexity: O(N) + public int longestConsecutive(int[] nums) { + // Time Complexity: O(N) + // Space Complexity: O(N) + Set uniq = new HashSet<>(); + for(int num : nums) { + uniq.add(num); + } + + // Time Complexity: O(N) + // * nested loop but is O(N) due to skipping non-root element + int maxLen = 0; + for(int num : uniq) { + // skip if num isn't the root(aka first number in the sequence) + if(uniq.contains(num - 1)) continue; + + // count till end of consecutive sequence + int len = 1; + for(int i = 1; uniq.contains(num + i); i++) { + len++; + } + + maxLen = Math.max(maxLen, len); + } + + return maxLen; + } +} diff --git a/top-k-frequent-elements/samcho0608.java b/top-k-frequent-elements/samcho0608.java new file mode 100644 index 0000000000..55b46e4b91 --- /dev/null +++ b/top-k-frequent-elements/samcho0608.java @@ -0,0 +1,70 @@ +import java.util.*; + +// link: https://leetcode.com/problems/top-k-frequent-elements/description/ +// difficulty: Medium + +// Time complexity: O(Nlogk) +// Space complexity: O(N) +class Solution1 { + // return: top k most freq elements + public int[] topKFrequent(int[] nums, int k) { + HashMap freq = new HashMap<>(); + // O (N) + for(int num : nums) { + freq.put(num, freq.getOrDefault(num, 0) + 1); // O(1) + } + + // O (N log k) + PriorityQueue heap = new PriorityQueue<>(Comparator.comparingInt(a -> a[1])); + for(int num : freq.keySet()) { + int f = freq.get(num); // O(1) + heap.add(new int[]{num, f}); // O(log k) + + if(heap.size() > k) heap.poll(); // O(log k) + } + + // O (N log k) + int[] result = new int[k]; + for(int i = 0; i < k; i++) { + result[i] = heap.poll()[0]; // O(log k) + } + + return result; + } +} + +// Time complexity: O(N) +// Space complexity: O(N) +class Solution2 { + public int[] topKFrequent(int[] nums, int k) { + // count frequencies + Map freq = new HashMap<>(); + for (int num : nums) { + freq.put(num, freq.getOrDefault(num, 0) + 1); + } + + // bucket: index = frequency, value = list of numbers + List> buckets = new ArrayList<>(nums.length + 1); + for (int i = 0; i <= nums.length; i++) { + buckets.add(new ArrayList<>()); + } + + for (var entry : freq.entrySet()) { + int num = entry.getKey(); + int count = entry.getValue(); + buckets.get(count).add(num); + } + + // gather top k frequent elements + int[] result = new int[k]; + int idx = 0; + for (int i = nums.length; i >= 0 && idx < k; i--) { + for (int num : buckets.get(i)) { + result[idx++] = num; + if (idx == k) break; + } + } + + return result; + } +} diff --git a/two-sum/samcho0608.java b/two-sum/samcho0608.java new file mode 100644 index 0000000000..652be4ea0b --- /dev/null +++ b/two-sum/samcho0608.java @@ -0,0 +1,35 @@ +import java.util.HashMap; +import java.util.Map; + +// link: https://leetcode.com/problems/two-sum/description/ +// difficulty: Easy +class Solution { + // Problem + // * exactly one solution + // * must use index only once + // * return: indices of two numbers that add up to `target` + // Solution: + // * Time Complexity: O(N) + // * Space Complexity: O(N) + public int[] twoSum(int[] nums, int target) { + // Space Complexity: O(N) + Map indexByNum = new HashMap<>(); + + // Time Complexity: O(N) + for(int i = 0; i < nums.length; i++) { + int numI = nums[i]; + indexByNum.put(numI, i); + } + + // Time Complexity: O(N) + for(int i = 0; i < nums.length; i++) { + int numI = nums[i]; + Integer compl = indexByNum.getOrDefault(target-numI, null); + + if(compl != null && i != compl) + return new int[] {i, compl}; + } + + return null; + } +}