Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- https://leetcode.com/problems/container-with-most-water/
- Runtime: 2 ms, faster than 94.52% of Java online submissions for Container With Most Water.
- Memory Usage: 39.5 MB, less than 96.15% of Java online submissions for Container With Most Water.
- */
- class Solution {
- public int maxArea(int[] height) {
- int waters = 0, l = 0, r = height.length-1;
- while (l < r) {
- int lh = height[l], rh = height[r];
- waters = Math.max(waters, (r-l) * Math.min(lh, rh));
- if (lh < rh) {
- ++l;
- } else {
- --r;
- }
- }
- return waters;
- }
- }
- /*
- https://leetcode.com/problems/trapping-rain-water/
- Runtime: 1 ms, faster than 98.12% of Java online submissions for Trapping Rain Water.
- Memory Usage: 37.7 MB, less than 95.89% of Java online submissions for Trapping Rain Water.
- */
- class Solution {
- public int trap(int[] height) {
- int waters = 0, l = 0, r = height.length - 1;
- while (l < r) {
- int lh = height[l], rh = height[r];
- if (lh < rh) {
- while(++l < r && lh >= height[l]) {
- waters += lh - height[l];
- }
- } else {
- while(l < --r && rh >= height[r]) {
- waters += rh - height[r];
- }
- }
- }
- return waters;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement