-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCandy Distribute Problem.cpp
More file actions
62 lines (55 loc) · 1.75 KB
/
Copy pathCandy Distribute Problem.cpp
File metadata and controls
62 lines (55 loc) · 1.75 KB
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
// problem statement link - https://leetcode.com/problems/candy/
// Brute Force
// Time : O(n²); Space : O(n)
class Solution {
public:
int candy(vector<int>& ratings) {
int n= ratings.size();
vector<int> candies(n,1);
bool flag = true;
while (flag)
{
flag = false;
for (int i = 0; i<ratings.size(); i++)
{
if (i != ratings.size() - 1 && ratings[i] > ratings[i + 1] && candies[i] <= candies[i + 1]) {
candies[i] = candies[i + 1] + 1;
flag = true;
}
if (i > 0 && ratings[i] > ratings[i - 1] && candies[i] <= candies[i - 1]) {
candies[i] = candies[i - 1] + 1;
flag = true;
}
}
}
int sum = 0;
for (int i = 0; i < ratings.size(); i++)
{
sum += candies[i];
}
return sum;
}
};
// Greedy Approach
// Time : O(n); Space : O(n)
class Solution {
public:
int candy(vector<int>& ratings) {
int n = ratings.size();
vector<int> L2R(n,1);
// considering only left neighbours
for (int i = 1; i < n; i++)
if (ratings[i] > ratings[i-1])
L2R[i] = L2R[i-1]+1;
vector<int> R2L(n,1);
// considering only right neighbours
for (int i = n-2; i >= 0; i--)
if (ratings[i] > ratings[i+1])
R2L[i] = R2L[i+1]+1;
int sum = 0;
// considering both neighbours
for (int i = 0; i < n; i++)
sum += max(L2R[i], R2L[i]);
return sum;
}
};