【LeetCode】011 Container With Most Water

题目:

Given n non-negative integers a1a2, ..., an, where each represents a point at coordinate (iai). n vertical lines are drawn such that the two endpoints of line i is at (iai) 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 and n is at least 2.

题解:

  暴力解即可,注意这个面积是长方形的,我刚开始犯傻给写成梯形了。。。两个思路:两个for循环遍历,结果就是TLE了。另一个就是头尾指针遍历法,时间复杂度降为O(n).以后遇到这种需要遍历的,要先想到头尾指针法,而不是两个for循环(其实也是双指针,只是位置不一样)。同时,与之前遇到的数组问题一样,小幅度的优化就是在大循环内就跳过重复项

Solution 1 (TLE)

class Solution {
public:
    int maxArea(vector<int>& height) {
        int area = 0, n = height.size();
        for(int i=0; i<n-1; i++) {
            for(int j=i+1; j<n; j++) {
                int new_area = (j-i)*min(height[i], height[j]);
                if(height[i]==0 || height[j]==0) new_area = 0;
                area = max(area, new_area);
            }
        }
        return area;
    }
};

  

Solution 2

class Solution {
public:
    int maxArea(vector<int>& height) {
        int area = 0, i = 0, j = height.size() - 1;
        while (i < j) {
            area = max(area, min(height[i], height[j]) * (j - i));
            height[i] < height[j] ? ++i : --j;
        }
        return area;
    }
};

Solution 3

class Solution {
public:
    int maxArea(vector<int>& height) {
        int area = 0;
        int i = 0, j = height.size() - 1;
        while (i < j) {
             int h = min(height[i], height[j]);
            area = max(area, (j - i) * h);
            while (height[i] <= h && i < j) i++;
            while (height[j] <= h && i < j) j--;
        }
        return area;
    }
}; 
时间: 2024-11-20 02:45:15

【LeetCode】011 Container With Most Water的相关文章

【LeetCode】11. Container With Most Water 解题报告

转载请注明出处:http://blog.csdn.net/crazy1235/article/details/51820937 Subject 出处:https://leetcode.com/problems/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

【LeetCode】11. 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 cont

【LeetCode】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 containe

LeetCode 011 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 con

【leetcode】Generate Parentheses

题目: 给定整数n,返回n对匹配的小括号字符串数组. For example, given n = 3, a solution set is: "((()))", "(()())", "(())()", "()(())", "()()()" 分析: 这种问题的模式是:1)问题的解有多个 ,2)每个解都是由多个有效的 "步骤" 组成的,3)变更以有解的某个或某些"步骤"

【LeetCode】Implement strStr()

Implement strStr() Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack. 标准KMP算法.可参考下文. http://blog.csdn.net/yaochunnian/article/details/7059486 核心思想在于求出模式串前缀与后缀中重复部分,将重复信息保存在n

【LeetCode】Add Two Numbers

You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. Input: (2 -> 4 -> 3) + (5 -> 6 ->

【LeetCode】Pascal&#39;s Triangle

Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 这题别想用通项公式做,n choose m里面的连乘必然溢出,老老实实逐层用定义做. class Solution { public: vector<vector<

【LeetCode】Copy List with Random Pointer

A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null. Return a deep copy of the list. 思路:第一遍正常复制链表,同时用哈希表保存链表中原始节点和新节点的对应关系,第二遍遍历链表的时候,再复制随机域. 这是一种典型的空间换时间的做法,n个节点,需要大小为O(n