Open azl397985856 opened 2 years ago
class Solution: def findContentChildren(self, g: List[int], s: List[int]) -> int: g.sort() s.sort() res = 0 for i in range(len(s)): if res< len(g) and s[i] >= g[res]: res += 1
return res
var findContentChildren = function (g, s) {
g = g.sort((a, b) => a - b);
s = s.sort((a, b) => a - b);
let result = 0;
let index = s.length - 1;
for (let i = g.length - 1; i >= 0; i--) {
if (index >= 0 && s[index] >= g[i]) {
result++;
index--;
}
}
return result;
};
class Solution(object):
def change(self, amount, coins):
"""
:type amount: int
:type coins: List[int]
:rtype: int
"""
dp = [0]*(amount + 1)
dp[0] = 1
# 遍历物品
for i in range(len(coins)):
# 遍历背包
for j in range(coins[i], amount + 1):
dp[j] += dp[j - coins[i]]
return dp[amount]
class Solution {
public:
int findContentChildren(vector
int i = 0, j = 0;
while (i < g.size() && j < s.size()) {
if (s[j] >= g[i]) {
i++;
}
j++;
}
return i;
}
};
class Solution:
def findContentChildren(self, g: List[int], s: List[int]) -> int:
h = []
for w in g:
heappush(h , -w)
s.sort(reverse = True)
j = 0
while h and j < len(s):
w = -heappop(h)
if w > s[j]:
continue
else:
j += 1
return j
贪心。
用尽可能小的饼干,去满足胃口尽可能小的孩子。
class Solution:
def findContentChildren(self, g: List[int], s: List[int]) -> int:
g.sort()
s.sort()
i = 0
j = 0
ans = 0
while j < len(s) and i < len(g):
if s[j] >= g[i]:
i += 1
ans += 1
j += 1
return ans
时间复杂度 O(nlogn)
空间复杂度 O(logn) 排序导致的
思路 每人最多一块小饼干,排序 优先满足需求小的
代码
class Solution:
def findContentChildren(self, g: List[int], s: List[int]) -> int:
g.sort()
s.sort()
i = j = 0
while i < len(g) and j < len(s):
if g[i] <= s[j]:
i += 1
j += 1
return i
复杂度 时间 O(mlogm+nlogn) 空间 O(logm+logn)
greedy + sort
class Solution:
def findContentChildren(self, g: List[int], s: List[int]) -> int:
g.sort()
s.sort()
i, j = 0, 0;
while i < len(g) and j < len(s):
if s[j]>=g[i]:
i+=1
j+=1
return i
Time: (max(M, N) * log(max(M,N)) M, N: length of s and g Space: O(1)
贪心
def findContentChildren(self, g: List[int], s: List[int]) -> int:
g.sort()
s.sort()
start, count = len(s)-1, 0
for index in range(len(g)-1, -1, -1):
if start>=0 :
if s[start]>=g[index]:
start -= 1
count += 1
else:
break
return count
class Solution { public int findContentChildren(int[] g, int[] s) { //1.首先需要将这两个数组排序一下,让他们有序,这样在遍历的时候往后走就相当于“给他吃” //也就是小孩子从胃口最小的到胃口最大的排,饼干从最小块的到最大块的排。 //2.因为我们要求的是满足孩子饱腹的最小的饼干,所以s[]数组只要一大于等于他,a就可以往后++了,相当于吃上了 //3.接着就到下一个小孩了,因为小孩的大小也是从小到大, //所以前面的饼干连第一个胃口小的孩子都吃不饱,更别说第二位了,所以饼干也得从上一位吃完的下一块试起。 //4.什么时候结束呢?知道孩子都吃完了或者饼干都遍历完了就退出 //前者是孩子都有的吃,而后者是饼干已经没得匹配了就中断了。 int a=0 ,b=0; Arrays.sort(g); Arrays.sort(s); while(a<g.length&&b<s.length){ if(g[a]<=s[b])a++; b++; } return a; } }
class Solution: def findContentChildren(self, g: List[int], s: List[int]) -> int: if not s: return 0 g.sort() s.sort() ans = 0 i = 0 j = 0 while i< len(s) and j <len(g): if g[j] <= s[i]: ans+=1 j +=1 i+=1 else: i+=1 return ans
class Solution:
def findContentChildren(self, g: List[int], s: List[int]) -> int:
h = []
for w in g:
heappush(h , -w)
s.sort(reverse = True)
j = 0
while h and j < len(s):
w = -heappop(h)
if w > s[j]:
continue
else:
j += 1
return j
func findContentChildren(g []int, s []int) int {
sort.Ints(g)
sort.Ints(s)
l,r:=0,0
for l<len(g)&&r<len(s){
if g[l] <= s[r]{
l++
}
r++
}
return l
}
思路
贪心算法。优先满足胃口小的孩子,两个指针分别指向g和s的起点,如果满足,则都向后移一位,如果不满足,则ps向后移一位。
代码
var findContentChildren = function(g, s) {
let res = 0;
g.sort((a, b) => a - b);
s.sort((a, b) => a - b);
let pg = 0, ps = 0;
while(pg < g.length && ps < s.length){
if(g[pg] <= s[ps]){
pg++;
ps++;
res++
}else{
ps++
}
};
return res;
};
复杂度分析
class Solution:
def findContentChildren(self, g: List[int], s: List[int]) -> int:
s.sort(reverse=True)
g.sort(reverse=True)
gi, si = 0, 0
count = 0
while gi < len(g) and si < len(s):
if s[si] >= g[gi]:
count += 1
si += 1
gi += 1
return count
var findContentChildren = function(g, s) {
g.sort((a,b)=>b-a);
s.sort((a,b)=>b-a);
let res = 0,index = 0;
for(let i = 0; i < s.length; i++){
while(index<g.length){
if(g[index]<=s[i]){
res++;
index++;
break;
}
index++;
}
}
return res;
};
贪心 + 双指针, 拍完序后, 先满足胃口小的孩子, 先比较最后的饼干是否满足最后孩子的胃口, 满足count + 1, 不满足往前移动指针
class Solution:
def findContentChildren(self, g: List[int], s: List[int]) -> int:
count = 0
kid, cookie = len(g) - 1, len(s) - 1
g = sorted(g)
s = sorted(s)
while min(kid, cookie) >= 0:
if g[kid] <= s[cookie]:
count += 1
cookie -= 1
kid -= 1
return count
class Solution {
public int findContentChildren(int[] g, int[] s) {
if(g == null || s == null){
return 0;
}
int res = 0;
Arrays.sort(g);
Arrays.sort(s);
int i = 0;
int j = 0;
while(i < g.length && j < s.length){
while(j < s.length && s[j] < g[i]){
j++;
}
if(j < s.length && s[j] >= g[i]){
res++;
i++;
j++;
}
}
return res;
}
}
··· class Solution: def findContentChildren(self, g: List[int], s: List[int]) -> int: s.sort(reverse=True) g.sort(reverse=True) gi, si = 0, 0 count = 0 while gi < len(g) and si < len(s): if s[si] >= g[gi]: count += 1 si += 1 gi += 1 return count ···
class Solution {
public int findContentChildren(int[] g, int[] s) {
Arrays.sort(g);
Arrays.sort(s);
int ans = 0, i = 0, j = 0;
while(i < g.length && j < s.length) {
if(g[i] <= s[j]) {
i++;
j++;
ans++;
} else {
j++;
}
}
return ans;
}
}
class Solution {
public int findContentChildren(int[] g, int[] s) {
Arrays.sort(g);
Arrays.sort(s);
int total = 0;
int j = 0;
for (int i=0; i<s.length && j<g.length; i++) {
if (s[i] >= g[j]) {
total++;
j++;
}
}
return total;
}
}
TC: O(nlogn) SC: O(1)
贪心算法
var findContentChildren = function(g, s) {
g = g.sort((a, b) => a - b)
s = s.sort((a, b) => a - b)
let count = 0;
for(let i = 0; i < g.length; i++){
while(s.length > 0){
if(g[i] <= s.shift()){
count++;
break;
}
}
}
return count;
};
空间复杂度 O(1) 时间复杂度 O(NlogN)
class Solution {
public:
int findContentChildren(vector<int>& g, vector<int>& s) {
sort(g.begin(), g.end());
sort(s.begin(), s.end());
int index = s.size() - 1;
int result = 0;
for (int i = g.size() - 1; i >= 0; i--) {
if (index >= 0 && s[index] >= g[i]) {
result++;
index--;
}
}
return result;
}
};
var findContentChildren = function(g, s) {
g.sort((left, right) => left - right)
s.sort((left, right) => left - right)
let ans = 0
let sIndex = 0
for(let i = 0; i < g.length; i++) {
// 找到第一个满足的饼干
getLeastCookie(sIndex, s.length - 1, g[i])
}
function getLeastCookie(start, end, target) {
if (start > end) {
return
}
const mid = (start + end) >>> 1
if (s[mid] >= target) {
if (mid === sIndex || s[mid - 1] < target) {
ans++
sIndex = mid + 1
} else {
getLeastCookie(start, mid - 1, target)
}
} else {
getLeastCookie(mid + 1, end, target)
}
}
return ans
};
m为g的长度, n为s的长度
class Solution:
def findContentChildren(self, g: List[int], s: List[int]) -> int:
g.sort()
s.sort()
p_g = 0
p_s = 0
n_g = len(g)
n_s = len(s)
result = 0
while p_g < n_g and p_s < n_s:
if g[p_g] <= s[p_s]:
result += 1
p_g += 1
p_s += 1
else:
p_s += 1
return result
time complexity: O(mlogm + nlogn) space complexity: O(logn + logm)
class Solution: def findContentChildren(self, g: List[int], s: List[int]) -> int: g.sort() s.sort() p_g = 0 p_s = 0 n_g = len(g) n_s = len(s) result = 0 while p_g < n_g and p_s < n_s: if g[p_g] <= s[p_s]: result += 1 p_g += 1 p_s += 1 else: p_s += 1 return result time complexity: O(mlogm + nlogn) space complexity: O(logn + logm)
贪心法:两个数组分别排序,然后同时遍历,如果s[i]>=g[j],即满足了条件,则计数并使i和j分别前移,否则只移i,直到能满足当前g[j]。i和j任一指针超出数组返回计数值。复杂度$O(m+n)$
class Solution:
# 贪心法:两个数组分别排序,然后同时遍历,如果`s[i]`>=`g[j]`,即满足了条件,则计数并使`i`和`j`分别前移,否则只移`i`,
# 直到能满足当前`g[j]`。`i`和`j`任一指针超出数组返回计数值。复杂度$O(m+n)$
def findContentChildren(self, g: List[int], s: List[int]) -> int:
g.sort()
s.sort()
idx = 0
ans = 0
m = len(g)
for cookie in s:
if cookie >= g[idx]:
ans += 1
idx += 1
if idx >= m:
return ans
return ans
class Solution {
public int findContentChildren(int[] g, int[] s) {
int a=0 ,b=0;
Arrays.sort(g);
Arrays.sort(s);
while(a<g.length&&b<s.length){
if(g[a]<=s[b])a++;
b++;
}
return a;
}
}
code:
public int findContentChildren(int[] g, int[] s) {
if(g == null || s == null){
return 0;
}
int ans = 0;
Arrays.sort(g);
Arrays.sort(s);
int i = 0;
int j = 0;
while(i < g.length && j < s.length){
while(j < s.length && s[j] < g[i]){
j++;
}
if(j < s.length && s[j] >= g[i]){
ans++;
i++;
j++;
}
}
return ans;
}
#首先要对g和s排序
#用两个指针分别指向两个列表的末尾
#循环
#如果满足`s[r_s] >= g[r_g]`,则都向前移动
#否则只需要移动g的指针
class Solution:
def findContentChildren(self, g: List[int], s: List[int]) -> int:
g.sort()
s.sort()
r_g = len(g) - 1
r_s = len(s) - 1
count = 0
while r_g >= 0 and r_s >= 0:
if s[r_s] >= g[r_g]:
count += 1
r_g -= 1
r_s -= 1
else:
r_g -= 1
return count
贪心
class Solution {
public int findContentChildren(int[] g, int[] s) {
Arrays.sort(g);
Arrays.sort(s);
int ans = 0, i = 0, j = 0;
while(i < g.length && j < s.length){
if(g[i] <= s[j]){
ans++;
i++;
j++;
}else{
j++;
}
}
return ans;
}
}
复杂度分析
思路:
贪心
复杂度分析:
代码(C++):
class Solution {
public:
int findContentChildren(vector<int>& g, vector<int>& s) {
if (s.empty()) return 0;
sort(g.begin(), g.end());
sort(s.begin(), s.end());
int res = 0;
for (int i = 0; i < s.size() && res < g.size(); i++) {
if (s[i] >= g[res]) res++;
}
return res;
}
};
贪心算法
var findContentChildren = function (g, s) {
const gn = g.length;
const sn = s.length;
g.sort((v1, v2) => v1 - v2);
s.sort((v1, v2) => v1 - v2);
let i = 0;
let j = 0;
let res = 0;
while (i < gn && j < sn) {
while (j < sn && s[j] < g[i]) j++;
if (j < sn) res++;
i++;
j++;
}
return res;
};
时间复杂度:O(mlogm+nlogn)
空间复杂度:O(logm+logn)
class Solution {
public int findContentChildren(int[] g, int[] s) {
Arrays.sort(g);
Arrays.sort(s);
int numOfChildren = g.length, numOfCookies = s.length;
int count = 0;
for (int i = 0, j = 0; i < numOfChildren && j < numOfCookies; i++, j++) {
while (j < numOfCookies && g[i] > s[j]) {
j++;
}
if (j < numOfCookies) {
count++;
}
}
return count;
}
}
class Solution(object):
def findContentChildren(self, g, s):
"""
:type g: List[int]
:type s: List[int]
:rtype: int
"""
m, n = len(g), len(s)
if m == 0 or n == 0:
return 0
g.sort()
s.sort()
l, r, ans = m - 1, n - 1, 0
while(l >= 0 and r >= 0):
if s[r] < g[0]:
break
if s[r] >= g[l]:
ans += 1
r -= 1
l -= 1
else:
l -= 1
return ans
class Solution {
public int findContentChildren(int[] g, int[] s) {
Arrays.sort(g);
Arrays.sort(s);
int numOfChildren = g.length, numOfCookies = s.length;
int count = 0;
for (int i = 0, j = 0; i < numOfChildren && j < numOfCookies; i++, j++) {
while (j < numOfCookies && g[i] > s[j]) {
j++;
}
if (j < numOfCookies) {
count++;
}
}
return count;
}
}
class Solution { public int findContentChildren(int[] g, int[] s) { int res = 0; Arrays.sort(g); Arrays.sort(s); int i = 0, j = 0; while (i < g.length && j < s.length) { if (g[i] <= s[j]) { res++; i++; j++; } else if (g[i] > s[j]) { j++; } } return res;
}
}
题目其实是需要找出 s 数组中元素大于等于比 g 数组中元素的个数。
如:
g = [1,2,3,4]
s = [1,1,1,1,1,1,1,4,1,1]
这个例子中,s 只有 [ 1, 4 ] 这两个大于等于 g 中的 [ 1, 2 ] 或 [ 1, 3 ] 或 [ 1, 4 ],而不管组合是哪一个,都是两个元素,所以结果是 2 。
首先对两个数组进行排序,排序时需要注意,js 的数组排序是将数组中每个元素 toString 转换后,进行排序的。所以可能会出现 11 排在 2 的前面。
之后双指针同时对两个两个数组进行遍历,对比两边的元素:
function numberSort( x: number, y: number ) {
if (x < y) {
return -1;
} else if (x > y) {
return 1;
} else {
return 0;
}}
function findContentChildren( g: number[], s: number[] ): number {
let ans: number = 0;
g.sort( numberSort );
s.sort( numberSort );
for ( let gIdx = 0, sIdx = 0; sIdx < s.length; ) {
if ( g[ gIdx ] <= s[ sIdx ] ) {
++ gIdx;
++ sIdx;
++ ans;
}else {
++ sIdx
}
}
return ans;
};
时间复杂度:O( n )。
空间复杂度:O( 1 )。
贪心
import java.util.Arrays;
class Solution {
public int findContentChildren(int[] g, int[] s) {
Arrays.sort(g);
Arrays.sort(s);
int ans = 0;
for (int i = 0, j = 0; i < g.length && j < s.length; j++) {
if (g[i] <= s[j]){
i++;
ans++;
}
}
return ans;
}
}
复杂度分析
思路
每个孩子最多一块饼干,能分到的孩子越多越好,则饼干分出去越多越好;
class Solution {
public int findContentChildren(int[] g, int[] s) {
int glen = g.length;
int slen = s.length;
if(slen == 0 || glen == 0) return 0;
Arrays.sort(g);
Arrays.sort(s);
int i = 0, j = 0,ans = 0;
while(i < glen && j < slen){
if(g[i] <= s[j]){
i++;
j++;
ans++;
}else{
j++;
}
}
return ans;
}
}
时间:$O(max(mlogm,nlogn))$,m是g的长度,n是s的长度
空间:$O(logn)$
排序+贪心
class Solution {
public:
int findContentChildren(vector<int>& g, vector<int>& s) {
sort(g.begin(),g.end());
sort(s.begin(),s.end());
int gcur = 0;
int scur = 0;
int count = 0;
while(gcur<g.size() && scur<s.size()){
if(g[gcur]-s[scur]<=0){ //够了,换下一个小朋友
gcur++;
count++;
}
scur++;
}
return count;
}
};
复杂度分析
时间复杂度:O(n)
空间复杂度:O(1)
降序排序,先用最大的饼干满足胃口最大的孩子,如果不满足,说明这个孩子无法被满足,换下一个孩子,如果能满足,换下一个饼干
class Solution(object):
def findContentChildren(self, g, s):
"""
:type g: List[int]
:type s: List[int]
:rtype: int
"""
count = 0
g.sort(reverse = True)
s.sort(reverse = True)
gi,si = 0,0
while gi < len(g) and si <len(s):
if g[gi]<=s[si]:
count += 1
si += 1
gi += 1
return count
时间复杂度:O(nlogn) 空间复杂度:O(1)
排序 + 贪心
class Solution(object):
def findContentChildren(self, g, s):
"""
:type g: List[int]
:type s: List[int]
:rtype: int
"""
g.sort()
s.sort()
n, m = len(g), len(s)
i = j = count = 0
while i < n and j < m:
while j < m and g[i] > s[j]:
j += 1
if j < m:
count += 1
i += 1
j += 1
return count
思路
贪心求解
class Solution {
public:
int findContentChildren(vector
sort(g.begin(), g.end());
sort(s.begin(), s.end());
int gleft =0;
int sleft =0;
int count =0;
while(gleft< g.size() && sleft< s.size())
{
if(g[gleft]<=s[sleft])
{
count++;
gleft++;
sleft++;
}
else
sleft++;
}
return count;
}
};
时间复杂度为 O(nlogn) 空间复杂度: O(logn)
class Solution { public int findContentChildren(int[] g, int[] s) { Arrays.sort(g); Arrays.sort(s); int numOfChildren = g.length, numOfCookies = s.length; int count = 0; for (int i = 0, j = 0; i < numOfChildren && j < numOfCookies; i++, j++) { while (j < numOfCookies && g[i] > s[j]) { j++; } if (j < numOfCookies) { count++; } } return count; } }
思路 排序 + 贪心 代码 class Solution(object): def findContentChildren(self, g, s): """ :type g: List[int] :type s: List[int] :rtype: int """ g.sort() s.sort() n, m = len(g), len(s) i = j = count = 0
while i < n and j < m:
while j < m and g[i] > s[j]:
j += 1
if j < m:
count += 1
i += 1
j += 1
return count
复杂度分析 时间复杂度:O(nlogn) 空间复杂度:O(1)
public int findContentChildren(int[] g, int[] s) {
int glen = g.length;
int slen = s.length;
if(slen == 0 || glen == 0) return 0;
Arrays.sort(g);
Arrays.sort(s);
int i = 0, j = 0,ans = 0;
while(i < glen && j < slen){
if(g[i] <= s[j]){
i++;
j++;
ans++;
}else{
j++;
}
}
return ans;
}
func findContentChildren(g []int, s []int) (ans int) {
sort.Slice(g, func(i, j int) bool {if g[i] > g[j] {return true};return false})
sort.Slice(s, func(i, j int) bool {if s[i] > s[j] {return true};return false})
glen := len(g)
slen := len(s)
a:=0
b:=0
for a<glen&&b<slen {
if g[a] <= s[b]{
ans++
a++
b++
} else {
a++
}
}
return
}
思路:优先满足小胃口
func findContentChildren(g []int, s []int) (res int) {
sort.Ints(g)
sort.Ints(s)
gFlag, sFlag := 0, 0
for gFlag < len(g) && sFlag < len(s) {
if g[gFlag] <= s[sFlag] {
res++
gFlag++
}
sFlag++
}
return
}
func findContentChildren(g []int, s []int) (res int) { sort.Ints(g) sort.Ints(s) gFlag, sFlag := 0, 0 for gFlag < len(g) && sFlag < len(s) { if g[gFlag] <= s[sFlag] { res++ gFlag++ } sFlag++ } return }
455. 分发饼干
入选理由
暂无
题目地址
https://leetcode-cn.com/problems/assign-cookies/
前置知识
暂无
题目描述