-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathelevation_map_problem.cpp
More file actions
104 lines (82 loc) · 2.64 KB
/
elevation_map_problem.cpp
File metadata and controls
104 lines (82 loc) · 2.64 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
#include <bits/stdc++.h>
using namespace std;
int total_area = 0;
int find_largest_index(int arr[], int low, int high)
{
int max = arr[low];
int max_index = low;
for (int i = low+1; i <= high; ++i)
{
if(max < arr[i]){
max = arr[i];
max_index = i;
}
}
return max_index;
}
int area_of_trapped_water(int arr[], int largest_index, int second_largest_index)
{
int est_area = arr[second_largest_index] * (abs(largest_index - second_largest_index) + 1);
int act_area = est_area - 2 * (arr[second_largest_index]);
int low = largest_index, high = second_largest_index;
if(low > high)
{
high = largest_index;
low = second_largest_index;
}
for (int i = low+1; i < high; ++i)
{
act_area = act_area - arr[i];
}
cout << "expected area: "<<est_area<<", actual area: "<<act_area<<endl;
return act_area;
}
void find_trapped_water(int arr[], int low, int high, int num)
{
if(low == high) return ;
cout << "low: "<<low<<", high: "<<high<<endl;
int left_largest_index=-1, right_largest_index=num;
int largest_index = find_largest_index(arr, low, high);
cout << "largest_index: "<<largest_index<<endl;
// The 2 below blocks check if the largest index is at the low value or high
// i.e. is the largest pillar on the either side of the sub-array
//if we have reached one end of the sub-array, we don't need to find out beyond that
//i.e. if it is on left side, we don't need to concern for it's left side beyond
// similarly, for right side.
if(largest_index > low)
left_largest_index = find_largest_index(arr, low, largest_index-1);
if(largest_index < high)
right_largest_index = find_largest_index(arr, largest_index+1, high);
cout << "left_largest_index: "<<left_largest_index<<", right_largest_index: "<<right_largest_index<<endl;
if(left_largest_index >= low)
{
total_area = total_area + area_of_trapped_water(arr, largest_index, left_largest_index);
cout << "lft total_area: " << total_area << endl;
find_trapped_water(arr, 0, left_largest_index, num);
}
if(right_largest_index <= high)
{
total_area = total_area + area_of_trapped_water(arr, largest_index, right_largest_index);
cout << "rt total_area: " << total_area << endl;
find_trapped_water(arr, right_largest_index, num-1, num);
}
}
int main()
{
int n;
int arr[3] = {2, 1, 2};
// cin >> n;
// int arr[n];
// for (int i = 0; i < n; ++i)
// {
// arr[i] = rand()%10 + 1;
// }
// for (int i = 0; i < n; ++i)
// {
// cout << arr[i] << endl;
// }
// int arr[12] = {1, 0, 2, 1, 0, 4, 0, 2, 0, 3, 0, 2};
find_trapped_water(arr, 0, 2, 3);
cout << total_area << endl;
return 0;
}