Skip to content

Commit 093d87b

Browse files
add 999
1 parent 11e5f1c commit 093d87b

File tree

3 files changed

+214
-0
lines changed

3 files changed

+214
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Your ideas/fixes/algorithms are more than welcome!
2727

2828
| # | Title | Solutions | Time | Space | Video | Difficulty | Tag
2929
|-----|----------------|---------------|---------------|---------------|--------|-------------|-------------
30+
|999|[Available Captures for Rook](https://leetcode.com/problems/available-captures-for-rook/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_999.java) | O(1) | O(1) | |Easy|
3031
|997|[Find the Town Judge](https://leetcode.com/problems/find-the-town-judge/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_997.java) | O(n) | O(n) | |Easy|
3132
|994|[Rotting Oranges](https://leetcode.com/problems/rotting-oranges/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_994.java) | O(m*2*n*2) | O(m*n) | |Easy| BFS
3233
|993|[Cousins in Binary Tree](https://leetcode.com/problems/cousins-in-binary-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_993.java) | O(n) | O(m) (m is length of the nodes that has the max number of nodes on the same level) | |Easy| Tree, BFS
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
package com.fishercoder.solutions;
2+
3+
/**
4+
* 999. Available Captures for Rook
5+
*
6+
* On an 8 x 8 chessboard, there is one white rook.
7+
* There also may be empty squares, white bishops, and black pawns.
8+
* These are given as characters 'R', '.', 'B', and 'p' respectively.
9+
* Uppercase characters represent white pieces, and lowercase characters represent black pieces.
10+
*
11+
* The rook moves as in the rules of Chess:
12+
* it chooses one of four cardinal directions (north, east, west, and south),
13+
* then moves in that direction until it chooses to stop, reaches the edge of the board,
14+
* or captures an opposite colored pawn by moving to the same square it occupies.
15+
* Also, rooks cannot move into the same square as other friendly bishops.
16+
*
17+
* Return the number of pawns the rook can capture in one move.
18+
*
19+
* Example 1:
20+
*
21+
* Input:[
22+
* 8 [".",".",".",".",".",".",".","."],
23+
* 7 [".",".",".","p",".",".",".","."],
24+
* 6 [".",".",".","R",".",".",".","p"],
25+
* 5 [".",".",".",".",".",".",".","."],
26+
* 4 [".",".",".",".",".",".",".","."],
27+
* 3 [".",".",".","p",".",".",".","."],
28+
* 2 [".",".",".",".",".",".",".","."],
29+
* 1 [".",".",".",".",".",".",".","."]]
30+
* a b c d e f g h
31+
*
32+
* Output: 3
33+
* Explanation:
34+
* In this example the rook is able to capture all the pawns.
35+
*
36+
* Example 2:
37+
*
38+
* Input:[
39+
* 8 [".",".",".",".",".",".",".","."],
40+
* 7 [".","p","p","p","p","p",".","."],
41+
* 6 [".","p","p","B","p","p",".","."],
42+
* 5 [".","p","B","R","B","p",".","."],
43+
* 4 [".","p","p","B","p","p",".","."],
44+
* 3 [".","p","p","p","p","p",".","."],
45+
* 2 [".",".",".",".",".",".",".","."],
46+
* 1 [".",".",".",".",".",".",".","."]]
47+
* a b c d e f g h
48+
*
49+
* Output: 0
50+
* Explanation:
51+
* Bishops are blocking the rook to capture any pawn.
52+
*
53+
* Example 3:
54+
*
55+
* Input: [
56+
* 8 [".",".",".",".",".",".",".","."],
57+
* 7 [".",".",".","p",".",".",".","."],
58+
* 6 [".",".",".","p",".",".",".","."],
59+
* 5 ["p","p",".","R",".","p","B","."],
60+
* 4 [".",".",".",".",".",".",".","."],
61+
* 3 [".",".",".","B",".",".",".","."],
62+
* 2 [".",".",".","p",".",".",".","."],
63+
* 1 [".",".",".",".",".",".",".","."]]
64+
* a b c d e f g h
65+
*
66+
* Output: 3
67+
* Explanation:
68+
* The rook can capture the pawns at positions b5, d6 and f5.
69+
*
70+
* Note:
71+
* board.length == board[i].length == 8
72+
* board[i][j] is either 'R', '.', 'B', or 'p'
73+
* There is exactly one cell with board[i][j] == 'R'
74+
*/
75+
public class _999 {
76+
public static class Solution1 {
77+
int[] directions = new int[] {0, 1, 0, -1, 0};
78+
79+
public int numRookCaptures(char[][] board) {
80+
int m = board.length;
81+
int n = board[0].length;
82+
int rowR = -1;
83+
int colR = -1;
84+
for (int i = 0; i < m; i++) {
85+
for (int j = 0; j < n; j++) {
86+
if (board[i][j] == 'R') {
87+
rowR = i;
88+
colR = j;
89+
break;
90+
}
91+
}
92+
}
93+
int count = 0;
94+
for (int i = 0; i < 4; i++) {
95+
int neighborRow = rowR + directions[i];
96+
int neighborCol = colR + directions[i + 1];
97+
if (neighborRow >= 0 && neighborRow < m
98+
&& neighborCol >= 0 && neighborCol < n
99+
&& board[neighborRow][neighborCol] != 'B') {
100+
if (directions[i] == 0 && directions[i + 1] == 1) {
101+
while (neighborCol < n) {
102+
if (board[neighborRow][neighborCol] == 'p') {
103+
count++;
104+
break;
105+
} else if (board[neighborRow][neighborCol] == 'B') {
106+
break;
107+
} else {
108+
neighborCol++;
109+
}
110+
}
111+
} else if (directions[i] == 1 && directions[i + 1] == 0) {
112+
while (neighborRow < m) {
113+
if (board[neighborRow][neighborCol] == 'p') {
114+
count++;
115+
break;
116+
} else if (board[neighborRow][neighborCol] == 'B') {
117+
break;
118+
} else {
119+
neighborRow++;
120+
}
121+
}
122+
} else if (directions[i] == 0 && directions[i + 1] == -1) {
123+
while (neighborCol >= 0) {
124+
if (board[neighborRow][neighborCol] == 'p') {
125+
count++;
126+
break;
127+
} else if (board[neighborRow][neighborCol] == 'B') {
128+
break;
129+
} else {
130+
neighborCol--;
131+
}
132+
}
133+
} else {
134+
while (neighborRow >= 0) {
135+
if (board[neighborRow][neighborCol] == 'p') {
136+
count++;
137+
break;
138+
} else if (board[neighborRow][neighborCol] == 'B') {
139+
break;
140+
} else {
141+
neighborRow--;
142+
}
143+
}
144+
}
145+
}
146+
}
147+
148+
return count;
149+
}
150+
}
151+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._999;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _999Test {
10+
private static _999.Solution1 solution1;
11+
private static char[][] board;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _999.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
board = new char[][] {
21+
{'.', '.', '.', '.', '.', '.', '.', '.'},
22+
{'.', '.', '.', 'p', '.', '.', '.', '.'},
23+
{'.', '.', '.', 'R', '.', '.', '.', 'p'},
24+
{'.', '.', '.', '.', '.', '.', '.', '.'},
25+
{'.', '.', '.', '.', '.', '.', '.', '.'},
26+
{'.', '.', '.', 'p', '.', '.', '.', '.'},
27+
{'.', '.', '.', '.', '.', '.', '.', '.'},
28+
{'.', '.', '.', '.', '.', '.', '.', '.'},
29+
};
30+
assertEquals(3, solution1.numRookCaptures(board));
31+
}
32+
33+
@Test
34+
public void test2() {
35+
board = new char[][] {
36+
{'.', '.', '.', '.', '.', '.', '.', '.'},
37+
{'.', 'p', 'p', 'p', 'p', 'p', '.', '.'},
38+
{'.', 'p', 'p', 'B', 'p', 'p', '.', '.'},
39+
{'.', 'p', 'B', 'R', 'B', 'p', '.', '.'},
40+
{'.', 'p', 'p', 'B', 'p', 'p', '.', '.'},
41+
{'.', 'p', 'p', 'p', 'p', 'p', '.', '.'},
42+
{'.', '.', '.', '.', '.', '.', '.', '.'},
43+
{'.', '.', '.', '.', '.', '.', '.', '.'},
44+
};
45+
assertEquals(0, solution1.numRookCaptures(board));
46+
}
47+
48+
@Test
49+
public void test3() {
50+
board = new char[][] {
51+
{'.', '.', '.', '.', '.', '.', '.', '.'},
52+
{'.', '.', '.', 'p', '.', '.', '.', '.'},
53+
{'.', '.', '.', 'p', '.', '.', '.', 'p'},
54+
{'p', 'p', '.', 'R', '.', 'p', 'B', '.'},
55+
{'.', '.', '.', '.', '.', '.', '.', '.'},
56+
{'.', '.', '.', 'B', '.', '.', '.', '.'},
57+
{'.', '.', '.', 'p', '.', '.', '.', '.'},
58+
{'.', '.', '.', '.', '.', '.', '.', '.'},
59+
};
60+
assertEquals(3, solution1.numRookCaptures(board));
61+
}
62+
}

0 commit comments

Comments
 (0)