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/jinhyungrhee.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import java.util.*;
class Solution {
public boolean containsDuplicate(int[] nums) {
/**
avg : O(NlogN)
worst : O(N^2)
*/
Arrays.sort(nums);

for (int i = 1; i < nums.length; i++) {
if (nums[i-1] == nums[i]) return true;
}

return false;
}
}
22 changes: 22 additions & 0 deletions house-robber/jinhyungrhee.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import java.util.*;
class Solution {

// dfs with memoization => O(N)
public int rob(int[] nums) {
Map<Integer,Integer> memo = new HashMap<>();
return dfs(0, nums, memo);
}

public int dfs(int start, int[] nums, Map<Integer, Integer> memo) {
if (memo.containsKey(start)) return memo.get(start);
if (start >= nums.length) {
memo.put(start, 0);
} else {
memo.put(start, Math.max(
nums[start] + dfs(start + 2, nums, memo),
dfs(start + 1, nums, memo))
);
}
return memo.get(start);
}
}
40 changes: 40 additions & 0 deletions longest-consecutive-sequence/jinhyungrhee.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import java.util.*;

class Solution {
public int longestConsecutive(int[] nums) {
Set<Integer> table = new HashSet<>();
int longest = 0;

for(int n : nums) {
table.add(n);
}

// O(N)
for (int num : table) {
if (!table.contains(num - 1)) {
int currentNum = num;
int currentStreak = 1;

while (table.contains(currentNum + 1)) {
currentNum++;
currentStreak++;
}
longest = Math.max(longest, currentStreak);
}
}

/** TIME OUT 발생!
*
for (int n : nums) {
if (table.contains(n - 1)) continue;
int len = 1;
while (table.contains(n + len)){
len++;
}
longest = Math.max(len, longest);
}
*/

return longest;
}
}
29 changes: 29 additions & 0 deletions top-k-frequent-elements/jinhyungrhee.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import java.util.*;
import java.util.stream.*;

class Solution {
public int[] topKFrequent(int[] nums, int k) {

Map<Integer, Integer> map = new HashMap<>();
for(int n : nums) {
if(!map.containsKey(n)) {
map.put(n, 1);
} else {
map.put(n, map.get(n) + 1);
}
}

Map<Integer, Integer> sortedMap = map.entrySet().stream()
.sorted(Map.Entry.comparingByValue(Comparator.reverseOrder()))
.collect(Collectors.toMap(
Map.Entry::getKey,
Map.Entry::getValue,
(oldVal, newVal) -> oldVal,
LinkedHashMap::new
));

List<Integer> list = sortedMap.keySet().stream().limit(k).toList();
return list.stream().mapToInt(Integer::intValue).toArray();
}
}

17 changes: 17 additions & 0 deletions two-sum/jinhyungrhee.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Solution {

// Method1. brute-force => O(N^2)
public int[] twoSum(int[] nums, int target) {
int size = nums.length;
int[] answer = new int[2];
for (int i = 0; i < size; i++) {
for (int j = i + 1; j < size; j++) {
if (nums[i] + nums[j] == target) {
answer[0] = i;
answer[1] = j;
}
}
}
return answer;
}
}