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

首頁 > 學院 > 開發設計 > 正文

Trapping Rain Water

2019-11-11 07:23:02
字體:
來源:轉載
供稿:網友

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;}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 鄄城县| 民勤县| 大庆市| 通辽市| 松原市| 康马县| 教育| 嵩明县| 黔西| 长兴县| 广宁县| 台中县| 桐城市| 米林县| 林周县| 宁陵县| 栾川县| 海安县| 苗栗市| 漯河市| 开平市| 准格尔旗| 山东省| 称多县| 茌平县| 乌鲁木齐县| 陆良县| 酒泉市| 高碑店市| 武汉市| 衡阳县| 青川县| 年辖:市辖区| 阜平县| 文化| 南岸区| 拉萨市| 绥中县| 嘉义市| 昌吉市| 丰镇市|