From afd0af83054144010327ced8b04473e716ff5dcf Mon Sep 17 00:00:00 2001 From: donghyeon95 Date: Wed, 4 Dec 2024 22:06:09 +0900 Subject: [PATCH 1/6] add solution: Contains Duplicate #217 --- contains-duplicate/donghyeon95.java | 30 +++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 contains-duplicate/donghyeon95.java diff --git a/contains-duplicate/donghyeon95.java b/contains-duplicate/donghyeon95.java new file mode 100644 index 000000000..f4076156d --- /dev/null +++ b/contains-duplicate/donghyeon95.java @@ -0,0 +1,30 @@ +import java.util.*; + +class Solution { + public boolean containsDuplicate(int[] nums) { + /* + * -- 풀이 -- + * nums를 순회하면서 HashSet에 데이터를 넣어서 중복되었는 지 확인한다. + * + * -- 시간 복잡도 -- + * 길이 N인 nums를 순환하는데 대한 시간 복잡도 => O(N) + * hashSet의 add에 대한 시간 복잡도 => O(1) + * 전체 시간 복잡도 O(1)*O(N) =O(n) + * ------------------------------------------ + * + * -- 공간 복잡도 -- + * 길이 N인 nums를 넣을 Hashset이 있어야 하기에 O(N) + * ------------------------------------------- + */ + + // 중복을 확인할 수 있는 set 선언 + HashSet hashSet = new HashSet<>(); + + for (int num: nums) { + // set에 있다면 + if (!hashSet.add(num)) return true; + } + + return false; + } +} \ No newline at end of file From bcb35c2af6e06995c2b52050b57adf5aa8522bd5 Mon Sep 17 00:00:00 2001 From: donghyeon95 Date: Sun, 8 Dec 2024 00:33:49 +0900 Subject: [PATCH 2/6] add solution: Valid Palindrome #220 --- valid-palindrome/donghyeon95.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 valid-palindrome/donghyeon95.java diff --git a/valid-palindrome/donghyeon95.java b/valid-palindrome/donghyeon95.java new file mode 100644 index 000000000..ced3edc7a --- /dev/null +++ b/valid-palindrome/donghyeon95.java @@ -0,0 +1,16 @@ +/* 첫 시도 + leetCode 기준 18ms + */ + +class Solution { + public boolean isPalindrome(String s) { + // 1. remove non-alphanumeric using regex + String alphanumeric = s.replaceAll("[^a-zA-Z0-9]", ""); + + // 2. change lowerCase + String lowerCase = alphanumeric.toLowerCase(); + + // 3. compare reverse String + return lowerCase.contentEquals(new StringBuffer(lowerCase).reverse()); + } +} \ No newline at end of file From fa9d5064aa5824d09f744be7f8262f540e7d494c Mon Sep 17 00:00:00 2001 From: donghyeon95 Date: Sun, 8 Dec 2024 00:40:17 +0900 Subject: [PATCH 3/6] =?UTF-8?q?fix:=20=EA=B0=9C=ED=96=89=20=EB=AC=B8?= =?UTF-8?q?=EC=9E=90=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- contains-duplicate/donghyeon95.java | 3 ++- valid-palindrome/donghyeon95.java | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/contains-duplicate/donghyeon95.java b/contains-duplicate/donghyeon95.java index f4076156d..82d9267b1 100644 --- a/contains-duplicate/donghyeon95.java +++ b/contains-duplicate/donghyeon95.java @@ -27,4 +27,5 @@ public boolean containsDuplicate(int[] nums) { return false; } -} \ No newline at end of file +} + diff --git a/valid-palindrome/donghyeon95.java b/valid-palindrome/donghyeon95.java index ced3edc7a..68a2a42fa 100644 --- a/valid-palindrome/donghyeon95.java +++ b/valid-palindrome/donghyeon95.java @@ -13,4 +13,5 @@ public boolean isPalindrome(String s) { // 3. compare reverse String return lowerCase.contentEquals(new StringBuffer(lowerCase).reverse()); } -} \ No newline at end of file +} + From f7bf00a1e029a98d76277629835c552624bb8631 Mon Sep 17 00:00:00 2001 From: donghyeon95 Date: Mon, 9 Dec 2024 16:03:54 +0900 Subject: [PATCH 4/6] feat: Top K Frequent Elements #237 --- top-k-frequent-elements/donghyeon95.java | 29 ++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 top-k-frequent-elements/donghyeon95.java diff --git a/top-k-frequent-elements/donghyeon95.java b/top-k-frequent-elements/donghyeon95.java new file mode 100644 index 000000000..92f8a2b69 --- /dev/null +++ b/top-k-frequent-elements/donghyeon95.java @@ -0,0 +1,29 @@ +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +class Solution { + public int[] topKFrequent(int[] nums, int k) { + HashMap hm = new HashMap<>(); + + for (int num: nums) { + hm.put(num, hm.getOrDefault(num, 0)+1); + } + + List> list = new ArrayList<>(hm.entrySet()); + list.sort(Map.Entry.comparingByValue(Collections.reverseOrder())); + + int index = 1; + ArrayList answer = new ArrayList<>(); + for (Map.Entry entry: list) { + if (index > k) break; + answer.add(entry.getKey()); + index++; + } + + return answer.stream().mapToInt(Integer::intValue).toArray(); + } +} + From 27d89c9791447cb49f5e654f5ab5c6f4c4d9606d Mon Sep 17 00:00:00 2001 From: donghyeon95 Date: Thu, 12 Dec 2024 20:56:50 +0900 Subject: [PATCH 5/6] feat: Longest Consecutive Sequence #240 --- longest-consecutive-sequence/donghyeon95.java | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 longest-consecutive-sequence/donghyeon95.java diff --git a/longest-consecutive-sequence/donghyeon95.java b/longest-consecutive-sequence/donghyeon95.java new file mode 100644 index 000000000..659a9a992 --- /dev/null +++ b/longest-consecutive-sequence/donghyeon95.java @@ -0,0 +1,34 @@ +import java.util.HashSet; + +public class Solution { + public int longestConsecutive(int[] nums) { + HashSet set = new HashSet<>(); + for (int num : nums) { + set.add(num); + } + + int maxStreak = 0; + + for (int num : nums) { + // 내가 시작 값이라면 + if (!set.contains(num - 1)) { + int currentNum = num; + int currentStreak = 1; + + // 나로부터 연결되는 값을 찾는다. + while (set.contains(currentNum + 1)) { + currentNum++; + currentStreak++; + } + + maxStreak = Math.max(maxStreak, currentStreak); + } + } + + return maxStreak; + } +} + + + + From a41297ff8b8eb64cccf0e8d22e76c54314fb42f6 Mon Sep 17 00:00:00 2001 From: donghyeon95 Date: Sat, 14 Dec 2024 16:25:48 +0900 Subject: [PATCH 6/6] feat: House Robber #264 --- house-robber/donghyeon95.java | 40 +++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 house-robber/donghyeon95.java diff --git a/house-robber/donghyeon95.java b/house-robber/donghyeon95.java new file mode 100644 index 000000000..e7c9fa0d0 --- /dev/null +++ b/house-robber/donghyeon95.java @@ -0,0 +1,40 @@ +import java.util.Arrays; + +class Solution { + private int[] dp; + + public int rob(int[] nums) { + // 점화식 + // f(x) = f(나를 선택) + f(나를 안선택) + // 100개라서 가능은 할 거 같다. + + dp = new int[100]; + + // 0도 가능 하다 + Arrays.fill(dp, -1); + + + return recurse(nums, 0); + + } + + public int recurse(int[] nums, int index) { + // 종료 조건 + if (index >= nums.length) return 0; + + // 이미 한번 처리가 되었다면 + if (dp[index] != -1) return dp[index]; + + int result = 0; + + // 나를 선택하는 경우, + result = Math.max(recurse(nums, index+2)+nums[index], result); + + // 나를 선택하지 않는 경우, + result = Math.max(recurse(nums, index+1), result); + + dp[index] = result; + return result; + } +} +