class Solution:
def kWeakestRows(self, mat: List[List[int]], k: int) -> List[int]:
m,n = len(mat),len(mat[0])
def search_bound(nums: List[int]): # O(logn)
nums = nums[::-1]
n = len(nums)
left, right = 0, n - 1
bound = 0
while left <= right:
mid = left + (right - left) // 2
if nums[mid] < 1:
left = mid + 1
else:
bound = n - mid
right = mid - 1
return bound
output = []
for row_index in range(m):
total = search_bound(mat[row_index])
output.append((total,row_index))
output = sorted(output, key=lambda x: x[0]) # O(nlogn)
return [output[i][-1] for i in range(k)]
訂單已配對司機
Feb 22, 2025鄰接矩陣 (Adjacent Matrix)
May 12, 2024透過標記已訪問過的島嶼為 “2” 來遍歷是否為同一個島嶼
Apr 19, 2024逐一計算 class Solution: def islandPerimeter(self, grid: List[List[int]]) -> int: xc,yc = len(grid[0]), len(grid) output = 0 for y in range(yc): for x in range(xc): if grid[y][x] == 1: borders = 4 if x >= 1:
Apr 18, 2024or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up