Skip to content

Commit b5b46ff

Browse files
authored
Merge pull request #1463 from Tessa1217/main
[Tessa1217] Week 07 Solutions
2 parents 7debfb4 + e75a811 commit b5b46ff

File tree

5 files changed

+275
-0
lines changed

5 files changed

+275
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* 문자열 s가 주어질 때 중복 문자가 없는 가장 긴 문자열 길이를 반환하세요.
3+
* */
4+
class Solution {
5+
// 시간복잡도: O(n)
6+
public int lengthOfLongestSubstring(String s) {
7+
8+
int maxLength = 0;
9+
10+
int left = 0;
11+
int right = 0;
12+
13+
// 알파벳 (대소문자), 숫자, 특수문자, 공백
14+
boolean[] visited = new boolean[128];
15+
16+
while (right < s.length()) {
17+
while (visited[s.charAt(right)]) {
18+
visited[s.charAt(left)] = false;
19+
left++;
20+
}
21+
visited[s.charAt(right)] = true;
22+
maxLength = Math.max(right - left + 1, maxLength);
23+
right++;
24+
}
25+
26+
return maxLength;
27+
}
28+
}
29+

number-of-islands/Tessa1217.java

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import java.util.LinkedList;
2+
import java.util.Queue;
3+
4+
/**
5+
* m x n 2D 행렬 grid가 주어질 때 섬의 수를 찾아서 반환하세요.
6+
* 섬(island)는 물(0)로 둘러싸여 있고 가로 또는 세로로 인접한 섬들과 연결되어 형성되어 있다.
7+
*/
8+
class Solution {
9+
10+
int[] dx = {-1, 1, 0, 0};
11+
int[] dy = {0, 0, 1, -1};
12+
13+
public int numIslands(char[][] grid) {
14+
int cnt = 0;
15+
for (int i = 0; i < grid.length; i++) {
16+
for (int j = 0; j < grid[i].length; j++) {
17+
// land라면
18+
if (grid[i][j] == '1') {
19+
20+
// bfs로 섬 탐색
21+
// bfs(i, j, grid);
22+
23+
// dfs로 섬 탐색
24+
dfs(i, j, grid);
25+
26+
cnt++;
27+
}
28+
}
29+
}
30+
return cnt;
31+
}
32+
33+
private void dfs(int x, int y, char[][] grid) {
34+
if (grid[x][y] == '1') {
35+
grid[x][y] = '0';
36+
}
37+
for (int i = 0; i < 4; i++) {
38+
int nx = x + dx[i];
39+
int ny = y + dy[i];
40+
if (nx >= 0 && nx < grid.length && ny >= 0 && ny < grid[0].length && grid[nx][ny] == '1') {
41+
dfs(nx, ny, grid);
42+
}
43+
}
44+
}
45+
46+
private void bfs(int startX, int startY, char[][] grid) {
47+
48+
Queue<int[]> queue = new LinkedList<>();
49+
queue.offer(new int[]{startX, startY});
50+
grid[startX][startY] = '0';
51+
52+
while (!queue.isEmpty()) {
53+
int[] current = queue.poll();
54+
for (int i = 0; i < 4; i++) {
55+
int newX = current[0] + dx[i];
56+
int newY = current[1] + dy[i];
57+
if (newX >= 0 && newX < grid.length && newY >= 0 && newY < grid[0].length) {
58+
if (grid[newX][newY] == '1') {
59+
queue.offer(new int[]{newX, newY});
60+
grid[newX][newY] = '0';
61+
}
62+
}
63+
}
64+
}
65+
}
66+
}
67+

reverse-linked-list/Tessa1217.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* int val;
5+
* ListNode next;
6+
* ListNode() {}
7+
* ListNode(int val) { this.val = val; }
8+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
9+
* }
10+
*/
11+
/**
12+
* 링크드 리스트 head가 주어질 때 뒤집은 리스트를 반환하세요.
13+
*/
14+
class Solution {
15+
public ListNode reverseList(ListNode head) {
16+
17+
if (head == null) {
18+
return head;
19+
}
20+
21+
ListNode prev = head;
22+
ListNode next = prev.next;
23+
head.next = null;
24+
25+
while (next != null) {
26+
ListNode temp = next.next;
27+
next.next = prev;
28+
prev = next;
29+
next = temp;
30+
}
31+
32+
return prev;
33+
}
34+
35+
}
36+

set-matrix-zeroes/Tessa1217.java

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import java.util.Arrays;
2+
3+
/**
4+
* m x n 행렬 matrix가 주어질 때, 요소가 0이라면 0을 포함하는 해당 요소를 포함하는 전체 행과 열을
5+
* 0으로 세팅하세요.
6+
*/
7+
class Solution {
8+
9+
// Follow Up : 공간 복잡도 개선 필요
10+
// mark first row and column as marker to set 0's
11+
// 시간복잡도: O(m * n), 공간복잡도: O(1)
12+
public void setZeroes(int[][] matrix) {
13+
14+
boolean firstZero = false;
15+
16+
for (int i = 0; i < matrix.length; i++) {
17+
for (int j = 0; j < matrix[0].length; j++) {
18+
if (matrix[i][j] == 0) {
19+
matrix[i][0] = 0;
20+
if (j == 0) {
21+
firstZero = true;
22+
} else {
23+
matrix[0][j] = 0;
24+
}
25+
}
26+
}
27+
}
28+
29+
for (int i = 1; i < matrix.length; i++) {
30+
for (int j = 1; j < matrix[0].length; j++) {
31+
if (matrix[i][0] == 0 || matrix[0][j] == 0) {
32+
matrix[i][j] = 0;
33+
}
34+
}
35+
}
36+
37+
if (matrix[0][0] == 0) {
38+
Arrays.fill(matrix[0], 0);
39+
}
40+
41+
if (firstZero) {
42+
for (int i = 0; i < matrix.length; i++) {
43+
matrix[i][0] = 0;
44+
}
45+
}
46+
}
47+
48+
// 시간복잡도: O (m * n), 공간복잡도: O(m + n)
49+
// public void setZeroes(int[][] matrix) {
50+
51+
// // 0을 포함하는 행과 열의 위치 저장
52+
// Set<Integer> rowsContainZeros = new HashSet<>();
53+
// Set<Integer> colsContainZeros = new HashSet<>();
54+
55+
// for (int i = 0; i < matrix.length; i++) {
56+
// for (int j = 0; j < matrix[0].length; j++) {
57+
// if (matrix[i][j] == 0) {
58+
// rowsContainZeros.add(i);
59+
// colsContainZeros.add(j);
60+
// }
61+
// }
62+
// }
63+
64+
// for (int row : rowsContainZeros) {
65+
// for (int j = 0; j < matrix[0].length; j++) {
66+
// matrix[row][j] = 0;
67+
// }
68+
// }
69+
70+
// for (int col : colsContainZeros) {
71+
// for (int i = 0; i < matrix.length; i++) {
72+
// matrix[i][col] = 0;
73+
// }
74+
// }
75+
// }
76+
77+
78+
}
79+

unique-paths/Tessa1217.java

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/**
2+
* m x n 행렬이 있을 때 로봇이 상단-좌측 가장자리에서 하단-우측 가장자리로 갈 수 있는 경우의 수 구하기
3+
* 로봇은 오른쪽 또는 밑으로만 움직일 수 있음
4+
*/
5+
class Solution {
6+
7+
// DFS 시간 초과로 DP로 구현
8+
// 시간복잡도: O(m * n)
9+
public int uniquePaths(int m, int n) {
10+
int[][] dp = new int[m][n];
11+
12+
// 첫째 열
13+
for (int i = 0; i < m; i++) {
14+
dp[i][0] = 1;
15+
}
16+
17+
// 첫째 행
18+
for (int j = 0; j < n; j++) {
19+
dp[0][j] = 1;
20+
}
21+
22+
for (int i = 1; i < m; i++) {
23+
for (int j = 1; j < n; j++) {
24+
dp[i][j] = dp[i - 1][j] + dp[i][j - 1]; // 위, 왼쪽 값
25+
}
26+
}
27+
28+
return dp[m - 1][n - 1];
29+
30+
}
31+
32+
// DFS 시간 초과
33+
// private int cnt = 0;
34+
// int[] dx = {1, 0};
35+
// int[] dy = {0, 1};
36+
37+
// public int uniquePaths(int m, int n) {
38+
// int[][] path = new int[m][n];
39+
// dfs(0, 0, path);
40+
// return cnt;
41+
// }
42+
43+
// private void dfs(int x, int y, int[][] path) {
44+
45+
// if (x == path.length - 1 && y == path[0].length - 1) {
46+
// cnt++;
47+
// return;
48+
// }
49+
50+
// path[x][y] = 1;
51+
52+
// for (int i = 0; i < 2; i++) {
53+
// int nx = x + dx[i];
54+
// int ny = y + dy[i];
55+
// if (nx >= 0 && nx < path.length && ny >= 0 && ny < path[0].length && path[nx][ny] != 1) {
56+
// dfs(nx, ny, path);
57+
// }
58+
// }
59+
60+
// path[x][y] = 0;
61+
62+
// }
63+
}
64+

0 commit comments

Comments
 (0)