leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

1329.cpp (1358B)


      1 class Solution {
      2   public:
      3     vector<vector<int>> diagonalSort(vector<vector<int>> &mat) {
      4         unordered_map<int, priority_queue<int, vector<int>, greater<int>>> um;
      5         int n = mat.size(), m = mat[0].size();
      6         for (int i = 0; i < n; i++) {
      7             for (int j = 0; j < m; j++) {
      8                 um[i - j].push(mat[i][j]);
      9             }
     10         }
     11 
     12         for (int i = 0; i < n; i++) {
     13             for (int j = 0; j < m; j++) {
     14                 mat[i][j] = um[i - j].top();
     15                 um[i - j].pop();
     16             }
     17         }
     18 
     19         return mat;
     20     }
     21 };
     22 
     23 // No extra memory
     24 class Solution {
     25   public:
     26     vector<vector<int>> diagonalSort(vector<vector<int>> &mat) {
     27         int n = mat.size(), m = mat[0].size();
     28         for (int k = 0; k < m; k++) {
     29             for (int i = 0, ik = k; i < n && ik < m; i++, ik++) {
     30                 for (int j = 0, jk = k; j < n && jk < m; j++, jk++) {
     31                     if (mat[i][ik] < mat[j][jk]) swap(mat[i][ik], mat[j][jk]);
     32                 }
     33             }
     34         }
     35 
     36         for (int k = 0; k < n; k++) {
     37             for (int i = 0, ik = k; i < m && ik < n; i++, ik++) {
     38                 for (int j = 0, jk = k; j < m && jk < n; j++, jk++) {
     39                     if (mat[ik][i] < mat[jk][j]) swap(mat[ik][i], mat[jk][j]);
     40                 }
     41             }
     42         }
     43 
     44         return mat;
     45     }
     46 };