Skip to content

Commit 1affa63

Browse files
authored
Merge pull request #2349 from dohyeon2/week1
[dohyeon2] WEEK 1 Solutions
2 parents 0e5ffe5 + aa308f2 commit 1affa63

File tree

5 files changed

+212
-0
lines changed

5 files changed

+212
-0
lines changed

contains-duplicate/dohyeon2.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import java.util.HashSet;
2+
3+
class Solution {
4+
// The problem is to check if there are any duplicate elements in the array.
5+
// So, I decided to use HashSet because it has O(1) time complexity for add and contains operations which are good to use for checking duplicates.
6+
public boolean containsDuplicate(int[] nums) {
7+
// the element type of the array is int, so create Integer HashSet
8+
// O(n) space complexity
9+
HashSet<Integer> exists = new HashSet<Integer>();
10+
// the nums array has length up to 10^5, so use int type
11+
// O(n) time complexity average
12+
// worst case: O(n log n) if many elements hash to the same bucket
13+
for(int i = 0; i < nums.length; i++){
14+
int num = nums[i];
15+
16+
if(exists.contains(num)){
17+
return true;
18+
}else{
19+
exists.add(num);
20+
}
21+
}
22+
return false;
23+
}
24+
}

house-robber/dohyeon2.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution {
2+
public int rob(int[] nums) {
3+
// Dynamic Planning
4+
// Time Complexity: O(n)
5+
// Space Complexity: O(n) for the dp array
6+
// When applying DP, it is important to define the accumulated state clearly.
7+
8+
if (nums.length == 1) {
9+
return nums[0];
10+
}
11+
12+
// The state of dp is maximum amount of robbery at i
13+
int[] dp = new int[nums.length];
14+
15+
dp[0] = nums[0];
16+
dp[1] = Math.max(nums[0], nums[1]);
17+
18+
for (int i = 2; i < nums.length; i++) {
19+
dp[i] = Math.max(
20+
// If we skip the current house,
21+
// we can take the maximum amount of robbery at i - 1
22+
dp[i - 1],
23+
// If we rob the current house, we cannot rob the previous house
24+
// so we can take the maximum amount of robbery at i - 2
25+
dp[i - 2] + nums[i]);
26+
}
27+
28+
return dp[nums.length - 1];
29+
}
30+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import java.util.HashSet;
2+
3+
class Solution {
4+
// This solution was inspired by:
5+
// https://www.algodale.com/problems/longest-consecutive-sequence/
6+
//
7+
// I initially believed this algorithm would run in O(n) time,
8+
// but it resulted in a Time Limit Exceeded error.
9+
//
10+
// Although the expected time complexity is O(n),
11+
// repeatedly calling set.iterator().next() might introduce overhead.
12+
// Iterating through the set using a for-loop may be a better approach.
13+
//
14+
// In this case, it seems preferable to follow the approach described here:
15+
// https://www.algodale.com/problems/longest-consecutive-sequence/#%ED%92%80%EC%9D%B4-3
16+
17+
public int oldApproach(int[] nums) {
18+
int count = 0;
19+
HashSet<Integer> set = new HashSet<>();
20+
for (int num : nums) {
21+
set.add(num);
22+
}
23+
24+
while (set.size() > 0) {
25+
int buffer = 1;
26+
// This may cause a Time Limit Exceeded error.
27+
Integer curr = set.iterator().next();
28+
set.remove(curr);
29+
Integer next = curr + 1;
30+
Integer prev = curr - 1;
31+
32+
while (set.contains(next)) {
33+
set.remove(next);
34+
next++;
35+
buffer++;
36+
}
37+
38+
while (set.contains(prev)) {
39+
set.remove(prev);
40+
prev--;
41+
buffer++;
42+
}
43+
44+
count = Math.max(count, buffer);
45+
}
46+
47+
return count;
48+
}
49+
50+
public int longestConsecutive(int[] nums) {
51+
int count = 0;
52+
53+
// The Set has O(n) space complexity,
54+
// because it may store up to n elements in memory.
55+
// Is this the correct way to evaluate space complexity?
56+
HashSet<Integer> set = new HashSet<>();
57+
for (int num : nums) {
58+
set.add(num);
59+
}
60+
61+
for (int num : set) {
62+
if (set.contains(num - 1)) {
63+
continue;
64+
}
65+
66+
int currentNum = num;
67+
int currentCount = 1;
68+
69+
while (set.contains(currentNum + 1)) {
70+
currentNum++;
71+
currentCount++;
72+
}
73+
74+
count = Math.max(count, currentCount);
75+
}
76+
77+
return count;
78+
}
79+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import java.util.HashMap;
2+
import java.util.ArrayList;
3+
4+
class Solution {
5+
public int[] topKFrequent(int[] nums, int k) {
6+
// time complexity O(n)
7+
// space complexity O(n)
8+
9+
HashMap<Integer, Integer> frequentMap = new HashMap<>();
10+
11+
for (int i = 0; i < nums.length; i++) {
12+
int num = nums[i];
13+
frequentMap.merge(num, 1, Integer::sum);
14+
}
15+
16+
ArrayList<Integer>[] buckets = new ArrayList[nums.length + 1];
17+
18+
for (int i = 0; i <= nums.length; i++) {
19+
buckets[i] = new ArrayList<>();
20+
}
21+
22+
frequentMap.forEach((Integer a, Integer b) -> {
23+
// Assign the largest values from the front of the array
24+
buckets[nums.length - b].add(a);
25+
});
26+
27+
int[] result = new int[k];
28+
29+
int pointer = 0;
30+
31+
for (int i = 0; i < buckets.length; i++) {
32+
ArrayList<Integer> list = buckets[i];
33+
if (list.size() == 0) {
34+
continue;
35+
}
36+
for (Integer num : list) {
37+
result[pointer] = (int) num;
38+
// Return the result when the pointer reaches k
39+
if (pointer == (k - 1))
40+
return result;
41+
pointer++;
42+
}
43+
}
44+
45+
return result;
46+
}
47+
}

two-sum/dohyeon2.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import java.util.HashMap;
2+
3+
class Solution {
4+
public int[] twoSum(int[] nums, int target) {
5+
// Approach : using HashMap to get index with the element in O(n) time
6+
// complexity
7+
// SpaceComplexity is also O(n)
8+
HashMap<Integer, Integer> numIndexMap = new HashMap<Integer, Integer>();
9+
10+
// Make key and value HashMap
11+
for (int i = 0; i < nums.length; i++) {
12+
int num = nums[i];
13+
numIndexMap.put(num, i);
14+
}
15+
16+
// Search for the other operand looping nums
17+
for (int i = 0; i < nums.length; i++) {
18+
int num = nums[i];
19+
int operand = target - num;
20+
Integer index = numIndexMap.get(operand);
21+
boolean indexExists = index != null;
22+
boolean indexExistsAndIndexIsNotTheNum = indexExists && i != index;
23+
if (indexExistsAndIndexIsNotTheNum) {
24+
return new int[] { i, index };
25+
}
26+
}
27+
28+
// If the valid answer is always exists this is not needed
29+
// But the compiler don't know about that
30+
return new int[2];
31+
}
32+
}

0 commit comments

Comments
 (0)