-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathZigzag_Array.cpp
More file actions
57 lines (50 loc) · 1.15 KB
/
Zigzag_Array.cpp
File metadata and controls
57 lines (50 loc) · 1.15 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
#include <bits/stdc++.h>
using namespace std;
int streak;
void getStreak(vector<int> a, int i, bool increasing) {
//cout << streak << " ";
if (i >= a.size() - 1) {
return;
}
if (increasing) {
if (a[i+1] > a[i]) {
streak++;
getStreak(a,i+1,true);
}
} else {
if (a[i+1] < a[i]) {
streak++;
getStreak(a,i+1,false);
}
}
}
int minimumDeletions(vector < int > a){
// Complete this function
int minDel = 0;
bool increasing;
for (int i = 0; i < a.size() - 1; i++) {
//cout << "i=" << i << ": ";
streak = 1;
if (a[i+1] > a[i]) {
getStreak(a,i,true);
} else {
getStreak(a,i,false);
}
i += streak - 2;
//cout << endl;
minDel += streak - 2;
}
return minDel;
}
int main() {
int n;
cin >> n;
vector<int> a(n);
for(int a_i = 0; a_i < n; a_i++){
cin >> a[a_i];
}
// Return the minimum number of elements to delete to make the array zigzag
int result = minimumDeletions(a);
cout << result << endl;
return 0;
}