Open azl397985856 opened 5 months ago
class Solution(object):
def findContentChildren(self, g, s):
g.sort(), s.sort()
gi, gn, si, sn, cnt = 0, len(g), 0, len(s), 0
while gi < gn and si < sn:
if g[gi] <= s[si]:
cnt += 1
gi += 1
si += 1
return cnt
const findContentChildren = function (g, s) {
g = g.sort((a, b) => a - b);
s = s.sort((a, b) => a - b);
let gi = 0;
let sj = 0;
let res = 0;
while (gi < g.length && sj < s.length) {
if (s[sj] >= g[gi]) {
gi++;
sj++;
res++;
} else {
sj++;
}
}
return res;
};
时间复杂度:O(nlogn) 空间复杂度:O(logn)
class Solution:
def findContentChildren(self, g: List[int], s: List[int]) -> int:
g = sorted(g)
s = sorted(s)
sidx = 0
rs = 0
for gidx in range(len(g)):
if sidx>=len(s):
return rs
while sidx<len(s) and g[gidx]>s[sidx]:
sidx+=1
if sidx<len(s):
rs+=1
sidx+=1
else:
return rs
return rs
class Solution {
public:
int findContentChildren(vector
class Solution {
public int findContentChildren(int[] g, int[] s) {
// 思路:先两个都排序 然后只要s>g就可以分配
// 时间复杂度:排序的时间复杂度 NLogN(N为g和s长度的较大值)
// 空间复杂度:O1
Arrays.sort(g);
Arrays.sort(s);
int j = 0;
int i = 0;
for (; i < g.length && j < s.length; ) {
if (g[i] <= s[j]) {
i++;
}
j++;
}
return i;
}
}
455. 分发饼干
入选理由
暂无
题目地址
https://leetcode-cn.com/problems/assign-cookies/
前置知识
暂无
题目描述