From f176004c5be510aeab7203fc34d5b118e610b4c1 Mon Sep 17 00:00:00 2001 From: bky373 Date: Thu, 6 Jun 2024 22:48:21 +0900 Subject: [PATCH] Solve week 5 problems --- 3sum/bky373.java | 37 ++++++++++++++++++++++++ encode-and-decode-strings/bky373.java | 30 +++++++++++++++++++ longest-consecutive-sequence/bky373.java | 25 ++++++++++++++++ product-of-array-except-self/bky373.java | 24 +++++++++++++++ top-k-frequent-elements/bky373.java | 26 +++++++++++++++++ 5 files changed, 142 insertions(+) create mode 100644 3sum/bky373.java create mode 100644 encode-and-decode-strings/bky373.java create mode 100644 longest-consecutive-sequence/bky373.java create mode 100644 product-of-array-except-self/bky373.java create mode 100644 top-k-frequent-elements/bky373.java diff --git a/3sum/bky373.java b/3sum/bky373.java new file mode 100644 index 000000000..611d3efca --- /dev/null +++ b/3sum/bky373.java @@ -0,0 +1,37 @@ +/** + * time: O(n^2) + * space: O(n) + * + * - time: becasue of two nested loop and inner loop having a linear time complexity. + * - space: because of a HashSet to store the triplets. + */ +class Solution { + + public List> threeSum(int[] nums) { + Arrays.sort(nums); + int i = 0, j, k; + int ni = 0, nj, nk; + Set> res = new HashSet<>(); + while (i < nums.length && ni <= 0) { + ni = nums[i]; + j = i + 1; + k = nums.length - 1; + while (j < k) { + nj = nums[j]; + nk = nums[k]; + int sum = ni + nj + nk; + if (sum < 0) { + j++; + } else if (sum > 0) { + k--; + } else { + res.add(List.of(ni, nj, nk)); + j++; + } + } + i++; + } + return res.stream() + .toList(); + } +} diff --git a/encode-and-decode-strings/bky373.java b/encode-and-decode-strings/bky373.java new file mode 100644 index 000000000..388609f74 --- /dev/null +++ b/encode-and-decode-strings/bky373.java @@ -0,0 +1,30 @@ +/** + * time: O(N) + * space: O(N) + */ +public class Codec { + + // Encodes a list of strings to a single string. + public String encode(List strs) { + StringBuilder sb = new StringBuilder(); + for (String str : strs) { + sb.append(str.length()) + .append(':') + .append(str); + } + return sb.toString(); + } + + // Decodes a single string to a list of strings. + public List decode(String s) { + List decoded = new ArrayList<>(); + int i = 0; + while (i < s.length()) { + int searchIndex = s.indexOf(':', i); + int chunkSize = Integer.parseInt(s.substring(i, searchIndex)); + i = searchIndex + chunkSize + 1; + decoded.add(s.substring(searchIndex + 1, i)); + } + return decoded; + } +} diff --git a/longest-consecutive-sequence/bky373.java b/longest-consecutive-sequence/bky373.java new file mode 100644 index 000000000..a28645e49 --- /dev/null +++ b/longest-consecutive-sequence/bky373.java @@ -0,0 +1,25 @@ +/** + * time: O(N) + * space: O(N) + */ +class Solution { + + public int longestConsecutive(int[] nums) { + Set numSet = new HashSet<>(); + for (int n : nums) { + numSet.add(n); + } + int longest = 0; + for (int n : nums) { + if (numSet.contains(n - 1)) { + continue; + } + int seq = 1; + while (numSet.contains(n + seq)) { + seq++; + } + longest = Math.max(longest, seq); + } + return longest; + } +} diff --git a/product-of-array-except-self/bky373.java b/product-of-array-except-self/bky373.java new file mode 100644 index 000000000..ca4488481 --- /dev/null +++ b/product-of-array-except-self/bky373.java @@ -0,0 +1,24 @@ +/** + * time: O(N) + * space: O(N) + */ +class Solution { + public int[] productExceptSelf(int[] nums) { + int len = nums.length; + int[] res = new int[len]; + int[] left = Arrays.copyOf(nums, len); + int[] right = Arrays.copyOf(nums, len); + for (int i = 1; i < len; i++) { + left[i] *= left[i - 1]; + } + for (int i = len - 2; i >= 0; i--) { + right[i] *= right[i + 1]; + } + for (int i = 1; i < len - 1; i++) { + res[i] = left[i - 1] * right[i + 1]; + } + res[0] = right[1]; + res[len - 1] = left[len - 2]; + return res; + } +} diff --git a/top-k-frequent-elements/bky373.java b/top-k-frequent-elements/bky373.java new file mode 100644 index 000000000..3f150c1e4 --- /dev/null +++ b/top-k-frequent-elements/bky373.java @@ -0,0 +1,26 @@ +/** + * time: O(N log k) + * space: O(N + k) + * + * - time: N 개를 순회하며 heap 에 요소 추가, 최대 k 개만큼 요소를 추가하여 log k 만큼 시간이 곱해짐. + * - space: 빈도 수를 저장하기 위해 N, 힙에 요소를 저장하기 위해 K 만큼 저장 공간을 사용함. + */ +class Solution { + + public int[] topKFrequent(int[] nums, int k) { + Map freqMap = new HashMap<>(); + for (int n : nums) { + freqMap.put(n, freqMap.getOrDefault(n, 0) + 1); + } + Queue pq = new PriorityQueue<>(Comparator.comparingInt(freqMap::get)); + for (Integer n : freqMap.keySet()) { + pq.add(n); + if (pq.size() > k) { + pq.poll(); + } + } + return IntStream.range(0, k) + .map(i -> pq.poll()) + .toArray(); + } +}