Skip to content

Commit 80f8320

Browse files
committed
Solutions of 995, 997, 998, 999
1 parent a25bf70 commit 80f8320

File tree

5 files changed

+240
-0
lines changed

5 files changed

+240
-0
lines changed

900-1000q/995.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
'''
2+
In an array A containing only 0s and 1s, a K-bit flip consists of choosing a (contiguous) subarray of length K and simultaneously changing every 0 in the subarray to 1, and every 1 in the subarray to 0.
3+
4+
Return the minimum number of K-bit flips required so that there is no 0 in the array. If it is not possible, return -1.
5+
6+
Input: A = [0,1,0], K = 1
7+
Output: 2
8+
Explanation: Flip A[0], then flip A[2]
9+
'''
10+
11+
class Solution:
12+
def minKBitFlips(self, a: 'List[int]', k: 'int') -> 'int':
13+
from collections import deque
14+
q = deque()
15+
res = 0
16+
for i in range(len(a)):
17+
if len(q) % 2 != 0:
18+
if a[i] == 1:
19+
res += 1
20+
q.append(i+k-1)
21+
else:
22+
if a[i] == 0:
23+
res += 1
24+
q.append(i+k-1)
25+
if q and q[0] == i: q.popleft()
26+
if q and q[-1] >= len(a): return -1
27+
return res

900-1000q/997.py

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
'''
2+
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.
3+
4+
If the town judge exists, then:
5+
6+
The town judge trusts nobody.
7+
Everybody (except for the town judge) trusts the town judge.
8+
There is exactly one person that satisfies properties 1 and 2.
9+
You are given trust, an array of pairs trust[i] = [a, b] representing that the person labelled a trusts the person labelled b.
10+
11+
If the town judge exists and can be identified, return the label of the town judge. Otherwise, return -1.
12+
13+
14+
15+
Example 1:
16+
17+
Input: N = 2, trust = [[1,2]]
18+
Output: 2
19+
Example 2:
20+
21+
Input: N = 3, trust = [[1,3],[2,3]]
22+
Output: 3
23+
'''
24+
25+
class Solution(object):
26+
def findJudge(self, N, trust):
27+
"""
28+
:type N: int
29+
:type trust: List[List[int]]
30+
:rtype: int
31+
"""
32+
if not trust:
33+
return 1
34+
mapping = {}
35+
unique = set()
36+
for truste_list in trust:
37+
unique.add(truste_list[0])
38+
if truste_list[1] in mapping:
39+
mapping[truste_list[1]] += 1
40+
else:
41+
mapping[truste_list[1]] = 1
42+
43+
unique_set = len(unique)
44+
for key, value in mapping.items():
45+
if value == unique_set:
46+
return key
47+
return -1
48+

900-1000q/998.py

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
'''
2+
We 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.
3+
4+
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:
5+
6+
If A is empty, return null.
7+
Otherwise, let A[i] be the largest element of A. Create a root node with value A[i].
8+
The left child of root will be Construct([A[0], A[1], ..., A[i-1]])
9+
The right child of root will be Construct([A[i+1], A[i+2], ..., A[A.length - 1]])
10+
Return root.
11+
Note that we were not given A directly, only a root node root = Construct(A).
12+
13+
Suppose B is a copy of A with the value val appended to it. It is guaranteed that B has unique values.
14+
15+
Return Construct(B).
16+
17+
18+
19+
Example 1:
20+
21+
22+
23+
Input: root = [4,1,3,null,null,2], val = 5
24+
Output: [5,4,null,1,3,null,null,2]
25+
Explanation: A = [1,4,2,3], B = [1,4,2,3,5]
26+
Example 2:
27+
28+
29+
Input: root = [5,2,4,null,1], val = 3
30+
Output: [5,2,4,null,1,null,3]
31+
Explanation: A = [2,1,5,4], B = [2,1,5,4,3]
32+
'''
33+
# Definition for a binary tree node.
34+
# class TreeNode(object):
35+
# def __init__(self, x):
36+
# self.val = x
37+
# self.left = None
38+
# self.right = None
39+
40+
class Solution(object):
41+
42+
def insertIntoMaxTree(self, root, val):
43+
"""
44+
:type root: TreeNode
45+
:type val: int
46+
:rtype: TreeNode
47+
"""
48+
new_node = TreeNode(val)
49+
if not root:
50+
return new_node
51+
52+
if root.val < val:
53+
new_node.left = root
54+
return new_node
55+
56+
nrwwt = root
57+
start, prev = root.right, root
58+
59+
while start:
60+
if(start.val > val):
61+
prev = start
62+
start = start.right
63+
else:
64+
break
65+
66+
67+
prev.right = new_node
68+
if not start:
69+
new_node.right = start
70+
else:
71+
new_node.left = start
72+
73+
return root
74+

900-1000q/999.py

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
'''
2+
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.
3+
4+
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.
5+
6+
Return the number of pawns the rook can capture in one move.
7+
8+
Input: [[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".","R",".",".",".","p"],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."]]
9+
Output: 3
10+
Explanation:
11+
In this example the rook is able to capture all the pawns.
12+
'''
13+
14+
class Solution(object):
15+
def numRookCaptures(self, board):
16+
"""
17+
:type board: List[List[str]]
18+
:rtype: int
19+
"""
20+
result = 0
21+
rook_index = (0, 0)
22+
for row in range(len(board)):
23+
for col in range(len(board[0])):
24+
if board[row][col] == 'R':
25+
rook_index = (row, col)
26+
break
27+
28+
flag = True
29+
col = rook_index[1]-1
30+
pawn = 0
31+
while col >= 0:
32+
if board[rook_index[0]][col] == 'B':
33+
flag = False
34+
break
35+
if board[rook_index[0]][col] == 'p':
36+
pawn += 1
37+
break
38+
col -= 1
39+
if flag and pawn != 0:
40+
result += 1
41+
42+
flag = True
43+
col = rook_index[1]+1
44+
pawn = 0
45+
while col < len(board[0]):
46+
if board[rook_index[0]][col] == 'B':
47+
flag = False
48+
break
49+
if board[rook_index[0]][col] == 'p':
50+
pawn += 1
51+
break
52+
col += 1
53+
54+
if flag and pawn != 0:
55+
result += 1
56+
57+
flag = True
58+
row = rook_index[0]+1
59+
pawn = 0
60+
while row < len(board):
61+
if board[row][rook_index[1]] == 'B':
62+
flag = False
63+
break
64+
65+
if board[row][rook_index[1]] == 'p':
66+
pawn += 1
67+
break
68+
row += 1
69+
70+
if flag and pawn != 0:
71+
result += 1
72+
73+
pawn = 0
74+
flag = True
75+
row = rook_index[0]-1
76+
while row >= 0:
77+
if board[row][rook_index[1]] == 'B':
78+
flag = False
79+
break
80+
if board[row][rook_index[1]] == 'p':
81+
pawn += 1
82+
break
83+
row -= 1
84+
if flag and pawn != 0:
85+
result += 1
86+
87+
return result

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ Python solution of problems from [LeetCode](https://leetcode.com/).
77
##### [Problems 900-1000](./900-1000q/)
88
| # | Title | Solution | Difficulty |
99
|---| ----- | -------- | ---------- |
10+
|999|[Available Captures for Rook](https://leetcode.com/problems/available-captures-for-rook/)|[Python](./900-1000q/999.py)|Medium|
11+
|998|[Maximum Binary Tree II](https://leetcode.com/problems/maximum-binary-tree-ii) | [Python](./900-1000q/998.py) | Easy|
12+
|997|[Find the Town Judge](https://leetcode.com/problems/find-the-town-judge/) | [Python](./900-1000q/997.py) | Easy|
13+
|995|[Minimum Number of K Consecutive Bit Flips](https://leetcode.com/problems/minimum-number-of-k-consecutive-bit-flips/) | [Python](./900-1000q/995.py) | Hard|
1014
|994|[Rotting Oranges](https://leetcode.com/problems/rotting-oranges/) | [Python](./900-1000q/994.py)|Easy|
1115
|993|[Cousins in Binary Tree](https://leetcode.com/problems/cousins-in-binary-tree/) | [Python](./900-1000q/993.py)|Easy|
1216
|991|[Broken Calculator](https://leetcode.com/problems/broken-calculator/) | [Python](./900-1000q/991.py)|Medium|

0 commit comments

Comments
 (0)