Skip to content

Commit 5ec3c57

Browse files
committed
solve problem Assign Cookies
1 parent ca0c1eb commit 5ec3c57

File tree

5 files changed

+70
-0
lines changed

5 files changed

+70
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ All solutions will be accepted!
126126
|263|[Ugly Number](https://leetcode-cn.com/problems/ugly-number/description/)|[java/py/js](./algorithms/UglyNumber)|Easy|
127127
|783|[Minimum Distance Between Bst Nodes](https://leetcode-cn.com/problems/minimum-distance-between-bst-nodes/description/)|[java/py/js](./algorithms/MinimumDistanceBetweenBstNodes)|Easy|
128128
|747|[Largest Number At Least Twice Of Others](https://leetcode-cn.com/problems/largest-number-at-least-twice-of-others/description/)|[java/py/js](./algorithms/LargestNumberAtLeastTwiceOfOthers)|Easy|
129+
|455|[assign cookies](https://leetcode-cn.com/problems/assign-cookies/description/)|[java/py/js](./algorithms/AssignCookies)|Easy|
129130

130131
# Database
131132
|#|Title|Solution|Difficulty|

algorithms/AssignCookies/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Assign Cookies
2+
We have to sort the list first, but which type of sort function is the best?
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public int findContentChildren(int[] g, int[] s) {
3+
Arrays.sort(g);
4+
Arrays.sort(s);
5+
6+
int count = 0,
7+
index = 0;
8+
9+
for (int i = 0; i < s.length; i++) {
10+
if (s[i] >= g[index]) {
11+
count++;
12+
index++;
13+
14+
if (index == g.length) {
15+
break;
16+
}
17+
}
18+
}
19+
20+
return count;
21+
}
22+
}

algorithms/AssignCookies/solution.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* @param {number[]} g
3+
* @param {number[]} s
4+
* @return {number}
5+
*/
6+
var findContentChildren = function(g, s) {
7+
g.sort((a, b) => a - b)
8+
s.sort((a, b) => a - b)
9+
10+
let count = 0,
11+
index = 0
12+
13+
for (let i = 0; i < s.length; i++) {
14+
if (s[i] >= g[index]) {
15+
count++
16+
index++
17+
18+
if (index == g.length) {
19+
break
20+
}
21+
}
22+
}
23+
24+
return count
25+
};

algorithms/AssignCookies/solution.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution(object):
2+
def findContentChildren(self, g, s):
3+
"""
4+
:type g: List[int]
5+
:type s: List[int]
6+
:rtype: int
7+
"""
8+
g.sort()
9+
s.sort()
10+
count = 0
11+
index = 0
12+
13+
for i in s:
14+
if i >= g[index]:
15+
count += 1
16+
index += 1
17+
if index == len(g):
18+
break
19+
20+
return count

0 commit comments

Comments
 (0)