LeetCode //C - 1030. Matrix Cells in Distance Order

1030. Matrix Cells in Distance Order

You are given four integers row, cols, rCenter, and cCenter. There is a rows x cols matrix and you are on the cell with the coordinates (rCenter, cCenter).

Return the coordinates of all cells in the matrix, sorted by their distance from (rCenter, cCenter) from the smallest distance to the largest distance. You may return the answer in any order that satisfies this condition.

The distance between two cells ( r 1 , c 1 ) (r_1, c_1) (r1,c1) and ( r 2 , c 2 ) (r_2, c_2) (r2,c2) is ∣ r 1 − r 2 ∣ + ∣ c 1 − c 2 ∣ |r_1 - r_2| + |c_1 - c_2| ∣r1−r2∣+∣c1−c2∣.

Example 1:

Input: rows = 1, cols = 2, rCenter = 0, cCenter = 0
Output: [[0,0],[0,1]]
Explanation: The distances from (0, 0) to other cells are: [0,1]

Example 2:

Input: rows = 2, cols = 2, rCenter = 0, cCenter = 1
Output: [[0,1],[0,0],[1,1],[1,0]]
Explanation: The distances from (0, 1) to other cells are: [0,1,1,2]

The answer [[0,1],[1,1],[0,0],[1,0]] would also be accepted as correct.

Example 3:

Input: rows = 2, cols = 3, rCenter = 1, cCenter = 2
Output: [[1,2],[0,2],[1,1],[0,1],[1,0],[0,0]]
Explanation: The distances from (1, 2) to other cells are: [0,1,1,2,2,3]

There are other answers that would also be accepted as correct, such as [[1,2],[1,1],[0,2],[1,0],[0,1],[0,0]].

Constraints:
  • 1 <= rows, cols <= 100
  • 0 <= rCenter < rows
  • 0 <= cCenter < cols

From: LeetCode

Link: 1030. Matrix Cells in Distance Order


Solution:

Ideas:
  • Store every cell with its Manhattan distance to (rCenter, cCenter).
  • Sort all cells by distance.
  • Build the required int** result array.
Code:
c 复制代码
typedef struct {
    int r;
    int c;
    int dist;
} Cell;

static int cmp(const void *a, const void *b) {
    Cell *x = (Cell *)a;
    Cell *y = (Cell *)b;
    return x->dist - y->dist;
}

/**
 * Return an array of arrays of size *returnSize.
 * The sizes of the arrays are returned as *returnColumnSizes array.
 * Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
 */
int** allCellsDistOrder(int rows, int cols, int rCenter, int cCenter, int* returnSize, int** returnColumnSizes) {
    int total = rows * cols;
    *returnSize = total;

    Cell *cells = (Cell *)malloc(sizeof(Cell) * total);
    int idx = 0;

    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            cells[idx].r = i;
            cells[idx].c = j;
            cells[idx].dist = abs(i - rCenter) + abs(j - cCenter);
            idx++;
        }
    }

    qsort(cells, total, sizeof(Cell), cmp);

    int **result = (int **)malloc(sizeof(int *) * total);
    *returnColumnSizes = (int *)malloc(sizeof(int) * total);

    for (int i = 0; i < total; i++) {
        result[i] = (int *)malloc(sizeof(int) * 2);
        result[i][0] = cells[i].r;
        result[i][1] = cells[i].c;
        (*returnColumnSizes)[i] = 2;
    }

    free(cells);
    return result;
}
相关推荐
weixin_3077791318 小时前
基于Vosk与CTranslate2的实时语音识别翻译系统 —— 完整C++实现详解
人工智能·算法·自动化·语音识别·原型模式
akarinnnn18 小时前
深入理解内存函数:原理、应用与优化
c语言·网络·数据结构·算法
一行代码一行诗++18 小时前
for循环中的break和continue
数据结构·算法
Tisfy18 小时前
LeetCode 3043.最长公共前缀的长度:哈希表(不转string)
算法·leetcode·散列表·题解·哈希表
代码中介商18 小时前
排序算法完全指南(三):插入排序深度详解
算法·排序算法
承渊政道18 小时前
【贪心算法】(经典实战应用解析(六):整数替换、俄罗斯套娃信封问题、可被三整除的最⼤和、距离相等的条形码、重构字符串)
c++·算法·leetcode·贪心算法·排序算法·动态规划·哈希算法
宠..18 小时前
VS Code SSH 远程连接 Ubuntu 并实现快速运行(C/C++示例)
java·运维·c语言·开发语言·c++·ubuntu·ssh
WL_Aurora18 小时前
Python 算法基础篇之排序算法(二):希尔、快速、归并
python·算法·排序算法
闻缺陷则喜何志丹18 小时前
【图论 树 启发式合并】P7165 [COCI2020-2021#1] Papričice|普及+
c++·算法·启发式算法·图论··洛谷
AI科技星18 小时前
基于平行素数对等腰梯形网格拓扑的完备性证明哥德巴赫猜想1+1
c语言·开发语言·网络·量子计算·agi