Skip to content

Commit 804af96

Browse files
authored
[samcho0608] WEEK 01 solutions (#1995)
[samcho0608] WEEK 01 solutions
2 parents 71233d5 + 9ecb343 commit 804af96

File tree

5 files changed

+202
-0
lines changed

5 files changed

+202
-0
lines changed

contains-duplicate/samcho0608.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import java.util.HashSet;
2+
3+
// link: https://leetcode.com/problems/contains-duplicate/description/
4+
// difficulty: Easy
5+
class Solution {
6+
// Problem:
7+
// * return: does any value appear more than once in the array
8+
// Solution:
9+
// * Time Complexity: O(N)
10+
// * Space Complexity: O(N)
11+
public boolean containsDuplicate(int[] nums) {
12+
// Space Complexity: O(N)
13+
HashSet<Integer> uniqNums = new HashSet<>();
14+
15+
// Time Complexity: O(N)
16+
for(int num : nums) {
17+
if(!uniqNums.add(num)) return true;
18+
}
19+
20+
return false;
21+
}
22+
}

house-robber/samcho0608.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// link: https://leetcode.com/problems/house-robber/description/
2+
// difficulty: Medium
3+
class Solution {
4+
// Problem:
5+
// * can't rob two adj houses in the same night
6+
// * return: max amount of money robbale in one night
7+
// Solution:
8+
// * Time Complexity: O(N)
9+
// * Space Complexity: O(N)
10+
public int rob(int[] nums) {
11+
if(nums.length == 1) return nums[0];
12+
if(nums.length == 2) return Math.max(nums[0], nums[1]);
13+
14+
// maxSum[i] = max sum possible with nums[i]
15+
int[] maxSum = new int[nums.length];
16+
maxSum[0] = nums[0];
17+
maxSum[1] = nums[1];
18+
19+
for(int i = 2; i < nums.length; i++) {
20+
if(i == 2) {
21+
maxSum[i] = nums[i] + maxSum[i-2];
22+
continue;
23+
}
24+
25+
// adj houses(i-1) can't be robbed
26+
// choices are:
27+
// 1. i-2 th house (choosing without skipping a house)
28+
// 2. i-3 th house (choosing with skipping a house)
29+
// * choosing < i-4 th houses wouldn't be optimal because it'll be missing either of 1. or 2.
30+
maxSum[i] = nums[i] + Math.max(maxSum[i-2], maxSum[i-3]);
31+
}
32+
33+
return Math.max(maxSum[nums.length-2], maxSum[nums.length-1]);
34+
}
35+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import java.util.HashSet;
2+
import java.util.Set;
3+
4+
// link: https://leetcode.com/problems/longest-consecutive-sequence/
5+
// difficulty: Medium
6+
class Solution {
7+
// Problem:
8+
// * nums is unsorted
9+
// * return: length of longest consecutive elements sequence
10+
// * req: O(N) time
11+
// Solution:
12+
// * Time Complexity: O(N)
13+
// * Space Complexity: O(N)
14+
public int longestConsecutive(int[] nums) {
15+
// Time Complexity: O(N)
16+
// Space Complexity: O(N)
17+
Set<Integer> uniq = new HashSet<>();
18+
for(int num : nums) {
19+
uniq.add(num);
20+
}
21+
22+
// Time Complexity: O(N)
23+
// * nested loop but is O(N) due to skipping non-root element
24+
int maxLen = 0;
25+
for(int num : uniq) {
26+
// skip if num isn't the root(aka first number in the sequence)
27+
if(uniq.contains(num - 1)) continue;
28+
29+
// count till end of consecutive sequence
30+
int len = 1;
31+
for(int i = 1; uniq.contains(num + i); i++) {
32+
len++;
33+
}
34+
35+
maxLen = Math.max(maxLen, len);
36+
}
37+
38+
return maxLen;
39+
}
40+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import java.util.*;
2+
3+
// link: https://leetcode.com/problems/top-k-frequent-elements/description/
4+
// difficulty: Medium
5+
6+
// Time complexity: O(Nlogk)
7+
// Space complexity: O(N)
8+
class Solution1 {
9+
// return: top k most freq elements
10+
public int[] topKFrequent(int[] nums, int k) {
11+
HashMap<Integer, Integer> freq = new HashMap<>();
12+
// O (N)
13+
for(int num : nums) {
14+
freq.put(num, freq.getOrDefault(num, 0) + 1); // O(1)
15+
}
16+
17+
// O (N log k)
18+
PriorityQueue<int[]> heap = new PriorityQueue<>(Comparator.comparingInt(a -> a[1]));
19+
for(int num : freq.keySet()) {
20+
int f = freq.get(num); // O(1)
21+
heap.add(new int[]{num, f}); // O(log k)
22+
23+
if(heap.size() > k) heap.poll(); // O(log k)
24+
}
25+
26+
// O (N log k)
27+
int[] result = new int[k];
28+
for(int i = 0; i < k; i++) {
29+
result[i] = heap.poll()[0]; // O(log k)
30+
}
31+
32+
return result;
33+
}
34+
}
35+
36+
// Time complexity: O(N)
37+
// Space complexity: O(N)
38+
class Solution2 {
39+
public int[] topKFrequent(int[] nums, int k) {
40+
// count frequencies
41+
Map<Integer, Integer> freq = new HashMap<>();
42+
for (int num : nums) {
43+
freq.put(num, freq.getOrDefault(num, 0) + 1);
44+
}
45+
46+
// bucket: index = frequency, value = list of numbers
47+
List<List<Integer>> buckets = new ArrayList<>(nums.length + 1);
48+
for (int i = 0; i <= nums.length; i++) {
49+
buckets.add(new ArrayList<>());
50+
}
51+
52+
for (var entry : freq.entrySet()) {
53+
int num = entry.getKey();
54+
int count = entry.getValue();
55+
buckets.get(count).add(num);
56+
}
57+
58+
// gather top k frequent elements
59+
int[] result = new int[k];
60+
int idx = 0;
61+
for (int i = nums.length; i >= 0 && idx < k; i--) {
62+
for (int num : buckets.get(i)) {
63+
result[idx++] = num;
64+
if (idx == k) break;
65+
}
66+
}
67+
68+
return result;
69+
}
70+
}

two-sum/samcho0608.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import java.util.HashMap;
2+
import java.util.Map;
3+
4+
// link: https://leetcode.com/problems/two-sum/description/
5+
// difficulty: Easy
6+
class Solution {
7+
// Problem
8+
// * exactly one solution
9+
// * must use index only once
10+
// * return: indices of two numbers that add up to `target`
11+
// Solution:
12+
// * Time Complexity: O(N)
13+
// * Space Complexity: O(N)
14+
public int[] twoSum(int[] nums, int target) {
15+
// Space Complexity: O(N)
16+
Map<Integer, Integer> indexByNum = new HashMap<>();
17+
18+
// Time Complexity: O(N)
19+
for(int i = 0; i < nums.length; i++) {
20+
int numI = nums[i];
21+
indexByNum.put(numI, i);
22+
}
23+
24+
// Time Complexity: O(N)
25+
for(int i = 0; i < nums.length; i++) {
26+
int numI = nums[i];
27+
Integer compl = indexByNum.getOrDefault(target-numI, null);
28+
29+
if(compl != null && i != compl)
30+
return new int[] {i, compl};
31+
}
32+
33+
return null;
34+
}
35+
}

0 commit comments

Comments
 (0)