当前位置:网站首页>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 :
边栏推荐
- Isn't this another go bug?
- Perhaps the greatest romance of programmers is to commemorate their dead mother with a software
- 【小程序】编译预览小程序时,出现-80063错误提示
- 应用配置管理,基础原理分析
- 数据管理:业务数据清洗,落地实现方案
- Empty encoded password警告原因
- C language: sorting with custom functions
- Shardingsphere-proxy-5.0.0 implementation of capacity range partition (V)
- Echo framework: automatically add requestid
- Kitten paw: FOC control 15-mras method of PMSM
猜你喜欢

The concept of TP FP TN FN in machine learning

Common core resource objects of kubernetes

C language: structure array implementation to find the lowest student record

Cross domain and jsonp
![[Hongke case] how can 3D data become operable information Object detection and tracking](/img/d8/ccda595db67b66eb01f3d55956f4cb.png)
[Hongke case] how can 3D data become operable information Object detection and tracking

Data management: business data cleaning and implementation scheme

跨域和JSONP

C language: on the right shift of matrix

Everything I see is the category of my precise positioning! Open source of a new method for saliency map visualization

牛学长周年庆活动:软件大促限时抢,注册码免费送!
随机推荐
Is it safe to open an account for shares of tongdaxin?
Social recruitment interview is indispensable -- 1000 interview questions for Android engineers from Internet companies
Server performance monitoring: Best Practices for server monitoring
The best Base64 encoding and decoding tutorial in the whole network, with 6 examples!
Echo framework: automatically add requestid
numpy.linalg.lstsq(a,b,rcond=-1)解析
[OSG] OSG development (04) - create multiple scene views
[applet] when compiling the preview applet, a -80063 error prompt appears
Apple iphone14 is equipped with Beidou navigation system. What are the advantages of Beidou vs GPS?
What are the two types of digital factories
Everything I see is the category of my precise positioning! Open source of a new method for saliency map visualization
这不会又是一个Go的BUG吧?
7 tips for preventing DDoS Attacks
[applet] realize the effect of double column commodities
【小程序】编译预览小程序时,出现-80063错误提示
[Hongke case] how can 3D data become operable information Object detection and tracking
13 `bs_ duixiang. Tag tag ` get a tag object
抓取开机logcat
【CVPR 2020】会议版本:A Physics-based Noise Formation Model for Extreme Low-light Raw Denoising
Real time preview of RTSP video based on webrtc