当前位置:网站首页>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
}
}
边栏推荐
- Spark overview
- Send mail using WP mail SMTP plug-in
- Spark 结构化流写入Hudi 实践
- Patent inquiry website
- PolyWorks script development learning notes (4) - data import and alignment using file import
- Arduino handles JSON data, arduinojson assistant
- Flink学习笔记(十)Flink容错机制
- CATIA automation object architecture - detailed explanation of application objects (I) document/settingcontrollers
- Word segmentation in full-text indexing
- Principles of computer composition - cache, connection mapping, learning experience
猜你喜欢

Go language - JSON processing

PolyWorks script development learning notes (III) -treeview advanced operation

Win10 install elk

软件测试工程师是做什么的 通过技术测试软件程序中是否有漏洞

Hudi learning notes (III) analysis of core concepts

LeetCode每日一题(1162. As Far from Land as Possible)

Using Hudi in idea

NPM install installation dependency package error reporting solution

Apply for domain name binding IP to open port 80 record

LeetCode每日一题(2090. K Radius Subarray Averages)
随机推荐
Jenkins learning (III) -- setting scheduled tasks
Starting from 0, use pnpm to build a demo managed by monorepo
PowerDesigner does not display table fields, only displays table names and references, which can be modified synchronously
LeetCode每日一题(931. Minimum Falling Path Sum)
[point cloud processing paper crazy reading classic version 13] - adaptive graph revolutionary neural networks
Jestson Nano自定义根文件系统创建(支持NVIDIA图形库的最小根文件系统)
全球KYC服务商ADVANCE.AI 活体检测产品通过ISO国际安全认证 产品能力再上一新台阶
LeetCode每日一题(2115. Find All Possible Recipes from Given Supplies)
Word segmentation in full-text indexing
QT qstring:: number apply base conversion
Crawler career from scratch (I): crawl the photos of my little sister ① (the website has been disabled)
LeetCode每日一题(1162. As Far from Land as Possible)
Principles of computer composition - cache, connection mapping, learning experience
【Kotlin学习】类、对象和接口——定义类继承结构
Global KYC service provider advance AI in vivo detection products have passed ISO international safety certification, and the product capability has reached a new level
Install database -linux-5.7
Hudi data management and storage overview
Crawler career from scratch (II): crawl the photos of my little sister ② (the website has been disabled)
LeetCode每日一题(2212. Maximum Points in an Archery Competition)
小王叔叔的博客目录【持续更新中】