diff --git a/contains-duplicate/hyunpill3.java b/contains-duplicate/hyunpill3.java new file mode 100644 index 0000000000..9704e64f02 --- /dev/null +++ b/contains-duplicate/hyunpill3.java @@ -0,0 +1,12 @@ +class Solution { + public boolean containsDuplicate(int[] nums) { + HashSet set = new HashSet<>(); + for (int num : nums) { + if (set.contains(num)) { + return true; + } + set.add(num); + } + return false; + } +} diff --git a/house-robber/hyunpill3.java b/house-robber/hyunpill3.java new file mode 100644 index 0000000000..d475ae668c --- /dev/null +++ b/house-robber/hyunpill3.java @@ -0,0 +1,15 @@ +class Solution { + public int rob(int[] nums) { + int next = 0; + int next2 = 0; + + for (int num : nums) { + int current = Math.max(next, next2 + num); + + next2 = next; + next = current; + } + + return next; + } +} diff --git a/longest-consecutive-sequence/hyunpill3.java b/longest-consecutive-sequence/hyunpill3.java new file mode 100644 index 0000000000..699acf08e7 --- /dev/null +++ b/longest-consecutive-sequence/hyunpill3.java @@ -0,0 +1,26 @@ +class Solution { + public int longestConsecutive(int[] nums) { + Set set = new HashSet<>(); + for (int num : nums) { + set.add(num); + } + + int res = 0; + + for (int num : set) { + if (!set.contains(num - 1)) { + int current = num; + int count = 1; + + while (set.contains(current + 1)) { + current++; + count++; + } + + res = Math.max(res, count); + } + } + + return res; + } +} diff --git a/top-k-frequent-elements/hyunpill3.java b/top-k-frequent-elements/hyunpill3.java new file mode 100644 index 0000000000..73c49fa568 --- /dev/null +++ b/top-k-frequent-elements/hyunpill3.java @@ -0,0 +1,17 @@ +class Solution { + public int[] topKFrequent(int[] nums, int k) { + List list = new ArrayList<>(); + Arrays.stream(nums) + .boxed() + .collect(Collectors.groupingBy(x -> x)) + .entrySet() + .stream() + .sorted(Map.Entry.comparingByValue((o1, o2) -> Integer.compare(o2.size(), o1.size()))) + .forEachOrdered(x -> { + if (list.size() < k) + list.add(x.getKey()); + }); + + return list.stream().mapToInt(Integer::intValue).toArray(); + } +} diff --git a/two-sum/hyunpill3.java b/two-sum/hyunpill3.java new file mode 100644 index 0000000000..8096ab286b --- /dev/null +++ b/two-sum/hyunpill3.java @@ -0,0 +1,12 @@ +class Solution { + public int[] twoSum(int[] nums, int target) { + HashMap map = new HashMap<>(); + for (int i = 0; i < nums.length; i++) { + int num2 = target - nums[i]; + if (map.containsKey(num2)) { + return new int[] { map.get(num2), i}; + } + map.put(nums[i], i) + } + } +}