diff --git a/contains-duplicate/5YoonCheol.java b/contains-duplicate/5YoonCheol.java new file mode 100644 index 000000000..c3c1cbd85 --- /dev/null +++ b/contains-duplicate/5YoonCheol.java @@ -0,0 +1,16 @@ +class Solution { + /** + * 시간 복잡도: O(N) + * 공간 복잡도: O(N) + */ + public boolean containsDuplicate(int[] nums) { + Set set = new HashSet<>(); + + for (int num : nums) { + if (set.contains(num)) return true; + set.add(num); + } + + return false; + } +} diff --git a/house-robber/5YoonCheol.java b/house-robber/5YoonCheol.java new file mode 100644 index 000000000..358308310 --- /dev/null +++ b/house-robber/5YoonCheol.java @@ -0,0 +1,19 @@ +class Solution { + public int rob(int[] nums) { + //배열 길이 0이면 털 수 있는 집이 없음. + if (nums.length == 0) return 0; + //배열 길이가 1이면 한 집만 털 수 있음. + if (nums.length == 1) return nums[0]; + + //동적 계획법으로 풀이 + int[] dp = new int[nums.length]; + dp[0] = nums[0]; + dp[1] = Math.max(nums[0], nums[1]); + + //배열 크기가 2이상일 경우 최대 금액의 범위 확장 + for (int i = 2; i < nums.length; i++) { + dp[i] = Math.max(dp[i - 2] + nums[i], dp[i - 1]); + } + return dp[nums.length - 1]; + } +} diff --git a/longest-consecutive-sequence/5YoonCheol.java b/longest-consecutive-sequence/5YoonCheol.java new file mode 100644 index 000000000..6932478b1 --- /dev/null +++ b/longest-consecutive-sequence/5YoonCheol.java @@ -0,0 +1,34 @@ +import java.util.*; + +class Solution { + public int longestConsecutive(int[] nums) { + if (nums == null || nums.length == 0) return 0; + + //모든 요소 HashSet 삽입 + HashSet set = new HashSet<>(); + for (int num : nums) { + set.add(num); + } + + int longest = 0; + + // 시작지점 체크 + for (int num : nums) { + //배열 요소보다 1 작은 수 가 없는 경우 새로운 시작 지점이 됨 + if (!set.contains(num - 1)) { + int start = num; + int currentLength = 1; + + // 1씩 증가시키면서 연속된 수의 개수 탐색 + while (set.contains(start + 1)) { + start++; + currentLength++; + } + // 기존 longest와 현재 연속된 수를 비교 + longest = Math.max(longest, currentLength); + } + } + + return longest; + } +} diff --git a/top-k-frequent-elements/5YoonCheol.java b/top-k-frequent-elements/5YoonCheol.java new file mode 100644 index 000000000..55e4f1598 --- /dev/null +++ b/top-k-frequent-elements/5YoonCheol.java @@ -0,0 +1,21 @@ +import java.util.*; + +class Solution { + public int[] topKFrequent(int[] nums, int k) { + Map map = new HashMap<>(); + for (int num : nums) { + map.put(num, map.getOrDefault(num, 0) + 1); + } + + //value값을 통한 key 정렬 + List list = new ArrayList<>(map.keySet()); + list.sort((a,b)->map.get(b) - map.get(a)); + + //상위 k개 key 추출 + int[] res = new int[k]; + for (int i = 0; i < k; i++) { + res[i] = list.get(i); + } + return res; + } +} diff --git a/valid-palindrome/5YoonCheol.java b/valid-palindrome/5YoonCheol.java new file mode 100644 index 000000000..ed5a82675 --- /dev/null +++ b/valid-palindrome/5YoonCheol.java @@ -0,0 +1,16 @@ +class Solution { + public boolean isPalindrome(String s) { + //정규식으로 영문,숫자 아닌 경우 "" 치환 + //StringBuilder를 통해 문자열 가공 + //거꾸로 뒤집은 것과 원래의 문자열이 일치할 경우 팰린드롬 + s = s.replaceAll("[^a-zA-Z0-9]", "").toLowerCase(); + StringBuilder sb = new StringBuilder(s); + if(sb.reverse().toString().equals(s)){ + return true; + } + + return false; + } +} + +