## 題解 ### 二分搜索+排序 ```python= 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)] ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up