LeetCode 热题100——day05-盛水最多的容器

LeetCode 热题100——day05-盛水最多的容器

题目

给定一个长度为 n 的整数数组 height 。有 n 条垂线,第 i 条线的两个端点是 (i, 0)(i, height[i])

找出其中的两条线,使得它们与 x 轴共同构成的容器可以容纳最多的水。

返回容器可以储存的最大水量。

说明:你不能倾斜容器。

题目要求

示例 1:

img

输入:[1,8,6,2,5,4,8,3,7]
输出:49 
解释:图中垂直线代表输入数组 [1,8,6,2,5,4,8,3,7]。在此情况下,容器能够容纳水(表示为蓝色部分)的最大值为 49

示例 2:

输入:height = [1,1]
输出:1

解题思路

先读题,这题是要求这个面积问题,面积公式就是:(右侧下标 - 左侧下标) * Math.min(右侧下标, 左侧下标)

思路

既然要求最大值,那么可以使用for的循环嵌套去做,定义一个变量装最大值max,外层for循环遍历数组,内层从外层的下一个元素继续去遍历,如果算出来的面积值大于max中装的最大值,那就将max重新赋值。这个思路看似可以去解决问题,但是这是暴力枚举法,时间复杂度相当高,都达到O(n^2)了,如果这个数据量非常庞大,指数级别的增长,真是不敢想象的时间复杂度啊。所以我们不要这样去做。

**换一个思路:**如果我们同时从左右两边开始,首先面积这个最大值就是进行比较取最大,这部分比较简单。

核心就是什么时候动指针,动那个?:就是,每次进行计算,当前最大和当前计算面积取最大放max,然后移动左右相对来说高度低的那侧的指针,因为能够确保去拿一个相对来说更高的高度,这样便能取最大到max中,因为取小的永远不会大过那个大的的面积。这样时间复杂度是不是直接将为O(n)了!

明白这点,来看解题1吧。

剪枝:要降低复杂度我们可以想剪枝操作,剪枝的条件很简单,就是取一个最小值,去装每次比较出来最低的那个数,因为如果新出来的数小于等于那个被去掉的小的数,那么还不如不算,因为x轴上的长度一直在减小,小于等于之前被比下去的数再乘当前这个数,肯定是要比最大值小的啊,即使是等于也会小(1*当前y轴最大的值)平方,所以这类数压根不用计算,直接剪枝就行。来看解题2吧。

解题答案

解题1:

public static void main(String[] args) {
    System.out.println(maxArea(new int[]{1,8,6,2,5,4,8,3,7}));
}

public static int maxArea(int[] height) {
    int max = 0;
    int left = 0;
    int right = height.length - 1;

    while (left < right) {
        int area = Math.min(height[left], height[right]) * (right - left);
        max = Math.max(max, area);

        if (height[left] < height[right]) {			// 移动相对小的那边的指针
            left++;
        } else {
            right--;
        }
    }
    return max;
}

解题2

public static void main(String[] args) {
    System.out.println(maxArea(new int[]{1,8,6,2,5,4,8,3,7}));
}

public static int maxArea(int[] height) {
        int max = 0;
        int minHeight = 0;
        int left = 0;
        int right = height.length - 1;

        while (left < right) {
            while (height[left] <= minHeight && left < right) {			// 剪枝 + 移动指针
                left ++;
            }
            while (height[right] <= minHeight && left < right) {		// 剪枝 + 移动指针
                right --;
            }
            minHeight = Math.min(height[left], height[right]);			// 取两边小的那个值,也就是低的那个边长为下一次剪枝的比较值
            max = Math.max(max, minHeight * (right - left));
        }
        return max;
}
  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. Two Sum 2. Add Two Numbers 3. Longest Substring Without Repeating Characters 4. Median of Two Sorted Arrays 5. Longest Palindromic Substring 6. ZigZag Conversion 7. Reverse Integer 8. String to Integer (atoi) 9. Palindrome Number 10. Regular Expression Matching 11. 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 18. 4Sum 19. Remove Nth Node From End of List 20. Valid Parentheses 21. Merge Two Sorted Lists 22. Generate Parentheses 23. Swap Nodes in Pairs 24. Reverse Nodes in k-Group 25. Remove Duplicates from Sorted Array 26. Remove Element 27. Implement strStr() 28. Divide Two Integers 29. Substring with Concatenation of All Words 30. Next Permutation 31. Longest Valid Parentheses 32. Search in Rotated Sorted Array 33. Search for a Range 34. Find First and Last Position of Element in Sorted Array 35. Valid Sudoku 36. Sudoku Solver 37. Count and Say 38. Combination Sum 39. Combination Sum II 40. First Missing Positive 41. Trapping Rain Water 42. Jump Game 43. Merge Intervals 44. Insert Interval 45. Unique Paths 46. Minimum Path Sum 47. Climbing Stairs 48. Permutations 49. Permutations II 50. Rotate Image 51. Group Anagrams 52. Pow(x, n) 53. Maximum Subarray 54. Spiral Matrix 55. Jump Game II 56. Merge k Sorted Lists 57. Insertion Sort List 58. Sort List 59. Largest Rectangle in Histogram 60. Valid Number 61. Word Search 62. Minimum Window Substring 63. Unique Binary Search Trees 64. Unique Binary Search Trees II 65. Interleaving String 66. Maximum Product Subarray 67. Binary Tree Inorder Traversal 68. Binary Tree Preorder Traversal 69. Binary Tree Postorder Traversal 70. Flatten Binary Tree to Linked List 71. Construct Binary Tree from Preorder and Inorder Traversal 72. Construct Binary Tree from Inorder and Postorder Traversal 73. Binary Tree Level Order Traversal 74. Binary Tree Zigzag Level Order Traversal 75. Convert Sorted Array to Binary Search Tree 76. Convert Sorted List to Binary Search Tree 77. Recover Binary Search Tree 78. Sum Root to Leaf Numbers 79. Path Sum 80. Path Sum II 81. Binary Tree Maximum Path Sum 82. Populating Next Right Pointers in Each Node 83. Populating Next Right Pointers in Each Node II 84. Reverse Linked List 85. Reverse Linked List II 86. Partition List 87. Rotate List 88. Remove Duplicates from Sorted List 89. Remove Duplicates from Sorted List II 90. Intersection of Two Linked Lists 91. Linked List Cycle 92. Linked List Cycle II 93. Reorder List 94. Binary Tree Upside Down 95. Binary Tree Right Side View 96. Palindrome Linked List 97. Convert Binary Search Tree to Sorted Doubly Linked List 98. Lowest Common Ancestor of a Binary Tree 99. Lowest Common Ancestor of a Binary Search Tree 100. Binary Tree Level Order Traversal II
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值