-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path16.水桶装水.cpp
55 lines (52 loc) · 1.1 KB
/
16.水桶装水.cpp
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
#include <stdio.h>
#include <malloc.h>
int maxWater(int s[], int n)
{
int left = 0, max_left = 0;
int right = n - 1, max_right = n - 1;
int max_water = (n - 1) * (s[left] > s[right] ? s[right] : s[left]);
while (left < right)
{
if (s[left] >= s[right])
{
int temp = s[right];
while (left < right && temp >= s[right])
right--;
int water = (right - left) * (s[left] > s[right] ? s[right] : s[left]);
if (water > max_water)
{
max_water = water;
max_right = right;
max_left = left;
}
}
else if (s[left] < s[right])
{
int temp = s[left];
while (left < right && temp >= s[left])
left++;
int water = (right - left) * (s[left] > s[right] ? s[right] : s[left]);
if (water > max_water)
{
max_water = water;
max_right = right;
max_left = left;
}
}
}
printf_s("max_left:%d,max_right:%d\n", max_left + 1, max_right + 1);
return max_water;
}
int main()
{
int n;
printf_s("please input n\n");
scanf_s("%d", &n);
int* s = (int*)malloc(sizeof(int) * n);
for (int i(0); i < n; i++)
{
scanf_s("%d", s + i);
}
printf_s("%d\n", maxWater(s, n));
}
//