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;
}
相关推荐
ZhengEnCi3 小时前
Agent 架构怎么选:从单 Agent 到多 Agent 编排的选型决策指南
算法·架构
秋天的怀念_David4 小时前
第二十二夜 · 数字迷雾
java·程序人生·算法
Pniubi4 小时前
力扣283移动零(双指针法)
算法·leetcode·职场和发展
hhb_6184 小时前
大模型落地三重优化实战
人工智能·算法·机器学习
云和数据.ChenGuang4 小时前
光哥说Python RAG 系统需要哪些评估指标:从检索到生成的全链路评测指南
网络·人工智能·python·算法·langchain
2601_970703836 小时前
工业除湿机租赁:把一份租金合同拆开看
大数据·数据结构·算法·数据库架构·推荐算法
心中有你02146 小时前
Java实现Dijkstra最短路径算法(邻接表 + 优先队列)
java·数据库·算法
ZhengEnCi7 小时前
同样的模型,为什么本地和云 API 差别那么大:量化精度、聊天模板与采样参数的三大元凶
人工智能·算法·llm
Zwawa7 小时前
串口为什么会丢字节:从轮询到中断和环形缓冲区
c语言·单片机·嵌入式
Flynn Liang7 小时前
RAG 答非所问/幻觉怎么破?Spring AI QuestionAnswerAdvisor 召回优化实战(TopK/重排/混合检索)
人工智能·算法·spring·rag·幻觉·springai