Skip to content

Commit aeb9221

Browse files
committed
Week1 solutions
1 parent 8901b1d commit aeb9221

File tree

5 files changed

+106
-0
lines changed

5 files changed

+106
-0
lines changed

contains-duplicate/Yooncheol_Oh.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
/**
3+
* 시간 복잡도: O(N)
4+
* 공간 복잡도: O(N)
5+
*/
6+
public boolean containsDuplicate(int[] nums) {
7+
Set<Integer> set = new HashSet<>();
8+
9+
for (int num : nums) {
10+
if (set.contains(num)) return true;
11+
set.add(num);
12+
}
13+
14+
return false;
15+
}
16+
}

house-robber/Yooncheol_Oh.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public int rob(int[] nums) {
3+
//배열 길이 0이면 털 수 있는 집이 없음.
4+
if (nums.length == 0) return 0;
5+
//배열 길이가 1이면 한 집만 털 수 있음.
6+
if (nums.length == 1) return nums[0];
7+
8+
//동적 계획법으로 풀이
9+
int[] dp = new int[nums.length];
10+
dp[0] = nums[0];
11+
dp[1] = Math.max(nums[0], nums[1]);
12+
13+
//배열 크기가 2이상일 경우 최대 금액의 범위 확장
14+
for (int i = 2; i < nums.length; i++) {
15+
dp[i] = Math.max(dp[i - 2] + nums[i], dp[i - 1]);
16+
}
17+
return dp[nums.length - 1];
18+
}
19+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import java.util.*;
2+
3+
class Solution {
4+
public int longestConsecutive(int[] nums) {
5+
if (nums == null || nums.length == 0) return 0;
6+
7+
//모든 요소 HashSet 삽입
8+
HashSet<Integer> set = new HashSet<>();
9+
for (int num : nums) {
10+
set.add(num);
11+
}
12+
13+
int longest = 0;
14+
15+
// 시작지점 체크
16+
for (int num : nums) {
17+
//배열 요소보다 1 작은 수 가 없는 경우 새로운 시작 지점이 됨
18+
if (!set.contains(num - 1)) {
19+
int start = num;
20+
int currentLength = 1;
21+
22+
// 1씩 증가시키면서 연속된 수의 개수 탐색
23+
while (set.contains(start + 1)) {
24+
start++;
25+
currentLength++;
26+
}
27+
// 기존 longest와 현재 연속된 수를 비교
28+
longest = Math.max(longest, currentLength);
29+
}
30+
}
31+
32+
return longest;
33+
}
34+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import java.util.*;
2+
3+
class Solution {
4+
public int[] topKFrequent(int[] nums, int k) {
5+
Map<Integer, Integer> map = new HashMap<>();
6+
for (int num : nums) {
7+
map.put(num, map.getOrDefault(num, 0) + 1);
8+
}
9+
10+
//value값을 통한 key 정렬
11+
List<Integer> list = new ArrayList<>(map.keySet());
12+
list.sort((a,b)->map.get(b) - map.get(a));
13+
14+
//상위 k개 key 추출
15+
int[] res = new int[k];
16+
for (int i = 0; i < k; i++) {
17+
res[i] = list.get(i);
18+
}
19+
return res;
20+
}
21+
}

valid-palindrome/Yooncheol_Oh.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public boolean isPalindrome(String s) {
3+
//정규식으로 영문,숫자 아닌 경우 "" 치환
4+
//StringBuilder를 통해 문자열 가공
5+
//거꾸로 뒤집은 것과 원래의 문자열이 일치할 경우 팰린드롬
6+
s = s.replaceAll("[^a-zA-Z0-9]", "").toLowerCase();
7+
StringBuilder sb = new StringBuilder(s);
8+
if(sb.reverse().toString().equals(s)){
9+
return true;
10+
}
11+
12+
return false;
13+
}
14+
}
15+
16+

0 commit comments

Comments
 (0)