当前位置:网站首页>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
}
}
边栏推荐
- Go language - IO project
- [point cloud processing paper crazy reading classic version 13] - adaptive graph revolutionary neural networks
- Jetson Nano 自定义启动图标kernel Logo cboot logo
- Uncle Wang's blog directory [constantly updating]
- Vscode Arduino installation Library
- [kotlin learning] operator overloading and other conventions -- overloading the conventions of arithmetic operators, comparison operators, sets and intervals
- Go language - Reflection
- unbuntu(debian)下TFTP服务器搭建及测试
- Patent inquiry website
- 软件测试工程师是做什么的 通过技术测试软件程序中是否有漏洞
猜你喜欢
Jenkins learning (II) -- setting up Chinese
Directory and switching operation in file system
Liteide is easy to use
小王叔叔的博客目录【持续更新中】
Hudi学习笔记(三) 核心概念剖析
PolyWorks script development learning notes (4) - data import and alignment using file import
Redis learning (I)
Navicat, MySQL export Er graph, er graph
Utilisation de hudi dans idea
Please tell me how to set vscode
随机推荐
PolyWorks script development learning notes (III) -treeview advanced operation
The idea of compiling VBA Encyclopedia
Django operates Excel files through openpyxl to import data into the database in batches.
Please tell me how to set vscode
Utilisation de hudi dans idea
LeetCode每日一题(1024. Video Stitching)
There is no open in default browser option in the right click of the vscade editor
Jetson nano custom boot icon kernel logo CBOOT logo
LeetCode每日一题(1856. Maximum Subarray Min-Product)
PowerDesigner does not display table fields, only displays table names and references, which can be modified synchronously
【Kotlin学习】类、对象和接口——定义类继承结构
LeetCode每日一题(985. Sum of Even Numbers After Queries)
1922. Count Good Numbers
[set theory] order relation (eight special elements in partial order relation | ① maximum element | ② minimum element | ③ maximum element | ④ minimum element | ⑤ upper bound | ⑥ lower bound | ⑦ minimu
Explanation of the answers to the three questions
Logstash+jdbc data synchronization +head display problems
WARNING: You are using pip version 21.3.1; however, version 22.0.3 is available. Prompt to upgrade pip
[kotlin learning] control flow of higher-order functions -- lambda return statements and anonymous functions
用Redis实现分布式锁
从0开始使用pnpm构建一个Monorepo方式管理的demo