代码随想录二刷Day35
今日任务
860.柠檬水找零
406.根据身高重建队列
452.用最少数量的箭引爆气球
语言:Go
860. 柠檬水找零
链接:https://leetcode.cn/problems/lemonade-change/
不需要用二维数组,直接记录5和10的个数即可(懒得更新了)
func lemonadeChange(bills []int) bool {
five, ten := 0, 0
for i := 0; i < len(bills); i++ {
if bills[i] == 5 {
five++;
} else if bills[i] == 10 {
if five <= 0 {
return false
} else {
five--;
ten++
}
} else if bills[i] == 20 {
if five >= 1 && ten >= 1 {
five--;
ten--;
} else if five >= 3 && ten == 0 {
five -= 3
} else {
return false
}
}
}
return true
}
406. 根据身高重建队列
链接:https://leetcode.cn/problems/queue-reconstruction-by-height/
func reconstructQueue(people [][]int) [][]int {
sort.Slice(people, func(i, j int) bool {
if people[i][0] != people[j][0] {
return people[i][0] > people[j][0]
} else {
return people[i][1] < people[j][1]
}
})
for i := 1; i < len(people); i++ {
var tmp []int
tmp = people[i]
j := 0
for j = i; j > tmp[1]; j-- {
people[j] = people[j - 1]
}
people[j] = tmp
}
return people
}
452. 用最少数量的箭引爆气球
链接:https://leetcode.cn/problems/minimum-number-of-arrows-to-burst-balloons/
不断更新每对范围的右边界
func min(x, y int) int {
if x < y {
return x
} else {
return y
}
}
func findMinArrowShots(points [][]int) int {
sort.Slice(points, func(i, j int) bool{
return points[i][0] < points[j][0]
})
res := 1 //至少1支
for i := 0; i < len(points) - 1; i++ {
if points[i][1] < points[i + 1][0] {
res++
} else {
points[i + 1][1] = min(points[i][1], points[i + 1][1])
}
}
return res
}