-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAllocation Game
More file actions
73 lines (68 loc) · 1.97 KB
/
Allocation Game
File metadata and controls
73 lines (68 loc) · 1.97 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
63
64
65
66
67
68
69
70
71
72
73
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package solution;
import java.util.HashSet;
import java.util.Set;
/**
*
* @author junye.mao
*/
public class Solution {
public int candy(int[] ratings) {
if(ratings.length==1||ratings.length==0)return ratings.length;
//初始化,pre是前一个的发糖数量
int ans=0,pre=0;
boolean f=true;
int i=0;
while(i<ratings.length-1){
//上升处理
boolean u=false;
while((i<ratings.length-1)&&ratings[i]<ratings[i+1]){
ans+=pre+1;
pre++;
i++;
u=true;
}
//如果最初是上升趋势
if(u==true&&f==true){
ans+=pre+1;
pre++; u=false;
}
//相等情况的处理
int ping=0;
while((i<ratings.length-1)&&ratings[i]==ratings[i+1]){
f=false;
ping++;
//如果最初是相等的情况
if(i==0)ping++;
if(ratings.length==i+2){
i++;ans+=ping;
break;
}
if(ratings[i+1]!=ratings[i+2]){
ans+=ping;
pre=1;
}
i++;
}
//下降处理
int n=0;
boolean flag=false;
while((i<ratings.length-1)&&ratings[i]>ratings[i+1]){
n++;
i++;
flag=true;
f=false;
}
if(flag==true){
ans+=(n*n+n)/2;
if(n>=pre)ans+=n+1-pre;
pre=1;
}
}
return ans;
}
}