当前位置:网站首页>leetcode:5289. 公平分发饼干【看数据范围 + dfs剪枝】
leetcode:5289. 公平分发饼干【看数据范围 + dfs剪枝】
2022-06-12 18:36:00 【白速龙王的回眸】


分析
一开始没看数据范围 想用排序加双指针试试 完全没有想法
然后数据这么小就可以暴力了
就是dfs+回溯 把当前的饼干试试分给每一个小朋友
然后分完之后看看小朋友中的最大值就是最不公平数
然后求一个最小值即可
注意需要剪枝:
如果当前的最大值已经大于记录的最小值就可以剪掉了
ac code
class Solution:
def distributeCookies(self, cookies: List[int], k: int) -> int:
n = len(cookies)
#own = [0] * k
ans = inf
def dfs(now, own):
#print(now, own)
nonlocal ans
if now == n:
ans = min(ans, max(own))
return
if max(own) >= ans:
return
for i in range(k):
own[i] += cookies[now]
dfs(now + 1, own)
own[i] -= cookies[now]
dfs(0, [0] * k)
return ans
总结
dfs没有剪枝吃了一波超时
注意dfs之后有没有显然的剪枝方法
边栏推荐
- Difference between rxjs of() and of ({})
- Introduction to service grid and istio - continued
- Quickly copy the request in browser F12 to postman/ or generate the corresponding code of the relevant language
- 网盘和对象云存储管理之磁盘映射工具比较
- 常用问题排查工具和分析神器,值得收藏
- Gospel of audio and video developers, rapid integration of AI dubbing capability
- yoloe 目标检测使用笔记
- MySQL - > > symbol usage JSON related
- Title 66: input 3 numbers a, B, C, and output them in order of size.
- Problems that the sap Spartacus e-commerce cloud UI shipping method does not display in the unit test environment
猜你喜欢

Introduction to reinforcement learning and analysis of classic items 1.3

When openharmony meets openeuler
![Interior design style type, rendering 100 invitation code [1a12]](/img/90/8bbfbe33c5b412498744c0ea0ed559.jpg)
Interior design style type, rendering 100 invitation code [1a12]

What is SAP support package stack

MYSQL:Expression #4 of SELECT list is not in GROUP BY clause and contains nonaggregated column

C language learning -- data storage in memory

MySQL advanced learning notes

Adjust CEPH cluster image source

间隔两个月,我的第二次上榜纪念日【2022.6.2】

Still using Microsoft office, 3 fairy software, are you sure you don't want to try?
随机推荐
CVPR 2022 Oral 大连理工提出SCI:快速、超强的低光照图像增强方法
dumi 搭建文档型博客
VirtualLab基礎實驗教程-4.單縫衍射
Shenzhen has been shut down for 7 days since March 14. Home office experience | community essay solicitation
HTTP cache < strong cache and negotiation cache >
常用问题排查工具和分析神器,值得收藏
JS for Fibonacci sequence
看不懂Kotlin源码?从Contracts 函数说起~
CEPH deploy offline deployment of CEPH cluster and error reporting FAQ
Leetcode topic [string] - Sword pointing offer 05- replace spaces
C language learning -- data storage in memory
General differences between SQL server versions released by Microsoft in different periods so far, for reference
GD32F4xx控制DGUS 变量显示
torch.where的新用法(很老但是大家忽略的用法)
Stack in JS (including leetcode examples) < continuous update ~>
Title 37: sorting 10 numbers
In 2021, the global spice and seasoning revenue is about 18720million US dollars, and it is expected to reach 25960million US dollars in 2028
Typescript advanced type (2)
C language operation database (SQLite3) call interface function
Write a select based concurrent server