新博文地址:[leetcode]Container With Most Water
http://oj.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 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.
Note: You may not slant the container.
第一反应肯定是暴力,时间复杂度为n^2,毫无意外的超时了,leetcode的case 的“ 长度”还是蛮变态的。双指针在leetcode中的应用还是蛮多的。
public int maxArea(int[] height) { int left = 0; int right = height.length - 1; int max = 0; while(left < right){ int high = height[left] > height[right] ? height[right] : height[left]; int area = (right - left) * high; if(area > max){ max = area; }//短板效应,只有挪动短板才可能“增大”面积 if(height[right]> height[left]){ left++; }else{ right--; } } return max; }
相关推荐
【LeetCode】Container With Most Water 是一道经典的计算机算法题,主要涉及到数组操作和动态规划的优化技巧。题目要求在给定的一组非负整数坐标中找到两条线,使得它们与x轴形成的容器能容纳最多的水。问题的核心...
在LeetCode上的问题"Container With Most Water"是一个经典的计算机科学问题,主要涉及到算法设计和优化。这个问题要求我们找到两个垂直线,它们与x轴形成一个容器,使得该容器能容纳最多的水。这个问题可以看作是二...
c c语言_leetcode 0011_container_with_most_water.zip
java入门 java_leetcode题解之011_Container_With_Most_Water
js js_leetcode题解之11-container-with-most-water.js
c语言入门 C语言_leetcode题解之11-container-with-most-water.c
Container With Most Water LeetCode 19 Remove Nth Node From End of List LeetCode 42 Trapping Rain Water LeetCode 61 RotateList LeetCode 75 Sort Colors LeetCode 125 Valid Palindrome LeetCode 167 Two Sum...
Container with Most Water" 是一个编程问题,源自LeetCode等在线编程平台的挑战题目。这个题目要求我们用JavaScript编写算法来解决一个关于找到两个竖直木板形成的容器能容纳的最大水量的问题。 【描述】在实际的...
11. Container With Most Water 13. Roman to Integer 15. 3Sum 16. 3Sum Closest 17. Letter Combinations of a Phone Number 18. 4Sum 19. Remove Nth Node From End of List 20. Valid Parentheses 21. Merge Two...
Container with Most Water 14 Longest Common Prefix 15 Three Sum 16 Three Sum Closest 20 Valid Parentheses 26 Remove Duplicates from Sorted Array 48 Rotate Image 53 Maximum Subarray 55 Jump Game 56 ...
Container With Most Water 盛最多水的容器 13 Roman to Integer 罗马数字转整数 14 Longest Common Prefix 最长公共前缀 20 Valid Parentheses 有效的括号 26 Remove Duplicates from Sorted Array 删除排序数组中...
with Ruby 13. Roman to Integer 查表,通过从前往前筛选字符串,把代表的值一个个加起来 26. Remove Duplicates from Sorted Array 难度easy的题目。根据题目要求,是不能新建数组。只能在原来的基础上做修改。基本...
Container With Most Water 12. Integer to Roman 13. Roman to Integer 14. Longest Common Prefix 15. 3Sum 20. Valid Parentheses 21. Merge Two Sorted Lists 22. Generate Parentheses 25. Reverse Nodes in k-...
Container With Most Water 中等 12 Integer to Roman 中等 13 Roman to Integer 简单 14 Longest Common Prefix 简单 15 3Sum 中等 16 3Sum Closest 中等 17 Letter Combinations of a Phone Number DFS 中等 18 4...
leetcode双人赛LeetCode 编号 题目 难度 题型 备注 Two Sum 简单 Add Two Numbers 中等 链结串列 重要 Longest Substring Without Repeating Characters 中等 字串 重要 Median of Two Sorted Arrays 困难 数学 ...
LeetCode刷题总结 1.Two Sum 2.Add Two Numbers 3.Longest Substring Without Repeating Characters 4.Median of Two Sorted Arrays 5.Longest Palindromic Substring (Manacher算法待完成) 6.ZigZag Conversion 7....
分割数组求最大差值leetcode LeetCode 学习之路 记录自己完成LeetCode的代码和结果。 序号 中文名称 英文名称 ...Container With Most Water 59.3% 中等 12 Integer to Roman 61.8% 中等 13 Roman to In
Leetcode回文串拼接 ...11.Container With Most Water 12.Integer To Roman 13.Roman To Integer 289 347 380 442 457 Circular Array Loop 535 Encode and Decode TinyURL 560 565 566 Maximum Binary T
Container With Most Water 012 Integer to Roman 013 Roman to Integer 014 Longest Common Prefix 015 3Sum 016 3Sum Closest 017 Letter Combinations of a Phone Number 018 4Sum 020 Valid Parentheses 022 ...