forked from algorithm019/algorithm019
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassignCookies.java
More file actions
58 lines (51 loc) · 1.83 KB
/
assignCookies.java
File metadata and controls
58 lines (51 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
class Solution042 {
public int findContentChildren(int[] g, int[] s) {
int g0 = 0;
int s0 = 0;
//先对两个数组排序,g是胃口大小数组,s是饼干大小数组
Arrays.sort(g);
Arrays.sort(s);
if( g == null|| s == null) return 0;
//这里循环的条件是两个指针不达到两个数组的长度,也就是没有元素移动了
while(g0<g.length && s0<s.length){
if(g[g0]<=s[s0]){
//当胃口比饼干小,那么匹配成功,分给小孩
g0++;
}
s0++;
}
return g0;
}
}
public class assignCookies {
public static int[] stringToIntegerArray(String input) {
input = input.trim();
input = input.substring(1, input.length() - 1);
if (input.length() == 0) {
return new int[0];
}
String[] parts = input.split(",");
int[] output = new int[parts.length];
for(int index = 0; index < parts.length; index++) {
String part = parts[index].trim();
output[index] = Integer.parseInt(part);
}
return output;
}
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String line;
while ((line = in.readLine()) != null) {
int[] g = stringToIntegerArray(line);
line = in.readLine();
int[] s = stringToIntegerArray(line);
int ret = new Solution042().findContentChildren(g, s);
String out = String.valueOf(ret);
System.out.print(out);
}
}
}