public class Solution {
public int findContentChildren(int[] g, int[] s) {//g是孩子的贪婪值,s是饼干的尺寸。
PriorityQueue<Integer> pqG = new PriorityQueue();
PriorityQueue<Integer> pqS = new PriorityQueue();
for(int i = 0;i < g.length;i++){
pqG.add(g[i]);
}
for(int j = 0;j < s.length;j++){
pqS.add(s[j]);
}
int output = 0;
while(!(pqG.isEmpty()||pqS.isEmpty())){
int G = pqG.peek();
int S = pqS.peek();
if(S>=G){
output++;
pqG.poll();
}
pqS.poll();
}
return output;
}
public int findContentChildren(int[] g, int[] s) {//g是孩子的贪婪值,s是饼干的尺寸。
PriorityQueue<Integer> pqG = new PriorityQueue();
PriorityQueue<Integer> pqS = new PriorityQueue();
for(int i = 0;i < g.length;i++){
pqG.add(g[i]);
}
for(int j = 0;j < s.length;j++){
pqS.add(s[j]);
}
int output = 0;
while(!(pqG.isEmpty()||pqS.isEmpty())){
int G = pqG.peek();
int S = pqS.peek();
if(S>=G){
output++;
pqG.poll();
}
pqS.poll();
}
return output;
}
}
优美解法:
Arrays.sort(g);
Arrays.sort(s);
int i = 0;
for(int j=0;i<g.length && j<s.length;j++)
{if(g[i]<=s[j]) i++;}
return i;