Leetcode285场周赛

6029. 射箭比赛中的最大得分

在这里插入图片描述

  • 凡是小于50的都可以考虑二进制枚举:
class Solution:
    def maximumBobPoints(self, numArrows: int, aliceArrows: List[int]) -> List[int]:
        ans = [0] * 12
        _max = 0
        for mask in range(1 << 12):
            score = 0
            cost = 0
            flag = True
            cur = [0] * 12 
            for i in range(12):
                if (1 << i) & mask:
                    cost += aliceArrows[i] + 1
                    if cost > numArrows:
                        flag = False
                        break
                    score += i
                    cur[i] = aliceArrows[i] + 1
                if flag:
                    if score > _max:
                        _max = score
                        ans = cur[::]
        yu = numArrows - sum(ans)
        ans[0] += yu
        return ans
  • 没有写出的搜索(代码@灵剑2012):
class Solution:
    def maximumBobPoints(self, numArrows: int, aliceArrows: List[int]) -> List[int]:
        ans = 0
        plan = [0] * 10

        def search(i, arrows, current, current_plan):
            nonlocal ans, plan
            # 确定了i个位置的方案了
            if i == len(aliceArrows):
                if current > ans:
                    ans = current
                    plan = current_plan[:]
                return
            # 第i个位置可以赢的情况
            if aliceArrows[i] + 1 <= arrows:
                current_plan.append(aliceArrows[i] + 1)
                search(i + 1, arrows -
                       aliceArrows[i] - 1, current + i, current_plan)
                current_plan.pop()
            # 不赢的情况
            current_plan.append(0)
            search(i + 1, arrows, current, current_plan)
            # 列表是直接传递的本身,所以要回退
            # 因为0的返回有可能回溯到前面是1的情况,必须pop出后面所有的0才是当前状态!
            current_plan.pop()
        search(1, numArrows, 0, [])
        return [numArrows - sum(plan)] + plan

6030. 由单个字符重复的最长子字符串

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Can__er

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值