diff --git a/contains-duplicate/se6816.java b/contains-duplicate/se6816.java new file mode 100644 index 0000000000..bb0507b5e2 --- /dev/null +++ b/contains-duplicate/se6816.java @@ -0,0 +1,15 @@ +/** +Problem 217 : contains duplicate +Summary : 주어진 배열에서 중복이 있으면 true, 없으면 false + +*/ + +class Solution { + public boolean containsDuplicate(int[] nums) { + long count = Arrays.stream(nums) + .distinct() + .count(); + + return count != nums.length; + } +} diff --git a/house-robber/se6816.java b/house-robber/se6816.java new file mode 100644 index 0000000000..8b050899ee --- /dev/null +++ b/house-robber/se6816.java @@ -0,0 +1,22 @@ +/** +Problem 198 : House Robber +Summary : +- dp를 이용하여, 이전 결과를 저장하면서 문제를 해결한다. + +*/ + +class Solution { + public int rob(int[] nums) { + int[] dp = new int[nums.length+1]; + dp[1] = nums[0]; + if(nums.length != 1) { + dp[2] = nums[1]; + } + + for(int i = 2; i < nums.length; i++) { + dp[i+1] = Math.max((nums[i] + dp[i-1]), (nums[i] + dp[i-2])); + } + + return Math.max(dp[nums.length], dp[nums.length-1]); + } +} diff --git a/longest-consecutive-sequence/se6816.java b/longest-consecutive-sequence/se6816.java new file mode 100644 index 0000000000..7a21bd02c4 --- /dev/null +++ b/longest-consecutive-sequence/se6816.java @@ -0,0 +1,43 @@ +/** +Problem 128 : Longest Consecutive Sequence +Summary : +- set에 모든 숫자를 저장한다. +- 매개변수로 주어진 배열을 돌면서 해당 숫자의 이전 숫자가 set에 존재하는지 확인한다. +- 연속적인 숫자 배열의 길이를 구하고, 별도의 set에 기록한다. +- 해당 방법을 사용하면 시간복잡도가 O(N)이 된다. + +*/ +class Solution { + public int longestConsecutive(int[] nums) { + int max = 0; + + Set set = new HashSet<>(); + for(int num : nums) { + set.add(num); + } + Set completeSet = new HashSet<>(); + for(int i = 0; i < nums.length; i++) { + if(!set.contains(nums[i]-1) && !completeSet.contains(nums[i])){ + int sequence = getSequence(nums[i], set); + max= Math.max(sequence, max); + completeSet.add(nums[i]); + } + } + + return max; + + + } + + public int getSequence(int startNum, Set numSet) { + int result = 1; + while(true) { + startNum++; + if(!numSet.contains(startNum)) { + break; + } + result++; + } + return result; + } +} diff --git a/top-k-frequent-elements/se6816.java b/top-k-frequent-elements/se6816.java new file mode 100644 index 0000000000..ea48adca6c --- /dev/null +++ b/top-k-frequent-elements/se6816.java @@ -0,0 +1,73 @@ +/** +Problem 347 : Top K Frequent Elements +Summary : +- Map을 통해 숫자의 중복 횟수를 구한다. +- 배열을 통해, 중복 횟수를 인덱스로 해당 숫자를 연결 리스트를 통해 값을 저장한다. +- 중복 횟수를 저장한 배열에서 마지막 인덱스부터 for문을 돌리면서 k개까지 리턴 배열에 숫자를 저장한다. +- 제약 조건에서 응답이 유일하다는 것을 보장하고 있으므로, 개수가 부족하거나, 중복에 대해서는 고려하지 않아도 된다. +- 정렬을 이용하면 최소한 시간복잡도가 O(NlogN)이지만 해당 방법은 O(N)이 가능하다. + +*/ + +class Solution { + class Node { + int num; + Node next; + public Node(int n){ + num = n; + } + } + class NodeList { + Node head; + Node tail; + + public NodeList() { + } + + public void add(Node node){ + if(head == null){ + head = node; + tail = node; + return; + } + + tail.next = node; + tail = tail.next; + } + + public boolean empty() { + return head == null; + } + } + + public int[] topKFrequent(int[] nums, int k) { + NodeList[] list = new NodeList[nums.length+1]; + int[] result = new int[k]; + Map map= new HashMap<>(); + + for(int i=0; i < list.length; i++) { + list[i] = new NodeList(); + } + + for(int i = 0; i < nums.length; i++) { + map.put(nums[i], map.getOrDefault(nums[i], 0) + 1); + } + + for(Map.Entry entry : map.entrySet()) { + list[entry.getValue()].add(new Node(entry.getKey())); + } + + int idx = 0; + for(int i=list.length-1; (i >= 0) && (idx < k); i--) { + if(!list[i].empty()) { + Node head = list[i].head; + while(head != null) { + result[idx] = head.num; + head = head.next; + idx++; + } + } + } + return result; + } +} diff --git a/two-sum/se6816.java b/two-sum/se6816.java new file mode 100644 index 0000000000..9093d2485b --- /dev/null +++ b/two-sum/se6816.java @@ -0,0 +1,24 @@ +/** +Problem 1 : Two Sum +Summary : +- for문으로 순회하면서, target에서 뺀 값을 저장한다. +- 저장된 값과 일치하는 인덱스를 만나면 해당 값을 리턴한다. +- 기본 For문이 O(N^2)이라면, 해당 방법의 경우 O(N)이 가능하다. + +*/ + +class Solution { + + public int[] twoSum(int[] nums, int target) { + Map indexMap = new HashMap<>(); + int[] result = null; + for(int idx=0; idx