-
Notifications
You must be signed in to change notification settings - Fork 0
/
0601拥有最多糖果的孩子.html
66 lines (53 loc) · 2.77 KB
/
0601拥有最多糖果的孩子.html
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
59
60
61
62
63
64
65
66
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
// *****************************************************************************************************
// 给你一个数组 candies 和一个整数 extraCandies ,其中 candies[i] 代表第 i 个孩子拥有的糖果数目。
// 对每一个孩子,检查是否存在一种方案,将额外的 extraCandies 个糖果分配给孩子们之后,此孩子有 最多 的糖果。注意,允许有多个孩子同时拥有 最多 的糖果数目。
//
// 示例 1:
// 输入:candies = [2,3,5,1,3], extraCandies = 3
// 输出:[true,true,true,false,true]
// 解释:
// 孩子 1 有 2 个糖果,如果他得到所有额外的糖果(3个),那么他总共有 5 个糖果,他将成为拥有最多糖果的孩子。
// 孩子 2 有 3 个糖果,如果他得到至少 2 个额外糖果,那么他将成为拥有最多糖果的孩子。
// 孩子 3 有 5 个糖果,他已经是拥有最多糖果的孩子。
// 孩子 4 有 1 个糖果,即使他得到所有额外的糖果,他也只有 4 个糖果,无法成为拥有糖果最多的孩子。
// 孩子 5 有 3 个糖果,如果他得到至少 2 个额外糖果,那么他将成为拥有最多糖果的孩子。
// 示例 2:
// 输入:candies = [4,2,1,1,2], extraCandies = 1
// 输出:[true,false,false,false,false]
// 解释:只有 1 个额外糖果,所以不管额外糖果给谁,只有孩子 1 可以成为拥有糖果最多的孩子。
// 示例 3:
// 输入:candies = [12,1,12], extraCandies = 10
// 输出:[true,false,true]
// *****************************************************************************************************
// 方法一
var kidsWithCandies = function(candies, extraCandies) {
var max = Math.max.apply(null, candies);
let ans = [];
candies.forEach(element => {
if(element + extraCandies >= max) {
ans.push(true);
} else {
ans.push(false);
}
});
return ans;
};
// 方法二
var kidsWithCandies = (candies, extraCandies) => {
var max = Math.max(...candies)
return candies.map(cur => cur + extraCandies >= max)
}
var candies = [2,3,5,1,3], extraCandies = 3;
kidsWithCandies(candies,extraCandies);
</script>
</body>
</html>