Container With Most Water

Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai).

n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0).

Find two lines, which together with x-axis forms a container,


such that the container contains the most water.

Note: You may not slant the container.

  1. /*************************************************************************
  2. > File Name: LeetCode011.c
  3. > Author: Juntaran
  4. > Mail: Jacinthmail@gmail.com
  5. > Created Time: Wed 27 Apr 2016 02:11:36 AM CST
  6. ************************************************************************/
  7.  
  8. /*************************************************************************
  9.  
  10. Container With Most Water
  11.  
  12. Given n non-negative integers a1, a2, ..., an,
  13.  
  14. where each represents a point at coordinate (i, ai).
  15.  
  16. n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0).
  17.  
  18. Find two lines, which together with x-axis forms a container,
  19.  
  20. such that the container contains the most water.
  21.  
  22. Note: You may not slant the container.
  23.  
  24. ************************************************************************/
  25.  
  26. #include <stdio.h>
  27.  
  28. int maxArea(int* height, int heightSize) {
  29.  
  30. int left = ;
  31. int right = heightSize - ;
  32. int max = ;
  33.  
  34. while( left < right ){
  35.  
  36. int water = ( right - left ) * ( height[left] < height[right] ? height[left++] : height[right--] );
  37. max = max > water ? max : water;
  38. printf("Each water is %d\n", water);
  39. }
  40. printf("Max water is %d\n", max);
  41. return max;
  42.  
  43. }
  44.  
  45. int main()
  46. {
  47. int heights[] = {, , , , , , , };
  48. int size = ;
  49.  
  50. maxArea( heights, size );
  51.  
  52. return ;
  53. }

LeetCode 11的更多相关文章

  1. LeetCode 11. Container With Most Water (装最多水的容器)

    Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai).  ...

  2. [LeetCode] 11. Container With Most Water 装最多水的容器

    Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). ...

  3. LeetCode 11 水池蓄水问题

    今天给大家分享的是一道LeetCode中等难度的题,难度不大,但是解法蛮有意思.我们一起来看题目: Link Container With Most Water Difficulty Medium 题 ...

  4. Java实现 LeetCode 11 盛最多水的容器

    11. 盛最多水的容器 给定 n 个非负整数 a1,a2,-,an,每个数代表坐标中的一个点 (i, ai) .在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0) ...

  5. 如何装最多的水? — leetcode 11. Container With Most Water

    炎炎夏日,还是呆在空调房里切切题吧. Container With Most Water,题意其实有点噱头,简化下就是,给一个数组,恩,就叫 height 吧,从中任选两项 i 和 j(i <= ...

  6. LeetCode——11. Container With Most Water

    一.题目链接:https://leetcode.com/problems/container-with-most-water/ 二.题目大意: 给定n个非负整数a1,a2....an:其中每一个整数对 ...

  7. LeetCode 11 Container With Most Water(分支​判断问题)

    题目链接 https://leetcode.com/problems/container-with-most-water/?tab=Description   Problem: 已知n条垂直于x轴的线 ...

  8. LeetCode(11)题解: Container With Most Water

    https://leetcode.com/problems/container-with-most-water/ 题目: Given n non-negative integers a1, a2, . ...

  9. leetcode 11. Container With Most Water 、42. Trapping Rain Water 、238. Product of Array Except Self 、407. Trapping Rain Water II

    11. Container With Most Water https://www.cnblogs.com/grandyang/p/4455109.html 用双指针向中间滑动,较小的高度就作为当前情 ...

随机推荐

  1. 第二百四十四、五天 how can I 坚持

    昨天忘了.不知咋忘的,加班加迷糊了? 昨天联调接口,又加班了,好歹基本调通了. 今天,下午,开会,有点被领导批的意思,不是批我,是批我们团队. 团队. 不懂自己. 这样做有意义嘛. 睡觉.好烦. 到底 ...

  2. Spring Named Parameters examples in SimpleJdbcTemplate

    In JdbcTemplate, SQL parameters are represented by a special placeholder "?" symbol and bi ...

  3. JavaIO(01)File类详解

    File类 file类中的主要方法和变量   常量: 表示路径的分割符:(windows) 作用:根据java可移植性的特点,编写路径一定要符合本地操作系统要求的分割符: public static ...

  4. Mysql常见报错解决方法

    一:登录报错 ERROR 1045 (28000): Access denied for user 'mysql'@'localhost' (using password: NO) mysql日志文件 ...

  5. UIImage 相关操作

    修改UIImage大小 修改UISlider的最大值和最小值图片的时候,发现需要修改图片的大小,否则会导致UISlider变形.目前苹果还不支持直接修改UIImage类的大小,只能修改UIImageV ...

  6. UIImageView旋转任意角度

    -(UIImageView *) makeRotation:(UIImageView *)image speedX:(float)X speedY:(float)Y { //    头文件中需要定义  ...

  7. C#中的ref和out的区别

    转载原地址 http://www.cnblogs.com/gjahead/archive/2008/02/28/1084871.html ref和out的区别在C# 中,既可以通过值也可以通过引用传递 ...

  8. django控制admin的model显示列表

    class goods(models.Model):    name = models.CharField(max_length=300)    price = models.IntegerField ...

  9. Codeforces Round #328 (Div. 2) D. Super M 虚树直径

    D. Super M Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/592/problem/D ...

  10. 在 C# 中加载自己编写的动态链接库

    一.发生的背景    在开发新项目中使用了新的语言开发 C# 和新的技术方案 WEB Service,但是在新项目中,一些旧的模块需要继续使用,一般是采用 C 或 C++ 或 Delphi 编写的,如 ...