Skip to content

Commit 5589fc9

Browse files
authored
Merge pull request #415 from jaejeong1/main
[jaejeong1] Week 04 Solutions
2 parents ee59d0d + 232f11a commit 5589fc9

File tree

4 files changed

+167
-0
lines changed

4 files changed

+167
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import java.util.HashSet;
2+
import java.util.Set;
3+
4+
class SolutionLongestConsecutiveSequence {
5+
6+
public int longestConsecutive(int[] nums) {
7+
// 정렬되지 않은 정수 nums 배열이 주어지면 가장 긴 연속 요소 시퀀스 길이를 반환
8+
// O(N) 시간 내 실행되야함
9+
// 전부 해시맵에 때려넣고, 키를 꺼내 연속 요소가 있는지 확인한다
10+
// 연속 요소가 있으면 answer를 1 증가시키고, 연속 요소는 제거한다
11+
// 시간복잡도: O(N), 공간복잡도: O(N)
12+
13+
Set<Integer> set = new HashSet<>();
14+
for (var num : nums) {
15+
set.add(num);
16+
}
17+
var answer = 0;
18+
for (var num : nums) {
19+
var length = 1;
20+
21+
if (set.contains(num-1)) {
22+
set.remove(num);
23+
var minusKey = num;
24+
while (set.contains(--minusKey)) {
25+
length++;
26+
set.remove(minusKey);
27+
}
28+
}
29+
30+
if (set.contains(num+1)) {
31+
set.remove(num);
32+
var plusKey = num;
33+
while (set.contains(++plusKey)) {
34+
length++;
35+
set.remove(plusKey);
36+
}
37+
}
38+
39+
if (length > answer) {
40+
answer = length;
41+
}
42+
}
43+
44+
return answer;
45+
}
46+
}

missing-number/jaejeong1.java

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class SolutionMissingNumber {
2+
public int missingNumber(int[] nums) {
3+
// N = 배열의 길이
4+
// 0 ~ N 의 합을 구하고, nums의 모든 값을 다 빼면 정답
5+
// 시간복잡도: O(N), 공간복잡도: O(1)
6+
var N = nums.length;
7+
var sum = (N * (N+1)) / 2;
8+
9+
for (var num : nums) {
10+
sum -= num;
11+
}
12+
13+
return sum;
14+
}
15+
}

valid-palindrome/jaejeong1.java

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
class SolutionValidPalindrome {
2+
3+
public boolean isPalindrome(String s) {
4+
// 대문자를 소문자로 변환
5+
// 영문자와 숫자만 남기고 모두 제거
6+
// 앞뒤로 읽어도 같은 경우 팰린드롬
7+
// 종류 별 처리 방법
8+
// 공백: 무시
9+
// 소문자, 숫자: 통과
10+
// 그 외: 무시
11+
// 종료 조건: lt >= rt
12+
// 시간복잡도: O(N), 공간복잡도: O(1)
13+
var idxLt = 0;
14+
var idxRt = s.length() - 1;
15+
16+
char charLt;
17+
char charRt;
18+
19+
while (idxLt < idxRt) {
20+
charLt = s.charAt(idxLt);
21+
charRt = s.charAt(idxRt);
22+
23+
// 영문 또는 숫자일때까지 lt++
24+
while(!Character.isLetterOrDigit(charLt) && idxLt < s.length() - 1) {
25+
idxLt++;
26+
charLt = s.charAt(idxLt);
27+
}
28+
29+
// 영문 또는 숫자일때까지 rt--
30+
while(!Character.isLetterOrDigit(charRt) && idxRt > 0) {
31+
idxRt--;
32+
charRt = s.charAt(idxRt);
33+
}
34+
35+
// 대문자면 소문자로 변환 + lt, rt 범위가 겹쳤거나 lt와 rt 가 다르면 false 반환
36+
if (idxLt < idxRt) {
37+
if (Character.isUpperCase(charLt)) {
38+
charLt = Character.toLowerCase(charLt);
39+
}
40+
41+
if (Character.isUpperCase(charRt)) {
42+
charRt = Character.toLowerCase(charRt);
43+
}
44+
45+
if (charLt != charRt) {
46+
return false;
47+
}
48+
}
49+
50+
idxLt++;
51+
idxRt--;
52+
}
53+
54+
return true;
55+
}
56+
}

word-search/jaejeong.java

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
class SolutionWordSearch {
2+
3+
int[] dx = new int[]{0, 0, -1, 1};
4+
int[] dy = new int[]{-1, 1, 0, 0};
5+
6+
public boolean exist(char[][] board, String word) {
7+
// 상하좌우 방문 여부 체크하면서 DFS 탐색
8+
// index 기준으로 word와 비교하면서 같을 때만 추가 탐색
9+
// 시간복잡도: O(M * N * 4^L) > M: board 행, N: board 열, 4: 상하좌우 4방향, L: word의 길이
10+
// 공간복잡도: O(L) > word의 길이
11+
for (int i = 0; i < board.length; i++) {
12+
for (int j = 0; j < board[0].length; j++) {
13+
if (dfs(board, i, j, word, 0)) {
14+
return true;
15+
}
16+
}
17+
}
18+
return false;
19+
}
20+
21+
public boolean dfs(char[][] board, int x, int y, String word, int index) {
22+
if (index >= word.length()) {
23+
return true;
24+
}
25+
// board 밖이면 return false
26+
if (x >= board.length || y >= board[0].length || x < 0 || y < 0) {
27+
return false;
28+
}
29+
// 이미 방문했거나 정답 조건에 맞지 않으면 return false
30+
if (board[x][y] != word.charAt(index)) {
31+
return false;
32+
}
33+
34+
// 현재 위치의 문자를 임시로 변경하여 방문 처리
35+
char temp = board[x][y];
36+
board[x][y] = '#';
37+
38+
index++;
39+
for (int i = 0; i < 4; i++) {
40+
if (dfs(board, x + dx[i], y + dy[i], word, index)) {
41+
return true;
42+
}
43+
}
44+
45+
// 방문 처리를 되돌림
46+
board[x][y] = temp;
47+
48+
return false;
49+
}
50+
}

0 commit comments

Comments
 (0)