当前位置:网站首页>leetcode: 455. 分发饼干
leetcode: 455. 分发饼干
2022-08-08 03:38:00 【uncle_ll】
455. 分发饼干
来源:力扣(LeetCode)
链接: https://leetcode.cn/problems/assign-cookies/
假设你是一位很棒的家长,想要给你的孩子们一些小饼干。但是,每个孩子最多只能给一块饼干。
对每个孩子 i,都有一个胃口值 g[i],这是能让孩子们满足胃口的饼干的最小尺寸;并且每块饼干j,都有一个尺寸 s[j] 。如果 s[j] >= g[i],我们可以将这个饼干 j 分配给孩子 i ,这个孩子会得到满足。你的目标是尽可能满足越多数量的孩子,并输出这个最大数值。
示例 1:
输入: g = [1,2,3], s = [1,1]
输出: 1
解释:
你有三个孩子和两块小饼干,3个孩子的胃口值分别是:1,2,3。
虽然你有两块小饼干,由于他们的尺寸都是1,你只能让胃口值是1的孩子满足。
所以你应该输出1。
示例 2:
输入: g = [1,2], s = [1,2,3]
输出: 2
解释:
你有两个孩子和三块小饼干,2个孩子的胃口值分别是1,2。
你拥有的饼干数量和尺寸都足以让所有孩子满足。
所以你应该输出2.
提示:
- 1 < = g . l e n g t h < = 3 ∗ 1 0 4 1 <= g.length <= 3 * 10^4 1<=g.length<=3∗104
- 0 < = s . l e n g t h < = 3 ∗ 1 0 4 0 <= s.length <= 3 * 10^4 0<=s.length<=3∗104
- 1 < = g [ i ] , s [ j ] < = 2 31 − 1 1 <= g[i], s[j] <= 2^{31} - 1 1<=g[i],s[j]<=231−1
解法
- 贪心算法: 在自己力所能及的能力里去尽可能满足更多的人,贪心思想,尽量满足最低要求的,然后逐步往上。对小孩排序,对饼干排序;双指针思想,二者进行比较,如果能够满足,则两个下标指针往右走,如果不能满足,饼干的下标往右走;
代码实现
贪心算法
python实现
class Solution:
def findContentChildren(self, g: List[int], s: List[int]) -> int:
if not g or not s:
return 0
g.sort()
s.sort()
print(s)
num = 0
g_len = len(g)
s_len = len(s)
i = 0
j = 0
while i < g_len and j < s_len:
if g[i] <= s[j]:
num += 1
i += 1
j += 1
else:
j += 1
return num
c++实现
class Solution {
public:
int findContentChildren(vector<int>& g, vector<int>& s) {
if (g.size() == 0 && s.size() == 0)
return 0;
sort(g.begin(), g.end());
sort(s.begin(), s.end());
int num = 0, i = 0, j = 0;
while (i < g.size() && j < s.size()) {
if (g[i] <= s[j]) {
num++;
i++;
j++;
}
else
j++;
}
return num;
}
};
复杂度分析
- 时间复杂度: O ( M l o g M + N l o g N ) O(MlogM + NlogN) O(MlogM+NlogN) 排序花费时间, M,N分别为g和s的长度
- 空间复杂度: O ( l o g M + l o g N ) O(logM + logN) O(logM+logN)
边栏推荐
- egg-validate-custom validation method error language (error Chinese prompt)
- 意识的概念框架:我们的意识从注意图式产生?
- 农产品直播带货持续升温,经济日报:冲流量勿忘质量
- vulnhub-DC-3靶机渗透记录
- 1.9 and orderly array insert PTA problem sets
- NVIDIA - DeepStream configuration file parsing
- Solve the problem of word flashback when Endnote inserts references
- 高效记忆法
- mmedicting的get_flops.py的使用
- 小程序优化实践
猜你喜欢

The project management process and key points for each link

egg-validate-custom validation method error language (error Chinese prompt)

小程序优化实践

CGAN theory explanation and code implementation

Simulate login - add cookies, use postmanget to request web page data

egg-Alibaba Cloud SMS Configuration

农产品直播带货持续升温,经济日报:冲流量勿忘质量

妙才周刊

模拟登录——添加cookies,使用postmanget请求网页数据

包 package
随机推荐
ICML 2022 | LeNSE: 基于子图的大规模组合优化: 实现了 140 多倍的速度提升
CGAN theory explanation and code implementation
SIGIR'22 | 广告间排序和广告内创意优选联合优化
意识的概念框架:我们的意识从注意图式产生?
Vulfocus Shooting Range Scenario Mode - Intranet Dead End
如何验证期货公司开户的正规性
KDD'22 | CausalMTA: 基于因果推断的无偏广告多触点归因技术
以0为底或以1为底对图片迭代次数的影响
从精装到智装,下一波浪潮浮现,听听智能家居的大咖们怎么说?
Redis persistence mechanism, master-slave, sentry, cluster parsing cluster solution
Overseas Metaverse media must pay attention to the integrity of the propaganda
How does SQL 2016 track which store or statement caused the record table record to be modified
VSCode opens some records of C (embedded) projects
嵌入式面试题
响应式pbootcms模板健身器械类网站
杭电多校6 1009. Map
The project management process and key points for each link
easypoi自定义模板导出
New User Plane Design and Key Technologies in the 6G Era
1.9 and orderly array insert PTA problem sets