diff options
author | terminaldweller <thabogre@gmail.com> | 2022-04-04 20:32:19 +0000 |
---|---|---|
committer | terminaldweller <thabogre@gmail.com> | 2022-04-04 20:32:19 +0000 |
commit | bee546b9efce8f576c85ecca70c896074a918468 (patch) | |
tree | 322c685eaee3841736479a016f4efd8d5fd15096 /1337/main.cpp | |
parent | update (diff) | |
download | leetcode-bee546b9efce8f576c85ecca70c896074a918468.tar.gz leetcode-bee546b9efce8f576c85ecca70c896074a918468.zip |
update
Diffstat (limited to '')
-rw-r--r-- | 1337/main.cpp | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/1337/main.cpp b/1337/main.cpp new file mode 100644 index 0000000..1e3ab45 --- /dev/null +++ b/1337/main.cpp @@ -0,0 +1,56 @@ +#include "header.hpp" + +class Solution { +public: + std::vector<int> kWeakestRows(std::vector<std::vector<int>> &mat, int k) { + std::pair<std::vector<int>, std::vector<int>> sums; + sums.first.reserve(mat.size()); + sums.second.reserve(mat.size()); + int row = 0; + int sum = 0; + + for (auto &iter : mat) { + for (auto &ya_iter : iter) { + sum += ya_iter; + if (0 == ya_iter) { + break; + } + } + sums.first[row] = row; + sums.second[row] = sum; + row++; + sum = 0; + } + + for (int i = 0; i < row - 1; ++i) { + for (int j = 0; j < row - 1; ++j) { + if (sums.second[j] > sums.second[j + 1]) { + std::swap(sums.first[j], sums.first[j + 1]); + std::swap(sums.second[j], sums.second[j + 1]); + } + } + } + + /* for (int i = 0; i < row; ++i) { */ + /* std::cout << sums.first[i] << ":" << sums.second[i] << "\n"; */ + /* } */ + + std::vector<int> result(k); + std::copy(sums.first.begin(), sums.first.begin() + k, result.begin()); + return result; + } +}; + +int main(int argc, char **argv) { + Solution solution; + std::vector<std::vector<int>> mat = {{1, 1, 0, 0, 0}, + {1, 1, 1, 1, 0}, + {1, 0, 0, 0, 0}, + {1, 1, 0, 0, 0}, + {1, 1, 1, 1, 1}}; + auto result = solution.kWeakestRows(mat, 3); + for (int i = 0; i < 3; ++i) { + std::cout << result[i] << "\n"; + } + return 0; +} |