329. Longest Increasing Path in a Matrix

复制代码
package LeetCode_329

import java.util.*

/**
 * 329. Longest Increasing Path in a Matrix
 * https://leetcode.com/problems/longest-increasing-path-in-a-matrix/description/
 *
 * Given an integer matrix, find the length of the longest increasing path.
From each cell, you can either move to four directions: left, right, up or down.
You may NOT move diagonally or move outside of the boundary (i.e. wrap-around is not allowed).

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

Example 2:
Input: nums =
[
[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.
 * */
class Solution {
    /*
    * solution:BFS+Memorization;
    * Time complexity:O(mn), every neighbor access 4 times (constant time);
     * Space complexity:O(mn);
    * */
    fun longestIncreasingPath(matrix: Array<IntArray>): Int {
        if (matrix == null || matrix.isEmpty()) {
            return 0
        }
        var result = 1
        val direction = intArrayOf(0, 1, 0, -1, 0)
        val m = matrix.size
        val n = matrix[0].size
        val dp = Array(m, { IntArray(n) })
        for (i in 0 until m) {
            for (j in 0 until n) {
                //with memorization, below is O(1)
                if (dp[i][j] > 0) {
                    continue
                }
                //Pair to save x,y
                val queue = LinkedList<Pair<Int, Int>>()
                queue.offer(Pair(i, j))
                var count = 1
                while (queue.isNotEmpty()) {
                    //keep tracking this node's path
                    count++
                    val size = queue.size
                    for (k in 0 until size) {
                        val current = queue.poll()
                        //check it 4 directions
                        /*
                        * get the new direction like:
                        * x,y+1
                        * x,y-1
                        * x-1,y
                        * x+1,y
                        * */
                        for (d in 0 until 4) {
                            val x = current.first + direction[d]
                            val y = current.second + direction[d + 1]
                            /*
                            * check if need to update:
                            * 1. x,y cannot cross the boundary;
                            * 2. the neighbor value should be grater than current value;
                            * 3. the dp value of neighbor should be less than the dp value of current;
                            * */
                            if (x < 0 || y < 0 || x >= m || y >= n ||
                                matrix[x][y] <= matrix[current.first][current.second] || count <= dp[x][y]) {
                                continue
                            }
                            dp[x][y] = count
                            result = Math.max(result, count)
                            queue.offer(Pair(x, y))
                        }
                    }
                }
            }
        }
        return result
    }
}
复制代码

 Similar Problem:

934. Shortest Bridge

posted @   johnny_zhao  阅读(90)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示