diff --git a/11.container-with-most-water.java b/11.container-with-most-water.java new file mode 100644 index 0000000..e0b93ff --- /dev/null +++ b/11.container-with-most-water.java @@ -0,0 +1,23 @@ +/* + * @lc app=leetcode id=11 lang=java + * + * [11] Container With Most Water + */ + +// @lc code=start +class Solution { + public int maxArea(int[] height) { + int l = 0, r = height.length -1, max = 0; + while(l < r) { + max = Math.max(max, (r-l)*Math.min(height[l], height[r])); + if(height[l] < height[r]) { + l++; + } else { + r--; + } + } + return max; + } +} +// @lc code=end + diff --git a/407.trapping-rain-water-ii.java b/407.trapping-rain-water-ii.java new file mode 100644 index 0000000..cff1d91 --- /dev/null +++ b/407.trapping-rain-water-ii.java @@ -0,0 +1,95 @@ +/* + * @lc app=leetcode id=407 lang=java + * + * [407] Trapping Rain Water II + */ + +// @lc code=start +class Solution { + public int trapRainWater(int[][] heightMap) { + PriorityQueue pq = new PriorityQueue<>((a, b)->{ + return a.h - b.h; + }); + boolean[][] visited = visited(heightMap); + pushBorders(heightMap, visited, pq); + int[][] dirs = new int[][] { + new int[] {1, 0}, + new int[] {-1, 0}, + new int[] {0, 1}, + new int[] {0,-1} + }; + int cap = 0; + while(!pq.isEmpty()) { + Node cur = pq.poll(); + int x, y; + for(int i=0; i= 0 && x < heightMap.length && y >= 0 && y < heightMap[0].length && !visited[x][y]) { + visited[x][y] = true; + int curh = heightMap[x][y]; + if(heightMap[x][y] < cur.h) { + cap += cur.h - heightMap[x][y]; + curh = cur.h; + } + pq.add(new Node(x, y, curh)); + } + } + } + return cap; + } + + + private void pushBorders(int[][] hm, boolean[][] visited, PriorityQueue pq) { + int r = hm.length, c = hm[0].length; + for(int i=0;i=0) { + rmax = Math.max(rmax, height[r]); + } + } + } + return cap; + } +} +// @lc code=end +