From cd99fcbb086385f03f280b1e1f2a9e453f1c5124 Mon Sep 17 00:00:00 2001 From: Tessa1217 Date: Sun, 30 Mar 2025 15:43:45 +0900 Subject: [PATCH 1/7] add: contains duplicate solution --- contains-duplicate/Tessa1217.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 contains-duplicate/Tessa1217.java diff --git a/contains-duplicate/Tessa1217.java b/contains-duplicate/Tessa1217.java new file mode 100644 index 000000000..8767618ee --- /dev/null +++ b/contains-duplicate/Tessa1217.java @@ -0,0 +1,16 @@ +import java.util.HashSet; +import java.util.Set; + +class Solution { + + public boolean containsDuplicate(int[] nums) { + + Set distincts = new HashSet<>(); + + for (int i = 0; i < nums.length; i++) { + distincts.add(nums[i]); + } + + return distincts.size() != nums.length; + } +} From 8377a45d5d616fa4c55441caffd452c8d52b2c5c Mon Sep 17 00:00:00 2001 From: Tessa1217 Date: Sun, 30 Mar 2025 15:56:33 +0900 Subject: [PATCH 2/7] update: add comments --- contains-duplicate/Tessa1217.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/contains-duplicate/Tessa1217.java b/contains-duplicate/Tessa1217.java index 8767618ee..fd23ca27b 100644 --- a/contains-duplicate/Tessa1217.java +++ b/contains-duplicate/Tessa1217.java @@ -3,6 +3,10 @@ class Solution { + /** 217. 중복된 수 + * 정수 배열 nums가 주어졌을 때 배열 요소 중 한 개 이상이 두 번 이상 중복되어 + * 나타는 경우 true를, 모든 배열의 요소가 고유한 경우 false를 반환 + */ public boolean containsDuplicate(int[] nums) { Set distincts = new HashSet<>(); @@ -14,3 +18,4 @@ public boolean containsDuplicate(int[] nums) { return distincts.size() != nums.length; } } + From 79067b6694add610a476aaccc1b65dd7e3e1f052 Mon Sep 17 00:00:00 2001 From: Tessa1217 Date: Mon, 31 Mar 2025 19:55:44 +0900 Subject: [PATCH 3/7] add: two sum solution --- two-sum/Tessa1217.java | 48 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 two-sum/Tessa1217.java diff --git a/two-sum/Tessa1217.java b/two-sum/Tessa1217.java new file mode 100644 index 000000000..944de102b --- /dev/null +++ b/two-sum/Tessa1217.java @@ -0,0 +1,48 @@ +import java.util.Map; +import java.util.HashMap; + +class Solution { + // 정수 배열 nums와 정수 target가 주어질 때 두 정수의 합이 target이 되는 배열 요소의 인덱스를 반환 + // 입력받은 배열에는 하나의 해답만 존재한다고 가정할 수 있으며 같은 요소를 한 번 이상 사용할 수는 없다. + // 반환하는 인덱스의 정렬은 신경쓰지 않아도 된다. + + public int[] twoSum(int[] nums, int target) { + + // 힌트를 참고하여 시간 복잡도 O(n^2) 이하로 줄이기 + // 힌트: Like maybe a hash map to speed up the search? + + int[] answer = new int[2]; + + Map numMap = new HashMap<>(); + + for (int i = 0; i < nums.length; i++) { + if (numMap.containsKey(target - nums[i])) { + answer[0] = numMap.get(target - nums[i]); + answer[1] = i; + break; + } + numMap.put(nums[i], i); + } + + return answer; + + } + + // public int[] twoSum(int[] nums, int target) { + + // // 전체 탐색 진행 + // int[] answer = new int[2]; + + // for (int i = 0; i < nums.length - 1; i++) { + // for (int j = i + 1; j < nums.length; j++) { + // if (nums[i] + nums[j] == target) { + // answer[0] = i; + // answer[1] = j; + // } + // } + // } + + // return answer; + // } + +} From 89fbff90189006af2b2b7d6d83b3ee8bf2c94a37 Mon Sep 17 00:00:00 2001 From: Tessa1217 Date: Mon, 31 Mar 2025 19:57:20 +0900 Subject: [PATCH 4/7] add: house robber solution --- house-robber/Tessa1217.java | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 house-robber/Tessa1217.java diff --git a/house-robber/Tessa1217.java b/house-robber/Tessa1217.java new file mode 100644 index 000000000..f50b97c8d --- /dev/null +++ b/house-robber/Tessa1217.java @@ -0,0 +1,24 @@ +class Solution { + + // 전문적인 강도인 당신은 거리에 있는 집들을 털 계획을 세우고 있다. + // 조건: 인접한 집들은 연결된 보안 경비 시스팀이 있어 인접한 집을 같은 날에 털 경우 자동적으로 경찰에 연락이 간다. + // 각 집에 쌓아둔 돈의 양 배열 (정수 배열)이 주어질 때 경찰한테 걸리지 않고 털 수 있는 최대 돈의 양을 반환하시오. + public int rob(int[] nums) { + + // 조건: 1 == nums.length (털 집이 한 곳 뿐) + if (nums.length == 1) { + return nums[0]; + } + + // DP로 계산 + nums[1] = Math.max(nums[0], nums[1]); + + for (int i = 2; i < nums.length; i++) { + nums[i] = Math.max(nums[i - 1], nums[i - 2] + nums[i]); + } + + return nums[nums.length - 1]; + + } + +} \ No newline at end of file From 32db53be68dc086c7275342c8e3b528341049015 Mon Sep 17 00:00:00 2001 From: Tessa1217 Date: Mon, 31 Mar 2025 19:59:05 +0900 Subject: [PATCH 5/7] update: add line break --- house-robber/Tessa1217.java | 3 ++- two-sum/Tessa1217.java | 2 ++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/house-robber/Tessa1217.java b/house-robber/Tessa1217.java index f50b97c8d..18aa0afb8 100644 --- a/house-robber/Tessa1217.java +++ b/house-robber/Tessa1217.java @@ -21,4 +21,5 @@ public int rob(int[] nums) { } -} \ No newline at end of file +} + diff --git a/two-sum/Tessa1217.java b/two-sum/Tessa1217.java index 944de102b..b94a74378 100644 --- a/two-sum/Tessa1217.java +++ b/two-sum/Tessa1217.java @@ -46,3 +46,5 @@ public int[] twoSum(int[] nums, int target) { // } } + + From b9037b5d16a0959a382fa27b08aea931981c7052 Mon Sep 17 00:00:00 2001 From: Tessa1217 Date: Thu, 3 Apr 2025 20:42:23 +0900 Subject: [PATCH 6/7] add: longest consecutive sequence solution --- longest-consecutive-sequence/Tessa1217.java | 32 +++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 longest-consecutive-sequence/Tessa1217.java diff --git a/longest-consecutive-sequence/Tessa1217.java b/longest-consecutive-sequence/Tessa1217.java new file mode 100644 index 000000000..60a998a17 --- /dev/null +++ b/longest-consecutive-sequence/Tessa1217.java @@ -0,0 +1,32 @@ + +/** + * 정렬되지 않은 정수 배열 nums가 주어질 때 가장 긴 연속적인 요소 배열의 길이를 반환 + * */ +import java.util.Arrays; +public class Solution { + + public int longestConsecutive(int[] nums) { + + if (nums.length == 0) { + return 0; + } + + Arrays.sort(nums); + + int maxCnt = 1; + int cnt = 1; + for (int i = 0; i < nums.length - 1; i++) { + if (nums[i] == nums[i + 1]) { + continue; + } + if (nums[i] + 1 == nums[i + 1]) { + cnt++; + } else { + cnt = 1; + } + maxCnt = Math.max(maxCnt, cnt); + } + return maxCnt; + } + +} From f58ae303d9ff9af9aac15f90e96489ed1604183f Mon Sep 17 00:00:00 2001 From: Tessa1217 Date: Thu, 3 Apr 2025 20:45:29 +0900 Subject: [PATCH 7/7] add: top k frequent elements solution --- top-k-frequent-elements/Tessa1217.java | 34 ++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 top-k-frequent-elements/Tessa1217.java diff --git a/top-k-frequent-elements/Tessa1217.java b/top-k-frequent-elements/Tessa1217.java new file mode 100644 index 000000000..74fa3be7f --- /dev/null +++ b/top-k-frequent-elements/Tessa1217.java @@ -0,0 +1,34 @@ + +/** + * 정수 배열 nums와 정수 k가 주어질 때 자주 반복되는 값을 K개 반환 + * */ +import java.util.Map; +import java.util.HashMap; +import java.util.Arrays; + +public 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); + } + + int[][] arr = map.entrySet().stream() + .map((e) -> new int[]{e.getKey(), e.getValue()}) + .toArray(int[][]::new); + + Arrays.sort(arr, (f1, f2) -> f2[1] - f1[1]); + + int[] frequency = new int[k]; + for (int i = 0; i < k; i++) { + frequency[i] = arr[i][0]; + } + + return frequency; + + } + +} + +