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;
}
相关推荐
HugoStudio_SWAN1 小时前
洛谷 P1420 / P1179 / B4262 最长连号、数字统计与词频统计——统计的三种面孔
c++·学习·程序人生·算法
是隼人2 小时前
buuctf-pwn [HarekazeCTF2019]baby_rop2(64位ret2libc)题解(学习过程持续更新)
c语言·学习·安全·pwn入门·ctf入门
青 春 记 忆2 小时前
LeetCode 350. 两个数组的交集 II|Python 解法详解
python·算法·leetcode
阳明山水3 小时前
销量预测2025—2026:从模型竞赛到系统融合的范式转型
人工智能·深度学习·算法·机器学习·架构
桐盛科技3 小时前
拒绝“漂绿”风险:基于边缘计算的碳排放数据清洗算法与实时校验逻辑
人工智能·算法·边缘计算
wuyk5553 小时前
107.FreeRTOS 链表深度解析:从原理到面试满分答案
c语言·开发语言·数据结构·stm32·单片机·链表·面试
Tongzhi20263 小时前
通芝科技考勤软件三大发展趋势:AI大模型、无感识别与数据底座
大数据·科技·算法·爬山算法
eBest数字化转型方案4 小时前
Route Optimization for FMCG:多目标排线算法的工程实现
大数据·人工智能·算法
residual_fan4 小时前
持续对比强化学习(Continual Contrastive Reinforcement Learning)论文分享
人工智能·算法·数据挖掘·数据分析
s_w.h5 小时前
【 计网 】序列化与反序列化
linux·服务器·网络·算法·bash