Skip to content

[bky373] Solve week 5 problems #120

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 1 commit into from
Jun 7, 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
37 changes: 37 additions & 0 deletions 3sum/bky373.java
Original file line number Diff line number Diff line change
@@ -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<List<Integer>> threeSum(int[] nums) {
Arrays.sort(nums);
int i = 0, j, k;
int ni = 0, nj, nk;
Set<List<Integer>> 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();
}
}
30 changes: 30 additions & 0 deletions encode-and-decode-strings/bky373.java
Original file line number Diff line number Diff line change
@@ -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<String> 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<String> decode(String s) {
List<String> 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;
}
}
25 changes: 25 additions & 0 deletions longest-consecutive-sequence/bky373.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* time: O(N)
* space: O(N)
*/
class Solution {

public int longestConsecutive(int[] nums) {
Set<Integer> 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;
}
}
24 changes: 24 additions & 0 deletions product-of-array-except-self/bky373.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
26 changes: 26 additions & 0 deletions top-k-frequent-elements/bky373.java
Original file line number Diff line number Diff line change
@@ -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<Integer, Integer> freqMap = new HashMap<>();
for (int n : nums) {
freqMap.put(n, freqMap.getOrDefault(n, 0) + 1);
}
Queue<Integer> 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();
}
}