Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d145eda

Browse files
daily update
1 parent 1cb9265 commit d145eda

File tree

3 files changed

+202
-0
lines changed

3 files changed

+202
-0
lines changed
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
2+
3+
# 1431_KidsWithTheGreatestNumberOfCandies_拥有最多糖果的孩子
4+
5+
## 📌题目详情
6+
7+
[leetcode 题目地址](https://leetcode.com/problems/kids-with-the-greatest-number-of-candies/)
8+
9+
[leetcode-cn 题目地址](https://leetcode-cn.com/problems/kids-with-the-greatest-number-of-candies/)
10+
11+
📗Difficulty:**Easy**
12+
13+
🎯Tags:
14+
15+
+ **[Array](https://leetcode.com/tag/array/)**
16+
17+
---
18+
19+
## 📃题目描述
20+
21+
给你一个数组 `candies` 和一个整数 `extraCandies` ,其中 `candies[i]` 代表第 `i` 个孩子拥有的糖果数目。
22+
23+
对每一个孩子,检查是否存在一种方案,将额外的 `extraCandies` 个糖果分配给孩子们之后,此孩子有 **最多** 的糖果。注意,允许有多个孩子同时拥有 **最多** 的糖果数目。
24+
25+
26+
27+
**样例 1:**
28+
29+
```
30+
输入:candies = [2,3,5,1,3], extraCandies = 3
31+
输出:[true,true,true,false,true]
32+
解释:
33+
孩子 1 有 2 个糖果,如果他得到所有额外的糖果(3个),那么他总共有 5 个糖果,他将成为拥有最多糖果的孩子。
34+
孩子 2 有 3 个糖果,如果他得到至少 2 个额外糖果,那么他将成为拥有最多糖果的孩子。
35+
孩子 3 有 5 个糖果,他已经是拥有最多糖果的孩子。
36+
孩子 4 有 1 个糖果,即使他得到所有额外的糖果,他也只有 4 个糖果,无法成为拥有糖果最多的孩子。
37+
孩子 5 有 3 个糖果,如果他得到至少 2 个额外糖果,那么他将成为拥有最多糖果的孩子。
38+
```
39+
40+
41+
42+
**样例 2:**
43+
44+
```
45+
输入:candies = [4,2,1,1,2], extraCandies = 1
46+
输出:[true,false,false,false,false]
47+
解释:只有 1 个额外糖果,所以不管额外糖果给谁,只有孩子 1 可以成为拥有糖果最多的孩子。
48+
```
49+
50+
51+
52+
**提示:**
53+
54+
- `2 <= candies.length <= 100`
55+
- `1 <= candies[i] <= 100`
56+
- `1 <= extraCandies <= 50`
57+
58+
59+
60+
****
61+
62+
## 🏹🎯解题思路
63+
64+
2020 年 6 月 1 日,国际儿童节。
65+
66+
leetcode-cn 6 月份每日一题的第一题。
67+
68+
69+
70+
如果希望一个小朋友拥有最多的糖果,那么最佳的选择是把全部的 `extraCandies` 都给这个小朋友一个人。如果这么操作后,还是比持有原始糖果数最多的小朋友少,那么返回 `false`
71+
72+
一次遍历,找出 `candies` 数组中的最大值,比较 `candies[i] + extraCandies >= maxCandies` 即可。
73+
74+
75+
76+
#### 代码实现
77+
78+
```java
79+
public class Solution1 {
80+
public List<Boolean> kidsWithCandies(int[] candies, int extraCandies) {
81+
int maxCandies = Integer.MIN_VALUE;
82+
for (int num : candies) {
83+
maxCandies = Math.max(num, maxCandies);
84+
}
85+
List<Boolean> res = new LinkedList<>();
86+
for (int num : candies) {
87+
if (num + extraCandies >= maxCandies) {
88+
res.add(true);
89+
} else {
90+
res.add(false);
91+
}
92+
}
93+
return res;
94+
}
95+
96+
public static void main(String[] args) {
97+
int[] candies = {2, 3, 5, 1, 3};
98+
int extraCandies = 3;
99+
List<Boolean> res = new Solution1().kidsWithCandies(candies, extraCandies);
100+
for (Boolean i : res) {
101+
System.out.println(i);
102+
}
103+
}
104+
}
105+
```
106+
107+
108+
109+
#### 复杂度分析
110+
111+
+ 时间复杂度: `O(n)` 。遍历 2 次数组。
112+
+ 空间复杂度:`O(1)` 。不计算最后返回结果需要的额外空间。
113+
114+
115+
116+
## 💡总结
117+
118+
2020 年 6 月 1 日,星期一。
119+
120+
今日 A 股市场全面上涨,上证指数上涨 2.21 %,收于 2915.43 点;深证成指上涨 3.31 %,收于 11102.15 点。
121+
122+
儿童节快乐~
123+
124+
125+
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package ltd.ryantech.array.kidsWithTheGreatestNumberOfCandies1431;
2+
3+
import java.util.LinkedList;
4+
import java.util.List;
5+
6+
/**
7+
* @author jerry
8+
* @program leetcode
9+
* @package_name ltd.ryantech.array.kidsWithTheGreatestNumberOfCandies1431
10+
* @description 拥有最多糖果的孩子
11+
* @leetcode_CN_url // https://leetcode-cn.com/problems/kids-with-the-greatest-number-of-candies/
12+
* @leetcode_US_url // https://leetcode.com/problems/kids-with-the-greatest-number-of-candies/
13+
* @hard_level Easy
14+
* @tag Array // https://leetcode-cn.com/tag/array/
15+
* @create 2020年06月01日 14:48
16+
**/
17+
18+
public class Solution1 {
19+
public List<Boolean> kidsWithCandies(int[] candies, int extraCandies) {
20+
int maxCandies = Integer.MIN_VALUE;
21+
for (int num : candies) {
22+
maxCandies = Math.max(num, maxCandies);
23+
}
24+
List<Boolean> res = new LinkedList<>();
25+
for (int num : candies) {
26+
if (num + extraCandies >= maxCandies) {
27+
res.add(true);
28+
} else {
29+
res.add(false);
30+
}
31+
}
32+
return res;
33+
}
34+
35+
public static void main(String[] args) {
36+
int[] candies = {2, 3, 5, 1, 3};
37+
int extraCandies = 3;
38+
List<Boolean> res = new Solution1().kidsWithCandies(candies, extraCandies);
39+
for (Boolean i : res) {
40+
System.out.println(i);
41+
}
42+
}
43+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package ltd.ryantech.array.kidsWithTheGreatestNumberOfCandies1431
2+
3+
/**
4+
* @author jerry
5+
* @program leetcode
6+
* @package_name ltd.ryantech.array.kidsWithTheGreatestNumberOfCandies1431
7+
* @description 拥有最多糖果的孩子
8+
* @leetcode_CN_url // https://leetcode-cn.com/problems/kids-with-the-greatest-number-of-candies/
9+
* @leetcode_US_url // https://leetcode.com/problems/kids-with-the-greatest-number-of-candies/
10+
* @hard_level Easy
11+
* @tag Array // https://leetcode-cn.com/tag/array/
12+
* @create 2020年06月01日 14:48
13+
**/
14+
15+
fun kidsWithCandies(candies: IntArray, extraCandies: Int): BooleanArray {
16+
var maxCandies: Int = Int.MIN_VALUE
17+
for (num: Int in candies) {
18+
maxCandies = num.coerceAtLeast(maxCandies)
19+
}
20+
val res = BooleanArray(candies.size)
21+
for (idx: Int in candies.indices) {
22+
res[idx] = candies[idx] + extraCandies >= maxCandies
23+
}
24+
return res
25+
}
26+
27+
fun main(args: Array<String>) {
28+
val candies: IntArray = intArrayOf(2, 3, 5, 1, 3)
29+
val extraCandies: Int = 3
30+
val res: BooleanArray = kidsWithCandies(candies, extraCandies)
31+
for (i: Boolean in res) {
32+
println(i)
33+
}
34+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /