1329. Sort the Matrix Diagonally

A matrix diagonal is a diagonal line of cells starting from some cell in either the topmost row or leftmost column and going in the bottom-right direction until reaching the matrix’s end. For example, the matrix diagonal starting from mat[2][0], where mat is a 6 x 3 matrix, includes cells mat[2][0], mat[3][1], and mat[4][2].

Given an m x n matrix mat of integers, sort each matrix diagonal in ascending order and return the resulting matrix.
 

Example 1:

在这里插入图片描述

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]]
Explanation: These arrays also are accepted [-5,-1,1,2,3] , [-3,-1,2,-2,4].

Example 2:

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]]

Constraints:
  • m == mat.length
  • n == mat[i].length
  • 1 <= m, n <= 100
  • 1 <= mat[i][j] <= 100

From: LeetCode
Link: 1329. Sort the Matrix Diagonally


Solution:

Ideas:

Sort each diagonal independently. A diagonal is identified by its starting cell on the top row or left column. For each diagonal, extract its elements into a temporary array, sort it in ascending order (using qsort), and then write the sorted values back along the same diagonal. The process covers all cells exactly once. The maximum diagonal length is at most 100, so a fixed‑size buffer works. The result matrix is allocated and returned with the required column‑size array.

Code:
#include <stdlib.h>

static int cmp(const void *a, const void *b) {
    return (*(int*)a - *(int*)b);
}

int** diagonalSort(int** mat, int matSize, int* matColSize, int* returnSize, int** returnColumnSizes) {
    int m = matSize;
    int n = matColSize[0];          // all rows have the same length

    // Allocate result matrix
    int** res = (int**)malloc(m * sizeof(int*));
    *returnColumnSizes = (int*)malloc(m * sizeof(int));
    *returnSize = m;
    for (int i = 0; i < m; i++) {
        res[i] = (int*)malloc(n * sizeof(int));
        (*returnColumnSizes)[i] = n;
    }

    // Temporary array for each diagonal (max length = min(m, n) <= 100)
    int temp[100];

    // 1. Diagonals starting from the top row (including the main diagonal)
    for (int startCol = 0; startCol < n; startCol++) {
        int r = 0, c = startCol;
        int len = 0;
        while (r < m && c < n) {
            temp[len++] = mat[r][c];
            r++;
            c++;
        }
        qsort(temp, len, sizeof(int), cmp);
        r = 0;
        c = startCol;
        for (int i = 0; i < len; i++) {
            res[r][c] = temp[i];
            r++;
            c++;
        }
    }

    // 2. Diagonals starting from the left column (skip (0,0) which is already done)
    for (int startRow = 1; startRow < m; startRow++) {
        int r = startRow, c = 0;
        int len = 0;
        while (r < m && c < n) {
            temp[len++] = mat[r][c];
            r++;
            c++;
        }
        qsort(temp, len, sizeof(int), cmp);
        r = startRow;
        c = 0;
        for (int i = 0; i < len; i++) {
            res[r][c] = temp[i];
            r++;
            c++;
        }
    }

    return res;
}
Logo

鲲鹏昇腾开发者社区是面向全社会开放的“联接全球计算开发者,聚合华为+生态”的社区,内容涵盖鲲鹏、昇腾资源,帮助开发者快速获取所需的知识、经验、软件、工具、算力,支撑开发者易学、好用、成功,成为核心开发者。

更多推荐