【代码随想录二刷】Day35-贪心-Go

代码随想录二刷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
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值