-
-
Notifications
You must be signed in to change notification settings - Fork 110
/
Copy pathCandies.java
45 lines (42 loc) · 1.1 KB
/
Candies.java
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
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
static int n;
public static void main(String[] args) {
int i,arr[],temp=1,prev=0,count[];
long candies=0;
Scanner scan = new Scanner(System.in);
n = scan.nextInt();
arr = new int[n];
count = new int[n];
for(i=0;i<n;i++){
scan.nextLine();
arr[i] = scan.nextInt();
count[i] = 1;
}
for(i=1;i<n;i++){
if(arr[i]>arr[i-1]){
count[i] = count[i-1]+1;
}
/*
else if(i!=0 && arr[i]<arr[i-1]){
int j = i;
while(j>0 && arr[j] < arr[j-1]){
count[j-1] = Math.max(count[j-1],count[j] + 1);
j--;
}
}*/
}
for(i=n-2;i>=0;i--){
if(arr[i]>arr[i+1]){
count[i]=Math.max(count[i],count[i+1]+1);
}
}
for(i=0;i<n;i++)
candies += count[i];
System.out.print(candies);
}
}