-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcandy_2.cpp
More file actions
88 lines (73 loc) · 1.31 KB
/
Copy pathcandy_2.cpp
File metadata and controls
88 lines (73 loc) · 1.31 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include<iostream>
#include<vector>
using namespace std;
int sol(vector<int>&a)
{
/* int n=a.size();
vector<int>left(n,1);
vector<int>right(n,1);
int ans=0;
//left[0]=a[0];
for(int i=1;i<a.size();i++)
{
if(a[i]>a[i-1])
{
left[i]=left[i-1]+1;
}
else{
left[i]=1;
}
}
//right[n-1]=a[n-1];
for(int j=n-2;j>=0;j--)
{
if(a[j]>a[j+1])
{
right[j]=right[j+1]+1;
}
else{
right[j]=1;
}
}
for(int k=0;k<a.size();k++)
{
ans+=max(left[k],right[k]);
}
return ans;*/
int n=a.size();
int sum=1,i=1;
while(i<n)
{
if(a[i]==a[i-1])
{
sum+=1;
continue;
}
int peak=1;
while(i<n && a[i]>a[i-1])
{
peak++;
sum+=peak;
i++;
}
int down=1;
while(i<n && a[i]<a[i-1])
{
sum+=down;
down++;
i++;
}
if(peak>down)
{
sum+=peak-down;
i++;
}
}
return sum;
}
int main()
{
vector<int>a={1,0,2};
cout<<sol(a);
return 0;
}