-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathm3189.py
29 lines (23 loc) · 904 Bytes
/
m3189.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class Solution:
def minMoves(self, rooks: List[List[int]]) -> int:
remainingRows = set(range(len(rooks)))
remainingCols = remainingRows.copy()
extraRows = []
extraCols = []
for x, y in rooks :
if x in remainingRows :
remainingRows.remove(x)
else :
heapq.heappush(extraRows, x)
if y in remainingCols :
remainingCols.remove(y)
else :
heapq.heappush(extraCols, y)
remainingRows = sorted(list(remainingRows), reverse=True)
remainingCols = sorted(list(remainingCols), reverse=True)
output = 0
while remainingRows :
output += abs(remainingRows.pop() - heapq.heappop(extraRows))
while remainingCols :
output += abs(remainingCols.pop() - heapq.heappop(extraCols))
return output