Skip to content

Commit 03b4bc9

Browse files
author
lewis617
committed
Move assignCookies from ArrayList to LeetCode
1 parent 330cbc0 commit 03b4bc9

File tree

4 files changed

+27
-27
lines changed

4 files changed

+27
-27
lines changed

ArrayList/ArrayList.js

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -129,20 +129,6 @@ function ArrayList() {
129129
}
130130
return -1;
131131
};
132-
133-
this.findContentChildren = function (cookies) {
134-
this.quickSort();
135-
cookies.sort(function (a, b) {
136-
return a - b;
137-
});
138-
var i = 0; // 满足的孩子数量
139-
for (var j = 0; i < array.length && j < cookies.length; j++) { // 遍历饼干
140-
if (array[i] <= cookies[j]) {
141-
i++;
142-
}
143-
}
144-
return i;
145-
}
146132
}
147133

148134
module.exports = ArrayList;

ArrayList/ArrayList.test.js

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,4 @@ test('ArrayList', function () {
4141
expect(array.binarySearch(3)).toBe(2);
4242
expect(array.binarySearch(6)).toBe(-1);
4343
expect(array.binarySearch(2)).toBe(1);
44-
45-
array = createNonSortedArray();
46-
expect(array.findContentChildren([1, 1])).toBe(1);
47-
expect(array.findContentChildren([1, 2, 3])).toBe(3);
48-
49-
// test for sort() function, eg: 10 and 9, sort will return a wrong answer.
50-
array = new ArrayList();
51-
array.insert(10);
52-
array.insert(9);
53-
array.insert(8);
54-
array.insert(7);
55-
array.insert(6);
56-
expect(array.findContentChildren([7, 6, 5])).toBe(2);
5744
});
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
var findContentChildren = require('../assignCookies');
2+
3+
test('assignCookies', function () {
4+
expect(findContentChildren([1, 3, 5, 4, 2], [1, 1])).toBe(1);
5+
expect(findContentChildren([10, 9, 8, 7, 6], [7, 6, 5])).toBe(2);
6+
});

LeetCode/assignCookies.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @param {number[]} g
3+
* @param {number[]} s
4+
* @return {number}
5+
*/
6+
var findContentChildren = function (g, s) {
7+
var sortFunc = function (a, b) {
8+
return a - b;
9+
};
10+
g.sort(sortFunc);
11+
s.sort(sortFunc);
12+
var i = 0;
13+
for (var j = 0; i < g.length && j < s.length; j++) {
14+
if (g[i] <= s[j]) {
15+
i++;
16+
}
17+
}
18+
return i;
19+
};
20+
21+
module.exports = findContentChildren;

0 commit comments

Comments
 (0)