diff --git a/README.md b/README.md index c1ff05ecb..3dd0546b2 100644 --- a/README.md +++ b/README.md @@ -54,6 +54,10 @@ LeetCode Problems' Solutions | # | Title | Solution | Difficulty | | :-: | - | - | :-: | +| 1001 | [Grid Illumination](https://leetcode.com/problems/grid-illumination "网格照明") | [Go](https://github.com/openset/leetcode/tree/master/problems/grid-illumination) | Hard | +| 999 | [Available Captures for Rook](https://leetcode.com/problems/available-captures-for-rook "车的可用捕获量") | [Go](https://github.com/openset/leetcode/tree/master/problems/available-captures-for-rook) | Easy | +| 998 | [Maximum Binary Tree II](https://leetcode.com/problems/maximum-binary-tree-ii "最大二叉树 II") | [Go](https://github.com/openset/leetcode/tree/master/problems/maximum-binary-tree-ii) | Medium | +| 997 | [Find the Town Judge](https://leetcode.com/problems/find-the-town-judge "找到小镇的法官") | [Go](https://github.com/openset/leetcode/tree/master/problems/find-the-town-judge) | Easy | | 996 | [Number of Squareful Arrays](https://leetcode.com/problems/number-of-squareful-arrays "正方形数组的数目") | [Go](https://github.com/openset/leetcode/tree/master/problems/number-of-squareful-arrays) | Hard | | 995 | [Minimum Number of K Consecutive Bit Flips](https://leetcode.com/problems/minimum-number-of-k-consecutive-bit-flips "K 连续位的最小翻转次数") | [Go](https://github.com/openset/leetcode/tree/master/problems/minimum-number-of-k-consecutive-bit-flips) | Hard | | 994 | [Rotting Oranges](https://leetcode.com/problems/rotting-oranges "腐烂的橘子") | [Go](https://github.com/openset/leetcode/tree/master/problems/rotting-oranges) | Easy | diff --git a/problems/available-captures-for-rook/README.md b/problems/available-captures-for-rook/README.md new file mode 100644 index 000000000..980726ca0 --- /dev/null +++ b/problems/available-captures-for-rook/README.md @@ -0,0 +1,66 @@ + + + + + + + +[< Previous](https://github.com/openset/leetcode/tree/master/problems/maximum-binary-tree-ii "Maximum Binary Tree II") + +[Next >](https://github.com/openset/leetcode/tree/master/problems/grid-illumination "Grid Illumination") + +## 999. Available Captures for Rook (Easy) + +
On an 8 x 8 chessboard, there is one white rook. There also may be empty squares, white bishops, and black pawns. These are given as characters 'R', '.', 'B', and 'p' respectively. Uppercase characters represent white pieces, and lowercase characters represent black pieces.
+ +The rook moves as in the rules of Chess: it chooses one of four cardinal directions (north, east, west, and south), then moves in that direction until it chooses to stop, reaches the edge of the board, or captures an opposite colored pawn by moving to the same square it occupies. Also, rooks cannot move into the same square as other friendly bishops.
+ +Return the number of pawns the rook can capture in one move.
+ ++ +
Example 1:
+ ++Input: [[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".","R",".",".",".","p"],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."]] +Output: 3 +Explanation: +In this example the rook is able to capture all the pawns. ++ +
Example 2:
+ ++Input: [[".",".",".",".",".",".",".","."],[".","p","p","p","p","p",".","."],[".","p","p","B","p","p",".","."],[".","p","B","R","B","p",".","."],[".","p","p","B","p","p",".","."],[".","p","p","p","p","p",".","."],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."]] +Output: 0 +Explanation: +Bishops are blocking the rook to capture any pawn. ++ +
Example 3:
+ ++Input: [[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".","p",".",".",".","."],["p","p",".","R",".","p","B","."],[".",".",".",".",".",".",".","."],[".",".",".","B",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".",".",".",".",".","."]] +Output: 3 +Explanation: +The rook can capture the pawns at positions b5, d6 and f5. ++ +
+ +
Note:
+ +board.length == board[i].length == 8board[i][j] is either 'R', '.', 'B', or 'p'board[i][j] == 'R'In a town, there are N people labelled from 1 to N. There is a rumor that one of these people is secretly the town judge.
If the town judge exists, then:
+ +You are given trust, an array of pairs trust[i] = [a, b] representing that the person labelled a trusts the person labelled b.
If the town judge exists and can be identified, return the label of the town judge. Otherwise, return -1.
+ +
Example 1:
+ ++Input: N = 2, trust = [[1,2]] +Output: 2 ++ +
Example 2:
+ ++Input: N = 3, trust = [[1,3],[2,3]] +Output: 3 ++ +
Example 3:
+ ++Input: N = 3, trust = [[1,3],[2,3],[3,1]] +Output: -1 ++ +
Example 4:
+ ++Input: N = 3, trust = [[1,2],[2,3]] +Output: -1 ++ +
Example 5:
+ ++Input: N = 4, trust = [[1,3],[1,4],[2,3],[2,4],[4,3]] +Output: 3+ +
+
Note:
+ +1 <= N <= 1000trust.length <= 10000trust[i] are all differenttrust[i][0] != trust[i][1]1 <= trust[i][0], trust[i][1] <= NOn a N x N grid of cells, each cell (x, y) with 0 <= x < N and 0 <= y < N has a lamp.
Initially, some number of lamps are on. lamps[i] tells us the location of the i-th lamp that is on. Each lamp that is on illuminates every square on its x-axis, y-axis, and both diagonals (similar to a Queen in chess).
For the i-th query queries[i] = (x, y), the answer to the query is 1 if the cell (x, y) is illuminated, else 0.
After each query (x, y) [in the order given by queries], we turn off any lamps that are at cell (x, y) or are adjacent 8-directionally (ie., share a corner or edge with cell (x, y).)
Return an array of answers. Each value answer[i] should be equal to the answer of the i-th query queries[i].
+ +
Example 1:
+ ++Input: N = 5, lamps = [[0,0],[4,4]], queries = [[1,1],[1,0]] +Output: [1,0] +Explanation: +Before performing the first query we have both lamps [0,0] and [4,4] on. +The grid representing which cells are lit looks like this, where [0,0] is the top left corner, and [4,4] is the bottom right corner: +1 1 1 1 1 +1 1 0 0 1 +1 0 1 0 1 +1 0 0 1 1 +1 1 1 1 1 +Then the query at [1, 1] returns 1 because the cell is lit. After this query, the lamp at [0, 0] turns off, and the grid now looks like this: +1 0 0 0 1 +0 1 0 0 1 +0 0 1 0 1 +0 0 0 1 1 +1 1 1 1 1 +Before performing the second query we have only the lamp [4,4] on. Now the query at [1,0] returns 0, because the cell is no longer lit. ++ +
+ +
Note:
+ +1 <= N <= 10^90 <= lamps.length <= 200000 <= queries.length <= 20000lamps[i].length == queries[i].length == 2We are given the root node of a maximum tree: a tree where every node has a value greater than any other value in its subtree.
Just as in the previous problem, the given tree was constructed from an list A (root = Construct(A)) recursively with the following Construct(A) routine:
A is empty, return null.A[i] be the largest element of A. Create a root node with value A[i].root will be Construct([A[0], A[1], ..., A[i-1]])root will be Construct([A[i+1], A[i+2], ..., A[A.length - 1]])root.Note that we were not given A directly, only a root node root = Construct(A).
Suppose B is a copy of A with the value val appended to it. It is guaranteed that B has unique values.
Return Construct(B).
+ +
Example 1:
+ +

+Input: root = [4,1,3,null,null,2], val = 5 +Output: [5,4,null,1,3,null,null,2] +Explanation: A = [1,4,2,3], B = [1,4,2,3,5] ++ +
Example 2:
+

+Input: root = [5,2,4,null,1], val = 3 +Output: [5,2,4,null,1,null,3] +Explanation: A = [2,1,5,4], B = [2,1,5,4,3] ++ +
Example 3:
+

+Input: root = [5,2,3,null,1], val = 4 +Output: [5,2,4,null,1,3] +Explanation: A = [2,1,5,3], B = [2,1,5,3,4] ++ +
+
Note:
+ +1 <= B.length <= 100