标签:high Container area int height Water Most low container
题目描述:
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.
解这道题,首先要读懂题目。题目要求找到一组i,j,使得area = min(ai, aj) *(j - i)最大。这题我没解出来,直接贴出答案。
solution:
int maxArea(vector<int> &height) { int low = 0; int high = height.size() - 1; int area = 0; while (low < high) { area = max(area, min(height[low], height[high]) * (high - low)); height[low] < height[high] ? ++low : --high; } return area; }
原文链接:https://oj.leetcode.com/discuss/1074/anyone-who-has-a-o-n-algorithm
真有这么难想到吗?最近有点颓废,得振作起来,接着A题。
转载于:https://www.cnblogs.com/gattaca/p/4274155.html
标签:high,Container,area,int,height,Water,Most,low,container 来源: https://blog.csdn.net/weixin_34403693/article/details/93401722
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。