2022-08-28 23:32:10 +02:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cassert>
|
|
|
|
#include <cstddef>
|
|
|
|
#include <iostream>
|
|
|
|
#include <iterator>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2024-01-03 12:06:42 +01:00
|
|
|
template <typename T> void print_matrix(const std::vector<std::vector<T>> &m) {
|
|
|
|
for (const auto &row : m) {
|
2022-08-28 23:32:10 +02:00
|
|
|
for (auto x : row) {
|
|
|
|
std::cout << x << " ";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::cout << "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::cout << "\n";
|
|
|
|
}
|
|
|
|
|
2024-01-03 12:06:42 +01:00
|
|
|
template <typename T> class diagonal {
|
|
|
|
std::vector<std::vector<T>> &matrix;
|
2022-08-28 23:32:10 +02:00
|
|
|
std::size_t x;
|
|
|
|
std::size_t y;
|
|
|
|
|
|
|
|
class diagonal_iter {
|
2024-01-03 12:06:42 +01:00
|
|
|
std::vector<std::vector<T>> &m;
|
2022-08-28 23:32:10 +02:00
|
|
|
std::size_t x;
|
|
|
|
std::size_t y;
|
|
|
|
|
2024-01-03 12:06:42 +01:00
|
|
|
public:
|
2022-08-28 23:32:10 +02:00
|
|
|
using difference_type = std::ptrdiff_t;
|
|
|
|
using value_type = T;
|
2024-01-03 12:06:42 +01:00
|
|
|
using pointer = T *;
|
|
|
|
using reference = T &;
|
2022-08-28 23:32:10 +02:00
|
|
|
using iterator_category = std::random_access_iterator_tag;
|
|
|
|
|
2024-01-03 12:06:42 +01:00
|
|
|
diagonal_iter(std::vector<std::vector<T>> &matrix, std::size_t x,
|
|
|
|
std::size_t y)
|
|
|
|
: m(matrix), x(x), y(y) {}
|
2022-08-28 23:32:10 +02:00
|
|
|
|
2024-01-03 12:06:42 +01:00
|
|
|
bool operator!=(const diagonal_iter &rhs) const {
|
2022-08-28 23:32:10 +02:00
|
|
|
return m != rhs.m || x != rhs.x || y != rhs.y;
|
|
|
|
}
|
2024-01-03 12:06:42 +01:00
|
|
|
bool operator==(const diagonal_iter &rhs) const {
|
|
|
|
return !(*this != rhs);
|
|
|
|
}
|
2022-08-28 23:32:10 +02:00
|
|
|
|
2024-01-03 12:06:42 +01:00
|
|
|
diagonal_iter &operator++() {
|
2022-08-28 23:32:10 +02:00
|
|
|
x++;
|
|
|
|
y++;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2024-01-03 12:06:42 +01:00
|
|
|
diagonal_iter operator--() {
|
2022-08-28 23:32:10 +02:00
|
|
|
x--;
|
|
|
|
y--;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
reference operator*() const { return m[y][x]; }
|
|
|
|
|
2024-01-03 12:06:42 +01:00
|
|
|
diagonal_iter operator-(difference_type n) const {
|
|
|
|
return diagonal_iter{m, x - n, y - n};
|
2022-08-28 23:32:10 +02:00
|
|
|
}
|
2024-01-03 12:06:42 +01:00
|
|
|
int operator-(const diagonal_iter &rhs) const { return x - rhs.x; }
|
2022-08-28 23:32:10 +02:00
|
|
|
|
2024-01-03 12:06:42 +01:00
|
|
|
diagonal_iter operator+(difference_type n) const {
|
|
|
|
return diagonal_iter{m, x + n, y + n};
|
2022-08-28 23:32:10 +02:00
|
|
|
}
|
|
|
|
|
2024-01-03 12:06:42 +01:00
|
|
|
bool operator<(const diagonal_iter &rhs) const { return x < rhs.x; }
|
2022-08-28 23:32:10 +02:00
|
|
|
|
2024-01-03 12:06:42 +01:00
|
|
|
diagonal_iter &operator=(const diagonal_iter &rhs) {
|
2022-08-28 23:32:10 +02:00
|
|
|
if (this != &rhs) // not a self-assignment
|
|
|
|
{
|
|
|
|
this->m = rhs.m;
|
|
|
|
this->x = rhs.x;
|
|
|
|
this->y = rhs.y;
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-01-03 12:06:42 +01:00
|
|
|
public:
|
|
|
|
diagonal(std::vector<std::vector<T>> &matrix, std::size_t x, std::size_t y)
|
|
|
|
: matrix(matrix), x(x), y(y) {}
|
2022-08-28 23:32:10 +02:00
|
|
|
|
2024-01-03 12:06:42 +01:00
|
|
|
diagonal_iter begin() const { return diagonal_iter{matrix, x, y}; }
|
2022-08-28 23:32:10 +02:00
|
|
|
|
2024-01-03 12:06:42 +01:00
|
|
|
diagonal_iter end() const {
|
2022-08-28 23:32:10 +02:00
|
|
|
auto max_x = matrix[y].size();
|
|
|
|
auto max_y = matrix.size();
|
|
|
|
|
|
|
|
auto steps = std::min(max_x - x, max_y - y);
|
|
|
|
|
2024-01-03 12:06:42 +01:00
|
|
|
return diagonal_iter{matrix, x + steps, y + steps};
|
2022-08-28 23:32:10 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-01-03 12:06:42 +01:00
|
|
|
template <typename T> class diagonals {
|
|
|
|
std::vector<std::vector<T>> &_matrix;
|
2022-08-28 23:32:10 +02:00
|
|
|
|
|
|
|
class diagonals_iter {
|
2024-01-03 12:06:42 +01:00
|
|
|
std::vector<std::vector<T>> &m;
|
2022-08-28 23:32:10 +02:00
|
|
|
std::size_t x;
|
|
|
|
std::size_t y;
|
|
|
|
|
2024-01-03 12:06:42 +01:00
|
|
|
public:
|
|
|
|
diagonals_iter(std::vector<std::vector<T>> &matrix, std::size_t x,
|
|
|
|
std::size_t y)
|
|
|
|
: m(matrix), x(x), y(y) {}
|
2022-08-28 23:32:10 +02:00
|
|
|
|
2024-01-03 12:06:42 +01:00
|
|
|
bool operator!=(const diagonals_iter &rhs) const {
|
2022-08-28 23:32:10 +02:00
|
|
|
return m != rhs.m || x != rhs.x || y != rhs.y;
|
|
|
|
}
|
|
|
|
|
2024-01-03 12:06:42 +01:00
|
|
|
diagonals_iter &operator++() {
|
2022-08-28 23:32:10 +02:00
|
|
|
if (y != 0) {
|
|
|
|
// iterating through diagonals down the first column
|
|
|
|
y++;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// iterating the diagonals along the first row
|
|
|
|
x++;
|
|
|
|
if (x == m.front().size()) {
|
|
|
|
// switching to diagonals in the first column
|
|
|
|
x = 0;
|
|
|
|
y++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2024-01-03 12:06:42 +01:00
|
|
|
diagonal<T> operator*() const { return diagonal{m, x, y}; }
|
2022-08-28 23:32:10 +02:00
|
|
|
};
|
|
|
|
|
2024-01-03 12:06:42 +01:00
|
|
|
public:
|
|
|
|
diagonals(std::vector<std::vector<T>> &matrix) : _matrix(matrix) {}
|
|
|
|
diagonals_iter begin() { return diagonals_iter{_matrix, 0, 0}; }
|
|
|
|
diagonals_iter end() { return diagonals_iter{_matrix, 0, _matrix.size()}; }
|
2022-08-28 23:32:10 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
class Solution {
|
2024-01-03 12:06:42 +01:00
|
|
|
public:
|
|
|
|
std::vector<std::vector<int>>
|
|
|
|
diagonalSort(std::vector<std::vector<int>> mat) {
|
2022-08-28 23:32:10 +02:00
|
|
|
for (auto d : diagonals(mat)) {
|
|
|
|
std::sort(d.begin(), d.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
return mat;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-01-03 12:06:42 +01:00
|
|
|
static void test_case_1() {
|
2022-08-28 23:32:10 +02:00
|
|
|
// Input: mat = [[3,3,1,1],[2,2,1,2],[1,1,1,2]]
|
|
|
|
// Output: [[1,1,1,1],[1,2,2,2],[1,2,3,3]]
|
|
|
|
|
|
|
|
Solution s;
|
2024-01-03 12:06:42 +01:00
|
|
|
assert((s.diagonalSort(std::vector{std::vector{3, 3, 1, 1},
|
|
|
|
std::vector{2, 2, 1, 2},
|
|
|
|
std::vector{1, 1, 1, 2}}) ==
|
|
|
|
std::vector{std::vector{1, 1, 1, 1}, std::vector{1, 2, 2, 2},
|
|
|
|
std::vector{1, 2, 3, 3}}));
|
2022-08-28 23:32:10 +02:00
|
|
|
}
|
|
|
|
|
2024-01-03 12:06:42 +01:00
|
|
|
static void test_case_2() {
|
2022-08-28 23:32:10 +02:00
|
|
|
// Input: mat =
|
|
|
|
// [[11,25,66,1,69,7],[23,55,17,45,15,52],[75,31,36,44,58,8],[22,27,33,25,68,4],[84,28,14,11,5,50]]
|
|
|
|
// Output:
|
|
|
|
// [[5,17,4,1,52,7],[11,11,25,45,8,69],[14,23,25,44,58,15],[22,27,31,36,50,66],[84,28,75,33,55,68]]
|
|
|
|
|
|
|
|
Solution s;
|
2024-01-03 12:06:42 +01:00
|
|
|
assert((s.diagonalSort(std::vector{std::vector{11, 25, 66, 1, 69, 7},
|
|
|
|
std::vector{23, 55, 17, 45, 15, 52},
|
|
|
|
std::vector{75, 31, 36, 44, 58, 8},
|
|
|
|
std::vector{22, 27, 33, 25, 68, 4},
|
|
|
|
std::vector{84, 28, 14, 11, 5, 50}}) ==
|
|
|
|
std::vector{std::vector{5, 17, 4, 1, 52, 7},
|
|
|
|
std::vector{11, 11, 25, 45, 8, 69},
|
|
|
|
std::vector{14, 23, 25, 44, 58, 15},
|
|
|
|
std::vector{22, 27, 31, 36, 50, 66},
|
|
|
|
std::vector{84, 28, 75, 33, 55, 68}}));
|
2022-08-28 23:32:10 +02:00
|
|
|
}
|
|
|
|
|
2024-01-03 12:06:42 +01:00
|
|
|
int main() {
|
2022-08-28 23:32:10 +02:00
|
|
|
test_case_1();
|
|
|
|
test_case_2();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|