国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設計 > 正文

Trapping Rain Water

2019-11-11 06:11:44
字體:
供稿:網(wǎng)友

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.

The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped.Thanks Marcos for contributing this image!

int trap(int A[], int n){	int left[n];	int right[n];	int leftMax = 0;	for (int i = 0; i < n; i++)	{		left[i] = leftMax;		leftMax = max(leftMax, A[i]);	}	int rightMax = 0;	for (int i = n-1; i >= 0; i--)	{		right[i] = rightMax;		rightMax = max(rightMax, A[i]);	}	int result = 0;	for (int i = 0; i < n; i++)	{		int temp = min(left[i], right[i]) - A[i];		if (temp > 0)		{			result += temp;		}	}	return result;}


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 上林县| 东宁县| 丰镇市| 藁城市| 恩施市| 贡山| 岢岚县| 正定县| 肥城市| 兴安盟| 扎囊县| 汉阴县| 齐齐哈尔市| 股票| 台南市| 安达市| 仙居县| 扬州市| 平顺县| 公安县| 都昌县| 巨鹿县| 凤台县| 威信县| 永嘉县| 西安市| 运城市| 东城区| 潜江市| 府谷县| 广丰县| 通州市| 大同市| 高碑店市| 长汀县| 当涂县| 青岛市| 神木县| 侯马市| 巢湖市| 昌乐县|