-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCandy.java
More file actions
53 lines (46 loc) · 1.37 KB
/
Candy.java
File metadata and controls
53 lines (46 loc) · 1.37 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
//{ 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[]) {
// code here
int[] leftToRight = new int[N];
int[] rightToLeft = new int[N];
leftToRight[0] = 1;
rightToLeft[N-1] = 1;
int j = N-1;
for(int i = 1; i < N; i++) {
if(ratings[i] > ratings[i-1]) leftToRight[i] = leftToRight[i-1]+1;
else leftToRight[i] = 1;
}
for(int i = N-2; i >= 0; i--) {
if(ratings[i] > ratings[i+1]) rightToLeft[i] = rightToLeft[i+1]+1;
else rightToLeft[i] = 1;
}
int cnt = 0;
for(int i = 0; i < N; i++)
cnt += Math.max(leftToRight[i], rightToLeft[i]);
return cnt;
}
}