-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFloodDepth.cpp
More file actions
141 lines (134 loc) · 3.31 KB
/
FloodDepth.cpp
File metadata and controls
141 lines (134 loc) · 3.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include <algorithm>
#include <numeric>
#include <climits>
#include <stdlib.h>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &A) {
// write your code in C++14 (g++ 6.2.0)
//cout << "checking " << i << endl;
//differentiate
vector<int> dA(A.size()+1, 0);
int local_max_depth = 0;
int max_depth = 0;
int local_height = 0;
if(A.size() < 3)
{
return 0;
}
//first
if(A[1] < A[0])
{
dA[0] = A[0];
//cout << "found first peak" << endl;
}
/*else if((A[i-1]>A[i])&&(A[i+1] > A[i]))
{
dA[i] = -A[i];
}*/
else
{
dA[0] = 0;
}
if(dA[0] > 0)
{
if(dA[0] > local_height)
{
//cout << "starting local height" << endl;
local_height = dA[0];
if(local_max_depth > max_depth)
{
max_depth = local_max_depth;
}
local_max_depth = 0;
}
}
if((local_height - A[0]) > local_max_depth)
{
local_max_depth = (local_height - A[0]);
}
for(int i = 1;i < A.size();i++)
{
//cout << A[i-1] << " " << A[i] << " " << A[i+1] << endl;
if((A[i-1]<A[i])&&(A[i+1] < A[i]))
{
dA[i] = A[i];
}
/*else if((A[i-1]>A[i])&&(A[i+1] > A[i]))
{
dA[i] = -A[i];
}*/
else
{
dA[i] = 0;
}
if(dA[i] > 0)
{
if(dA[i] > local_height)
{
local_height = dA[i];
if(local_max_depth > max_depth)
{
//cout << "nex max depth" << local_max_depth << endl;
max_depth = local_max_depth;
}
local_max_depth = 0;
}
else
{
//local depth so far
if((local_max_depth - (local_height - A[i])) > max_depth)
{
max_depth = local_max_depth - (local_height - A[i]);
}
}
}
if((local_height - A[i]) > local_max_depth)
{
//cout << "should come here" << endl;
//cout << "new local depth " << (local_height - A[i]) << endl;
local_max_depth = (local_height - A[i]);
}
}
int j = A.size() - 1;
if((A[j-1]<A[j])&&(A[j+1] < A[j]))
{
//cout << A[j] << endl;
dA[j] = A[j];
}
/*else if((A[j-1]>A[j])&&(A[j+1] > A[j]))
{
dA[j] = -A[j];
}*/
else
{
dA[j] = 0;
}
if(dA[j] > 0)
{
//cout << "d" << endl;
if(dA[j] > local_height)
{
local_height = dA[j];
if(local_max_depth > max_depth)
{
max_depth = local_max_depth;
}
local_max_depth = 0;
}
else
{
local_max_depth = (local_max_depth) - (local_height - dA[j]);
if(local_max_depth > max_depth)
{
max_depth = local_max_depth;
}
}
}
if((local_height - A[j]) > local_max_depth)
{
//cout << "or here" << endl;
local_max_depth = (local_height - A[j]);
}
return max_depth;
}