Skip to content
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
16 changes: 16 additions & 0 deletions contains-duplicate/hoyeongkwak.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
time complexity : O(n)
space complexity : O(n)
*/
class Solution {
public boolean containsDuplicate(int[] nums) {
HashSet<Integer> uniqueNums = new HashSet<Integer>();

for (int num:nums) {
if (!uniqueNums.add(num)) {
return true;
}
}
return false;
}
}
15 changes: 15 additions & 0 deletions house-robber/hoyeongkwak.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
time complexity : O(n)
space complexity : O(n)
*/
class Solution {
public int rob(int[] nums) {
int[] dp = new int[nums.length + 1];
dp[0] = 0;
dp[1] = nums[0];
for (int i = 2; i < dp.length; i++) {
dp[i] = Math.max(dp[i - 1], dp[i - 2] + nums[i - 1]);
}
return dp[dp.length - 1];
}
}
27 changes: 27 additions & 0 deletions longest-consecutive-sequence/hoyeongkwak.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
time complexity : O(n)
space complexity : O(n)
*/
class Solution {
public int longestConsecutive(int[] nums) {
int maxLen = 0;
Set<Integer> numSet = new HashSet<Integer>();
for (int num: nums) {
numSet.add(num);
}

for (int num : numSet) {
if (!numSet.contains(num - 1)){
int continueCnt = 1;
int current = num;

while (numSet.contains(current + 1)) {
current++;
continueCnt++;
}
maxLen = Math.max(continueCnt, maxLen);
}
}
return maxLen;
}
}
24 changes: 24 additions & 0 deletions top-k-frequent-elements/hoyeongkwak.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
time complexity : O(nlogn)
space complexity : O(n)
*/
class Solution {
public int[] topKFrequent(int[] nums, int k) {
HashMap<Integer, Integer> numMap = new HashMap<>();
for (int num:nums) {
if (numMap.containsKey(num)) {
int value = numMap.get(num);
numMap.put(num, value + 1);
} else {
numMap.put(num, 1);
}
}
List<Map.Entry<Integer, Integer>> entryList = new ArrayList<>(numMap.entrySet());
entryList.sort((a, b) -> b.getValue() - a.getValue());
int[] result = new int[k];
for (int i = 0; i < k; i++) {
result[i] = entryList.get(i).getKey();
}
return result;
}
}
17 changes: 17 additions & 0 deletions two-sum/hoyeongkwak.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
time complexity : O(n)
space complexity : O(n)
*/
class Solution {
public int[] twoSum(int[] nums, int target) {
HashMap<Integer, Integer> sumMap = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
int secondNum = target - nums[i];
if (sumMap.containsKey(secondNum)) {
return new int[] { sumMap.get(secondNum), i };
}
sumMap.put(nums[i], i);
}
return new int[] {};
}
}