Skip to content

Commit 9bc9599

Browse files
authored
Merge branch 'main' into yeji/programmers
2 parents 290444e + c4a6cb9 commit 9bc9599

File tree

8 files changed

+298
-41
lines changed

8 files changed

+298
-41
lines changed

.idea/sonarlint/issuestore/index.pb

Lines changed: 0 additions & 17 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main/java/sgyj/leetcode/Readme.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,29 @@
11
# 코딩테스트 ALL IN ONE
22

33
## 반복문
4+
45
- [1. TwoSum](https://leetcode.com/problems/two-sum/)
56

67
## LinkedList
8+
79
- [1472. Design Browser History](https://leetcode.com/problems/design-browser-history/)
810

911
## Stack
12+
1013
- [20. valid parentheses](https://leetcode.com/problems/valid-parentheses)
1114
- [739. Daily Temperatures](https://leetcode.com/problems/daily-temperatures/)
1215

1316
## Hashtable
14-
- [128. Longest Consecutive Sequence](https://leetcode.com/problems/longest-consecutive-sequence/)
17+
18+
- [128. Longest Consecutive Sequence](https://leetcode.com/problems/longest-consecutive-sequence/)
19+
20+
## Traversal
21+
22+
- [236. Lowest Common Ancestor of a Binary Tree](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/description/)
23+
- [104. Maximum Depth of Binary Tree](https://leetcode.com/problems/maximum-depth-of-binary-tree/)
24+
25+
## Graph
26+
27+
- [200. Number of Islands](https://leetcode.com/problems/number-of-islands/description/)
28+
- [1091. Shortest Path in Binary Matrix](https://leetcode.com/problems/shortest-path-in-binary-matrix/)
29+
- [841. Keys and Rooms](https://leetcode.com/problems/keys-and-rooms/description/)
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package sgyj.leetcode.yeji.section5;
2+
3+
//104. Maximum Depth of Binary Tree
4+
5+
import java.util.ArrayDeque;
6+
import java.util.Deque;
7+
8+
/**
9+
* Definition for a binary tree node.
10+
* public class TreeNode {
11+
* int val;
12+
* TreeNode left;
13+
* TreeNode right;
14+
* TreeNode() {}
15+
* TreeNode(int val) { this.val = val; }
16+
* TreeNode(int val, TreeNode left, TreeNode right) {
17+
* this.val = val;
18+
* this.left = left;
19+
* this.right = right;
20+
* }
21+
* }
22+
*/
23+
public class Solution104 {
24+
public int maxDepth( TreeNode root) {
25+
int answer = 0;
26+
if(root == null) return 0;
27+
Deque<TreeNode> q = new ArrayDeque<>();
28+
root.updateDepth();
29+
q.offer(root );
30+
while(!q.isEmpty()){
31+
int len = q.size();
32+
for(int i=0; i<len; i++){
33+
TreeNode node = q.poll();
34+
if(node.left != null){
35+
node.left.updateDepth();
36+
q.offer( node.left );
37+
}
38+
if(node.right != null){
39+
node.right.updateDepth();
40+
q.offer( node.right );
41+
}
42+
}
43+
answer++;
44+
}
45+
46+
return answer;
47+
}
48+
49+
public static void main ( String[] args ) {
50+
51+
}
52+
}
53+
54+
class TreeNode{
55+
int val;
56+
int depth;
57+
TreeNode left;
58+
TreeNode right;
59+
60+
public TreeNode () {
61+
62+
}
63+
64+
TreeNode(int val){
65+
this.val = val;
66+
}
67+
TreeNode( int val, TreeNode left, TreeNode right){
68+
this.val = val;
69+
this.left = left;
70+
this.right = right;
71+
}
72+
73+
public void updateDepth (){
74+
this.depth++;
75+
}
76+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package sgyj.leetcode.yeji.section5;
2+
3+
public class Solution104_2 {
4+
5+
public static int maxDepth( TreeNode root){
6+
int answer = 0;
7+
if(root == null) return 0;
8+
if(root.left == null && root.right == null) return 1;
9+
int left = maxDepth( root.left );
10+
int right = maxDepth( root.right );
11+
answer = Math.max( left,right );
12+
answer++;
13+
return answer;
14+
}
15+
//3,9,20,null,null,15,7
16+
public static void main ( String[] args ) {
17+
TreeNode root = new TreeNode(1);
18+
root.left = new TreeNode(9);
19+
root.right = new TreeNode(20);
20+
root.right.left = new TreeNode(15);
21+
root.right.right = new TreeNode(7);
22+
23+
System.out.println(maxDepth(root));
24+
}
25+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package sgyj.leetcode.yeji.section5;
2+
3+
import java.util.ArrayDeque;
4+
import java.util.Deque;
5+
6+
// 1091. Shortest Path in Binary Matrix
7+
public class Solution1091 {
8+
private static int[] dx = {-1,1,0,0,1,1,-1,-1};
9+
private static int[] dy = {0,0,-1,1,1,-1,-1,1};
10+
private static boolean[][] visited;
11+
12+
public static int shortestPathBinaryMatrix(int[][] grid) {
13+
int answer = -1;
14+
int n = grid.length;
15+
visited = new boolean[n][n];
16+
17+
Deque<Location> q = new ArrayDeque<>();
18+
if(grid[0][0] != 0) return -1;
19+
visited[0][0] = true;
20+
q.offer( new Location(0,0,1 ) );
21+
while ( !q.isEmpty() ){
22+
int len = q.size();
23+
for(int i=0; i<len; i++){
24+
Location curNode = q.poll();
25+
if(curNode.x == n-1 && curNode.y == n-1) {
26+
answer = curNode.len;
27+
break;
28+
}
29+
for(int d=0; d<dx.length; d++){
30+
int x = curNode.x + dx[d];
31+
int y = curNode.y + dy[d];
32+
if(x>=0 && x<n && y>=0 && y<n && !visited[x][y] && grid[x][y] == 0){
33+
visited[x][y] = true;
34+
q.offer( new Location( x,y , curNode.len+1 ) );
35+
}
36+
}
37+
}
38+
}
39+
return answer;
40+
}
41+
42+
public static void main ( String[] args ) {
43+
int[][] grid = {
44+
// [[0,0,0],[1,1,0],[1,1,0]]
45+
{0,0,0},{1,1,0},{1,1,0}
46+
};
47+
48+
System.out.println(shortestPathBinaryMatrix( grid ));
49+
}
50+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package sgyj.leetcode.yeji.section5;
2+
3+
import java.util.ArrayDeque;
4+
import java.util.Deque;
5+
6+
// 200. Number of Islands
7+
public class Solution200 {
8+
private static int[] dx = {-1,1,0,0};
9+
private static int[] dy = {0,0,-1,1};
10+
private static int m;
11+
private static int n;
12+
private static char[][] targetGrid;
13+
private static boolean[][] visited;
14+
15+
public static int numIslands(char[][] grid) {
16+
int answer = 0;
17+
m = grid.length;
18+
n = grid[0].length;
19+
visited = new boolean[m][n];
20+
targetGrid = grid;
21+
for(int i=0; i<m; i++){
22+
for(int j=0; j<n; j++){
23+
if(!visited[i][j] && grid[i][j]==49){
24+
bfs(i,j);
25+
answer++;
26+
}
27+
}
28+
}
29+
return answer;
30+
}
31+
32+
public static void bfs(int x, int y){
33+
Deque<Location> q = new ArrayDeque<>();
34+
q.offer( new Location( x,y ) );
35+
visited[x][y] = true;
36+
while ( !q.isEmpty() ){
37+
Location currentNode = q.poll();
38+
for(int d=0; d<dx.length; d++){
39+
int curX = currentNode.x + dx[d];
40+
int curY = currentNode.y + dy[d];
41+
if(curX>=0 && curX<m && curY>=0 && curY<n){
42+
if(targetGrid[curX][curY] != 48 && !visited[curX][curY]){
43+
visited[curX][curY] = true;
44+
q.offer( new Location( curX,curY ) );
45+
}
46+
}
47+
}
48+
}
49+
}
50+
51+
52+
public static void main ( String[] args ) {
53+
char[][] grid = {
54+
{'1','1','1','1','0'},
55+
{'1','1','0','1','0'},
56+
{'1','1','0','0','0'},
57+
{'0','0','0','0','0'}
58+
};
59+
System.out.println(numIslands(grid));
60+
61+
}
62+
63+
}
64+
65+
class Location{
66+
int x;
67+
int y;
68+
69+
int len;
70+
71+
Location(int x, int y){
72+
this.x = x;
73+
this.y = y;
74+
}
75+
76+
Location(int x, int y, int len){
77+
this.x = x;
78+
this.y = y;
79+
this.len = len;
80+
}
81+
}
Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
package sgyj.leetcode.yeji.section5;
22

3-
import java.util.ArrayList;
4-
import java.util.HashMap;
5-
import java.util.List;
6-
import java.util.Map;
7-
import java.util.concurrent.ConcurrentHashMap;
8-
93
/**
104
* Definition for a binary tree node.
115
* public class TreeNode {
@@ -17,33 +11,33 @@
1711
*/
1812
public class Solution236 {
1913

20-
public static TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
14+
public static Child lowestCommonAncestor( Child root, Child p, Child q) {
2115
return null;
2216
}
2317

2418
public static void main ( String[] args ) {
2519
// 3,5,1,6,2,0,8,null,null,7,4
26-
TreeNode p = new TreeNode( 5 );
27-
TreeNode q = new TreeNode( 4 );
28-
TreeNode root = new TreeNode( 3 );
29-
root.left = new TreeNode( 5 );
30-
root.right = new TreeNode( 1 );
31-
root.left.left = new TreeNode( 6 );
32-
root.left.right = new TreeNode( 2 );
33-
root.right.left = new TreeNode( 0 );
34-
root.right.right = new TreeNode( 8 );
35-
root.left.right.left = new TreeNode( 7 );
36-
root.left.right.right = new TreeNode( 4 );
37-
TreeNode treeNode = lowestCommonAncestor( root, p, q );
20+
Child p = new Child( 5 );
21+
Child q = new Child( 4 );
22+
Child root = new Child( 3 );
23+
root.left = new Child( 5 );
24+
root.right = new Child( 1 );
25+
root.left.left = new Child( 6 );
26+
root.left.right = new Child( 2 );
27+
root.right.left = new Child( 0 );
28+
root.right.right = new Child( 8 );
29+
root.left.right.left = new Child( 7 );
30+
root.left.right.right = new Child( 4 );
31+
Child treeNode = lowestCommonAncestor( root, p, q );
3832
System.out.println(treeNode.val);
3933
}
4034
}
4135

42-
class TreeNode{
36+
class Child {
4337
int val;
44-
TreeNode left;
45-
TreeNode right;
46-
TreeNode(int x){
38+
Child left;
39+
Child right;
40+
Child ( int x){
4741
this.val = x;
4842
}
4943
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package sgyj.leetcode.yeji.section5;
2+
3+
import java.util.ArrayDeque;
4+
import java.util.Deque;
5+
import java.util.List;
6+
7+
// 841. Keys and Rooms
8+
public class Solution841 {
9+
private static boolean[] visited;
10+
11+
public boolean canVisitAllRooms(List<List<Integer>> rooms) {
12+
int n = rooms.size();
13+
visited = new boolean[n];
14+
Deque<Integer> q = new ArrayDeque<>();
15+
q.offer(0);
16+
visited[0] = true;
17+
while(!q.isEmpty()){
18+
int curNode = q.poll();
19+
for(int i=0; i<rooms.get(curNode).size(); i++){
20+
int target = rooms.get(curNode).get(i);
21+
if(!visited[target]){
22+
q.offer(target);
23+
visited[target] = true;
24+
}
25+
}
26+
}
27+
28+
for(boolean v : visited){
29+
if(!v) return false;
30+
}
31+
return true;
32+
}
33+
}

0 commit comments

Comments
 (0)