From 3568b91227533ac3c2753e0527bcdb3acef0130b Mon Sep 17 00:00:00 2001 From: sora0319 Date: Sat, 26 Jul 2025 14:44:19 +0900 Subject: [PATCH 1/5] solve contains duplicate problem --- contains-duplicate/sora0319.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/contains-duplicate/sora0319.java b/contains-duplicate/sora0319.java index e94573295..fba6784e3 100644 --- a/contains-duplicate/sora0319.java +++ b/contains-duplicate/sora0319.java @@ -1,4 +1,21 @@ import java.util.*; +// 개선 방향 +class Solution { + public boolean containsDuplicate(int[] nums) { + Set duplication = new HashSet<>(); + + for(int n : nums){ + if(duplication.contains(n)){ + return true; + } + duplication.add(n); + } + return false; + } +} + + +// 초기 문제 풀이 class Solution { public boolean containsDuplicate(int[] nums) { Arrays.sort(nums); From fe794af00894dd0969607004983bb15b616df2fd Mon Sep 17 00:00:00 2001 From: sora0319 Date: Sat, 26 Jul 2025 17:39:40 +0900 Subject: [PATCH 2/5] solve two sum problem --- two-sum/sora0319.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/two-sum/sora0319.java b/two-sum/sora0319.java index 061f8424c..65fefc645 100644 --- a/two-sum/sora0319.java +++ b/two-sum/sora0319.java @@ -1,4 +1,23 @@ import java.util.*; + +// 개선안 +class Solution { + public int[] twoSum(int[] nums, int target) { + Map checkNums = new HashMap<>(); + + for(int i = 0; i < nums.length; i++){ + if(checkNums.containsKey(target - nums[i])){ + int place = checkNums.get(target - nums[i]); + return new int[]{place, i}; + } + checkNums.put(nums[i], i); + } + return new int[]{}; + } +} + + +// 초기 구성안 class Solution { public int[] twoSum(int[] nums, int target) { Map element = new HashMap<>(); From dd0a5aae4988af1aeb678f77dd121059b3cbf42a Mon Sep 17 00:00:00 2001 From: sora0319 Date: Sat, 26 Jul 2025 19:43:38 +0900 Subject: [PATCH 3/5] solve top k frequent elements problem --- top-k-frequent-elements/sora0319.java | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/top-k-frequent-elements/sora0319.java b/top-k-frequent-elements/sora0319.java index cd772e1f1..8c8ff7e4c 100644 --- a/top-k-frequent-elements/sora0319.java +++ b/top-k-frequent-elements/sora0319.java @@ -1,5 +1,29 @@ import java.util.*; +// 다른 방안 +class Solution { + public int[] topKFrequent(int[] nums, int k) { + Map counting = new HashMap<>(); + + for(int n : nums){ + if(!counting.containsKey(n)){ + counting.put(n,0); + } + counting.put(n, counting.get(n)+1); + } + + List>countList = new LinkedList<>(counting.entrySet()); + countList.sort(Map.Entry.comparingByValue(Comparator.reverseOrder())); + + int[] answer = countList.stream() + .limit(k) + .mapToInt(Map.Entry::getKey) + .toArray(); + return answer; + } +} + +// 초안 class Solution { public int[] topKFrequent(int[] nums, int k) { Map counts = new HashMap<>(); From 615e84a9a8ddcdff34aa1a795fe66e895b68b2bf Mon Sep 17 00:00:00 2001 From: sora0319 Date: Sat, 26 Jul 2025 20:20:53 +0900 Subject: [PATCH 4/5] solve longest consecutive sequence --- longest-consecutive-sequence/sora0319.java | 23 ++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/longest-consecutive-sequence/sora0319.java b/longest-consecutive-sequence/sora0319.java index e767709e3..fe192e509 100644 --- a/longest-consecutive-sequence/sora0319.java +++ b/longest-consecutive-sequence/sora0319.java @@ -1,5 +1,28 @@ import java.util.*; +// 2번째 푼 코드 +class Solution { + public int longestConsecutive(int[] nums) { + Set set = new HashSet<>(); + for (int n : nums) { + set.add(n); + } + + int cntMax = 0; + for (int n : set) { + if (!set.contains(n - 1)) { + int end = n; + while (set.contains(end + 1)) { + end++; + } + cntMax = Math.max(cntMax, end - n + 1); + } + } + + return cntMax; + } +} +// 처음 풀어본 코드 class Solution { public int longestConsecutive(int[] nums) { Set checkList = new HashSet<>(); From dcd9377bacded0811e8642220af35fb936218c72 Mon Sep 17 00:00:00 2001 From: sora0319 Date: Sat, 26 Jul 2025 21:05:55 +0900 Subject: [PATCH 5/5] solve house robber problem --- house-robber/sora0319.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/house-robber/sora0319.java b/house-robber/sora0319.java index b90c85fe4..f68943269 100644 --- a/house-robber/sora0319.java +++ b/house-robber/sora0319.java @@ -1,4 +1,20 @@ import java.util.*; +// 2번째 풀이 +class Solution { + public int rob(int[] nums) { + int[] dp = new int[nums.length+1]; + dp[1] = nums[0]; + + for(int i = 1; i < nums.length; i++){ + dp[i+1] = Math.max(dp[i-1] + nums[i], dp[i]); + } + return dp[nums.length]; + } +} + + + +// 1번째 풀이 class Solution { public int rob(int[] nums) { int[] house = new int[nums.length];