diff --git a/contains-duplicate/std-freejia.java b/contains-duplicate/std-freejia.java new file mode 100644 index 000000000..6f8941c6c --- /dev/null +++ b/contains-duplicate/std-freejia.java @@ -0,0 +1,13 @@ +class Solution { + public boolean containsDuplicate(int[] nums) { + Set set = new HashSet<>(); + + for (int i = 0; i < nums.length; i++) { + if (i > 0 && set.contains(nums[i])) { + return true; + } + set.add(nums[i]); + } + return false; + } +} diff --git a/house-robber/std-freejia.java b/house-robber/std-freejia.java new file mode 100644 index 000000000..630808ef8 --- /dev/null +++ b/house-robber/std-freejia.java @@ -0,0 +1,15 @@ +class Solution { + public int rob(int[] nums) { + int numsLen = nums.length; + int[] dp = new int[numsLen]; + + if (numsLen == 1) return nums[0]; + dp[0] = nums[0]; + dp[1] = Math.max(nums[0], nums[1]); + + for (int i = 2; i < numsLen; i++){ + dp[i] = Math.max(dp[i-2] + nums[i], dp[i-1]); + } + return dp[numsLen-1]; + } +} diff --git a/longest-consecutive-sequence/std-freejia.java b/longest-consecutive-sequence/std-freejia.java new file mode 100644 index 000000000..cc7789276 --- /dev/null +++ b/longest-consecutive-sequence/std-freejia.java @@ -0,0 +1,25 @@ +/** 시간초과 발생합니다. 해법 찾는 중입니다 */ +class Solution { + public int longestConsecutive(int[] nums) { + + Set uniqueNum = new HashSet<>(); + for (int num : nums) { // 수열 내 중복 숫자 제거 + uniqueNum.add(num); + } + + int uniqueLen = uniqueNum.size(); + int longest = 0; + + for(int targetNum: uniqueNum){ + + int n = 1; + for(int j = targetNum + 1; j < targetNum + uniqueLen; j++) { + if (!uniqueNum.contains(j)) break; + n++; + } + longest = Math.max(n, longest); + + } + return longest; + } +} diff --git a/top-k-frequent-elements/std-freejia.java b/top-k-frequent-elements/std-freejia.java new file mode 100644 index 000000000..4667b7c44 --- /dev/null +++ b/top-k-frequent-elements/std-freejia.java @@ -0,0 +1,18 @@ +class Solution { + public int[] topKFrequent(int[] nums, int k) { + Map map = new HashMap<>(); + for (int i = 0; i < nums.length; i++) { + map.put(nums[i], map.getOrDefault(nums[i], 0) + 1); + } + // value 를 기준으로 정렬 + List> entrySet = new ArrayList<>(map.entrySet()); + entrySet.sort((a, b) -> b.getValue().compareTo(a.getValue())); + + int[] result = new int[k]; + for (int i = 0; i < k; i++) { + + result[i] = entrySet.get(i).getKey(); + } + return result; + } +} diff --git a/two-sum/std-freejia.java b/two-sum/std-freejia.java new file mode 100644 index 000000000..c904793a1 --- /dev/null +++ b/two-sum/std-freejia.java @@ -0,0 +1,16 @@ +class Solution { + public static int[] twoSum(int[] nums, int target) { + HashMap map = new HashMap<>(); + + for (int first = 0; first < nums.length; first++) { + int targetKey = target - nums[first]; + if (map.containsKey(targetKey)) { + int second = map.get(targetKey); + return new int[]{second, first}; + } + map.put(nums[first], first); + } + + return null; + } +}