Algorithms—42.Trapping Rain Water
2015-06-19 22:30
288 查看
public class Solution {
public int trap(int[] height) {
if (height.length<=1) {
return 0;
}
int a=0;
int h=height[a];
/**
* 求出最高峰
*/
for (int i = 1; i < height.length; i++) {
if (height[i]>h) {
a=i;
h=height[i];
}
}
Map<String, Integer> leftMap=new HashMap<String, Integer>();
Map<String, Integer> rightMap=new HashMap<String, Integer>();
leftMap.put("result", 0);
leftMap.put("a",a);
rightMap.put("result", 0);
rightMap.put("a",a);
Map<String, Integer> leftrRsult=new Solution().left(leftMap, height);
Map<String, Integer> rightResult=new Solution().right(leftMap, height);
return leftrRsult.get("result")+rightResult.get("result");
}
/**
* 求左侧
* @param height
* @param a
* @return
*/
public Map<String, Integer> left(Map<String, Integer> m,int[] height){
Map<String, Integer> map=new HashMap<String, Integer>();
if (m==null||m.get("a")==null) {
return m;
}
if (m.get("a")<=1) {
return m;
}
int h=height[0];
int result=m.get("result");
int sum=0;
int a=0;
for (int i = 1; i <m.get("a"); i++) {
if (height[i]<h) {
sum+=(h-height[i]);
}else {
sum=0;
h=height[i];
a=i;
}
}
result+=sum;
map.put("result", result);
map.put("a", a);
return left(map, height);
}
/**
* 求右侧
* @param height
* @param a
* @return
*/
public Map<String, Integer> right(Map<String, Integer> m,int[] height){
Map<String, Integer> map=new HashMap<String, Integer>();
if (m==null||m.get("a")==null) {
return m;
}
if (m.get("a")>=height.length-2) {
return m;
}
int h=height[height.length-1];
int result=m.get("result");
int sum=0;
int a=0;
for (int i = height.length-1; i>m.get("a"); i--) {
if (height[i]<h) {
sum+=(h-height[i]);
}else {
sum=0;
h=height[i];
a=i;
}
}
result+=sum;
map.put("result", result);
map.put("a", a);
return right(map, height);
}
}
思路:先找出最高峰,然后分为左右两个部分,以左边为例说明,从开始端作为参照开始往右遍历,如果遇到小于参照值的,那么加上参照值与当前值的差值,否则更换参照值;求出靠近最高峰最大值之间的蓄水量,然后递归进行再次求解。右边同理。
public int trap(int[] height) {
if (height.length<=1) {
return 0;
}
int a=0;
int h=height[a];
/**
* 求出最高峰
*/
for (int i = 1; i < height.length; i++) {
if (height[i]>h) {
a=i;
h=height[i];
}
}
Map<String, Integer> leftMap=new HashMap<String, Integer>();
Map<String, Integer> rightMap=new HashMap<String, Integer>();
leftMap.put("result", 0);
leftMap.put("a",a);
rightMap.put("result", 0);
rightMap.put("a",a);
Map<String, Integer> leftrRsult=new Solution().left(leftMap, height);
Map<String, Integer> rightResult=new Solution().right(leftMap, height);
return leftrRsult.get("result")+rightResult.get("result");
}
/**
* 求左侧
* @param height
* @param a
* @return
*/
public Map<String, Integer> left(Map<String, Integer> m,int[] height){
Map<String, Integer> map=new HashMap<String, Integer>();
if (m==null||m.get("a")==null) {
return m;
}
if (m.get("a")<=1) {
return m;
}
int h=height[0];
int result=m.get("result");
int sum=0;
int a=0;
for (int i = 1; i <m.get("a"); i++) {
if (height[i]<h) {
sum+=(h-height[i]);
}else {
sum=0;
h=height[i];
a=i;
}
}
result+=sum;
map.put("result", result);
map.put("a", a);
return left(map, height);
}
/**
* 求右侧
* @param height
* @param a
* @return
*/
public Map<String, Integer> right(Map<String, Integer> m,int[] height){
Map<String, Integer> map=new HashMap<String, Integer>();
if (m==null||m.get("a")==null) {
return m;
}
if (m.get("a")>=height.length-2) {
return m;
}
int h=height[height.length-1];
int result=m.get("result");
int sum=0;
int a=0;
for (int i = height.length-1; i>m.get("a"); i--) {
if (height[i]<h) {
sum+=(h-height[i]);
}else {
sum=0;
h=height[i];
a=i;
}
}
result+=sum;
map.put("result", result);
map.put("a", a);
return right(map, height);
}
}
思路:先找出最高峰,然后分为左右两个部分,以左边为例说明,从开始端作为参照开始往右遍历,如果遇到小于参照值的,那么加上参照值与当前值的差值,否则更换参照值;求出靠近最高峰最大值之间的蓄水量,然后递归进行再次求解。右边同理。
相关文章推荐
- AlphaMobileControls 控件的使用 AlphaImageButton
- 偶尔转帖:AI会议的总结(by南大周志华)
- MonkyTalk学习-6-Verify-WaitFor & WaitForNot
- pthread_cond_wait
- grails domain里面beforeinsert无效
- MacBook Air 安装 Ubuntu 双系统
- A Painless Q-learning Tutorial (一个 Q-learning 算法的简明教程)
- 【Training versus Testing】林轩田机器学习基石
- 短信接收Main函数
- 朴素贝叶斯分类法 Naive Bayes ---R
- poj 2305 Basic remains 高精度取余
- 地形算法(Terrain)
- 实时查看日志文件 tail -f gallerylogs.log
- LeetCode 217 Contains Duplicate
- 【codechef】Chef and Bracket-Pairs (分层dp)
- 十大经典数据挖掘算法(9) 朴素贝叶斯分类器 Naive Bayes
- You possibly will not often be capable of find the money for Obtain Diesel engine Designer watches
- 使用AIDL实现进程间的通信
- project继承domain管理权限
- ajax和grails后台的搭配