Skip to content

Commit 3fc06d1

Browse files
committed
Week1 Solutions
1 parent 53ce7c7 commit 3fc06d1

File tree

5 files changed

+99
-0
lines changed

5 files changed

+99
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
time complexity : O(n)
3+
space complexity : O(n)
4+
*/
5+
class Solution {
6+
public boolean containsDuplicate(int[] nums) {
7+
HashSet<Integer> uniqueNums = new HashSet<Integer>();
8+
9+
for (int num:nums) {
10+
if (!uniqueNums.add(num)) {
11+
return true;
12+
}
13+
}
14+
return false;
15+
}
16+
}

house-robber/hoyeongkwak.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*
2+
time complexity : O(n)
3+
space complexity : O(n)
4+
*/
5+
class Solution {
6+
public int rob(int[] nums) {
7+
int[] dp = new int[nums.length + 1];
8+
dp[0] = 0;
9+
dp[1] = nums[0];
10+
for (int i = 2; i < dp.length; i++) {
11+
dp[i] = Math.max(dp[i - 1], dp[i - 2] + nums[i - 1]);
12+
}
13+
return dp[dp.length - 1];
14+
}
15+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
time complexity : O(n)
3+
space complexity : O(n)
4+
*/
5+
class Solution {
6+
public int longestConsecutive(int[] nums) {
7+
int maxLen = 0;
8+
Set<Integer> numSet = new HashSet<Integer>();
9+
for (int num: nums) {
10+
numSet.add(num);
11+
}
12+
13+
for (int num : numSet) {
14+
if (!numSet.contains(num - 1)){
15+
int continueCnt = 1;
16+
int current = num;
17+
18+
while (numSet.contains(current + 1)) {
19+
current++;
20+
continueCnt++;
21+
}
22+
maxLen = Math.max(continueCnt, maxLen);
23+
}
24+
}
25+
return maxLen;
26+
}
27+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
time complexity : O(nlogn)
3+
space complexity : O(n)
4+
*/
5+
class Solution {
6+
public int[] topKFrequent(int[] nums, int k) {
7+
HashMap<Integer, Integer> numMap = new HashMap<>();
8+
for (int num:nums) {
9+
if (numMap.containsKey(num)) {
10+
int value = numMap.get(num);
11+
numMap.put(num, value + 1);
12+
} else {
13+
numMap.put(num, 1);
14+
}
15+
}
16+
List<Map.Entry<Integer, Integer>> entryList = new ArrayList<>(numMap.entrySet());
17+
entryList.sort((a, b) -> b.getValue() - a.getValue());
18+
int[] result = new int[k];
19+
for (int i = 0; i < k; i++) {
20+
result[i] = entryList.get(i).getKey();
21+
}
22+
return result;
23+
}
24+
}

two-sum/hoyeongkwak.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
time complexity : O(n)
3+
space complexity : O(n)
4+
*/
5+
class Solution {
6+
public int[] twoSum(int[] nums, int target) {
7+
HashMap<Integer, Integer> sumMap = new HashMap<>();
8+
for (int i = 0; i < nums.length; i++) {
9+
int secondNum = target - nums[i];
10+
if (sumMap.containsKey(secondNum)) {
11+
return new int[] { sumMap.get(secondNum), i };
12+
}
13+
sumMap.put(nums[i], i);
14+
}
15+
return new int[] {};
16+
}
17+
}

0 commit comments

Comments
 (0)