diff --git a/contains-duplicate/jinhyungrhee.java b/contains-duplicate/jinhyungrhee.java new file mode 100644 index 000000000..0c3b63093 --- /dev/null +++ b/contains-duplicate/jinhyungrhee.java @@ -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; + } +} diff --git a/house-robber/jinhyungrhee.java b/house-robber/jinhyungrhee.java new file mode 100644 index 000000000..5270bff57 --- /dev/null +++ b/house-robber/jinhyungrhee.java @@ -0,0 +1,22 @@ +import java.util.*; +class Solution { + + // dfs with memoization => O(N) + public int rob(int[] nums) { + Map memo = new HashMap<>(); + return dfs(0, nums, memo); + } + + public int dfs(int start, int[] nums, Map 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); + } +} diff --git a/longest-consecutive-sequence/jinhyungrhee.java b/longest-consecutive-sequence/jinhyungrhee.java new file mode 100644 index 000000000..9e59de4c9 --- /dev/null +++ b/longest-consecutive-sequence/jinhyungrhee.java @@ -0,0 +1,40 @@ +import java.util.*; + +class Solution { + public int longestConsecutive(int[] nums) { + Set 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; + } +} diff --git a/top-k-frequent-elements/jinhyungrhee.java b/top-k-frequent-elements/jinhyungrhee.java new file mode 100644 index 000000000..9aa6b22ae --- /dev/null +++ b/top-k-frequent-elements/jinhyungrhee.java @@ -0,0 +1,29 @@ +import java.util.*; +import java.util.stream.*; + +class Solution { + public int[] topKFrequent(int[] nums, int k) { + + Map map = new HashMap<>(); + for(int n : nums) { + if(!map.containsKey(n)) { + map.put(n, 1); + } else { + map.put(n, map.get(n) + 1); + } + } + + Map 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 list = sortedMap.keySet().stream().limit(k).toList(); + return list.stream().mapToInt(Integer::intValue).toArray(); + } +} + diff --git a/two-sum/jinhyungrhee.java b/two-sum/jinhyungrhee.java new file mode 100644 index 000000000..5d840cbec --- /dev/null +++ b/two-sum/jinhyungrhee.java @@ -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; + } +}