当前位置:网站首页>Algorithm --- paint the house (kotlin)
Algorithm --- paint the house (kotlin)
2022-07-29 02:58:00 【Xiaomi technology Android research and development caoxinyu】
subject
If there is a row of houses , common n individual , Every house can be painted red 、 One of the three colors blue or green , You need to paint all the houses and make the two adjacent houses not the same color .
Of course , Because the prices of different colors of paint on the market are different , So the cost of painting the house in different colors is different . The cost of painting each house in a different color is one n x 3 Positive integer matrix of costs To represent the .
for example ,costs[0][0] It means the first one 0 The cost of painting the house red ;costs[1][2] It means the first one 1 The cost of painting the house green , And so on .
Please calculate the minimum cost of painting all the houses .
Example 1:
Input : costs = [[17,2,17],[16,16,5],[14,3,19]]
Output : 10
explain : take 0 No. 1 house painted blue ,1 No. 1 house painted green ,2 No. 1 house painted blue .
Minimum cost : 2 + 5 + 3 = 10.
Example 2:
Input : costs = [[7,6,2]]
Output : 2
Tips :
costs.length == n
costs[i].length == 3
1 <= n <= 100
1 <= costs[i][j] <= 20
source : Power button (LeetCode)
link :https://leetcode.cn/problems/JEj789
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Solutions

resolvent
fun minCost2(costs: Array<IntArray>): Int {
val n = costs.size
val dp = Array(n) {
Array(3) {
0 } }
costs[0].forEachIndexed {
index, int ->
dp[0][index] = int
}
for (i in 1 until n){
for (j in 0..2){
dp[i][j] = dp[i - 1][(j + 1) % 3].coerceAtMost(dp[i - 1][(j + 2) % 3]) + costs[i][j]
}
}
return Arrays.stream(dp[n - 1]).min {
o1: Int, o2: Int -> o1 - o2 }.get()
}
summary
1. Think more about using for loop instead of copy Code
For example, there are only 3 Number How to get the left and right arrays
2.dp Arrays can also be binary arrays Can be changed according to needs
边栏推荐
- Comic algorithm_ Xiaohuihui interview
- Mysql复合查询(重要)
- Asemi rectifier bridge s25vb100, s25vb100 parameters, s25vb100 application
- R language error: compilation failed for package '****‘
- 12.书写规则-静态模式
- VASP calculation task error: M_ divide:can not subdivide 8 nodes by 6
- Verilog的时间系统任务----$time、$stime、$realtime
- Analysis of concepts and terms in data warehouse
- 常用hooks总结
- HTB-Blocky
猜你喜欢

TP5.0 小程序用户无需登录,直接获取用户手机号。

vasp计算任务报错:M_divide:can not subdivide 8 nodes by 6

R语言ERROR: compilation failed for package ‘****‘

融云 IM & RTC 能力上新盘点

Day 8 notes

idea配置web容器与war打包

PHP process communication series (I) named pipes

Mongodb index (3)

C language: Little Lele and hexadecimal conversion

Analyzing the subjective consciousness of emotional resonance between robots and human beings
随机推荐
Self organization is the two-way rush of managers and members
看机器人教育引领素质教育主流
扫雷简单版
HTB-Blue
万字详解 Google Play 上架应用标准包格式 AAB
Multithreading realizes concurrent reading and execution of multi use case files +selenium grid4 realizes distributed deployment of test framework
IDEA安装后无法启动
OSPF experiment
冰冰学习笔记:运算符重载---日期类的实现
TP5.0 小程序用户无需登录,直接获取用户手机号。
PHP process communication series (I) named pipes
11. Writing rules - pseudo target
Deliver temperature with science and technology, vivo appears at the digital China Construction Summit
Analysis of Project-based Learning Creativity in steam Education
Idea replaces the contents of all files
【luogu P8352】小 N 的独立集(DP套DP)(性质)
Summary of classic problems in Flink production environment
Rocbos open source micro community light forum source code
vim常用命令
Jinshan cloud returns to Hong Kong for listing: Hong Kong stock rush of Chinese to B cloud manufacturers