forked from alexprut/HackerRank
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution.java
50 lines (42 loc) · 1.23 KB
/
Solution.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
46
47
48
49
50
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
static BigInteger candies(int n, int[] arr) {
int[] cache = new int[arr.length];
cache[0] = 1;
for (int i = 1; i < arr.length; i++) {
if (arr[i-1] < arr[i]) {
cache[i] = cache[i-1] + 1;
}
if (arr[i-1] >= arr[i]) {
cache[i] = 1;
}
}
for (int i = arr.length - 2; i >= 0; i--) {
if (arr[i] > arr[i+1]) {
if (cache[i] <= cache[i+1]) {
cache[i] = cache[i+1] + 1;
}
}
}
BigInteger sum = BigInteger.valueOf(0);
for (int i = 0; i < cache.length; i++) {
sum = sum.add(BigInteger.valueOf(cache[i]));
}
return sum;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] arr = new int[n];
for(int arr_i = 0; arr_i < n; arr_i++){
arr[arr_i] = in.nextInt();
}
BigInteger result = candies(n, arr);
System.out.println(result);
in.close();
}
}