From 3dd4bd7e69d0964788a44a8d884e6b6fdceefa96 Mon Sep 17 00:00:00 2001 From: Lam Haoyin Date: Sun, 27 Mar 2022 10:25:38 +0800 Subject: [PATCH] feat: optimise 220327 [cpp] --- cpp/2203/220327.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/cpp/2203/220327.cpp b/cpp/2203/220327.cpp index d1a5958..1fca45a 100644 --- a/cpp/2203/220327.cpp +++ b/cpp/2203/220327.cpp @@ -14,6 +14,20 @@ */ class Solution { +public: + static std::vector kWeakestRows(const std::vector>& mat, int k) { + int n = mat.size(); + std::vector 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: static std::vector kWeakestRows(const std::vector>& mat, int k) { int n = mat.size();