diff --git a/2202/220215-CN.cpp b/2202/220215-CN.cpp new file mode 100644 index 0000000..edfc31e --- /dev/null +++ b/2202/220215-CN.cpp @@ -0,0 +1,36 @@ +#include +#include + +/** + * 1380. Lucky Numbers in a Matrix + * Given an m x n matrix of distinct numbers, return all lucky numbers in the matrix in any order. + * A lucky number is an element of the matrix such that it is the minimum element in its row and maximum in its column. + */ + +class Solution { +public: + static std::vector luckyNumbers (const std::vector>& matrix) { + int m = matrix.size(), n = m ? matrix.front().size() : 0; + std::vector rowMin(m), colMax(n, -0x7FFFFFFF); + for (int i = 0; i < m; ++i) + rowMin[i] = *std::min_element(matrix[i].begin(), matrix[i].end()); + for (int i = 0; i < n; ++i) + for (int j = 0; j < m; ++j) + colMax[i] = std::max(matrix[j][i], colMax[i]); + + std::vector ret; + for (int i = 0; i < m; ++i) + for (int j = 0; j < n; ++j) + if (matrix[i][j] == rowMin[i] && matrix[i][j] == colMax[j]) + ret.push_back(matrix[i][j]); + return ret; + } +}; + +int main() { + const auto ret = Solution::luckyNumbers({{3,7,8},{9,11,13},{15,16,17}}); + for (int i : ret) { + std::printf("%d ", i); + } + return 0; +} diff --git a/2202/CMakeLists.txt b/2202/CMakeLists.txt index bd3759f..3ea8ac5 100644 --- a/2202/CMakeLists.txt +++ b/2202/CMakeLists.txt @@ -3,4 +3,4 @@ PROJECT(2202) SET(CMAKE_CXX_STANDARD 23) -ADD_EXECUTABLE(2202 220214.cpp) +ADD_EXECUTABLE(2202 220215-CN.cpp)