当前位置:网站首页>2021-11-19:[0,4,7]:0 means that the stone here has no color. If it turns red
2021-11-19:[0,4,7]:0 means that the stone here has no color. If it turns red
2022-06-24 01:11:00 【Fuda scaffold constructor's daily question】
2021-11-19:0,4,7 : 0 The stone here has no color , If it turns red, the price is 4, If it turns blue, the price is 7,1,X,X : 1 It means that the stone here is already red , And you can't change the color , So the last two numbers X meaningless ,2,X,X : 2 It means that the stone here is already blue , And you can't change the color , So the last two numbers X meaningless , The color can only be 0、1、2, The price must be >=0, Give you a batch of such small arrays , Finally, all stones must have color , And there are as many red and blue , Returns the minimum cost . If you can't do it anyway, all stones have color 、 And there are as many red and blue , return -1. From little red book .
answer 2021-11-19:
1. Sort . See code for details .
2. Statistics are colorless , Red , Number of blue .
3. If red or blue is more than half , Go straight back to -1.
4. Traverse , Calculate the minimum cost . See code for details .
Time complexity : Sort of .
Spatial complexity : Sort of .
The code to use golang To write . The code is as follows :
package main
import (
"fmt"
"sort"
)
func main() {
stones := [][]int{{0, 2, 4}, {0, 4, 1}}
ret := minCost(stones)
fmt.Println(ret)
}
func minCost(stones [][]int) int {
n := len(stones)
if (n & 1) != 0 {
return -1
}
sort.Slice(stones, func(i, j int) bool {
a := stones[i]
b := stones[j]
if a[0] == 0 && b[0] == 0 {
return b[1]-b[2]-a[1]+a[2] < 0
} else {
return a[0]-b[0] < 0
}
})
zero := 0
red := 0
blue := 0
cost := 0
for i := 0; i < n; i++ {
if stones[i][0] == 0 {
zero++
cost += stones[i][1]
} else if stones[i][0] == 1 {
red++
} else {
blue++
}
}
if red > (n>>1) || blue > (n>>1) {
return -1
}
blue = zero - ((n >> 1) - red)
for i := 0; i < blue; i++ {
cost += stones[i][2] - stones[i][1]
}
return cost
}The results are as follows :
边栏推荐
- [applet] realize the effect of double column commodities
- Definition of logic
- CVPR2022 | 可精简域适应
- Version ` zlib 1.2.9 "not found (required by / lib64 / libpng16.so.16)
- 跨域和JSONP
- 【小程序】编译预览小程序时,出现-80063错误提示
- JS input / output statements, variables
- Isn't this another go bug?
- Pure JS implementation determines whether the IP is pinged
- Use recursion to form a multi-level directory tree structure, with possibly the most detailed notes of the whole network.
猜你喜欢

实时计算框架:Spark集群搭建与入门案例

一次 MySQL 误操作导致的事故,「高可用」都顶不住了!

LMS Virtual. Derivation method of lab acoustic simulation results

Cvpr2022 𞓜 thin domain adaptation

ShardingSphere-proxy-5.0.0容量范围分片的实现(五)

JS input / output statements, variables

应用配置管理,基础原理分析

An accident caused by a MySQL misoperation, and the "high availability" cannot withstand it!

Real time computing framework: Spark cluster setup and introduction case
![[ICPR 2021] tiny object detection in aerial images](/img/40/6d346f357a858f3787eeba58262801.png)
[ICPR 2021] tiny object detection in aerial images
随机推荐
[technology planting grass] skillfully use cloud function to create wechat web page authorization public service
【小程序】相对路径和绝对路径的表示符
ICML'22 | ProGCL: 重新思考图对比学习中的难样本挖掘
[planting grass by technology] 13 years' record of the prince of wool collecting on the cloud moving to Tencent cloud
钟珊珊:被爆锤后的工程师会起飞|OneFlow U
Version ` zlib 1.2.9 "not found (required by / lib64 / libpng16.so.16)
Application configuration management, basic principle analysis
想开户炒股,通过网上进行股票开户安全吗?-
Shengdun technology joined dragon lizard community to build a new open source ecosystem
Niu Xuechang's anniversary celebration: software promotion, limited time and free registration code!
Grab startup logcat
这不会又是一个Go的BUG吧?
How many of the 36 difficult points of activity do you know?, Android interview 2020
skywalking 安装部署实践
An accident caused by a MySQL misoperation, and the "high availability" cannot withstand it!
【小程序】实现双列商品效果
SQL database: summary of knowledge points, no suspension at the end of the period
【SPRS J P & RS 2022】小目标检测模块:A Normalized Gaussian Wasserstein Distance for Tiny Object Detection
Devops culture: Amazon leadership principles
【ICCV Workshop 2021】基于密度图的小目标检测:Coarse-grained Density Map Guided Object Detection in Aerial Images