From b897d019faca723b43cd5e08901edabec39941a3 Mon Sep 17 00:00:00 2001 From: wu xiangkai Date: Thu, 30 Oct 2025 14:47:22 +0800 Subject: [PATCH] =?UTF-8?q?feat:=202025-10-30=E6=89=93=E5=8D=A1=EF=BC=88?= =?UTF-8?q?=E6=8E=A5=E9=9B=A8=E6=B0=B4=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 11.container-with-most-water.java | 23 ++++++++ 407.trapping-rain-water-ii.java | 95 +++++++++++++++++++++++++++++++ 42.trapping-rain-water.java | 31 ++++++++++ 3 files changed, 149 insertions(+) create mode 100644 11.container-with-most-water.java create mode 100644 407.trapping-rain-water-ii.java create mode 100644 42.trapping-rain-water.java 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 +