From 39be3890bb223d450ef5df099ec7077ea5b66562 Mon Sep 17 00:00:00 2001 From: eunhwa99 Date: Tue, 10 Dec 2024 20:57:21 +0900 Subject: [PATCH 1/7] contains duplicate solution --- contains-duplicate/eunhwa99.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 contains-duplicate/eunhwa99.java 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; + + + } +} From ff39fdec62cabc2508b12c45d627cb56ba62fc85 Mon Sep 17 00:00:00 2001 From: eunhwa99 Date: Tue, 10 Dec 2024 21:07:08 +0900 Subject: [PATCH 2/7] valid palindrome solution --- valid-palindrome/eunhwa99.java | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 valid-palindrome/eunhwa99.java 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; + } +} From 32ed2baa8e51b39625f99ea72a780c6edca6e9ee Mon Sep 17 00:00:00 2001 From: eunhwa99 Date: Sat, 14 Dec 2024 15:41:53 +0900 Subject: [PATCH 3/7] top k frequent --- top-k-frequent-elements/eunhwa99.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 top-k-frequent-elements/eunhwa99.java diff --git a/top-k-frequent-elements/eunhwa99.java b/top-k-frequent-elements/eunhwa99.java new file mode 100644 index 000000000..cdc28da3e --- /dev/null +++ b/top-k-frequent-elements/eunhwa99.java @@ -0,0 +1,17 @@ + +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(); + } +} \ No newline at end of file From da9d2bf3bc2b73349c1279b0677c5ff04c0aac7c Mon Sep 17 00:00:00 2001 From: eunhwa99 Date: Sat, 14 Dec 2024 15:47:50 +0900 Subject: [PATCH 4/7] =?UTF-8?q?=EC=A4=84=EB=B0=94=EA=BF=88=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- top-k-frequent-elements/eunhwa99.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/top-k-frequent-elements/eunhwa99.java b/top-k-frequent-elements/eunhwa99.java index cdc28da3e..c51eb02f5 100644 --- a/top-k-frequent-elements/eunhwa99.java +++ b/top-k-frequent-elements/eunhwa99.java @@ -1,4 +1,3 @@ - import java.util.HashMap; import java.util.Map; class Solution { @@ -14,4 +13,4 @@ public static int[] topKFrequent(int[] nums, int k) { .mapToInt(Integer::intValue) .toArray(); } -} \ No newline at end of file +} From 898fa6f0acc517ea1f6e45af21649162b0c26835 Mon Sep 17 00:00:00 2001 From: eunhwa99 Date: Sat, 14 Dec 2024 16:38:00 +0900 Subject: [PATCH 5/7] Longest Consecutive Sequence --- longest-consecutive-sequence/eunhwa99.java | 23 ++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 longest-consecutive-sequence/eunhwa99.java 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; + } +} From 378a09601d1484f70e9708f2f8aaf60787e0af3f Mon Sep 17 00:00:00 2001 From: eunhwa99 Date: Sat, 14 Dec 2024 16:57:59 +0900 Subject: [PATCH 6/7] House Robber --- house-robber/eunhwa99.java | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 house-robber/eunhwa99.java diff --git a/house-robber/eunhwa99.java b/house-robber/eunhwa99.java new file mode 100644 index 000000000..0ed741be9 --- /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]; + } +} \ No newline at end of file From 4c8241e366c798a35d78463d396fc3c7e9d5ccb2 Mon Sep 17 00:00:00 2001 From: eunhwa99 Date: Sat, 14 Dec 2024 16:59:44 +0900 Subject: [PATCH 7/7] Add line --- house-robber/eunhwa99.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/house-robber/eunhwa99.java b/house-robber/eunhwa99.java index 0ed741be9..bcf0d5ffc 100644 --- a/house-robber/eunhwa99.java +++ b/house-robber/eunhwa99.java @@ -21,4 +21,4 @@ private int fun(int idx) { dp[idx] += Math.max(fun(idx + 2) + numArray[idx], fun(idx + 1)); return dp[idx]; } -} \ No newline at end of file +}