Question:
My code:
public class Solution {
public int maxArea(int[] height) {
if (height == null || height.length == 0 || height.length == 1)
return 0;
if (height.length == 2)
return Math.min(height[0], height[1]);
int max = 0;
int currArea = 0;
int head = 0;
int tail = height.length - 1;
while (head < tail) {
if (height[head] < height[tail]) {
currArea = height[head] * (tail - head);
head++;
}
else {
currArea = height[tail] * (tail - head);
tail--;
}
if (max < currArea)
max = currArea;
}
return max;
}
public static void main(String[] args) {
Solution test = new Solution();
int[] height = {0,14,6,2,10,9,4,1,10,3};
System.out.println(test.maxArea(height));
}
}
My test result:
這次作業做得太曲折了,所以說,一道題目的開始思路不能錯,錯了后面會越來越麻煩了。
我一開始想起了那道直方圖的題目,以為差不多。于是先寫了出來,然后那個時候腦子已經亂了。然后發現不一樣,直方圖必須是連續性的構成area,而這個水桶不需要,可以隔幾個小的然后構成area。
于是我開始畫圖,構建一個模型。模型最后千辛萬苦做了出來,花了許多圖。然后自以為對的,一個簡單的例子就讓我發現我的模型是那么的復雜,而且是那么的無效。
于是放棄。然后網上看了下解法。
瞬間吐血。
首尾分別設一個指針。然后計算構成的面積。同時,如果左邊的小于右邊,那么左邊指針右移。反之,右邊指針左移。以此循環,直到最后兩個指針相遇。
然后最大面積就得出來了。
具體見這個網頁。
http://blog.csdn.net/ljiabin/article/details/41673753
其實通過這種形式,直接排除掉了許多無意義的情況。
所以這道題目得事先想好,兩個棒子,一根長一根短,那么應該怎么計算面積。然后如果加上他們左右的棒子,復雜起來,又如何。
**
總結:我的思維還是太復雜。主要一開始想復雜了。然后后面跟著就都不對了。
還有,真他媽后悔用簡書來寫博客。編輯能力太差了。傳個圖片上來,應該傳不了,要傳兩次。。。哎,找個時間移植到CSDN上去了。
**
現在是凌晨了吧。準確的說,昨天是我的生日。然后也就是在看看小說,寫寫代碼中度過了。
本來想寫一篇熱血點的日志的,后來沒啥激情了,就算了。
然后又大了一歲。今年的這個生日我還是很看重的。因為我覺得這是一個時間點,區分了兩種狀態。
1.我對自己的理解以及對未來的規劃。
2.我對父母的認識。
3.我和我女朋友的關系。
我覺得現在的我有一個很大的壞毛病。口是心非。
但是,有些事,的確自己心里明白就好了。嘴上的我,應該是,隨意胡侃,嘻嘻哈哈的我。我喜歡在別人心里留下這樣的印象。
Anyway, Good luck, Richardo!
My code:
public class Solution {
public int maxArea(int[] height) {
if (height == null || height.length <= 1)
return 0;
int max = Integer.MIN_VALUE;
for (int i = 0; i < height.length; i++) {
for (int j = height.length - 1; j > i; j--) {
if (height[j] >= height[i]) {
int area = height[i] * (j - i);
max = Math.max(max, area);
break;
}
}
}
for (int i = height.length - 1; i >= 0; i--) {
for (int j = 0; j < i; j++) {
if (height[j] >= height[i]) {
int area = height[i] * (i - j);
max = Math.max(max, area);
break;
}
}
}
return max;
}
}
這是我的解法。但是是超時的。我就是想,面積最大那就是一定要最寬的。那么我就從右邊開始找到最大的。然后左邊移動一位,繼續找右邊的。
然后再從右邊來一次。超時。
看了答案,才知道,直接從兩側開始掃描就好了。
重寫如下:
My code:
public class Solution {
public int maxArea(int[] height) {
if (height == null || height.length <= 1)
return 0;
int max = Integer.MIN_VALUE;
int left = 0;
int right = height.length - 1;
while (left < right) {
int area = 0;
if (height[left] > height[right]) {
area = height[right] * (right - left);
max = Math.max(max, area);
right--;
}
else if (height[left] < height[right]) {
area = height[left] * (right - left);
max = Math.max(max, area);
left++;
}
else {
area = height[left] * (right - left);
max = Math.max(max, area);
right--;
left++;
}
}
return max;
}
}
記得第一遍做的時候就想了好久,想復雜了。。
這次做還是不會做。
要從宏觀的角度去思考問題。
Anyway, Good luck, Richardo!
My code:
public class Solution {
public int maxArea(int[] height) {
if (height == null || height.length == 0) {
return 0;
}
int begin = 0;
int end = height.length - 1;
int max = 0;
while (begin < end) {
max = Math.max(max, (end - begin) * Math.min(height[begin], height[end]));
if (height[begin] > height[end]) {
end--;
}
else if (height[begin] < height[end]) {
begin++;
}
else {
begin++;
end--;
}
}
return max;
}
}
差不多的解法。
記住雙指針,從兩側往中間掃描的做法。
Anyway, Good luck, Richardo! --- 08/11/2016