Skip to content

Commit dcd1321

Browse files
authored
Merge pull request #45 from harisdev-netizen/harisdev-branch
Merge pull request from harisdev-netizen/haris
2 parents f84cb57 + 5dd3315 commit dcd1321

File tree

2 files changed

+95
-0
lines changed

2 files changed

+95
-0
lines changed

L-A/0004 Candy ( L-A )/README.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# 0004 Candy ( L-A )
2+
3+
## Problem
4+
5+
There are n children standing in a line. Each child is assigned a rating value given in the integer array ratings.
6+
7+
You are giving candies to these children subjected to the following requirements:
8+
- Each child must have at least one candy.
9+
- Children with a higher rating get more candies than their neighbors.
10+
11+
Return the minimum number of candies you need to have to distribute the candies to the children.
12+
13+
## Test Case
14+
15+
```javascript
16+
Input: ratings = [1,0,2]
17+
Output: 5
18+
Explanation: You can allocate to the first, second and third child with 2, 1, 2 candies respectively.
19+
```
20+
21+
## Solution
22+
23+
```javascript
24+
var candy = function(ratings) {
25+
const candies = ratings.map(() => 1);
26+
const len = ratings.length;
27+
if (len <= 1) return len;
28+
29+
for (let index = 1; index < len; index++) {
30+
if (ratings[index] > ratings[index-1]) {
31+
candies[index] = candies[index-1] + 1;
32+
}
33+
}
34+
35+
let sum = candies[len-1];
36+
for (let index = len-2; index >= 0; index--) {
37+
if (ratings[index] > ratings[index+1] && candies[index] <= candies[index+1]) {
38+
candies[index] = candies[index+1] + 1;
39+
}
40+
sum += candies[index];
41+
}
42+
43+
return sum;
44+
};
45+
```
46+
47+
## How it works
48+
49+
- Given an array of integers representing ratings of candy, the goal is to distribute minimum candies among children such that a child with a higher rating gets more candies than their neighbor with a lower rating.
50+
- The code first initializes a new array called `candies` with the same length as the input `ratings` array, and fills it with ones. This means that initially, each child will receive at least one candy.
51+
- Next, the code checks if the input `ratings` array has a length of 1 or less, in which case the function simply returns the length of the array.
52+
- Then, the code loops through the input `ratings` array, starting from the second element, and compares each element to the previous element. If the current element has a higher rating than the previous element, the corresponding element in the `candies` array is updated to be one more than the previous element's value. This ensures that children with higher ratings receive more candies than their neighbors with lower ratings.
53+
- After that, the code loops through the `candies` array again, this time starting from the second-to-last element and going backwards. For each element, the code checks if it has a higher rating than its next neighbor and if its current number of candies is less than or equal to its next neighbor's number of candies. If both conditions are true, the current element's number of candies is updated to be one more than its next neighbor's number of candies. This makes sure that the distribution of candies is optimal and the minimum number of candies are used.
54+
- Finally, the code calculates the total number of candies distributed by summing up all the elements in the `candies` array and returns the sum.
55+
56+
## References
57+
58+
- [LeetCode](https://leetcode.com/problems/candy/)
59+
60+
## Problem Added By
61+
62+
- [Haris](https://github.com/harisdev-netizen)
63+
64+
## Contributing
65+
66+
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
67+
68+
Please make sure to update tests as appropriate.

L-A/0004 Candy ( L-A )/candy.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
function candy(ratings) {
2+
const candies = new Array(ratings.length).fill(1); // initialize candies array with 1 for each child
3+
let sum = candies.reduce((acc, val) => acc + val, 0); // sum up initial candies
4+
5+
// update candies for increasing ratings from left to right
6+
for (let i = 1; i < ratings.length; i++) {
7+
if (ratings[i] > ratings[i - 1]) {
8+
candies[i] = candies[i - 1] + 1;
9+
sum += candies[i] - 1; // add the extra candies used to the sum
10+
}
11+
}
12+
13+
// update candies for decreasing ratings from right to left
14+
for (let i = ratings.length - 2; i >= 0; i--) {
15+
if (ratings[i] > ratings[i + 1] && candies[i] <= candies[i + 1]) {
16+
candies[i] = candies[i + 1] + 1;
17+
sum += candies[i] - 1; // add the extra candies used to the sum
18+
}
19+
}
20+
21+
return sum;
22+
}
23+
24+
// We can test the function with some sample inputs
25+
console.log(candy([1,0,2]));
26+
console.log(candy([1,2,2]));
27+
console.log(candy([1,3,4,5,2]));

0 commit comments

Comments
 (0)