当前位置:网站首页>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
边栏推荐
- groupby 方法
- 从零开始实现lmax-Disruptor队列(六)Disruptor 解决伪共享、消费者优雅停止实现原理解析
- 创客教育的起源和内涵的基本理念
- etcd实现大规模服务治理应用实战
- Pgzero aircraft war
- Wechat applet - Advanced chapter Lin UI component library source code analysis button component (II)
- VASP calculation task error: M_ divide:can not subdivide 8 nodes by 6
- sqlilabs less-32~less-33
- 自组织是管理者和成员的双向奔赴
- Summary of classic problems in Flink production environment
猜你喜欢

Weekly recommended short videos: how to make product development more effective?

My approval function of conference OA project

cuda-gdb提示:/tmp/tmpxft_***.cudafe1.stub.c: No such file or directory.

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

Rocbos open source micro community light forum source code

瀚高数据库最佳实践配置工具HG_BP日志采集内容

sqlilabs less-32~less-33

Notes on the sixth day

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

Day 5 experiment
随机推荐
10.书写规则-文件搜寻
Stm32c8t6 encoder motor speed measurement and Arduino photoelectric module speed measurement
Flink kernel source code (VII) Flink SQL submission process
C language: Little Lele and Euclid
C语言:小乐乐与进制转换
架构师进阶,微服务设计与治理的 16 条常用原则
Verilog's time system tasks - $time, $stime, $realtime
The origin of Nacos' name
Self organization is the two-way rush of managers and members
[Luogu p8352] independent set of small n (DP set DP) (property)
DataGrip 如何导出和恢复整个数据库数据,使用单个 SQL 文件
PHP lucky draw system with background source code
TP5.0 小程序用户无需登录,直接获取用户手机号。
冰冰学习笔记:运算符重载---日期类的实现
Notes on the sixth day
Really time NTP service startup command
Mongodb index (3)
Analysis of OWT server source code (III) -- video module analysis of mixer in
Day 10 notes
Zone --- line segment tree lazy marking board sub problem