Open azl397985856 opened 2 years ago
C++
class Solution
{
public:
int findContentChildren(vector<int> &g, vector<int> &s)
{
int res = 0;
sort(g.begin(), g.end());
sort(s.begin(), s.end());
for (int i = 0, j = 0; i < g.size(); i++)
{
while (j < s.size() && g[i] > s[j])
j++;
if (j < s.size())
{
res++;
j++;
}
else
{
break;
}
}
return res;
}
class Solution {
public int findContentChildren(int[] g, int[] s) {
int res = 0, i = 0, j = 0;
Arrays.sort(g);
Arrays.sort(s);
while( i < g.length && j < s.length){
if(g[i] <= s[j]){
res++;
i++;
j++;
}else{
j++;
}
}
return res;
}
}
Greedy
class Solution {
public int findContentChildren(int[] g, int[] s) {
int res = 0;
Arrays.sort(g);
Arrays.sort(s);
if(s.length == 0 || g[0] > s[s.length - 1]) {
return 0;
}
int i =0, j = 0;
while(i < g.length & j < s.length) {
if(s[j] >= g[i]) {
res += 1;
i++;
}
j++;
}
return res;
}
}
Complexity Analysis
class Solution {
public:
int findContentChildren(vector
}
return cnt;
}
};
时间复杂度: O(mlogm + nlogn)
本题思路:用从大到小的 cookies 尽可能满足最多数量的小孩
class Solution {
public int findContentChildren(int[] g, int[] s) {
// 从小到大 sort
Arrays.sort(g);
Arrays.sort(s);
int num = 0;
int child = g.length - 1;
int cookie = s.length - 1;
while (child >=0 && cookie >= 0) {
// 最大的 cookie 可以满足最贪心的小孩
if (s[cookie] >= g[child]) {
num++;
cookie--;
child--;
} else {
// 最贪心的小孩过于贪心,无法满足
child--;
}
}
return num;
}
}
class Solution:
def findContentChildren(self, g: List[int], s: List[int]) -> int:
if not s: return 0
g.sort()
s.sort()
cnt=0
i = 0
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):
cnt +=1
j+=1
i+=1
return cnt
时间:O(mlogm+nlogn) 空间:O(logm+logn)
贪心
var findContentChildren = function(g, s) {
g.sort((a, b) => a - b);
s.sort((a, b) => a - b);
const n = g.length, m = s.length;
let i = 0, j = 0;
let count = 0;
while(i < n && j < m) {
if (s[j] >= g[i]) {
i++;
j++;
count++;
} else {
j++;
}
}
return count;
};
method: feed the smallest s (cookie) to smallest child (g): if not satisfied discard cookie, b/c larger child won't be satified if satified, count++ Time: O(N log N) Space: O(log N)
public int findContentChildren(int[] g, int[] s) {
Arrays.sort(g);
Arrays.sort(s);
int i = 0;
for (var cookie: s) {
if (i != g.length && cookie >= g[i]) {
i++;
}
}
return i;
}
贪心方法,一定是从最小的s[i]开始满足,能够满足最多的小朋友
class Solution {
public int findContentChildren(int[] g, int[] s) {
Arrays.sort(s);
Arrays.sort(g);
int res = 0;
int i = 0;
int j = 0;
while(i < g.length && j < s.length) {
if (g[i] <= s[j]) {
res++;
i++;
j++;
}else{
j++;
}
}
return res;
}
}
class Solution { public int findContentChildren(int[] g, int[] s) { Arrays.sort(g); Arrays.sort(s); int res = 0; for(int i = 0,j = 0; i < g.length && j < s.length; j++) { // 下一个饼干试下当前孩子合不合胃口 孩子合胃口了孩子才动 饼干一直后移 if(g[i] <= s[j]) { res ++; i ++; }
}
return res;
}
}
def findContentChildren(self, g: List[int], s: List[int]) -> int:
g.sort()
s.sort()
count = 0
idx = 0
while idx < len(s) and count < len(g):
if s[idx] >= g[count]:
count += 1
idx += 1
return count
class Solution {
public:
int findContentChildren(vector<int>& g, vector<int>& s) {
sort(g.begin(), g.end());
sort(s.begin(), s.end());
int res = 0;
for(int i = 0, j = 0; i < g.size(); i ++) {
while(j < s.size() && s[j] < g[i]) j ++;
if(j < s.size() && s[j] >= g[i]) {
j ++;
res ++;
}
else break;
}
return res;
}
};
贪心,先排序,然后双指针比较
class Solution:
def findContentChildren(self, g: List[int], s: List[int]) -> int:
g = sorted(g)
s = sorted(s)
i = j = 0
ans = 0
while i < len(g) and j < len(s):
if g[i] <= s[j]:
ans += 1
i += 1
j += 1
return ans
时间复杂度Onlogn 空间复杂度O1
Sort + Greedy
class Solution:
def findContentChildren(self, g: List[int], s: List[int]) -> int:
g.sort()
s.sort()
m,n=len(g)-1, len(s)-1
ans = 0
while m>=0 and n>=0:
if s[n]>=g[m]:
ans += 1
n -= 1
m -= 1
else:
m-=1
return ans
Time: O(max(m, n) log ( max(m, n)). m: # children, n: # cookies Space: O(log (max(m,n))
class Solution:
def findContentChildren(self, g: List[int], s: List[int]) -> int:
if not s:
return 0
g.sort()
s.sort()
ans = 0
i, j = 0, 0
while i < len(g) and j < len(s):
if g[i] <= s[j]:
ans += 1
i += 1
j += 1
else:
while j < len(s) and g[i] > s[j]:
j += 1
return ans
time O(nlogn)
space O(1)
greedy
class Solution:
def findContentChildren(self, g: List[int], s: List[int]) -> int:
res = 0
g.sort()
s.sort()
# print(g, s)
i, j = 0, 0
while i < len(g) and j < len(s):
if g[i] <= s[j]:
res += 1
i += 1
j += 1
else:
j += 1
return res
time O(max(g, s)) space O(1)
class Solution(object):
def findContentChildren(self, g, s):
"""
:type g: List[int]
:type s: List[int]
:rtype: int
"""
g = sorted(g);
s = sorted(s);
index_s = len(s)-1;
index_g = len(g)-1;
ans = 0;
while index_s >= 0 and index_g >= 0:
if s[index_s] >= g[index_g]:
ans += 1;
index_s -=1;
index_g -=1;
else:
index_g -= 1;
return ans
#time complexity: O(nlogn) due to sorting
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;
class Solution:
def findContentChildren(self, g: List[int], s: List[int]) -> int:
g.sort()
s.sort()
ans = 0
i, j = 0, 0
while i < len(g) and j < len(s):
if g[i] <= s[j]:
i += 1
ans += 1
j += 1
return ans
先将g和s排序,再用两个指针,每个饼干尝试一次,不满足条件则换下一块饼干
var findContentChildren = function(g, s) {
g.sort((a,b) => a - b);
s.sort((a,b) => a - b);
let i = 0, j = 0;
let res = 0;
while (i < g.length && j < s.length) {
if (s[j] >= g[i]) {
res++;
i++;
j++;
} else {
j++;
}
}
return res;
};
因为每个饼干只能分给一个小盆友,所以要满足尽可能多的小盆友,需要优先将小尺寸饼干给小胃口
1、先将g和s都排序
2、s和g都只遍历一次,当s[j] >= g[i],结果增加1
var findContentChildren = function(g, s) {
g = g.sort((a, b) => a - b);
s = s.sort((a, b) => a - b);
let res = 0;
let i = 0;
let j = 0;
while(i < g.length && j < s.length){
if(s[j] >= g[i]){
i++;
j++;
res++;
}
else{
j++;
}
}
return res;
};
1.先把孩子胃口和饼干大小从小到大排序(应该按照孩子的胃口从小到大的顺序依次满足每个孩子,且对于每个孩子,应该选择可以满足这个孩子的胃口且尺寸最小的饼干)
class Solution {
public:
int findContentChildren(vector<int>& g, vector<int>& s) {
sort(g.begin(),g.end());
sort(s.begin(),s.end());
int ans=0;
//临时变量,用于第二层循环时,不再回溯
int temp=0;
//i--g--胃口 j--s--饼干
for(int i=0; i<g.size();i++){
for(int j=temp; j<s.size(); j++){
//胃口小于饼干,则饼干给他,ans+1,临时变量也加+1,直接跳出,不再往后遍历
if(g[i] <= s[j]){
ans+=1;
temp= j+1;
break;
}
}
}
return ans;
}
};
greedy
public int findContentChildren(int[] g, int[] s) {
Arrays.sort(g);
Arrays.sort(s);
int res = 0, i = 0, j = 0, m = g.length, n = s.length;
while (i < m && j < n) {
if (g[i] > s[j]) {
j++;
}
else {
res++;
i++;
j++;
}
}
return res;
}
Time: O(nlogn) Space: O(1)
代码 假期后重写
class Solution:
def findContentChildren(self, g: List[int], s: List[int]) -> int:
if not s: return 0
g.sort()
s.sort()
cnt=0
i = 0
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):
cnt +=1
j+=1
i+=1
return cnt
Day65
1、两个数组分别进行排序
2、遍历每个饼干,看是否有孩子满足
3、有孩子满足就记录下来,并找下一个孩子
4、遍历完所有饼干结束
var findContentChildren = function(g, s) {
let count = 0;//计数且记录孩子的位置
g.sort((a, b) => a - b);
s.sort((a, b) => a - b);
//分别进行升序排序
for (let i = 0; i < s.length; i++) {//遍历饼干
if (count < g.length && g[count] <= s[i]) {
count++;
}
//如果有饼干能满足孩子,就记录,并找下一个孩子
}
return count;
};
时间复杂度:O(nlogn)
空间复杂度:O(log)
排序加贪心算法
class Solution:
def findContentChildren(self, g: List[int], s: List[int]) -> int:
g.sort()
s.sort()
g_len = len(g)
s_len = len(s)
i = 0
j = 0
count = 0
while i < g_len and j < s_len:
while j < s_len and s[j] < g[i]:
j += 1
if j < s_len and s[j] >= g[i]:
count += 1
i += 1
j += 1
return count
func findContentChildren(g []int, s []int) int {
sort.Ints(g)
sort.Ints(s)
gLen := len(g)
sLen := len(s)
i := 0
j := 0
count := 0
for i < gLen && j < sLen {
for j < sLen && s[j] < g[i] {
j++
}
if j < sLen && s[j] >= g[i] {
count++
}
i++
j++
}
return count
}
时间复杂度:O(MlogM+NlogN)
空间复杂度:O(1)
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 index = 0;
int result = 0;
for (int i = 0; i < s.length && index < g.length; i++) {
if (s[i] >= g[index]) {
index++;
result++;
}
}
return result;
}
}
Time Complexity: O(mlogm + nlogn)
Space Complexity: O(logm + logn)
class Solution {
public:
int findContentChildren(vector<int>& g, vector<int>& s) {
sort(g.begin(),g.end());
sort(s.begin(),s.end());
int gNum = 0, sNum = 0, count = 0;
while (gNum < g.size() && sNum < s.size())
{
if (g[gNum] <= s[sNum]){
gNum++;
sNum++;
count++;
}else{
sNum++;
}
}
return count;
}
};
class Solution
{
public:
int findContentChildren(vector<int> &g, vector<int> &s)
{
int res = 0;
sort(g.begin(), g.end());
sort(s.begin(), s.end());
for (int i = 0, j = 0; i < g.size(); i++)
{
while (j < s.size() && g[i] > s[j])
j++;
if (j < s.size())
{
res++;
j++;
}
else
{
break;
}
}
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 {
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 && j < g.length; i++) {
if (s[i] >= g[j]) {
j++;
res++;
}
}
return res;
}
}
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(vector<int>& g, vector<int>& s) {
sort(g.begin(), g.end());
sort(s.begin(), s.end());
int index = s.size() - 1; // 饼干数组的下表
int ans = 0;
for (int i = g.size() - 1; i >= 0; i--) {
if (index >= 0 && s[index] >= g[i]) {
ans++;
index--;
}
}
return ans;
}
};
复杂度分析
class Solution {
// time: O(mlogm) + O(nlogn) + O(min(m, n))
// space: O(1)
// two pointers + greedy
public int findContentChildren(int[] g, int[] s) {
Arrays.sort(g);
Arrays.sort(s);
int contentKids = 0;
int sIndex = 0, gIndex = 0;
while (sIndex < s.length && gIndex < g.length) {
if (g[gIndex] <= s[sIndex]) {
contentKids++;
gIndex++;
sIndex++;
} else {
sIndex++;
}
}
return contentKids;
}
}
class Solution:
def findContentChildren(self, g: List[int], s: List[int]) -> int:
if not s: return 0
g.sort()
s.sort()
cnt=0
i = 0
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):
cnt +=1
j+=1
i+=1
return cnt
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
/**
* @param {number[]} g
* @param {number[]} s
* @return {number}
*/
var findContentChildren = function(g, s) {
g = g.sort((a,b) => a-b);
s = s.sort((a,b) => a-b);
var gLen = g.length;
var sLen = s.length;
var i = 0;
var j = 0;
var maxNum = 0;
while(i < gLen && j < sLen){
if(s[j] >= g[i]){
i++;
j++;
maxNum++;
}else{
j++;
}
}
return maxNum;
};
class Solution {
public int findContentChildren(int[] g, int[] s) {
int child = 0;
int cookie = 0;
Arrays.sort(g);
Arrays.sort(s);
while (child < g.length && cookie < s.length){
if (g[child] <= s[cookie]){
child++;
}
cookie++;
}
return child;
}
}
//Time:O(nlogn)
//Space: O(1)
class Solution
{
public:
int findContentChildren(vector
return res;
}
int findContentChildren(vector<int>& g, vector<int>& s) {
sort(g.begin(),g.end());
sort(s.begin(),s.end());
int lg=0;
int ls=0;
while(lg<g.size()&&ls<s.size()){
if(g[lg]<=s[ls]){
ls++;
lg++;
}
else{
ls++;
}
}
return lg;
}
455. 分发饼干
入选理由
暂无
题目地址
https://leetcode-cn.com/problems/assign-cookies/
前置知识
暂无
题目描述