LeetCode //C - 329. Longest Increasing Path in a Matrix

329. Longest Increasing Path in a Matrix

Given an m x n integers matrix, return the length of the longest increasing path in matrix.

From each cell, you can either move in four directions: left, right, up, or down. You may not move diagonally or move outside the boundary (i.e., wrap-around is not allowed).

Example 1:

Input: matrix = \[9,9,4,6,6,8,2,1,1]
Output: 4
Explanation: The longest increasing path is 1, 2, 6, 9.

Example 2:

Input: matrix = \[3,4,5,3,2,6,2,2,1]
Output: 4
Explanation: The longest increasing path is 3, 4, 5, 6. Moving diagonally is not allowed.

Example 3:

Input: matrix = \[1]
Output: 1

Constraints:
  • m == matrix.length
  • n == matrixi.length
  • 1 <= m, n <= 200
  • 0 < = m a t r i x i j < = 2 31 − 1 0 <= matrixij <= 2^{31} - 1 0<=matrixij<=231−1

From: LeetCode

Link: 329. Longest Increasing Path in a Matrix


Solution:

Ideas:
  • DFS with Memoization: The solution uses Depth-First Search (DFS) combined with memoization to explore all possible paths starting from each cell in the matrix.
  • Memoization: An array memo is used to store the length of the longest path starting from each cell to avoid redundant calculations.
  • Directions Array: The array directions stores the four possible directions (up, down, left, right) in which one can move.
Code:
c 复制代码
int dfs(int** matrix, int m, int n, int** memo, int i, int j) {
    if (memo[i][j] != 0) return memo[i][j];
    
    int directions[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
    int maxLength = 1;
    
    for (int d = 0; d < 4; d++) {
        int x = i + directions[d][0];
        int y = j + directions[d][1];
        
        if (x >= 0 && x < m && y >= 0 && y < n && matrix[x][y] > matrix[i][j]) {
            int len = 1 + dfs(matrix, m, n, memo, x, y);
            maxLength = (len > maxLength) ? len : maxLength;
        }
    }
    
    memo[i][j] = maxLength;
    return maxLength;
}

int longestIncreasingPath(int** matrix, int matrixSize, int* matrixColSize) {
    if (matrixSize == 0 || matrixColSize[0] == 0) return 0;
    
    int m = matrixSize;
    int n = matrixColSize[0];
    
    int** memo = (int**)malloc(m * sizeof(int*));
    for (int i = 0; i < m; i++) {
        memo[i] = (int*)calloc(n, sizeof(int));
    }
    
    int result = 0;
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++) {
            int len = dfs(matrix, m, n, memo, i, j);
            result = (len > result) ? len : result;
        }
    }
    
    for (int i = 0; i < m; i++) {
        free(memo[i]);
    }
    free(memo);
    
    return result;
}
相关推荐
成都方航科技13 小时前
mes系统生产管理看板 mes系统在制造行业的应用解决方案 成都方航科技有限公司
科技·算法·制造
Lazionr13 小时前
二叉树入门:从概念到代码实现
c语言·数据结构
Black蜡笔小新13 小时前
自动化AI算法训练服务器DLTM零代码私有化构建企业自主可控AI智能体系
人工智能·算法·自动化
MegaDataFlowers13 小时前
104.二叉树的最大深度
算法
小侯不躺平.13 小时前
C++ Boost库【6】时间戳整体综合
开发语言·c++·算法
wuweijianlove14 小时前
算法稳定性分析中的输入扰动响应模型的技术6
算法
视觉算法小姥14 小时前
yolov11-obb在rk芯片部署的onnx模型输出的剪枝处理
算法·yolo·剪枝
KaMeidebaby14 小时前
卡梅德生物技术快报|糖蛋白纯化 Sevage 法工艺优化:正交与响应面法对比实操分析
人工智能·其他·算法·百度·新浪微博
前网易架构师-高司机14 小时前
ROS2 Jazzy+Gazebo Harmonic 环境下,用 URDF 搭建机器人,配置物理属性、插件与桥接,修复车轮和激光雷达故障 (手把手保姆级教程)
开发语言·算法·golang·机器人·ros
承渊政道14 小时前
我的创作纪念日写在创作第256天:从第一篇C语言博客,到一路向前的自己!
c语言·开发语言·笔记·学习·学习方法