We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
题目链接: https://leetcode-cn.com/problems/JEj789
难度: Medium 标签: 数组 动态规划
Medium
数组
动态规划
The text was updated successfully, but these errors were encountered:
Difficulty: 中等
Related Topics: 数组, 动态规划
假如有一排房子,共 n 个,每个房子可以被粉刷成红色、蓝色或者绿色这三种颜色中的一种,你需要粉刷所有的房子并且使其相邻的两个房子颜色不能相同。
n
当然,因为市场上不同颜色油漆的价格不同,所以房子粉刷成不同颜色的花费成本也是不同的。每个房子粉刷成不同颜色的花费是以一个 n x 3的正整数矩阵 costs 来表示的。
n x 3
costs
例如,costs[0][0] 表示第 0 号房子粉刷成红色的成本花费;costs[1][2] 表示第 1 号房子粉刷成绿色的花费,以此类推。
costs[0][0]
costs[1][2]
请计算出粉刷完所有房子最少的花费成本。
示例 1:
输入: costs = [[17,2,17],[16,16,5],[14,3,19]] 输出: 10 解释: 将 0 号房子粉刷成蓝色,1 号房子粉刷成绿色,2 号房子粉刷成蓝色。 最少花费: 2 + 5 + 3 = 10。
示例 2:
输入: costs = [[7,6,2]] 输出: 2
提示:
costs.length == n
costs[i].length == 3
1 <= n <= 100
1 <= costs[i][j] <= 20
注意:本题与主站 256 题相同:
Language: JavaScript
/** * @param {number[][]} costs * @return {number} */ var minCost = function(costs) { let [a, b, c] = costs[0]; for (let i = 1; i < costs.length; i++) { let [x, y, z] = costs[i]; x += Math.min(b, c); y += Math.min(a, c); z += Math.min(a, b); [a, b, c] = [x, y, z]; } return Math.min(a, b, c); };
Sorry, something went wrong.
No branches or pull requests
题目链接: https://leetcode-cn.com/problems/JEj789
难度:
Medium
标签:
数组
动态规划
The text was updated successfully, but these errors were encountered: