Trapping Rain Water I && II

Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.


The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped. Thanks Marcos for contributing this image!

Example:

Input: [0,1,0,2,1,0,1,3,2,1,2,1]
Output: 6

Analysis:

We first find out the max height in the array, then we start from the leftmost bar which is considered as the wall of the container. If there is a bar whose height is less than the wall, water will be saved above that bar. We do the same operation from rightmost to the highest bar position.

 1 public class Solution {
 2     public int trap(int[] height) {
 3         if (height == null || height.length <= 2) return 0;  
 4         int maxIndex = 0;  
 5         for (int i = 1; i < height.length; i++) {  
 6             if (height[i] > height[maxIndex]) {  
 7                 maxIndex = i;  
 8             }  
 9         }  
10         int leftMax = height[0];  
11         int total = 0;  
12         for (int i = 1; i < maxIndex; i++) {  
13             if (height[i] < leftMax) {  
14                 total += (leftMax - height[i]);  
15             } else {
16                 leftMax = height[i];  
17             }  
18         }  
19         int rightMax = height[height.length - 1];  
20         for (int i = height.length - 2; i > maxIndex; i--) {  
21             if (height[i] < rightMax) {  
22                 total += (rightMax - height[i]);  
23             } else {  
24                 rightMax = height[i];  
25             }  
26         }  
27         return total;  
28     }
29 }

 Trapping Rain Water II

Given an m x n matrix of positive integers representing the height of each unit cell in a 2D elevation map, compute the volume of water it is able to trap after raining.

 

Note:

Both m and n are less than 110. The height of each unit cell is greater than 0 and is less than 20,000.

 

Example:

Given the following 3x6 height map:
[
  [1,4,3,1,3,2],
  [3,2,1,3,2,4],
  [2,3,3,2,3,1]
]

Return 4.

The above image represents the elevation map [[1,4,3,1,3,2],[3,2,1,3,2,4],[2,3,3,2,3,1]] before the rain.

 

After the rain, water is trapped between the blocks. The total volume of water trapped is 4.

分析:

从四周出发,选取最低点(木桶原理),然后选取周围没有被visited的点。找到更低的点,则把当前点和低点的差值作为可以装水的量,注意,在加入新的点的时候,那个点的高度应该使用当前点的高度,这样我们就不用倒着回去找最高点了。

 1 class Solution {
 2     public int trapRainWater(int[][] heights) {
 3         if (heights == null || heights.length == 0 || heights[0].length == 0) return 0;
 4 
 5         PriorityQueue<Cell> queue = new PriorityQueue<>(1, (cell1, cell2) ->  cell1.height - cell2.height);
 6         int row = heights.length, col = heights[0].length;
 7         boolean[][] visited = new boolean[row][col];
 8 
 9         // add border cells to the queue.
10         for (int i = 0; i < row; i++) {
11             visited[i][0] = true;
12             visited[i][col - 1] = true;
13             queue.offer(new Cell(i, 0, heights[i][0]));
14             queue.offer(new Cell(i, col - 1, heights[i][col - 1]));
15         }
16 
17         for (int i = 0; i < col; i++) {
18             visited[0][i] = true;
19             visited[row - 1][i] = true;
20             queue.offer(new Cell(0, i, heights[0][i]));
21             queue.offer(new Cell(row - 1, i, heights[row - 1][i]));
22         }
23 
24         // from the borders, pick the shortest cell visited and check its neighbors:
25         // if the neighbor is shorter, collect the water it can trap and update its height as its height plus the water trapped
26        // add all its neighbors to the queue.
27         int[][] dirs = new int[][]{{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
28         int res = 0;
29         while (!queue.isEmpty()) {
30             Cell cell = queue.poll();
31             for (int[] dir : dirs) {
32                 int neighbor_row = cell.row + dir[0];
33                 int neighbor_col = cell.col + dir[1];
34                 if (neighbor_row >= 0 && neighbor_row < row && neighbor_col >= 0 && neighbor_col < col && !visited[neighbor_row][neighbor_col]) {
35                     visited[neighbor_row][neighbor_col] = true;
36                     res += Math.max(0, cell.height - heights[neighbor_row][neighbor_col]);
37                     queue.offer(new Cell(neighbor_row, neighbor_col, Math.max(heights[neighbor_row][neighbor_col], cell.height)));
38                 }
39             }
40         }
41         return res;
42     }
43 }
44 
45 class Cell {
46     int row;
47     int col;
48     int height;
49     public Cell(int row, int col, int height) {
50         this.row = row;
51         this.col = col;
52         this.height = height;
53     }
54 }

 

转载于:https://www.cnblogs.com/beiyeqingteng/p/5669106.html

内容概要:本文系统介绍了算术优化算法(AOA)的基本原理、核心思想及Python实现方法,并通过图像分割的实际案例展示了其应用价值。AOA是一种基于种群的元启发式算法,其核心思想来源于四则运算,利用乘除运算进行全局勘探,加减运算进行局部开发,通过数学优化器加速函数(MOA)和数学优化概率(MOP)动态控制搜索过程,在全局探索与局部开发之间实现平衡。文章详细解析了算法的初始化、勘探与开发阶段的更新策略,并提供了完整的Python代码实现,结合Rastrigin函数进行测试验证。进一步地,以Flask框架搭建前后端分离系统,将AOA应用于图像分割任务,展示了其在实际工程中的可行性与高效性。最后,通过收敛速度、寻优精度等指标评估算法性能,并提出自适应参数调整、模型优化和并行计算等改进策略。; 适合人群:具备一定Python编程基础和优化算法基础知识的高校学生、科研人员及工程技术人员,尤其适合从事人工智能、图像处理、智能优化等领域的从业者;; 使用场景及目标:①理解元启发式算法的设计思想与实现机制;②掌握AOA在函数优化、图像分割等实际问题中的建模与求解方法;③学习如何将优化算法集成到Web系统中实现工程化应用;④为算法性能评估与改进提供实践参考; 阅读建议:建议读者结合代码逐行调试,深入理解算法流程中MOA与MOP的作用机制,尝试在不同测试函数上运行算法以观察性能差异,并可进一步扩展图像分割模块,引入更复杂的预处理或后处理技术以提升分割效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值