-
Notifications
You must be signed in to change notification settings - Fork 2
/
question26.c
110 lines (92 loc) · 2.42 KB
/
question26.c
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
/*
Given n non negative integers representing an elevation map where width of each bar is 1.
Find the amt. of water that is trapped in between these bars after raining
METHOD1:
Find the highest bar to the left and right of each bar including that bar.
Then take the minimum of those two and subtract it from the height of the bar. That is the amount
of water it will store for itself. Repeat for each bar.
Time Complexity: O(n^2)
Space Complexity: O(1)
METHOD2:
Find the highest bar to the left of each bar including that bar and store it in an array (left).
Repeat the above exercise for right and store in array right.
Now water it can hold will be min(left[i],right[i])-height of ith index. Find the sum of all.
Time complexity: O(n)
Space complexity: O(n)
*/
//METHOD1
#include <stdio.h>
#include <stdlib.h>
int findMin(int a, int b){
return (a<b)?a:b;
}
int findMaxLeft(int arr[], int index){
int max = arr[index];
for(int i=index-1; i>=0;i--){
if(arr[i] > max){
max = arr[i];
}
}
return max;
}
int findMaxRight(int arr[], int index, int size){
int max = arr[index];
for(int i=index; i<size; i++){
if(max < arr[i]){
max = arr[i];
}
}
return max;
}
int main(){
int arr[] = {1,0,2,0,1,3,1,0,2};
int size = sizeof(arr)/sizeof(arr[0]);
int sum = 0, maxLeft, maxRight, min;
for(int i=1; i<size; i++){
maxLeft = findMaxLeft(arr,i);
maxRight = findMaxRight(arr,i, size);
min = findMin(maxLeft, maxRight);
sum += min-arr[i];
}
printf("the max amount of water it can store is %d\n", sum);
}
//METHOD2
#include <stdio.h>
#include <stdlib.h>
int findMin(int a, int b){
return (a<b)?a:b;
}
void computeLeftMax(int arr[], int compute[],int size){
int curr_max = compute[0] = arr[0];
for(int i=1; i<size;i++){
if(curr_max >= arr[i]){
compute[i] = curr_max;
}else{
compute[i] = arr[i];
curr_max = arr[i];
}
}
}
void computeRightMax(int arr[],int compute[], int size){
int curr_max = compute[size-1] = arr[size-1];
for(int i=size-2;i>=0;i--){
if(curr_max >= arr[i]){
compute[i] = curr_max;
}else{
compute[i] = arr[i];
curr_max = arr[i];
}
}
}
int main(){
int arr[] = {1,0,2,0,1,3,1,0,2};
int size = sizeof(arr)/sizeof(arr[0]);
int leftMax[size],rightMax[size];
computeLeftMax(arr,leftMax, size);
computeRightMax(arr,rightMax, size);
int sum = 0;
for(int i=0; i<size;i++){
sum += findMin(leftMax[i],rightMax[i]) - arr[i];
}
printf("the max amount of water it can store is %d\n", sum);
}