Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions contains-duplicate/suhyenim.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/* [5th/week01] 217. Contains Duplicate

1. 문제 요약
링크: https://leetcode.com/problems/contains-duplicate/
배열에서 동일한 숫자가 2번 이상 존재하면 true 반환, 아니면 false 반환

2. 풀이 로직
제출1: 반복문을 중첩으로 돌리면서, 동일한 값을 발견하면 바로 true 반환
실패: Time Limit Exceeded 발생
=> 시간 복잡도: O(n^2), 공간 복잡도: O(1)
class Solution {
public boolean containsDuplicate(int[] nums) {
for (int i = 0; i < nums.length; i++) {
for (int j = i + 1; j < nums.length; j++) {
if (nums[i] == nums[j]) {
return true;
}
}
}
return false;
}
}

풀이2: 크기를 오름차순으로 정렬해서, 바로 옆 숫자랑 겹치는지 확인
성공: Time: 21 ms (19.91%), Space: 56.4 MB (98.12%)
=> 시간 복잡도: O(nlog(n)), 공간 복잡도: O(1)
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;
}
}

풀이3: 배열을 순회하면서 HashSet에 겹치는 숫자가 존재하면 true 반환, 아니라면 HashSet에 본(seen) 숫자로 추가
성공: Time: 14 ms (61.51%), Space: 58.2 MB (61.9%)
=> 시간 복잡도: O(n), 공간 복잡도: O(n)
class Solution {
public boolean containsDuplicate(int[] nums) {
HashSet<Integer> seen = new HashSet<>();
for (int i = 0; i < nums.length; i++) {
if (seen.contains(nums[i])) return true;
seen.add(nums[i]);
}
return false;
}
}

3. TIL
그리고 메모리가 충분하고 빠른 속도가 필요할 때, HashSet을 사용해볼 수 있다.
하지만 메모리가 여유롭지 않다면, HashSet을 사용하는 방식보다는 기존의 정렬을 사용하는 방식이 알맞을 수 있다.

*/

import java.util.Arrays;

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;
}
}
61 changes: 61 additions & 0 deletions top-k-frequent-elements/suhyenim.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/* [5th/week01] 347. Top K Frequent Elements

1. 문제 요약
링크: https://leetcode.com/problems/top-k-frequent-elements/description/
가장 빈도가 높은 k개의 숫자를 배열로 반환

2. 풀이 로직
풀이1: 빈도 HashMap을 만들고 -> 높은 빈도 순으로 key 정렬 -> top k까지만 배열로 반환
성공: Time: 17 ms (12.72%), Space: 48.9 MB (34.62%)
=> 시간 복잡도: O(nlogn), 공간 복잡도: O(n)

class Solution {
public int[] topKFrequent(int[] nums, int k) {
Map<Integer, Integer> count = new HashMap<>();
for(int n : nums) {
if(!count.containsKey(n)) {
count.put(n, 1);
} else {
count.put(n, count.get(n) + 1);
}
}

List<Integer> keys = new ArrayList<>(count.keySet());
keys.sort((a, b) -> count.get(b) - count.get(a));

int[] answer = new int[k];
for (int i = 0; i < k; i++){
answer[i] = keys.get(i);
}

return answer;
}
}

3. TIL
value값 기준으로 key를 내림차순 정렬하는 방법: keys.sort((a, b) -> count.get(b) - count.get(a));

*/

class Solution {
public int[] topKFrequent(int[] nums, int k) {
Map<Integer, Integer> count = new HashMap<>();
for(int n : nums) {
if(!count.containsKey(n)) {
count.put(n, 1);
} else {
count.put(n, count.get(n) + 1);
}
}

List<Integer> keys = new ArrayList<>(count.keySet());
keys.sort((a, b) -> count.get(b) - count.get(a));

int[] answer = new int[k];
for (int i = 0; i < k; i++){
answer[i] = keys.get(i);
}

return answer;
}
}
57 changes: 57 additions & 0 deletions two-sum/suhyenim.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/* [5th/week01] 1. Two Sum

1. 문제 요약
링크: https://leetcode.com/problems/two-sum/description/
배열 내 두 숫자의 합이 target이 되는 key들을 배열로 반환
(특이점: 답은 반드시 하나만 존재)

2. 풀이 로직
제출1: 반복문을 중첩으로 돌리면서, 두 숫자의 합이 target일 때의 두 인덱스를 배열로 반환
성공: Time: 45 ms (30.61%), Space: 44.9 MB (63.79%)
=> 시간 복잡도: O(n^2), 공간 복잡도: O(1)
class Solution {
public int[] twoSum(int[] nums, int target) {
for (int i = 0; i < nums.length - 1; i++) {
for (int j = i + 1; j < nums.length; j++) {
if (nums[i] + nums[j] == target)
return new int[]{i, j};
}
}
return null;
}
}

풀이2: target과의 차가 HashMap의 key들 중에 존재하면 두 인덱스를 배열로 반환, 아니라면 HashMap에 value-key쌍으로 추가
성공: 2 ms (98.95%), Space: 45.1 MB (30.45%)
=> 시간 복잡도: O(n), 공간 복잡도: O(n)
class Solution {
public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> indices = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
int complement = target - nums[i];
if (indices.containsKey(complement)) {
int j = indices.get(complement);
return new int[]{j, i};
}
indices.put(nums[i], i);
}
return null;
}
}

3. TIL
HashTable을 활용해서 key-value 자료구조를 구현할 수 있다.

*/

class Solution {
public int[] twoSum(int[] nums, int target) {
for (int i = 0; i < nums.length - 1; i++) {
for (int j = i + 1; j < nums.length; j++) {
if (nums[i] + nums[j] == target)
return new int[]{i, j};
}
}
return null;
}
}