# 练习
# LeetCode:455.分饼干
var findContentChildren = function (g, s) {
const sortFunc = function (a, b) {
return a - b;
}
g.sort(sortFunc)
s.sort(sortFunc)
let i = 0;
s.forEach((n) => {
if (n >= g[i]) {
i++
}
})
return i
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
# LeetCode:122. 买卖股票的最佳时机 II
var maxProfit = function (prices) {
let profit = 0;
for (let i = 1; i < prices.length; i++) {
if (prices[i] > prices[i - 1]) {
profit += prices[i] - prices[i - 1]
}
}
return profit;
};
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9