diff --git a/contains-duplicate/dalpang81.java b/contains-duplicate/dalpang81.java new file mode 100644 index 000000000..2a8269ab3 --- /dev/null +++ b/contains-duplicate/dalpang81.java @@ -0,0 +1,11 @@ +//시간복잡도 : O(nlogn) +class Solution { + public boolean containsDuplicate(int[] nums) { + Arrays.sort(nums); + for(int i = 0; i < nums.length - 1; i++) { + if(nums[i] == nums[i+1]) + return true; + } + return false; + } +} diff --git a/house-robber/dalpang81.java b/house-robber/dalpang81.java new file mode 100644 index 000000000..e5f76b68a --- /dev/null +++ b/house-robber/dalpang81.java @@ -0,0 +1,19 @@ +//시간복잡도 O(n) +class Solution { + public int rob(int[] nums) { + if (nums.length == 1) { + return nums[0]; + } + + int temp2 = nums[0]; + int temp1 = Math.max(nums[0], nums[1]); + + for (int i = 2; i < nums.length; i++) { + int current = Math.max(temp1, nums[i] + temp2); + temp2 = temp1; + temp1 = current; + } + + return temp1; + } +} diff --git a/longest-consecutive-sequence/dalpang81.java b/longest-consecutive-sequence/dalpang81.java new file mode 100644 index 000000000..d5cfa414f --- /dev/null +++ b/longest-consecutive-sequence/dalpang81.java @@ -0,0 +1,33 @@ +//시간복잡도: O(n) +import java.util.*; + +class Solution { + public int longestConsecutive(int[] nums) { + Set numSet = new HashSet<>(); + for (int num : nums) { + numSet.add(num); + } + + int longNum = 0; + + // 각 숫자에 대해 시퀀스 시작 여부를 확인 + for (int num : numSet) { + // num-1이 없는 경우에만 시퀀스를 시작 + if (!numSet.contains(num - 1)) { + int currentNum = num; + int currentLong = 1; + + // 연속된 숫자를 탐색 + while (numSet.contains(currentNum + 1)) { + currentNum++; + currentLong++; + } + + // 가장 긴 시퀀스를 갱신 + longNum = Math.max(longNum, currentLong); + } + } + + return longNum; + } +} diff --git a/top-k-frequent-elements/dalpang81.java b/top-k-frequent-elements/dalpang81.java new file mode 100644 index 000000000..569869398 --- /dev/null +++ b/top-k-frequent-elements/dalpang81.java @@ -0,0 +1,31 @@ +//시간복잡도: O(n + mlogk) +import java.util.*; +class Solution { + public int[] topKFrequent(int[] nums, int k) { + Map map = new HashMap<>(); + + + for(int i : nums) { + map.put(i, map.getOrDefault(i,0) + 1); + } + + PriorityQueue> pq = + new PriorityQueue<>(Comparator.comparingInt(Map.Entry::getValue)); + + for (Map.Entry entry : map.entrySet()) { + pq.offer(entry); + if (pq.size() > k) { + pq.poll(); // Remove the least frequent element + } + } + + // Step 3: Extract the elements from the heap + int[] result = new int[k]; + for (int i = k - 1; i >= 0; i--) { + result[i] = pq.poll().getKey(); + } + + return result; + + } +} diff --git a/valid-palindrome/dalpang81.java b/valid-palindrome/dalpang81.java new file mode 100644 index 000000000..c2ef2bbea --- /dev/null +++ b/valid-palindrome/dalpang81.java @@ -0,0 +1,13 @@ +//시간복잡도: O(n) +class Solution { + public boolean isPalindrome(String s) { + s = s.toLowerCase().trim(); + s = s.replaceAll("[^a-z0-9]", ""); + + StringBuffer sb = new StringBuffer(s); + String reverse = sb.reverse().toString(); + + return(s.equals(reverse)); + + } +}