public int trap(int[] height) {
		int water = 0;
		if (height.length < 3) {
			return water;
		}
		
		// find max height and its position
		int maxHeight = 0;
		int maxHeightPos = 0;
		for (int i = 0; i < height.length; i++) {
			if (height[i] > maxHeight) {
				maxHeight = height[i];
				maxHeightPos = i;
			}
		}
		
		// calculate water before max height
		int preHeight = height[0];
		for (int i = 1; i < maxHeightPos; i++) {
			if (height[i] < preHeight) {
				water += preHeight - height[i];
			} else {
				preHeight = height[i];
			}
		}
		
		// calculate water after max height
		preHeight = height[height.length - 1];
		for (int i = height.length - 1; i > maxHeightPos; i--) {
			if (height[i] < preHeight) {
				water += preHeight - height[i];
			} else {
				preHeight = height[i];
			}
		}
		
		return water;
	}