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;
}
相关推荐
折哥的程序人生 · 物流技术专研2 小时前
Java面试85题图解版 · 特别篇:2026后端高频面试题复盘(算法底层逻辑+高并发架构设计全解析,附Java实战代码)
java·网络·数据库·算法·面试
玖玥拾3 小时前
C/C++ 基础笔记(十四)多态与模板编程
c语言·c++·多态·模板
想吃火锅10053 小时前
【leetcode】14.最长公共前缀js
算法·leetcode·职场和发展
云絮.4 小时前
数据库操作
数据库·mysql·算法·oracle
小林ixn5 小时前
LeetCode 206. 反转链表(迭代 + 递归详解)
算法·leetcode·链表
凡人叶枫5 小时前
Effective C++ 条款17:以独立语句将 newed 对象置入智能指针
java·linux·开发语言·c++·算法
菜鸟‍6 小时前
LeetCode 1 27 和 704 || 两数之和 移除元素 二分查找
算法·leetcode·职场和发展
caimouse7 小时前
Reactos 第1章 概述
c语言·开发语言·架构
退休倒计时7 小时前
【每日一题】LeetCode 142. 环形链表 II TypeScript
算法·leetcode·链表·typescript
啊森要自信8 小时前
【GUI自动化测试】控件、鼠标键盘操作与多场景自动化
c语言·开发语言·python·adb·ipython