-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path135.candy.java
More file actions
35 lines (26 loc) · 984 Bytes
/
Copy path135.candy.java
File metadata and controls
35 lines (26 loc) · 984 Bytes
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
class Solution {
int MAX = (2*10*10*10*10)+1;
public int candy(int[] ratings) {
// int[] reverseIndex = new int[MAX];
PriorityQueue<int[]> pq = new PriorityQueue<int[]>((a,b)->a[0]-b[0]);
int i = 0;
int n = ratings.length;
int[] dp = new int[ratings.length];
while(i<n){
pq.offer(new int[]{ratings[i], i});
dp[i++] = 1;
}
while(!pq.isEmpty()){
int[] curr = pq.poll();
int index = curr[1];
if(index-1 >= 0 && ratings[index] > ratings[index-1] && dp[index] <= dp[index-1])
dp[index] = dp[index-1]+1;
if(index+1 < n && ratings[index] > ratings[index+1] && dp[index] <= dp[index+1])
dp[index] = dp[index+1]+1;
}
int res = 0;
for(i=0;i<n;i++)
res += dp[i];
return res;
}
}