Skip to content

Commit f6a4ecf

Browse files
authored
Merge pull request #2263 from se6816/main
[se6816] WEEK 09 solutions
2 parents 570e44a + 69ed2d5 commit f6a4ecf

4 files changed

Lines changed: 213 additions & 0 deletions

File tree

linked-list-cycle/se6816.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
길이가 최대 10000이므로 10001까지 경우의 수를 돌아 확인하는 방법
3+
*/
4+
public class Solution {
5+
public boolean hasCycle(ListNode head) {
6+
int max = 10001;
7+
int idx = 0;
8+
boolean result = false;
9+
while(head != null) {
10+
idx++;
11+
head = head.next;
12+
if(idx > max) {
13+
result = true;
14+
break;
15+
}
16+
}
17+
18+
return result;
19+
}
20+
}
21+
/**
22+
2개의 포인터를 이용하여, 만나는지 확인하는 방법
23+
*/
24+
public class Solution2 {
25+
public boolean hasCycle(ListNode head) {
26+
ListNode first = head;
27+
ListNode second = head;
28+
boolean result = false;
29+
while(second != null && second.next != null) {
30+
first = first.next;
31+
second = second.next.next;
32+
if(first == second) {
33+
result = true;
34+
break;
35+
}
36+
}
37+
38+
return result;
39+
}
40+
}

number-of-islands/se6816.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
방문하지 않는 지점을 찾고, BFS로 인근 지점을 방문처리하며, 방문하지 않는 지점 개수를 카운틷하는 방식
3+
*/
4+
class Solution {
5+
int M;
6+
int N;
7+
boolean[][] visited;
8+
int[] moveX = {-1, 0, 0, 1};
9+
int[] moveY = {0, -1, 1, 0};
10+
public int numIslands(char[][] grid) {
11+
int result = 0;
12+
M = grid.length;
13+
N = grid[0].length;
14+
visited= new boolean[M][N];
15+
for(int i = 0; i < M; i++) {
16+
for(int j = 0; j < N; j++) {
17+
if(!visited[i][j] && grid[i][j] == '1') {
18+
visited[i][j] = true;
19+
result++;
20+
paint(i, j, visited, grid);
21+
}
22+
}
23+
}
24+
return result;
25+
26+
}
27+
public void paint(int x, int y, boolean[][] visited, char[][] grid) {
28+
for(int i = 0; i < 4; i++) {
29+
int tempX = x + moveX[i];
30+
int tempY = y + moveY[i];
31+
32+
if(isOutOfIndex(tempX, tempY)) {
33+
continue;
34+
}
35+
36+
if(visited[tempX][tempY]) {
37+
continue;
38+
}
39+
40+
if(grid[tempX][tempY] == '0') {
41+
continue;
42+
}
43+
44+
visited[tempX][tempY] = true;
45+
46+
paint(tempX,tempY, visited, grid);
47+
}
48+
}
49+
public boolean isOutOfIndex(int x, int y) {
50+
return x < 0 || x >= M || y < 0 || y >=N;
51+
}
52+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/**
2+
BFS를 통해, 방문 지점이 겹치는 부분을 찾는 방식
3+
*/
4+
class Solution {
5+
public static int[] moveX = {0, 1, 0, -1};
6+
public static int[] moveY = {1, 0, -1, 0};
7+
public boolean[][] POvisited;
8+
public boolean[][] AOvisited;
9+
public int N;
10+
public int M;
11+
public List<List<Integer>> pacificAtlantic(int[][] heights) {
12+
List<List<Integer>> result = new ArrayList<>();
13+
N = heights.length;
14+
M = heights[0].length;
15+
POvisited = new boolean[N][M];
16+
AOvisited = new boolean[N][M];
17+
Queue<int[]> que = new LinkedList<>();
18+
Queue<int[]> que2 = new LinkedList<>();
19+
20+
for(int i = 0; i < M; i++) {
21+
POvisited[0][i] = true;
22+
que.add(new int[]{0, i});
23+
AOvisited[N-1][i] = true;
24+
que2.add(new int[]{N-1, i});
25+
}
26+
27+
for(int i = 0; i < N; i++) {
28+
POvisited[i][0] = true;
29+
que.add(new int[]{i, 0});
30+
AOvisited[i][M-1] = true;
31+
que2.add(new int[]{i, M-1});
32+
}
33+
bfs(que, POvisited, heights);
34+
bfs(que2, AOvisited, heights);
35+
36+
for(int i = 0; i < N; i++) {
37+
for(int j = 0; j < M; j++) {
38+
if(POvisited[i][j] && AOvisited[i][j]) {
39+
result.add(new ArrayList<>(List.of(i, j)));
40+
}
41+
}
42+
}
43+
44+
return result;
45+
}
46+
47+
public void bfs(Queue<int[]> que, boolean[][] visited, int[][] heights) {
48+
while(!que.isEmpty()) {
49+
int[] node = que.poll();
50+
for(int i = 0; i < 4; i++) {
51+
int tempX = node[0] + moveX[i];
52+
int tempY = node[1] + moveY[i];
53+
if(isOutOfIndex(tempX, tempY)) {
54+
continue;
55+
}
56+
57+
if(visited[tempX][tempY]) {
58+
continue;
59+
}
60+
if(heights[tempX][tempY] < heights[node[0]][node[1]]) {
61+
continue;
62+
}
63+
64+
visited[tempX][tempY] = true;
65+
que.add(new int[]{tempX, tempY});
66+
}
67+
}
68+
}
69+
70+
public boolean isOutOfIndex(int x, int y){
71+
return x < 0 || x >= N || y < 0 || y >= M;
72+
}
73+
}

unique-paths/se6816.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
class Solution {
2+
public int[] moveX = {0, 1};
3+
public int[] moveY = {1, 0};
4+
int N;
5+
int M;
6+
public int uniquePaths(int m, int n) {
7+
int[][] dp = new int[m][n];
8+
boolean[][] visited = new boolean[m][n];
9+
int curX = 0;
10+
int curY = 0;
11+
N = n;
12+
M = m;
13+
dp[curX][curY] = 1;
14+
visited[curX][curY] = true;
15+
Queue<int[]> que = new ArrayDeque<>();
16+
que.offer(new int[]{0,0});
17+
18+
while(!que.isEmpty()) {
19+
int[] data = que.poll();
20+
21+
for(int i = 0; i < 2; i++) {
22+
int tempX = data[0] + moveX[i];
23+
int tempY = data[1] + moveY[i];
24+
25+
if(isOutOfIndex(tempX, tempY)) {
26+
continue;
27+
}
28+
29+
dp[tempX][tempY] += dp[data[0]][data[1]];
30+
31+
32+
if(visited[tempX][tempY]) {
33+
continue;
34+
}
35+
visited[tempX][tempY] = true;
36+
que.add(new int[]{tempX, tempY});
37+
38+
}
39+
}
40+
41+
return dp[M-1][N-1];
42+
43+
}
44+
45+
public boolean isOutOfIndex(int x, int y) {
46+
return x < 0 || x >= M || y < 0 || y >=N;
47+
}
48+
}

0 commit comments

Comments
 (0)