【Container With Most Water】cpp

题目:

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.

代码:

class Solution {
public:
    int maxArea(vector<int>& height) {
            if ( height.size()<2 ) return 0;
            int max_area = 0;
            int left = 0;
            int right = height.size()-1;
            while ( left<right )
            {
                if ( height[left]<=height[right] )
                {
                    max_area = std::max(max_area, (right-left)*height[left]);
                    left++;
                }
                else
                {
                    max_area =  std::max(max_area, (right-left)*height[right]);
                    right--;
                }
            }
            return max_area;
    }
};

tips:

试图用DP去做,但是没想出来;最后无奈落入了Greedy的俗套solution。

这个greedy的思路也是蛮巧的:从两头开始往中间greedy,头尾两个greedy一起变化才得到greedy的条件。

时间: 2024-08-29 06:58:55

【Container With Most Water】cpp的相关文章

【Pascal&#39;s Triangle II 】cpp

题目: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3,3,1]. Note:Could you optimize your algorithm to use only O(k) extra space? 代码: class Solution { public: vector<int> getRow(int rowIndex) { vector

【Trapping Rain Water】cpp

题目: Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining. For example, Given [0,1,0,2,1,0,1,3,2,1,2,1], return 6. The above elevation map is represented

【Recover Binary Search Tree】cpp

题目: Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing its structure. Note:A solution using O(n) space is pretty straight forward. Could you devise a constant space solution? confused what "{1,#,2,3}&

【Length of Last Word】cpp

题目: Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string. If the last word does not exist, return 0. Note: A word is defined as a character sequence consists of non-space

【Linked List Cycle II】cpp

题目: Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Follow up:Can you solve it without using extra space? 代码: 使用hashmap版 /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNod

【Binary Tree Inorder Traversal】cpp

题目: Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 2 / 3 return [1,3,2]. Note: Recursive solution is trivial, could you do it iteratively? 代码: /** * Definition for a binary tree node

【Binary Tree Preorder Traversal】cpp

题目: Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 2 / 3 return [1,2,3]. Note: Recursive solution is trivial, could you do it iteratively? 代码: /** * Definition for a binary tree nod

【Validate Binary Search Tree】cpp

题目: Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes with

【Binary Search Tree Iterator 】cpp

题目: Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST. Calling next() will return the next smallest number in the BST. Note: next() and hasNext() should run in average O(1) time and u