题目:
Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.
For example,
Given [0,1,0,2,1,0,1,3,2,1,2,1], return 6.
C代码
#include<stdio.h>
#include<stdlib.h>
#include<memory.h>
#include<assert.h>
#define max(a,b) (((a) > (b)) ? (a) : (b))
#define min(a,b) (((a) < (b)) ? (a) : (b))
/***********************************************************
* 函数名称: volume
* 功能描述:
* 参数定义: bar 指针
* len 长度
* 返回值 : 返回容量
***********************************************************/
int volume(int *bar, int len)
{
int volume = 0;
int height = 0;
int maxTemp = 0;
int *maxL = (int *)malloc(sizeof(int)*len);
int *maxR = (int *)malloc(sizeof(int)*len);
if (NULL == bar)
return 0;
if (NULL == maxL)
return 0;
if (NULL == maxR)
return 0;
memset(maxL, 0, sizeof(int)*len);
memset(maxR, 0, sizeof(int)*len);
if (len <= 2)
return 0;
/********从左到右扫描,找出每个坐标的最大左值********/
maxTemp = bar[0];
maxL[0] = 0;
for (int i = 1; i < len - 1; i++)
{
maxL[i] = maxTemp;
if (bar[i] > maxTemp)
{
maxTemp = bar[i];
}
}
/********从右到左扫描,找出每个坐标的最大右值********/
maxTemp = bar[len - 1];
maxR[len - 1] = 0;
for (int i = len - 2; i > 0; i--)
{
maxR[i] = maxTemp;
height = min(maxL[i], maxR[i]) - bar[i];
if (height > 0)
volume += height;
if (maxTemp < bar[i])
maxTemp = bar[i];
}
printf("bar[]= ");
for (int i = 0; i < len; i++)
{
printf("%d ",bar[i]);
}
printf("\n ");
printf("maxL[]= ");
for (int i = 0; i < len; i++)
{
printf("%d ", maxL[i]);
}
printf("\n ");
printf("maxR[]= ");
for (int i = 0; i < len; i++)
{
printf("%d ", maxR[i]);
}
printf("\n ");
free(maxL);
maxL = NULL;
free(maxR);
maxR = NULL;
return volume;
}
/***********************************************************
* 函数名称: Test_Volume
* 功能描述: 测试用例
* 参数定义:
***********************************************************/
void Test_Volume1()
{
int tBars[] = { 0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1 };
int tLen = sizeof(tBars) / sizeof(tBars[0]);
assert(6 == volume(tBars, tLen));
//printf("The pool size is %d !\n", volume(tBars, tLen));
}
void Test_Volume2()
{
int tBars[] = { 0, 1 };
int tLen = sizeof(tBars) / sizeof(tBars[0]);
assert(0 == volume(tBars, tLen));
//printf("The pool size is %d !\n", volume(tBars, tLen));
}
void Test_Volume3()
{
int tBars[] = { 1 };
int tLen = sizeof(tBars) / sizeof(tBars[0]);
assert(0 == volume(tBars, tLen));
//printf("The pool size is %d !\n", volume(tBars, tLen));
}
void Test_Volume4()
{
int tBars[] = { 5, 1, 0, 2, 5 };
int tLen = sizeof(tBars) / sizeof(tBars[0]);
assert(12 == volume(tBars, tLen));
//printf("The pool size is %d !\n", volume(tBars, tLen));
}
void Test_Volume5()
{
int tBars[] = { 1, 2, 3, 4, 5, 4, 3, 2, 1 };
int tLen = sizeof(tBars) / sizeof(tBars[0]);
assert(0 == volume(tBars, tLen));
//printf("The pool size is %d !\n", volume(tBars, tLen));
}
void Test_Volume6()
{
int tBars[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int tLen = sizeof(tBars) / sizeof(tBars[0]);
assert(0 == volume(tBars, tLen));
//printf("The pool size is %d !\n", volume(tBars, tLen));
}
void Test_Volume7()
{
int tBars[] = { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };
int tLen = sizeof(tBars) / sizeof(tBars[0]);
assert(0 == volume(tBars, tLen));
//printf("The pool size is %d !\n", volume(tBars, tLen));
}
/***********************************************************
* 函数名称: main
* 功能描述: 主函数
* 参数定义:
***********************************************************/
int main()
{
Test_Volume1();
Test_Volume2();
Test_Volume3();
Test_Volume4();
Test_Volume5();
Test_Volume6();
Test_Volume7();
return 0;
}