Leetcode解题笔记-maxArea

题目要求:

给定一个数组a1,a2,a3,a4,ai...作为一个水桶的两侧,找出两个最大的元素,作为水桶的两边,求最大面积解

个人解法:

1.两个指针一个从后往前,一个从前往后,相减两边的距离之后乘以j与i的距离,得出结果与之前结果进行比较

2.然后如此暴力的解法超时了

参考解法:

这个方法比较灵巧,比较左右两边的边界,因为水的最大容量是短板决定的,所以比较之后则移动短板即可,每次移动重新计算面积与最大面积进行比较,得出结果

例如:

左边比右边小则移动左边的,右边比左边小则移动右边的。

相关代码:

public int maxArea(int[] height){
if(height==null) return 0;
int right = height.length-1;
int left = 0;
int MaxArea = 0;
while(left<right){
int high = Math.min(height[right], height[left]);
int Area = high*(right-left);
MaxArea = Math.max(MaxArea, Area);
if(height[left]<height[right]){
left++;
}else{
right--;
}
}
return MaxArea;
}

时间: 2024-08-11 09:57:18

Leetcode解题笔记-maxArea的相关文章

122. Best Time to Buy and Sell Stock(二) leetcode解题笔记

122. Best Time to Buy and Sell Stock II Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell on

121. Best Time to Buy and Sell Stock (一) leetcode解题笔记

121. Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algor

110.Balanced Binary Tree Leetcode解题笔记

110.Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1. 很早以前做的了  准

Leetcode解题笔记,basic calculator 1&amp;&amp;basic calculator 2

Basic Calculator1 题目内容: 实现一个带有加减以及括号功能的小计算器,其中输入的没用负数,而且输入的内容也全部是合法表达式. 个人分析: 1.利用stack解题,将得出的临时结果放入stack中 2.遇到括号的时候将result放入stack中 心得: 1. 对于加减可以直接利用符号进行操作,专门设置一个sign的变量去存这个符号 2.转化字符串中数字固定方法:number = number*10+s.charAt(i)-'0' 3. 对于这类需要对中间过程进行记录的可以利用s

Leetcode解题笔记-Contains Duplicate &amp;&amp; Contains Duplicate II&amp;&amp;Contain Duplicate III

Contain Duplicate 题目要求: 给定一个数组如果有重复就返回true,如果没有就返回false 个人分析: 这个题比剔除重复的要简单许多,只需要判断就好 代码如下: public static boolean containsDuplicate(int[] nums){ if (nums.length==0) return false; Arrays.sort(nums); for(int i =1; i < nums.length; i++){ if(nums[i-1]==nu

Leetcode解题笔记-Merge sorted Array

题目要求: 将两个排序好的nums1和nums2数组合并成为一个数组,nums1中有足够的空间,有m+n的空间,在nums1中有m个元素,在nums2中有n个元素 解题思路: 将nums2中的元素归并到nums1中,对于nums1以及nums2从后往前进行遍历,归并到nums1中, while(i>-1&&j>-1)nums1[k--]= (nums1[i--]>nums2[j--])?nums1[i--]:nums2[j--];

LeetCode解题笔记 - 20. Valid Parentheses

这星期听别人说在做LeetCode,让他分享一题来看看.试了感觉挺有意思,可以培养自己的思路,还能方便的查看优秀的解决方案.准备自己也开始. 解决方案通常有多种多样,我觉得把自己的解决思路记录下来,阶段性查看,一定能对自己有帮助. 这是我做的第一题,所以记录也从这题开始,之后尽力以简短的说明,描述出思路,方便以后能回顾到简介明了的记录. 20. Valid Parentheses Given a string containing just the characters '(', ')', '{

LeetCode解题笔记 - 3. Longest Substring Without Repeating Characters

Given a string, find the length of the longest substring without repeating characters. Examples: Given "abcabcbb", the answer is "abc", which the length is 3. Given "bbbbb", the answer is "b", with the length of 1.

leetcode解题笔记-Remove Duplicates from Sorted Array

题目要求: 去除数组中相重复的的元素,并且返回新数组的长度.(要求不能再申请额外空间,只能在原来数组上进行操作) 个人理解: 1. 数组定义之后就是定长的不能改变,只能在原来数组上进行操作,不能像链表一样根据指针进行操作 2. 与remove element相似,利用数组中后续的元素进行代替,将数组的一部分变为无重复且排序好的结果子数组: if(nums[i-1]!=nums[i]) nums[id++] = nums[i]; // 如果前一项的值不等于后一项,那么在id+1项的值就等于当前项,