[LeetCode] Longest Increasing Path in a Matrix

Description

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:

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

Solution

There are two solutions: DFS + Memorization OR BFS + Indegree.

Only DFS or BFS will get TLE, so we must combined with Memorization or Indegree.

For DFS, we access all cells in the matrix: if a cell have been accessed, we remember the longest increasing path it can get for reusing. This is because if we access this cell again, we can just reuse the memorized path.

For BFS, we also must access all cells: if an adjcent value is smaller than the current value, which means the current value CANNOT be the beginning cells. So, we increase its indegree. For those cells which indegrees are 0, which means they are the smallest compared to their adjecent cells, they must be the begining of a path. So, we just put them into queue, then update the indegrees. Finally, we can get the longest increasing path.

DFS + Memorization Code

class Solution {
public:
    int m, n;
    int xdir[4] = {-1, 0, 1, 0}, ydir[4] = {0, -1, 0, 1};
    
    int dfs(int x, int y, vector<vector<int>>& matrix, vector<vector<int>>& level) {
        if(level[x][y] != -1)
            return level[x][y];
        
        level[x][y] = 1;
        for(int k = 0; k < 4; k++) {
            int tmpx = x + xdir[k], tmpy = y + ydir[k];
            if(tmpx < 0 || tmpx >= m || tmpy < 0 || tmpy >= n)
                continue;
            if(matrix[tmpx][tmpy] < matrix[x][y])
                level[x][y] = max(level[x][y], dfs(tmpx, tmpy, matrix, level) + 1);
        }
        return level[x][y];
    }
    
    int longestIncreasingPath(vector<vector<int>>& matrix) {
        m = matrix.size(), n = matrix[0].size();
        vector<vector<int>> level(m, vector<int>(n, -1));
        
        int ans = 1;
        for(int i = 0; i < m; i++) {
            for(int j = 0; j < n; j++) {
                ans = max(ans, level[i][j] = dfs(i, j, matrix, level));
            }
        }
        return ans;
    }
};

BFS + Indegree Code

class Solution {
public:
    int longestIncreasingPath(vector<vector<int>>& matrix) {
        int m = matrix.size(), n = matrix[0].size();
        vector<vector<int>> inDegree(m, vector<int>(n, 0));
        int xdir[4] = {-1, 0, 1, 0}, ydir[4] = {0, -1, 0, 1};
        
        queue<pair<int, int>> q;
        for(int i = 0; i < m; i++) {
            for(int j = 0; j < n; j++) {
                for(int k = 0; k < 4; k++) {
                    int tmpx = i + xdir[k], tmpy = j + ydir[k];
                    if(tmpx < 0 || tmpx >= m || tmpy < 0 || tmpy >= n || matrix[tmpx][tmpy] >= matrix[i][j])
                        continue;
                    inDegree[i][j]++;
                }
                if(!inDegree[i][j])
                    q.push({i, j});
            }
        }
        
        int level = 0;
        while(!q.empty()) {
            int size = q.size();
            while(size--) {
                auto p = q.front();
                q.pop();
                
                int i = p.first, j = p.second;
                for(int k = 0; k < 4; k++) {
                    int tmpx = i + xdir[k], tmpy = j + ydir[k];
                    if(tmpx < 0 || tmpx >= m || tmpy < 0 || tmpy >= n || matrix[tmpx][tmpy] <= matrix[i][j])
                        continue;
                    
                    inDegree[tmpx][tmpy]--;
                    if(!inDegree[tmpx][tmpy])
                        q.push({tmpx, tmpy});
                }
            }
            level++;
        }
        return level;
    }
};

Complexity

Time complexity: O(m ^ n)

Space complexity: O(m ^ n) for extra space

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
LeetCode是一个面向编程准备面试的在线平台,它提供了各种编程题目来帮助开发人员提高算法和数据结构的能力。下面是一个使用Java进行LeetCode刷题的指南: 1. 熟悉题库:首先,你需要熟悉LeetCode的题库。了解各种类型的题目,比如数组、字符串、链表、树等等,并了解它们对应的解决方法。 2. 制定计划:根据自己的时间和能力安排一个刷题计划,每天或每周刷一定数量的题目。有计划地刷题可以保证你的学习进度。 3. 注重基础:LeetCode要求高效的算法和数据结构知识,所以确保你掌握了一些基本的数据结构和算法,比如数组、链表、栈、队列、二叉树、图等等。 4. 解题过程:当你开始解题时,首先仔细阅读题目描述,并理解问题的要求。然后分析问题,尽量找到一种简单而高效的解决方案。 5. 编码实现:在理解和分析问题之后,使用Java编写代码来解决问题。编写简洁、高效的代码可以提高你的编码能力。 6. 测试和优化:编写测试用例来验证你的代码是否正确,并分析你的代码的时间复杂度和空间复杂度。根据测试结果,进行代码优化以提高性能。 7. 学习他人的解决方案:LeetCode上有很多高手,他们在解决问题时可能采用了一些巧妙的解决方案。阅读和学习他们的代码可以帮助你提高自己的解题能力。 8. 多实践,多思考:不仅仅完成题目,还需要总结经验和教训。多思考为什么这个解决方案是高效的,以及如何在其他类似问题上应用相同的思路。 通过遵循这个LeetCode刷题指南,你可以提高自己的算法和数据结构的能力,为面试做好准备。刷题不仅仅是为了通过面试,更是提升自己的编程技能和思维能力的一种训练。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值