-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathleetcode1431-kids-with-the-greatest-number-of-candies.cpp
68 lines (55 loc) · 1.49 KB
/
leetcode1431-kids-with-the-greatest-number-of-candies.cpp
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
67
68
/*
* leetcode1431-kids-with-the-greatest-number-of-candies.cpp
*
* @author Wang Guibao (wang_guibao@163.com)
* @date 2023/12/26 15:00
* @brief https://leetcode.com/problems/kids-with-the-greatest-number-of-candies
*/
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
vector<bool> kidsWithCandies(vector<int>& candies, int extraCandies) {
int max = 0;
int n = candies.size();
for (int i = 0; i < n; ++i) {
if (max < candies[i]) {
max = candies[i];
}
}
std::vector<bool> results(n);
for (int i = 0; i < n; ++i) {
if (candies[i] + extraCandies >= max) {
results[i] = true;
} else {
results[i] = false;
}
}
return results;
}
};
int main() {
while (1) {
std::vector<int> candies;
int extraCandies;
int n;
std::cout << "Input number of candies: ";
std::cin >> n;
std::cout << "Input " << n << " integers for candies: ";
for (int i = 0; i < n; ++i) {
int x;
std::cin >> x;
candies.push_back(x);
}
std::cout << "Input extra candies: ";
std::cin >> extraCandies;
Solution solution;
auto ret = solution.kidsWithCandies(candies, extraCandies);
for (auto x : ret) {
std::cout << x << ' ';
}
std::cout << std::endl;
}
return 0;
}