classSolution { public: vector<int> kWeakestRows(vector<vector<int>>& mat, int k){ int m = mat.size(), n = mat[0].size(); vector<pair<int, int>> power; for (int i = 0; i < m; ++i) { int l = 0, r = n - 1, pos = -1; while (l <= r) { int mid = (l + r) / 2; if (mat[i][mid] == 0) { r = mid - 1; } else { pos = mid; l = mid + 1; } } power.emplace_back(pos + 1, i); }
priority_queue q(greater<pair<int, int>>(), move(power)); vector<int> ans; for (int i = 0; i < k; ++i) { ans.push_back(q.top().second); q.pop(); } return ans; } };
funckWeakestRows(mat [][]int, k int) []int { h := hp{} for i, row := range mat { pow := sort.Search(len(row), func(j int)bool { return row[j] == 0 }) h = append(h, pair{pow, i}) } heap.Init(&h) ans := make([]int, k) for i := range ans { ans[i] = heap.Pop(&h).(pair).idx } return ans }
template<typename T> classHelper { staticintpartition(vector<T>& nums, int l, int r){ T pivot = nums[r]; int i = l - 1; for (int j = l; j <= r - 1; ++j) { if (nums[j] <= pivot) { i = i + 1; swap(nums[i], nums[j]); } } swap(nums[i + 1], nums[r]); return i + 1; }
// 基于随机的划分 staticintrandomized_partition(vector<T>& nums, int l, int r){ int i = rand() % (r - l + 1) + l; swap(nums[r], nums[i]); returnpartition(nums, l, r); }
staticvoidrandomized_selected(vector<T>& arr, int l, int r, int k){ if (l >= r) { return; } int pos = randomized_partition(arr, l, r); int num = pos - l + 1; if (k == num) { return; } elseif (k < num) { randomized_selected(arr, l, pos - 1, k); } else { randomized_selected(arr, pos + 1, r, k - num); } }
public: static vector<T> getLeastNumbers(vector<T>& arr, int k){ srand((unsigned)time(NULL)); randomized_selected(arr, 0, (int)arr.size() - 1, k); vector<T> vec; for (int i = 0; i < k; ++i) { vec.push_back(arr[i]); } return vec; } };
classSolution { public: vector<int> kWeakestRows(vector<vector<int>>& mat, int k){ int m = mat.size(), n = mat[0].size(); vector<pair<int, int>> power; for (int i = 0; i < m; ++i) { int l = 0, r = n - 1, pos = -1; while (l <= r) { int mid = (l + r) / 2; if (mat[i][mid] == 0) { r = mid - 1; } else { pos = mid; l = mid + 1; } } power.emplace_back(pos + 1, i); }
vector<pair<int, int>> minimum = Helper<pair<int, int>>::getLeastNumbers(power, k); sort(minimum.begin(), minimum.begin() + k); vector<int> ans; for (int i = 0; i < k; ++i) { ans.push_back(minimum[i].second); } return ans; } };
privatestaticvoidrandomizedSelected(int[][] arr, int l, int r, int k) { if (l >= r) { return; } intpos= randomizedPartition(arr, l, r); intnum= pos - l + 1; if (k == num) { return; } elseif (k < num) { randomizedSelected(arr, l, pos - 1, k); } else { randomizedSelected(arr, pos + 1, r, k - num); } }
// 基于随机的划分 privatestaticintrandomizedPartition(int[][] nums, int l, int r) { inti= (int) (Math.random() * (r - l + 1)) + l; swap(nums, r, i); return partition(nums, l, r); }
privatestaticintpartition(int[][] nums, int l, int r) { int[] pivot = nums[r]; inti= l - 1; for (intj= l; j <= r - 1; ++j) { if (compare(nums[j], pivot) <= 0) { i = i + 1; swap(nums, i, j); } } swap(nums, i + 1, r); return i + 1; }
privatestaticvoidswap(int[][] nums, int i, int j) { int[] temp = newint[nums[i].length]; System.arraycopy(nums[i], 0, temp, 0, nums[i].length); System.arraycopy(nums[j], 0, nums[i], 0, nums[i].length); System.arraycopy(temp, 0, nums[j], 0, nums[i].length); }
publicclassSolution { publicint[] KWeakestRows(int[][] mat, int k) { int m = mat.Length, n = mat[0].Length; Tuple<int, int>[] power = new Tuple<int, int>[m]; for (int i = 0; i < m; ++i) { int l = 0, r = n - 1, pos = -1; while (l <= r) { int mid = (l + r) / 2; if (mat[i][mid] == 0) { r = mid - 1; } else { pos = mid; l = mid + 1; } } power[i] = new Tuple<int, int>(pos + 1, i); }
Tuple<int, int>[] minimum = Helper.GetLeastNumbers(power, k); Array.Sort(minimum); int[] ans = newint[k]; for (int i = 0; i < k; ++i) { ans[i] = minimum[i].Item2; } return ans; } }
classHelper { static Random random = new Random(); publicstatic Tuple<int, int>[] GetLeastNumbers(Tuple<int, int>[] arr, int k) { RandomizedSelected(arr, 0, arr.Length - 1, k); Tuple<int, int>[] vec = new Tuple<int, int>[k]; for (int i = 0; i < k; ++i) { vec[i] = arr[i]; } return vec; }
staticvoidRandomizedSelected(Tuple<int, int>[] arr, int l, int r, int k) { if (l >= r) { return; } int pos = RandomizedPartition(arr, l, r); int num = pos - l + 1; if (k == num) { return; } elseif (k < num) { RandomizedSelected(arr, l, pos - 1, k); } else { RandomizedSelected(arr, pos + 1, r, k - num); } }
// 基于随机的划分 staticintRandomizedPartition(Tuple<int, int>[] nums, int l, int r) { int i = random.Next(r - l + 1) + l; Swap(nums, r, i); return Partition(nums, l, r); }
staticintPartition(Tuple<int, int>[] nums, int l, int r) { Tuple<int, int> pivot = nums[r]; int i = l - 1; for (int j = l; j <= r - 1; ++j) { if (Compare(nums[j], pivot) <= 0) { i = i + 1; Swap(nums, i, j); } } Swap(nums, i + 1, r); return i + 1; }
staticvoidSwap(Tuple<int, int>[] nums, int i, int j) { Tuple<int, int> temp = nums[i]; nums[i] = nums[j]; nums[j] = temp; }
funckWeakestRows(mat [][]int, k int) []int { m := len(mat) pairs := make([]pair, m) for i, row := range mat { pow := sort.Search(len(row), func(j int)bool { return row[j] == 0 }) pairs[i] = pair{pow, i} } rand.Seed(time.Now().UnixNano()) randomizedSelected(pairs, 0, m-1, k) pairs = pairs[:k] sort.Slice(pairs, func(i, j int)bool { a, b := pairs[i], pairs[j] return a.pow < b.pow || a.pow == b.pow && a.idx < b.idx }) ans := make([]int, k) for i, p := range pairs { ans[i] = p.idx } return ans }
funcrandomizedSelected(a []pair, l, r, k int) { if l >= r { return } pos := randomPartition(a, l, r) num := pos - l + 1 if k == num { return } if k < num { randomizedSelected(a, l, pos-1, k) } else { randomizedSelected(a, pos+1, r, k-num) } }
funcrandomPartition(a []pair, l, r int)int { i := rand.Intn(r-l+1) + l a[i], a[r] = a[r], a[i] return partition(a, l, r) }
funcpartition(a []pair, l, r int)int { pivot := a[r] i := l - 1 for j := l; j < r; j++ { if a[j].pow < pivot.pow || a[j].pow == pivot.pow && a[j].idx <= pivot.idx { i++ a[i], a[j] = a[j], a[i] } } a[i+1], a[r] = a[r], a[i+1] return i + 1 }