diff --git a/contains-duplicate/eunhwa99.java b/contains-duplicate/eunhwa99.java new file mode 100644 index 000000000..ca29e9508 --- /dev/null +++ b/contains-duplicate/eunhwa99.java @@ -0,0 +1,14 @@ +class Solution { + public boolean containsDuplicate(int[] nums) { + HashSet seen = new HashSet<>(); + for (int num : nums) { + if (!seen.add(num)) { + return true; + } + } + + return false; + + + } +} diff --git a/house-robber/eunhwa99.java b/house-robber/eunhwa99.java new file mode 100644 index 000000000..bcf0d5ffc --- /dev/null +++ b/house-robber/eunhwa99.java @@ -0,0 +1,24 @@ +import java.util.Arrays; + +class Solution { + int size = 0; + int[] numArray; + int[] dp; + + public int rob(int[] nums) { + size = nums.length; + dp = new int[size]; + // 배열의 모든 값을 -1로 변경 + Arrays.fill(dp, -1); + numArray = nums; + return fun(0); + } + + private int fun(int idx) { + if (idx >= size) return 0; + if (dp[idx] != -1) return dp[idx]; + dp[idx] = 0; // check + dp[idx] += Math.max(fun(idx + 2) + numArray[idx], fun(idx + 1)); + return dp[idx]; + } +} diff --git a/longest-consecutive-sequence/eunhwa99.java b/longest-consecutive-sequence/eunhwa99.java new file mode 100644 index 000000000..eaa5d0b09 --- /dev/null +++ b/longest-consecutive-sequence/eunhwa99.java @@ -0,0 +1,23 @@ +import java.util.HashSet; + +class Solution { + public int longestConsecutive(int[] nums) { + HashSet mySet = new HashSet(); + + for (int num : nums) { + mySet.add(num); + } + + int result = 0; + for (int num : mySet) { + int cnt = 1; + if (!mySet.contains(num - 1)) { + while (mySet.contains(++num)) { + ++cnt; + } + result = Math.max(cnt, result); + } + } + return result; + } +} diff --git a/top-k-frequent-elements/eunhwa99.java b/top-k-frequent-elements/eunhwa99.java new file mode 100644 index 000000000..c51eb02f5 --- /dev/null +++ b/top-k-frequent-elements/eunhwa99.java @@ -0,0 +1,16 @@ +import java.util.HashMap; +import java.util.Map; +class Solution { + public static int[] topKFrequent(int[] nums, int k) { + Map myMap = new HashMap<>(); + for (int num : nums) { + myMap.put(num, myMap.getOrDefault(num, 0) + 1); + } + return myMap.entrySet() + .stream() + .sorted((v1, v2) -> Integer.compare(v2.getValue(),v1.getValue())) + .map(Map.Entry::getKey) + .mapToInt(Integer::intValue) + .toArray(); + } +} diff --git a/valid-palindrome/eunhwa99.java b/valid-palindrome/eunhwa99.java new file mode 100644 index 000000000..8af372df8 --- /dev/null +++ b/valid-palindrome/eunhwa99.java @@ -0,0 +1,23 @@ +class Solution { + public boolean isPalindrome(String s) { + StringBuilder str = new StringBuilder(); + + for (int i = 0; i < s.length(); i++) { + char c = s.charAt(i); + if (Character.isLetterOrDigit(c)) { + str.append(Character.toLowerCase(c)); + } + } + + int left = 0, right = str.length() - 1; + while (left < right) { + if (str.charAt(left) != str.charAt(right)) { + return false; + } + left++; + right--; + } + + return true; + } +}