-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathq_10154_Candy.json
41 lines (41 loc) · 1.61 KB
/
q_10154_Candy.json
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
{
"question": {
"qid": "q_10154",
"diff": 2,
"tags": ["Greedy", "Two Pointers"],
"title": "Candy",
"prompt": [
"There are {{ n }} children standing in a line. Each child is assigned a rating value given in an integer array {{ ratings }}. You are giving candies to these children subjected to the following requirements:",
" \u2022 Each child must have at least one candy.",
" \u2022 Children with a higher rating get more candies than their neighbors.",
"What is the minimum number of candies you must give?"
],
"examples": [
{
"input": "[1,0,2]",
"output": "5",
"explanations": [
"In this case, 1 candy must be given to the second child, 2 candies to the first child, and 2 candies to the third child, so the total number of candies is 5."
]
}
],
"constraints": [
"1 <= n <= 2 * 10^4",
"0 <= ratings[i] <= 10^5 for each rating"
],
"functionArguments": ["ratings"],
"hints": [
"Start by giving each child 1 candy.",
"Then iterate from left to right, and update the number of candies for each child based on their rating and the rating of their left neighbor.",
"Similarly, iterate from right to left and update the number of candies for each child based on their rating and the rating of their right neighbor."
]
},
"testcases": [
{ "tid": "1", "input": [[1, 0, 2]], "expected": 5 },
{ "tid": "2", "input": [[1, 2, 2]], "expected": 4 },
{ "tid": "3", "input": [[1, 2, 87, 87, 87, 2, 1]], "expected": 13 }
],
"submitted": 0,
"accepted": 0,
"questionLevel": 1
}