Skip to content

Commit bac05c4

Browse files
committed
solve: 코딩테스트 문제 풀이 - 16일차
1 parent f2b10c3 commit bac05c4

File tree

3 files changed

+171
-0
lines changed

3 files changed

+171
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution
2+
{
3+
public int solution(int [][]board)
4+
{
5+
int n = board.length;
6+
int m = board[0].length;
7+
8+
int[][] dp = new int[n][m];
9+
int maxLen = 0;
10+
for (int i=0; i<n; i++) {
11+
for (int j=0; j<m; j++) {
12+
if (board[i][j] == 1) {
13+
if (i == 0 || j == 0) {
14+
dp[i][j] = board[i][j];
15+
} else {
16+
dp[i][j] = Math.min(Math.min(dp[i-1][j], dp[i][j-1]), dp[i-1][j-1]) + 1;
17+
}
18+
}
19+
maxLen = Math.max(maxLen, dp[i][j]);
20+
}
21+
}
22+
23+
return maxLen * maxLen;
24+
}
25+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import java.util.*;
2+
3+
class Solution {
4+
int[] dx = {0, 0, -1, 1};
5+
int[] dy = {1, -1, 0, 0};
6+
7+
int[] start;
8+
int[] lever;
9+
int[] end;
10+
11+
int n,m;
12+
char[][] map;
13+
14+
public int solution(String[] maps) {
15+
int answer = 0;
16+
17+
n = maps.length;
18+
m = maps[0].length();
19+
map = new char[n][m];
20+
for (int i=0; i<n; i++) {
21+
char[] row = maps[i].toCharArray();
22+
for (int j=0; j<m; j++) {
23+
map[i][j] = row[j];
24+
25+
if (row[j] == 'S') {
26+
start = new int[] {i, j};
27+
} else if (row[j] == 'L') {
28+
lever = new int[] {i, j};
29+
} else if (row[j] == 'E') {
30+
end = new int[] {i, j};
31+
}
32+
33+
}
34+
}
35+
36+
int toLever = bfs(start, lever);
37+
if (toLever == -1) return -1;
38+
39+
int toEnd = bfs(lever, end);
40+
if (toEnd == -1) return -1;
41+
42+
// System.out.println(start[0] + " " + start[1]);
43+
// System.out.println(lever[0] + " " + lever[1]);
44+
45+
return toLever + toEnd;
46+
}
47+
48+
public int bfs(int[] start, int[] end) {
49+
int[][] distance = new int[n][m];
50+
for (int i=0; i<n; i++) {
51+
Arrays.fill(distance[i], -1);
52+
}
53+
54+
Queue<int[]> queue = new LinkedList<>();
55+
int startY = start[0];
56+
int startX = start[1];
57+
queue.offer(new int[] {startY, startX});
58+
distance[startY][startX] = 0;
59+
60+
while (!queue.isEmpty()) {
61+
int[] curr = queue.poll();
62+
int cx = curr[1];
63+
int cy = curr[0];
64+
65+
if (cx == end[1] && cy == end[0]) {
66+
return distance[cy][cx];
67+
}
68+
69+
for (int i=0; i<4; i++) {
70+
int ny = cy + dy[i];
71+
int nx = cx + dx[i];
72+
73+
if (nx >= 0 && nx < m && ny >=0 && ny < n) {
74+
if (map[ny][nx] != 'X' && distance[ny][nx] == -1) {
75+
distance[ny][nx] = distance[cy][cx] + 1;
76+
queue.offer(new int[]{ny, nx});
77+
}
78+
}
79+
}
80+
}
81+
return -1;
82+
}
83+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import java.util.*;
2+
3+
class Solution {
4+
int height, width;
5+
int[] dx = { 0, 0, -1, 1 };
6+
int[] dy = { 1, -1, 0, 0 };
7+
boolean[][] visited;
8+
public int[] solution(String[] maps) {
9+
height = maps.length;
10+
width = maps[0].length();
11+
visited = new boolean[height][width];
12+
13+
int food = 0;
14+
List<Integer> land = new ArrayList<>();
15+
for (int i=0; i<height; i++) {
16+
for (int j=0; j<width; j++) {
17+
if (maps[i].charAt(j) == 'X' || visited[i][j]) continue;
18+
food = bfs(i, j, maps);
19+
land.add(food);
20+
}
21+
}
22+
23+
if (land.isEmpty()) {
24+
return new int[] {-1};
25+
}
26+
27+
Collections.sort(land);
28+
int n = land.size();
29+
int[] answer = new int[n];
30+
for (int i=0; i<n; i++) {
31+
answer[i] = land.get(i);
32+
}
33+
34+
return answer;
35+
}
36+
public int bfs(int y, int x, String[] maps) {
37+
Queue<int[]> queue = new LinkedList<>();
38+
queue.offer(new int[] {x, y});
39+
visited[y][x] = true;
40+
41+
int food = 0;
42+
while (!queue.isEmpty()) {
43+
int[] curr = queue.poll();
44+
int cx = curr[0];
45+
int cy = curr[1];
46+
47+
food += maps[cy].charAt(cx) - '0';
48+
49+
for (int i=0; i<4; i++) {
50+
int nx = cx + dx[i];
51+
int ny = cy + dy[i];
52+
53+
if (nx >= 0 && nx < width && ny >=0 && ny < height) {
54+
if (maps[ny].charAt(nx) != 'X' && !visited[ny][nx]) {
55+
visited[ny][nx] = true;
56+
queue.offer(new int[] {nx, ny});
57+
}
58+
}
59+
}
60+
}
61+
return food;
62+
}
63+
}

0 commit comments

Comments
 (0)