Explanation: You have 3 children and 2 cookies. The greed factors of 3 children are 1, 2, 3.
And even though you have 2 cookies, since their size is both 1, you could only make the child whose greed factor is 1 content.
You need to output 1.
Input: [1,2], [1,2,3]
Output: 2
Explanation: You have 2 children and 3 cookies. The greed factors of 2 children are 1, 2.
You have 3 cookies and their sizes are big enough to gratify all of the children,
You need to output 2.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class{
publicintfindContentChildren(int[] g, int[] s){
Arrays.sort(g);
Arrays.sort(s);
int gi = g.length-1;
int si = s.length-1;
int res = 0;
while(gi >= 0 && si >= 0){
if(g[gi] <= s[si]){
++res;
--gi;
--si;
}
else{
--gi;
}
}
return res;
}
}
392. Is Subsequence
Given a string s and a string t, check if s is subsequence of t.
You may assume that there is only lower case English letters in both s and t. t is potentially a very long (length ~= 500,000) string, and s is a short string (<=100).
A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, “ace” is a subsequence of “abcde” while “aec” is not).
Example:
1
2
3
4
5
s = "abc", t = "ahbgdc"
Return true.
s = "axc", t = "ahbgdc"
Return false.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class{
publicbooleanisSubsequence(String s, String t){
int sLen = s.length();
int tLen = t.length();
if(sLen > tLen){
returnfalse;
}
int si = 0;
int ti = 0;
while(si < s.length() && ti < t.length()){
if(s.charAt(si) == t.charAt(ti)){
++si;
++ti;
}
else{
++ti;
}
}
return si == sLen;
}
}
435. Non-overlapping Intervals
Given a collection of intervals, find the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping.
Note:
You may assume the interval’s end point is always bigger than its start point.
Intervals like [1,2] and [2,3] have borders “touching” but they don’t overlap each other.
Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Input: [ [1,2], [2,3], [3,4], [1,3] ]
Output: 1
Explanation: [1,3] can be removed and the rest of intervals are non-overlapping.
Input: [ [1,2], [1,2], [1,2] ]
Output: 2
Explanation: You need to remove two [1,2] to make the rest of intervals non-overlapping.
Input: [ [1,2], [2,3] ]
Output: 0
Explanation: You don't need to remove any of the intervals since they're already non-overlapping.
近期评论