当前位置:网站首页>Leetcode daily question (931. minimum falling path sum)
Leetcode daily question (931. minimum falling path sum)
2022-07-03 09:34:00 【wangjun861205】
Given an n x n array of integers matrix, return the minimum sum of any falling path through matrix.
A falling path starts at any element in the first row and chooses the element in the next row that is either directly below or diagonally left/right. Specifically, the next element from position (row, col) will be (row + 1, col - 1), (row + 1, col), or (row + 1, col + 1).
Example 1:

Input: matrix = [[2,1,3],[6,5,4],[7,8,9]]
Output: 13
Explanation: There are two falling paths with a minimum sum as shown.
Example 2:

Input: matrix = [[-19,57],[-40,-5]]
Output: -59
Explanation: The falling path with a minimum sum is shown.
Constraints:
- n == matrix.length == matrix[i].length
- 1 <= n <= 100
- -100 <= matrix[i][j] <= 100
dp[row][col] Is a matrix[row][col] The minimum score you can get at this point , Then we can deduce dp[row][col] = min(dp[row+1][col-1], dp[row][col], dp[row][col+1]), Because we can choose any element from the first line to start falling , therefore ans = min(dp[0][0], dp[0][1], …, dp[0]matrix[0].length - 1])
use std::collections::HashMap;
impl Solution {
fn dp(
matrix: &Vec<Vec<i32>>,
row: usize,
col: usize,
cache: &mut HashMap<(usize, usize), i32>,
) -> i32 {
if row == matrix.len() - 1 {
return matrix[row][col];
}
let left = if col > 0 {
if let Some(c) = cache.get(&(row + 1, col - 1)) {
*c
} else {
Solution::dp(matrix, row + 1, col - 1, cache)
}
} else {
i32::MAX
};
let right = if col < matrix[0].len() - 1 {
if let Some(c) = cache.get(&(row + 1, col + 1)) {
*c
} else {
Solution::dp(matrix, row + 1, col + 1, cache)
}
} else {
i32::MAX
};
let bottom = if row < matrix.len() - 1 {
if let Some(c) = cache.get(&(row + 1, col)) {
*c
} else {
Solution::dp(matrix, row + 1, col, cache)
}
} else {
i32::MAX
};
let ans = left.min(right).min(bottom) + matrix[row][col];
cache.insert((row, col), ans);
ans
}
pub fn min_falling_path_sum(matrix: Vec<Vec<i32>>) -> i32 {
let mut cache = HashMap::new();
let mut ans = i32::MAX;
for col in 0..matrix[0].len() {
ans = ans.min(Solution::dp(&matrix, 0, col, &mut cache))
}
ans
}
}
边栏推荐
- Liteide is easy to use
- Quickly use markdown to edit articles
- Directory and switching operation in file system
- Spark 概述
- [kotlin learning] classes, objects and interfaces - define class inheritance structure
- Esp32 at command does not respond
- Windows安装Redis详细步骤
- Detailed steps of windows installation redis
- Win10安装ELK
- LeetCode每日一题(2090. K Radius Subarray Averages)
猜你喜欢
![[kotlin learning] operator overloading and other conventions -- overloading the conventions of arithmetic operators, comparison operators, sets and intervals](/img/8d/938e232c1016cabe9ee0f72be87a22.png)
[kotlin learning] operator overloading and other conventions -- overloading the conventions of arithmetic operators, comparison operators, sets and intervals

Construction of simple database learning environment

PolyWorks script development learning notes (4) - data import and alignment using file import

There is no open in default browser option in the right click of the vscade editor
![[CSDN] C1 training problem analysis_ Part III_ JS Foundation](/img/b2/68d53ad09688f7fc922ac65e104f15.png)
[CSDN] C1 training problem analysis_ Part III_ JS Foundation
![[kotlin learning] classes, objects and interfaces - classes with non default construction methods or attributes, data classes and class delegates, object keywords](/img/ee/d982fd9e1f2283e09ad1a81d0b61b5.png)
[kotlin learning] classes, objects and interfaces - classes with non default construction methods or attributes, data classes and class delegates, object keywords

Vscode Arduino installation Library

Apply for domain name binding IP to open port 80 record

文件系统中的目录与切换操作

Using Hudi in idea
随机推荐
Leetcode daily question (968. binary tree cameras)
Go language - JSON processing
Common software open source protocols
Desktop icon recognition based on OpenCV
LeetCode每日一题(1024. Video Stitching)
Failed building wheel for argon2 cffi when installing Jupiter
npm install安装依赖包报错解决方法
[kotlin learning] control flow of higher-order functions -- lambda return statements and anonymous functions
Hudi 快速体验使用(含操作详细步骤及截图)
The server denied password root remote connection access
Logstash+jdbc data synchronization +head display problems
Win10 quick screenshot
LeetCode每日一题(1162. As Far from Land as Possible)
Esp32 at command does not respond
Hudi 集成 Spark 数据分析示例(含代码流程与测试结果)
Flink学习笔记(八)多流转换
LeetCode每日一题(968. Binary Tree Cameras)
Notes on numerical analysis (II): numerical solution of linear equations
Database execution error: SQL_ mode only_ full_ group_ by:
Find all possible recipes from given supplies