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); 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]; 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<>(); 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<>(); 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<>();