有趣的积水问题(Twitter编程面试题)

以下内容来自转载:

Twitter面试题:水沟积水问题

问题描述:“看下面这个图片”

“在这个图片里我们有不同高度的墙。这个图片由一个整数数组所代表,数组中每个数是墙的高度。上边的图可以表示为数组[2,5,1,2,3,4,7,7,6]”

“假如开始下雨了,那么墙之间的水坑能够装多少水呢?”

思路分析: 一种只需要一次遍历的方法(注:不知道算不算动态规划算法,请知道的高人鉴定一下)

1. 首先,用两个指针(left 和 right)分别指向数组的第一个元素和最后一个元素,左指针从左向右遍历,右指针从右向左遍历;

2. 初始化数组中一个元素(a[0])为左边遍历得到的最大值(max_left),最后一个元素(a[a.length-1])为从右边遍历得到的最大值(max_right);

3. 开始遍历,遍历条件为左指针小于右指针

4. 如果左边遍历的最大值小于右边遍历的最大值,说明只要有水沟(即小于左边最大值max_left的元素)就会有积水,因为右边的最大值可以保证左边水沟的积水不会流失掉

同样,如果左边遍历的最大值不小于右边遍历的最大值,只要右边有水沟(即小于右边最大值max_right的元素)就会有积水。


解决方案:Java实现代码

[java]  view plain  copy
  1. public class Twitter_puddle {  
  2.     public int caculate(int[] a) {  
  3.         if (a == nullreturn 0;  
  4.         int left = 0;  
  5.         int right = a.length - 1;  
  6.         int max_left = a[left];  
  7.         int max_right = a[right];  
  8.         int volume = 0;  
  9.           
  10.         while (left < right) {  
  11.             if (max_left < max_right) {  
  12.                 left++;  
  13.                 if (max_left < a[left]) {  
  14.                     max_left = a[left];  
  15.                 }  
  16.                 else {  
  17.                     volume += max_left - a[left];  
  18.                 }  
  19.             }  
  20.             else {  
  21.                 right--;  
  22.                 if (max_right < a[right]) {  
  23.                     max_right = a[right];  
  24.                 }  
  25.                 else {  
  26.                     volume += max_right - a[right];  
  27.                 }  
  28.             }  
  29.         }  
  30.         return volume;  
  31.     }  
  32. }  

测试代码

[java]  view plain  copy
  1. import static org.junit.Assert.*;  
  2.   
  3. import org.junit.Before;  
  4. import org.junit.Test;  
  5. import static org.hamcrest.Matchers.*;  
  6.   
  7. public class Twitter_puddleTest {  
  8.   
  9.     private Twitter_puddle puddle;  
  10.       
  11.     @Before  
  12.     public void before() {  
  13.         puddle = new Twitter_puddle();  
  14.     }  
  15.       
  16.     @Test  
  17.     public void testNull() {  
  18.         assertThat(puddle.caculate(null), is(0));  
  19.     }  
  20.   
  21.     @Test  
  22.     public void testOne() {  
  23.         int a[] = {1};  
  24.         assertThat(puddle.caculate(a), is(0));  
  25.     }  
  26.       
  27.     @Test  
  28.     public void testTwoRightHigher() {  
  29.         int a[] = {12};  
  30.         assertThat(puddle.caculate(a), is(0));  
  31.     }  
  32.       
  33.     @Test  
  34.     public void testTwoLeftHigher() {  
  35.         int a[] = {21};  
  36.         assertThat(puddle.caculate(a), is(0));  
  37.     }  
  38.       
  39.     @Test  
  40.     public void testTwoSameHight() {  
  41.         int a[] = {11};  
  42.         assertThat(puddle.caculate(a), is(0));  
  43.     }  
  44.       
  45.     @Test  
  46.     public void testThreeMiddleHigher() {  
  47.         int a[] = {121};  
  48.         assertThat(puddle.caculate(a), is(0));  
  49.     }  
  50.       
  51.     @Test  
  52.     public void testThreeMiddleLower() {  
  53.         int a[] = {212};  
  54.         assertThat(puddle.caculate(a), is(1));  
  55.     }  
  56.       
  57.     @Test  
  58.     public void testThreeSameHeight() {  
  59.         int a[] = {111};  
  60.         assertThat(puddle.caculate(a), is(0));  
  61.     }  
  62.       
  63.     @Test  
  64.     public void testRandom1() {  
  65.         int a[] = {251234776};  
  66.         assertThat(puddle.caculate(a), is(10));  
  67.     }  
  68.       
  69.     @Test  
  70.     public void testRandom2() {  
  71.         int a[] = {2513121776};  
  72.         assertThat(puddle.caculate(a), is(17));  
  73.     }  
  74.       
  75.     @Test  
  76.     public void testRandom3() {  
  77.         int a[] = {614675164};  
  78.         assertThat(puddle.caculate(a), is(13));  
  79.     }  
  80.       
  81.     @Test  
  82.     public void testRandom4() {  
  83.         int a[] = {666666666};  
  84.         assertThat(puddle.caculate(a), is(0));  
  85.     }  
  86. }  


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值