feat: optimise 220327 [cpp]

This commit is contained in:
Lam Haoyin 2022-03-27 10:25:38 +08:00
parent 874a36ed5c
commit 3dd4bd7e69
No known key found for this signature in database
GPG Key ID: 8C089CB1A2B7544F
1 changed files with 14 additions and 0 deletions

View File

@ -14,6 +14,20 @@
*/ */
class Solution { class Solution {
public:
static std::vector<int> kWeakestRows(const std::vector<std::vector<int>>& mat, int k) {
int n = mat.size();
std::vector<int> idMat(n), ret(n);
for (int i = 0; i < n; ++i)
idMat[ret[i] = i] = std::distance(mat[i].begin(), std::lower_bound(mat[i].begin(), mat[i].end(), 0, std::greater<>()));
std::sort(ret.begin(), ret.end(), [&](int x, int y) {
return idMat[x] == idMat[y] ? x < y : idMat[x] < idMat[y];
});
return {ret.begin(), ret.begin() + k};
}
};
class SolutionOld {
public: public:
static std::vector<int> kWeakestRows(const std::vector<std::vector<int>>& mat, int k) { static std::vector<int> kWeakestRows(const std::vector<std::vector<int>>& mat, int k) {
int n = mat.size(); int n = mat.size();