Open azl397985856 opened 2 years ago
贪心
class Solution {
public int findContentChildren(int[] g, int[] s) {
Arrays.sort(g);
Arrays.sort(s);
int i = 0, j = 0, ans = 0;
while (i < g.length && j < s.length) {
if (s[j] >= g[i]) {
++i;
++j;
++ans;
} else {
++j;
}
}
return ans;
}
}
class Solution(object):
def findContentChildren(self, g, s):
"""
:type g: List[int]
:type s: List[int]
:rtype: int
"""
g.sort()
s.sort()
i, j = 0, 0
content = 0
while i < len(g) and j < len(s):
if g[i] <= s[j]:
content += 1
i += 1
j += 1
else:
j += 1
return content
class Solution {
public int findContentChildren(int[] g, int[] s) {
Arrays.sort(g);
Arrays.sort(s);
int i = 0, j = 0;
while (i < g.length && j < s.length) {
if (g[i] <= s[j]) {
i++;
}
j++;
}
return i;
}
}
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;
}
}
复杂度 时间复杂度:O(nlogn) 空间复杂度:O(1)
first sort g and s\ Then use greedy + two pointers to check both cookie and child one by one
class Solution:
def findContentChildren(self, g: List[int], s: List[int]) -> int:
# first sort two list and then do greedy
g = sorted(g)
s = sorted(s)
gi = 0
sj = 0
res = 0
while gi < len(g) and sj < len(s):
if s[sj] >= g[gi]:
#if cookie is big enough, update res and move pointers
res += 1
gi += 1
sj += 1
else:
#if cookie is not big enough, need to move pointer for cookie
sj += 1
return res
Go Code:
func findContentChildren(g []int, s []int) int {
sort.Ints(g)
sort.Ints(s)
i,j := 0,0
for i<len(g) && j<len(s){
if g[i]<=s[j]{
i++
}
j++
}
return i
}
复杂度分析
令 n 为数组长度。
思路: 这道题给了我们一堆cookie,每个cookie的大小不同,还有一群小朋友,每个小朋友的胃口也不同的,问我们当前的cookie最多能满足几个小朋友。 典型的利用贪婪算法的题目,
复杂度分析:
代码(C++):
方法一、贪心,iterate cookies
class Solution {
public:
int findContentChildren(vector<int>& g, vector<int>& s) {
if (!s.size()) return 0;
sort(g.begin(), g.end());
sort(s.begin(), s.end());
int j = 0;
for (int i = 0; i < s.size() && j < g.size(); ++i) {
if(s[i] >= g[j])
++j;
}
return j;
}
};
贪心,排序+双指针
class Solution {
public int findContentChildren(int[] g, int[] s) {
Arrays.sort(g);
Arrays.sort(s);
int count = 0, cookie = 0;
for (int child = 0; child < g.length; child++) {
while (cookie < s.length) {
if (g[child] <= s[cookie++]) {
count++;
break;
}
}
}
return count;
}
}
https://leetcode.com/problems/assign-cookies/
使用最小的cookie去尽量满足胃口最小的孩子。这样可以满足尽量多的孩子。
class Solution {
public int findContentChildren(int[] g, int[] s) {
if (s.length == 0) return 0;
int count = 0;
Arrays.sort(g);
Arrays.sort(s);
int i = 0;
int j = 0;
while (i < g.length && j < s.length) {
if (g[i] <= s[j]) {
count++;
i++;
j++;
} else {
j++;
}
}
return count;
}
}
时间:O(nlogn)
空间:O(1)
first sort g and s Then use greedy + two pointers to check both cookie and child one by one
Algorithm in python3 class Solution: def findContentChildren(self, g: List[int], s: List[int]) -> int:
g = sorted(g)
s = sorted(s)
gi = 0
sj = 0
res = 0
while gi < len(g) and sj < len(s):
if s[sj] >= g[gi]:
#if cookie is big enough, update res and move pointers
res += 1
gi += 1
sj += 1
else:
#if cookie is not big enough, need to move pointer for cookie
sj += 1
return res
Complexity Analysis Time complexity: O(NlogN) Space complexity: O(1)
class Solution {
public:
int findContentChildren(vector
for (int i = 0; i < s.size() && j < g.size(); ++i)
{
if (s[i] >= g[j]) j++;
}
return j;
}
};
思路: super straightforward, just sort
class Solution {
public int findContentChildren(int[] g, int[] s) {
int result = 0;
if (s.length == 0) {
return result;
}
Arrays.sort(g);
Arrays.sort(s);
int idx = 0;
for (int i = 0; i < s.length && idx < g.length; i++) {
if (s[i] >= g[idx]) {
result++;
idx++;
}
}
return result;
}
}
Time Complexity: O(nlogn) Space Complexity: O(1)
class Solution:
def findContentChildren(self, g: List[int], s: List[int]) -> int:
g.sort()
s.sort()
child=0
cookie=0
while child < len(g) and cookie<len(s):
if g[child]<=s[cookie]:
child+=1
cookie+=1
return child
Time: O(nlogn)
Space: O(1)
代码: class Solution: def findContentChildren(self, g: List[int], s: List[int]) -> int: g.sort() s.sort() i=j=t=0 while i < len(g) and j< len(s): if g[i]<=s[j]: i+=1 j+=1 t+=1 else: j+=1 return t
class Solution {
public:
int findContentChildren(vector
时间:O(n*logn) 空间:O(1)
class Solution {
public int findContentChildren(int[] g, int[] s) {
Arrays.sort(g);
Arrays.sort(s);
int cnt = 0;
for (int i = 0; cnt < g.length && i < s.length; i ++) {
if (s[i] >= g[cnt]) {
cnt ++;
}
}
return cnt;
}
}
/**
var findContentChildren = function(g, s) {
g.sort((a,b)=>a-b);
s.sort((a,b)=>a-b);
let res = 0;
let i = g.length - 1,j = s.length - 1;
while(i>=0&&j>=0){
if(s[j]>=g[i]){
res ++;
j--;
i--;
}else{
i--;
continue;
}
}
return res;
};
To maximize the number of children content, we can maximize the efficiency of assignment by minimizing the difference between expectation and real gain. That is we assign a cookie to satisfy the greediest child it can satisfy. In this way, we can make the fullest use of each cookie and achieve our goal.
Based on the intuition above, we adopt the greedy algorithm and implement it by two pointer technique. To begin with, the two arrays are sorted to facilitate the match between the greediest and the best cookie. Once sorted, both of two pointers are placed at the end of the two arrays separately. While both p and q are no less than 0, the element pointed by p would be compared with the one pointed by q. If s[q]>=g[p], it means the match can be made and both p,q move towards 0 for one unit. And the result would increase by 1 to record this successful assignment. Otherwise, the match is not feasible and we have to find a better cookie by only moving q backward. When the while loop is terminated, the result would be returned as the final result.
class Solution:
def findContentChildren(self, g: List[int], s: List[int]) -> int:
g.sort()
s.sort()
p,q=len(g)-1,len(s)-1
result=0
while(p>=0 and q>=0):
if s[q]>=g[p]:
result=result+1
q=q-1
p=p-1
else:
p=p-1
return result
Time Complexity: O(max(Nglog(Ng), Nslog(Ns))
Space Complexity: O(1)
思路 贪心+双指针;通过对饼干和胃口排序,用双指针遍历 代码(C++)
class Solution {
public:
int findContentChildren(vector<int>& g, vector<int>& s) {
sort(g.begin(),g.end());
sort(s.begin(),s.end());
int gl = g.size();
int sl = s.size();
int gi=0 ,si = 0,res = 0;
while(gi < gl && si < sl)
{
if(s[si] >= g[gi])
{
si++;
gi++;
res++;
}
else
{
si++;
}
}
return res;
}
};
复杂度分析 时间复杂度:O(N^logn) 空间复杂度:O(1)
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;
};
class Solution:
def findContentChildren(self, g: List[int], s: List[int]) -> int:
g.sort(reverse=True)
s.sort(reverse=True)
gi, si = 0, 0
cnt = 0
while gi < len(g) and si < len(s):
if s[si] >= g[gi]:
cnt += 1
si += 1
gi += 1
return cnt
贪心
class Solution:
def findContentChildren(self, g: List[int], s: List[int]) -> int:
g.sort()
s.sort()
i=0
j=0
cnt=0
while i<len(g) and j<len(s):
while j<len(s) and s[j]<g[i] :
j+=1
if j<len(s) and i<len(g) and s[j]>=g[i]:
cnt+=1
i+=1
j+=1
return cnt
int findContentChildren(vector<int>& g, vector<int>& s) {
sort(g.begin(), g.end());
sort(s.begin(), s.end());
int count = 0;
for (int i = 0, j = 0; i < g.size() && j < s.size(); ++i, ++j) {
while (j < s.size() && g[i] > s[j]) {
++j;
}
if (j < s.size()) {
++count;
}
}
return count;
}
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;
}
}
时间复杂度:O(N^logn) 空间复杂度:O(1)
class Solution {
public:
int findContentChildren(vector<int>& g, vector<int>& s) {
sort(g.begin(), g.end());
sort(s.begin(), s.end());
int j=0;
for(int i=0; i<s.size()&&j<g.size(); i++)
{
if(s[i]>=g[j]) j++;
}
return j;
}
};
Time:O(n^logn) Space:O(1)
Order and then greedy
使用语言:Python3
class Solution:
def findContentChildren(self, g: List[int], s: List[int]) -> int:
count = 0
g_p = 0
s_p = 0
g.sort()
s.sort()
while s_p != len(s):
if s[s_p] >= g[g_p]:
count += 1
g_p += 1
s_p += 1
else:
s_p += 1
if g_p == len(g):
break
return count
复杂度分析 时间复杂度:O(nlogn) 空间复杂度:O(1)
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;
}
}
Order and greedy. Increase count and move to the next child if the child is content with the current cookie. Always move the cookie pointer to the next cookie.
class Solution:
def findContentChildren(self, g: List[int], s: List[int]) -> int:
g.sort()
s.sort()
i, j, count = 0, 0, 0
while i < len(g) and j < len(s):
if g[i] <= s[j]:
count += 1
i += 1
j += 1
return count
Time complexity: O(nlogn). Time for sorting the lists.
Space complexity: O(1).
贪心算法,先对两个数组进行排序,用尽量小的饼干去喂胃口小的孩子。如果满足条件可以喂,则更新答案和饼干数组指针,否则小孩数组指针不变,饼干数组指针前进一格
class Solution {
public:
int findContentChildren(vector<int>& g, vector<int>& s) {
sort(g.begin(), g.end());
sort(s.begin(), s.end());
int j = 0;
int res = 0;
for (int i = 0; i < g.size() && j < s.size(); ++i) {
if (s[j] >= g[i]) {
res++;
j++;
} else {
j++;
i--;
}
}
}
return res;
};
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; } }
func findContentChildren(g []int, s []int) (ans int) {
sort.Ints(g)
sort.Ints(s)
n, m := len(g), len(s)
for i, j := 0, 0; i < n && j < m; i++ {
for j < m && g[i] > s[j]{
j++
}
if j < m {
ans++
j++
}
}
return
}
/**
* @param {number[]} g
* @param {number[]} s
* @return {number}
*/
function sort(arr) {
arr.sort((a, b) => a - b);
}
var findContentChildren = function(g, s) {
sort(g);
sort(s);
console.log(g, s)
let res = 0;
for (let i = 0, j = 0; i < g.length && j < s.length; i++, j++) {
while (j < s.length && g[i] > s[j]) {
j++;
}
if (j < s.length) {
res++;
}
}
return res;
};
class Solution { public int findContentChildren(int[] g, int[] s) { Arrays.sort(g); Arrays.sort(s);
int cnt = 0;
for (int i = 0; cnt < g.length && i < s.length; i ++) {
if (s[i] >= g[cnt]) {
cnt ++;
}
}
return cnt;
}
}
贪心法,给最小胃口的小孩最小的饼干。
class Solution:
def findContentChildren(self, g: List[int], s: List[int]) -> int:
g.sort()
s.sort()
child, cookie = 0, 0
while child < len(g) and cookie < len(s):
if s[cookie] >= g[child]:
child += 1
cookie += 1
return child
贪心+双指针
将g与s排序后,用尽可能小的s满足g,如果完成则双指针向后移动,如果不满足则将s增大
class Solution:
def findContentChildren(self, g: List[int], s: List[int]) -> int:
g.sort()
s.sort()
count = 0
gi = 0
si = 0
while gi<len(g) and si<len(s):
if s[si]>=g[gi]:
count+=1
gi+=1
si+=1
else:
si+=1
return count
时间复杂度: O(nlogn) 排序
空间复杂度: O(1)
对小朋友的胃口和饼干的尺寸进行排序,优先让大的饼干满足胃口大的孩子即可
/**
* @param {number[]} g
* @param {number[]} s
* @return {number}
*/
var findContentChildren = function(g, s) {
g.sort((a , b) => a - b);
s.sort((a, b) => a - b);
let ans = 0;
let i = s.length - 1;
let j = g.length - 1;
while( i >= 0 && j >= 0 ) {
if(s[i] >= g[j]){
// 可以喂饱
ans++;
i--;
j--;
} else {
j--;
}
}
return ans;
};
时间复杂度: O(n)
额外空间复杂度: O(1)
排序
, 贪心
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:
def findContentChildren(self, g, s):
g.sort()
s.sort()
ret, point, ln = 0, -1, len(s)
for child in g:
while point < ln - 1:
point += 1
if s[point] >= child:
ret += 1
break
return ret
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) {
if(s.length==0)return 0;
Arrays.sort(g);
Arrays.sort(s);
int i=0,j=0;
while(i<g.length && j<s.length){
if(s[j]>=g[i]){
i++;
j++;
}else{
j++;
}
}
return i;
}
}
class Solution:
def findContentChildren(self, g: List[int], s: List[int]) -> int:
if len(s)==0:
return 0
g.sort()
s.sort()
left = 0
right = 0
res = 0
while left<len(g) and right < len(s):
if g[left]<=s[right]:
left+=1
right+=1
res +=1
else:
right+=1
return res
var findContentChildren = function (g, s) {
g.sort(asc);
s.sort(asc);
let gp = 0,
sp = 0;
while (sp < s.length && gp < g.length) {
if (s[sp] >= g[gp]) gp++;
sp++;
}
return gp;
function asc(a, b) {
return a - b;
}
};
class Solution: def findContentChildren(self, g: List[int], s: List[int]) -> 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:
def findContentChildren(self, g: List[int], s: List[int]) -> int:
g.sort()
s.sort()
ans = 0
gi = 0
si = 0
while gi < len(g) and si < len(s):
while si < len(s) and g[gi] > s[si]:
si += 1
if si < len(s):
ans += 1
gi += 1
si += 1
return ans
def findContentChildren(self, g: List[int], s: List[int]) -> int:
g.sort()
s.sort()
count = i = j = 0
while i < len(g) and j < len(s):
while j < len(s) and g[i] > s[j]:
j += 1
if j < len(s):
count += 1
i += 1
j += 1
return count
/**
* @param {number[]} g
* @param {number[]} s
* @return {number}
*/
var findContentChildren = function(g, s) {
g.sort((a, b) => a - b);
s.sort((a, b) => a - b);
let ans = 0;
for(let i = 0, j = 0; i < g.length && j < s.length; i++, j++) {
while(j < s.length && s[j] < g[i]) {
j++
}
if (j < s.length) {
ans++
}
}
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; } }
贪心+双指针,给胃口最小的孩子吃最小的饼干
/**
*
* @param {gi} 胃口大小
* @param {sj} 饼干大小
* @returns
*/
const handle = 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(N^logn) 空间复杂度:O(1)
思路:其实该题有二种思路,一种是拿饼干去找满足最低要求的小孩子的胃口,一种是那种胃口的值去找合适的饼干。(注意要做最低的要求,所以需要对二个数组进行排序!!!)
class Solution { public int findContentChildren(int[] g, int[] s) { int res =0; Arrays.sort(g); Arrays.sort(s); int j=0; for(int i=0;i<s.length;i++){ if(s[i]<g[j])continue; res++; j++; if(j>=g.length)break; } return res; } }
455. 分发饼干
入选理由
暂无
题目地址
https://leetcode-cn.com/problems/assign-cookies/
前置知识
暂无
题目描述