Skip to content

Commit e8fe2f5

Browse files
committed
- 7주차 풀이 커밋
1 parent 4a85208 commit e8fe2f5

File tree

3 files changed

+173
-0
lines changed

3 files changed

+173
-0
lines changed

number-of-islands/Geegong.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
public class Geegong {
2+
3+
/**
4+
* 1. 하나씩 훑어가면서 1이 되는 시점을 찾아 그 시점부터 dfs 로 방향값을 주어
5+
* 방문한 곳은 0으로 바꿔버려 다시는 탐색하지 못하도록 막아버림
6+
* 모든 방향값을 주면서 0으로 메꾼 후 1을 리턴하고 리턴된 값들을 누적하면 만들어낼 수 있는 섬의 총 갯수가 된다
7+
* time complexity : O (m*n*4) => O(m*n)
8+
* space complexity : O(m*n) // 따로 memoization 을 위한 변수는 없으나 재귀로 인해 콜스택 발생
9+
*/
10+
public static int[][] vectors = {{0,1}, {1,0}, {0,-1}, {-1,0}};
11+
public int numIslands(char[][] grid) {
12+
13+
int totalNumberOfIslands = 0;
14+
15+
for (int rowIdx=0; rowIdx < grid.length; rowIdx++) {
16+
for (int colIdx=0; colIdx < grid[0].length; colIdx++) {
17+
// 1이 되는 시점부터 섬이 되는지 체크한다.
18+
if (grid[rowIdx][colIdx] == '1') {
19+
totalNumberOfIslands += dfs(grid, rowIdx, colIdx);
20+
}
21+
}
22+
}
23+
return totalNumberOfIslands;
24+
}
25+
26+
public int dfs(char[][] origin, int rowIdx, int colIdx) {
27+
if (rowIdx < 0 || colIdx < 0) {
28+
// 의미 없는 리턴. 단순히 콜스택 이전으로 돌아가기 위해 임의값 리턴
29+
return 1;
30+
}
31+
32+
if (rowIdx >= origin.length || colIdx >= origin[0].length) {
33+
// 의미 없는 리턴. 단순히 콜스택 이전으로 돌아가기 위해 임의값 리턴
34+
return 1;
35+
}
36+
37+
if (origin[rowIdx][colIdx] == '0') {
38+
// 의미 없는 리턴. 단순히 콜스택 이전으로 돌아가기 위해 임의값 리턴
39+
return 1;
40+
}
41+
42+
origin[rowIdx][colIdx] = '0'; // 0 으로 셋팅해서 다음 섬을 찾을때 방문하지 못하도록 방어한다.
43+
44+
for (int[] vector : vectors) {
45+
int moveRow = vector[0];
46+
int moveCol = vector[1];
47+
48+
dfs(origin, rowIdx + moveRow, colIdx + moveCol);
49+
}
50+
51+
// 섬이 되는 구역을 다 돌았다면 하나의 섬이 하나 된다고 판단이 되므로 1 리턴
52+
return 1;
53+
}
54+
55+
}
56+

set-matrix-zeroes/Geegong.java

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import java.util.Arrays;
2+
3+
public class Geegong {
4+
5+
6+
/**
7+
* in-place 풀이여서 matrix 안에 0 으로 첫번째 row, column 에 0으로 채워야 하는 부분을 마킹해두는 식으로 풀이
8+
* time complexity : O(2MN) -> O(MN)
9+
* space complexity : O(1)
10+
* @param matrix
11+
*/
12+
public void setZeroes(int[][] matrix) {
13+
14+
// 가장 자리 top, left 에 1이 있는 지 체크
15+
// in-place 라고 풀었는데 가장 자리에 1이 있는지 체크는 변수 한두개 정도는 써도 되는듯..?
16+
boolean zeroExistsInTop = false;
17+
boolean zeroExistsInLeft = false;
18+
for (int colIndex=0; colIndex< matrix[0].length; colIndex++) {
19+
if (matrix[0][colIndex] == 0) {
20+
zeroExistsInTop = true;
21+
break;
22+
}
23+
}
24+
25+
for (int rowIndex = 0; rowIndex < matrix.length; rowIndex++) {
26+
if (matrix[rowIndex][0] == 0) {
27+
zeroExistsInLeft = true;
28+
break;
29+
}
30+
}
31+
32+
// 가장자리를 제외하고 안쪽에만 먼저 0으로 채워야되는 케이스들을 골라냄
33+
for (int rowIndex = 1; rowIndex < matrix.length; rowIndex++) {
34+
for (int colIndex=1; colIndex < matrix[0].length; colIndex++) {
35+
36+
if (matrix[rowIndex][colIndex] == 0) {
37+
matrix[0][colIndex] = 0;
38+
matrix[rowIndex][0] = 0;
39+
}
40+
}
41+
}
42+
43+
for (int rowIndex = 1; rowIndex < matrix.length; rowIndex++) {
44+
for (int colIndex = 1; colIndex < matrix[0].length; colIndex++) {
45+
if (matrix[0][colIndex] == 0 || matrix[rowIndex][0] == 0) {
46+
matrix[rowIndex][colIndex] = 0;
47+
}
48+
}
49+
}
50+
51+
if (zeroExistsInTop) {
52+
for (int colIndex=0; colIndex<matrix[0].length; colIndex++) {
53+
matrix[0][colIndex] = 0;
54+
}
55+
}
56+
57+
if (zeroExistsInLeft) {
58+
for (int rowIndex=0; rowIndex<matrix.length; rowIndex++) {
59+
matrix[rowIndex][0] = 0;
60+
}
61+
}
62+
63+
}
64+
}
65+

unique-paths/Geegong.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
public class Geegong {
2+
3+
/**
4+
* 1. vectors 를 정하고 이진트리를 dfs 처럼 방향을 주어가며 훑어간다.
5+
* 그런데 time limit exceeded ㅠㅠ
6+
* => 두 방향으로 계속 뻗어가기 때문에 time complexity 가 (m+n)^2 까지 된다 흡
7+
*
8+
* 2. dp 풀이 방법, 1차원 배열로 생각했을 때 dp배열의 인덱스는 인덱스만큼의 row, column 까지 도달하는 방법의 수를 계속해서
9+
* 누적해가는 방법으로 진행
10+
*
11+
* @param m
12+
* @param n
13+
* @return
14+
*/
15+
public static int[][] vectors = {{0,1}, {1,0}};
16+
public int uniquePaths(int m, int n) {
17+
// case 1. tle ㅠㅠ
18+
// return dfs( 0, 0, m, n);
19+
20+
// 뭔가 m*n 만큼의 배열이어야 할 것 같지만 사실 각 row마다의 column들에 방문할때의 방문 가능 갯수를 누적해가기때문에 n 만큼만 있어도 된다.
21+
int[] dp = new int[n];
22+
23+
dp[0] = 1; // 시작점부터 path 가능 갯수 1
24+
for(int rowIdx = 0; rowIdx < m; rowIdx++) {
25+
// 1부터 세는 이유는... 어차피 오른쪽으로 움직이기 때문예?
26+
for(int colIdx = 1; colIdx < n; colIdx++) {
27+
// dp[colIdx - 1] : 직전 column기준 0에서부터 도달할 수 있는 방법의 수
28+
// dp[colIdx] : rowIdx - 1이 었을때 0에서부터 [rowIdx - 1][colIdx] 까지 도달할 수 있는 방법의 수
29+
dp[colIdx] = dp[colIdx - 1] + dp[colIdx];
30+
}
31+
}
32+
33+
return dp[n - 1];
34+
}
35+
36+
// public int dfs(int startRow, int startBottom, int m, int n) {
37+
// if (startRow == m - 1 && startBottom == n - 1) {
38+
// return 1;
39+
// } else if (startRow >= m || startBottom >= n) {
40+
// return 0;
41+
// }
42+
//
43+
// int result = 0;
44+
//
45+
// for (int[] vector : vectors) {
46+
// result += dfs(vector[1] + startRow, vector[0] + startBottom, m, n);
47+
// }
48+
//
49+
// return result;
50+
// }
51+
}
52+

0 commit comments

Comments
 (0)