-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a0e3517
commit 0cb490a
Showing
4 changed files
with
200 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
## GFG Problem Of The Day | ||
|
||
### Date - 21 December, 2023 | ||
### Ques - [Candy](https://www.geeksforgeeks.org/problems/candy/1) | ||
![](https://badgen.net/badge/Level/Medium/yellow) | ||
|
||
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 atleast one candy. | ||
- Children with a higher rating than its neighbors get more candies than their neighbors. | ||
Return the minimum number of candies you need to have to distribute. | ||
|
||
### Approach Used | ||
**Initialization:** Check if the number of kids N is zero. If true, return 0 candies. | ||
|
||
**Candies Array:** Initialize an array candies to store the number of candies each kid receives. Initially, set each kid's candies to 1. | ||
|
||
**Traverse Left to Right:** Traverse the ratings array from left to right. If the current kid's rating is higher than the previous one, update the candies accordingly. | ||
|
||
**Traverse Right to Left:** Traverse the ratings array from right to left. If the current kid's rating is higher than the next one, update the candies, taking the maximum of the current value and the updated value. | ||
|
||
**Total Candies:** Calculate the total number of candies needed by summing up the values in the candies array. | ||
|
||
**Return:** Return the total number of candies. | ||
|
||
### Time and Auxiliary Space Complexity | ||
|
||
- **Time Complexity :** `O(N)` | ||
- **Auxiliary Space Complexity :** `O(N)`, because of array to store number of candies | ||
|
||
### Code (C++) | ||
```cpp | ||
class Solution { | ||
public: | ||
int minCandy(int N, vector<int> &ratings) { | ||
vector<int> v(N,1); | ||
for(int i=1;i<N;i++) | ||
{ | ||
if(ratings[i]>ratings[i-1]) | ||
v[i]=v[i-1]+1; | ||
} | ||
|
||
for(int i=N-2;i>=0;i--) | ||
{ | ||
if(ratings[i]>ratings[i+1]) | ||
v[i]=max(v[i],v[i+1]+1); | ||
} | ||
|
||
int sum=0; | ||
for(int i=0;i<N;i++) | ||
sum+=v[i]; | ||
|
||
return sum; | ||
} | ||
}; | ||
``` | ||
### Contribution and Support | ||
If you have a better solution or any queries / discussions , please visit our [discussion section](https://github.com/SHRbharat/gfg_potd/discussions). | ||
If you find this solution helpful, consider supporting us by giving a `⭐ star` to the [SHRbharat/gfg_potd](https://github.com/SHRbharat/gfg_potd) repository. | ||
![Total number of repository visitors](https://komarev.com/ghpvc/?username=SHRbharat&color=blueviolet&&label=Visitors) | ||
[![Total number of repository stars](https://img.shields.io/github/stars/SHRbharat/gfg_potd.svg)](https://github.com/SHRbharat/gfg_potd/stargazers) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
//{ Driver Code Starts | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
// } Driver Code Ends | ||
|
||
class Solution { | ||
public: | ||
int minCandy(int N, vector<int> &ratings) { | ||
vector<int> v(N,1); | ||
for(int i=1;i<N;i++) | ||
{ | ||
if(ratings[i]>ratings[i-1]) | ||
v[i]=v[i-1]+1; | ||
} | ||
|
||
for(int i=N-2;i>=0;i--) | ||
{ | ||
if(ratings[i]>ratings[i+1]) | ||
v[i]=max(v[i],v[i+1]+1); | ||
} | ||
|
||
int sum=0; | ||
for(int i=0;i<N;i++) | ||
sum+=v[i]; | ||
|
||
return sum; | ||
} | ||
}; | ||
|
||
//{ Driver Code Starts. | ||
|
||
int main() { | ||
int t; | ||
cin >> t; | ||
while (t--) { | ||
int N; | ||
cin >> N; | ||
|
||
vector<int> ratings(N); | ||
for (int i = 0; i < N; i++) { | ||
cin >> ratings[i]; | ||
} | ||
Solution obj; | ||
cout << obj.minCandy(N, ratings) << endl; | ||
} | ||
return 0; | ||
} | ||
// } Driver Code Ends |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
//{ Driver Code Starts | ||
// Initial Template for Java | ||
|
||
import java.util.*; | ||
import java.lang.*; | ||
import java.math.*; | ||
import java.io.*; | ||
|
||
class GFG { | ||
public static void main(String[] args) throws IOException { | ||
Scanner sc = new Scanner(System.in); | ||
int T = sc.nextInt(); | ||
while (T-- > 0) { | ||
int n = sc.nextInt(); | ||
int a[] = new int[n]; | ||
for (int i = 0; i < n; i++) { | ||
a[i] = sc.nextInt(); | ||
} | ||
Solution obj = new Solution(); | ||
int ans = obj.minCandy(n, a); | ||
System.out.println(ans); | ||
} | ||
} | ||
} | ||
|
||
// } Driver Code Ends | ||
|
||
// User function Template for Java | ||
class Solution { | ||
static int minCandy(int N, int ratings[]) { | ||
int candy[] = new int[N]; | ||
Arrays.fill(candy , 1); | ||
|
||
//left to right | ||
for(int i = 1 ; i < N ; i++){ | ||
if(ratings[i] > ratings[i-1]){ | ||
candy[i] = candy[i-1] + 1; | ||
} | ||
} | ||
|
||
//right to left | ||
for(int i = N-2 ; i >= 0 ; i--){ | ||
if(ratings[i] > ratings[i+1]){ | ||
candy[i] = Math.max(candy[i+1] + 1 , candy[i]); | ||
} | ||
} | ||
|
||
int count = 0; | ||
for(int i = 0 ; i < N ; i++){ | ||
count += candy[i]; | ||
} | ||
return count; | ||
} | ||
} |