Table of Contents

# Candy – LeetCode Solution Java , Python 3, Python 2 , C , C++, Best and Optimal Solutions , All you need.

There are `n`

children standing in a line. Each child is assigned a rating value given in the integer array `ratings`

.

You are giving candies to these children subjected to the following requirements:

- Each child must have at least one candy.
- Children with a higher rating get more candies than their neighbors.

Return *the minimum number of candies you need to have to distribute the candies to the children*.

**Example 1:**

Input:ratings = [1,0,2]Output:5Explanation:You can allocate to the first, second and third child with 2, 1, 2 candies respectively.

**Example 2:**

Input:ratings = [1,2,2]Output:4Explanation:You can allocate to the first, second and third child with 1, 2, 1 candies respectively. The third child gets 1 candy because it satisfies the above two conditions.

**Constraints:**

`n == ratings.length`

`1 <= n <= 2 * 10`

^{4}`0 <= ratings[i] <= 2 * 10`

^{4}

# C++ Candy LeetCode Solution

```
``````
class Solution {
public:
int candy(vector<int>& ratings) {
int s = 0;
int n = ratings.size();
vector<int> left(n, 1);
vector<int> right(n, 1);
for(int i=1; i<n; i++){
if(ratings[i] > ratings[i-1]){
left[i] = left[i-1]+1;
}
}
for(int i=n-2; i>=0; i--){
if(ratings[i] > ratings[i+1]){
right[i] = right[i+1]+1;
}
}
int sum = 0;
for(int i=0; i<n; i++){
sum += max(left[i], right[i]);
}
return sum;
}
};
```

# Java Candy LeetCode Solution

```
``````
public int candy(int[] ratings) {
int [] candies=new int[ratings.length];
Arrays.fill(candies,1);
int back=ratings.length-1;
//Scanning left side ratings of each child
for(int i=1;i<ratings.length;i++)
{
if(ratings[i]>ratings[i-1])
{
candies[i]=candies[i-1]+1;
}
}
//Scanning right side ratings of each child
for(int i=ratings.length-2;i>=0;i--)
{
if(ratings[i]>ratings[i+1])
{
candies[i]=Math.max(candies[i],candies[i+1]+1);
}
}
int totalCandies=0;
//calculating total candies from candies Array
for(int candie:candies)
{
totalCandies+=candie;
}
return totalCandies;
}
```

# Python 3 Candy LeetCode Solution

```
``````
candies=[1]*len(ratings)
if len(ratings)==1:
return 1
for i in range(1,len(ratings)):
if ratings[i]>ratings[i-1] and candies[i]<=candies[i-1]:
candies[i]=candies[i-1]+1
for j in range(len(ratings)-2,-1,-1):
if ratings[j]>ratings[j+1] and candies[j]<=candies[j+1]:
candies[j]=candies[j+1]+1
return sum(candies)
```

Array-1180

String-562

Hash Table-412

Dynamic Programming-390

Math-368

Sorting-264

Greedy-257

Depth-First Search-256

Database-215

Breadth-First Search-200

Tree-195

Binary Search-191

Matrix-176

Binary Tree-160

Two Pointers-151

Bit Manipulation-140

Stack-133

Heap (Priority Queue)-117

Design-116

Graph-108

Simulation-103

Prefix Sum-96

Backtracking-92

Counting-86

Sliding Window-73

Linked List-69

Union Find-66

Ordered Set-48

Monotonic Stack-47

Recursion-43

Trie-41

Binary Search Tree-40

Divide and Conquer-40

Enumeration-39

Bitmask-37

Queue-33

Memoization-32

Topological Sort-31

Geometry-30

Segment Tree-27

Game Theory-24

Hash Function-24

Binary Indexed Tree-21

Interactive-18

Data Stream-17

String Matching-17

Rolling Hash-17

Shortest Path-16

Number Theory-16

Combinatorics-15

Randomized-12

Monotonic Queue-9

Iterator-9

Merge Sort-9

Concurrency-9

Doubly-Linked List-8

Brainteaser-8

Probability and Statistics-7

Quickselect-7

Bucket Sort-6

Suffix Array-6

Minimum Spanning Tree-5

Counting Sort-5

Shell-4

Line Sweep-4

Reservoir Sampling-4

Eulerian Circuit-3

Radix Sort-3

Strongly Connected Componen-t2

Rejection Sampling-2

Biconnected Component-1

## Leave a comment below