Last active
December 30, 2015 17:19
-
-
Save junjiah/7859924 to your computer and use it in GitHub Desktop.
solved 'Container With Most Water ' on LeetCode (ONE AC lol) http://oj.leetcode.com/problems/container-with-most-water/ [better O(n) algorithm is also attached - from Web]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.Collections; | |
import java.util.Comparator; | |
import java.util.List; | |
/** | |
* Created with IntelliJ IDEA. | |
* User: EDFward | |
* Date: 12/8/13 | |
* Time: 9:56 PM | |
* Happy coding! | |
*/ | |
public class Solution { | |
public static void main(String[] args) { | |
int[] h = new int[]{5, 2, 7, 4}; | |
Solution s = new Solution(); | |
System.out.println(s.maxArea(h)); | |
} | |
public int maxArea(int[] height) { | |
// IMPORTANT: Please reset any member data you declared, as | |
// the same Solution instance will be reused for each test case. | |
if (height.length < 2) return 0; | |
List<Point> listOfPoint = new ArrayList<Point>(); | |
int[] pointLeft = new int[height.length+1], | |
pointRight = new int[height.length+1]; | |
for (int i = 0; i < height.length; ++i) { | |
listOfPoint.add(new Point(i, height[i])); | |
pointLeft[i] = i - 1; | |
pointRight[i] = i + 1; | |
} | |
int leftMost = 0, | |
rightMost = height.length-1; | |
Collections.sort(listOfPoint, new Comparator<Point>() { | |
public int compare(Point a, Point b) { | |
return a.y - b.y; | |
} | |
}); | |
int res = 0; | |
for (Point p : listOfPoint) { | |
if (leftMost == rightMost) break; // if final element | |
if (p.x == leftMost) | |
leftMost = pointRight[p.x]; | |
else if (p.x == rightMost) | |
rightMost = pointLeft[p.x]; | |
else { | |
pointRight[pointLeft[p.x]] = pointRight[p.x]; | |
pointLeft[pointRight[p.x]] = pointLeft[p.x]; | |
} | |
int area = 0; | |
if (rightMost-p.x > p.x-leftMost) | |
area = (rightMost-p.x)*p.y; | |
else | |
area = (p.x-leftMost)*p.y; | |
// update max area | |
if (area > res) res = area; | |
} | |
return res; | |
} | |
} | |
class Point { | |
public int x, y; | |
Point(int x, int y) { | |
this.x = x; | |
this.y = y; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
O(n) algorithm | |
*/ | |
class Solution { | |
public: | |
int maxArea(vector<int> &height) { | |
if (height.size() < 2) return 0; | |
int i = 0, j = height.size() - 1; | |
int maxarea = 0; | |
while(i < j) { | |
int area = 0; | |
if(height[i] < height[j]) { | |
area = height[i] * (j-i); | |
//Since i is lower than j, | |
//so there will be no jj < j that make the area from i,jj | |
//is greater than area from i,j | |
//so the maximum area that can benefit from i is already recorded. | |
//thus, we move i forward. | |
++i; | |
} else { | |
area = height[j] * (j-i); | |
//the same reason as above | |
--j; | |
} | |
if(maxarea < area) maxarea = area; | |
} | |
return maxarea; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment