LeetCode 42. Trapping Rain Water

Python解法

解题思路:

本思路需找到最高点左右遍历,时间复杂度O(nlogn),以下为向左遍历的过程。

  1. 将每一个点的高度和索引存成一个元组 (val, idx)
  2. 找到最高的点(可能有多个,任取一个),记为 (now_val, now_idx)。
  3. 向左找第一个val不大于now_val的点(left_val, left_idx)。
  4. 以left_val作为水平面,用height[left_val+1, now_val-1]中每一个点更新ans。
  5. now_val, now_idx = left_val, left_idx。
  6. 如果now_idx >= 0 ,重复执行3;否则执行7。
  7. 同理从最高点向右遍历更新ans。

向左找的具体操作

  1. 将每一个点的高度和索引存成一个元组 (val, idx)
  2. 将元组按val从大到小排序,若val相等,按idx从大到小排序。实现过程:直接sort,然后reverse。
  3. 从左向右遍历,找到第一个idx小于now_idx的节点(left_val, left_idx),用height[left+1, now-1]中的点更新ans。
  4. 更新now节点为left节点。
  5. 如果now_idx >= 0 ,重复执行3;否则结束循环,开始向右找。

向右找的具体操作

  1. 将每一个点的高度和索引存成一个元组 (val, idx)
  2. 将元组按val从大到小排序,若val相等,按idx从小到大排序。实现过程:用functools中的cmp_to_key重载排序过程。
  3. 从左向右遍历,找到第一个idx大于now_idx的节点(right_val, right_idx),用height[now+1, right-1]中的点更新ans。
  4. 更新now节点为lright节点。
  5. 如果now_idx >= 0 ,重复执行3;否则执行6。
  6. 输出ans。
注意:向左向右只有排序不同,其他操作类似,

利用functools中的cmp_to_key重载排序过程代码如下:

def mcmp(a, b):
val1,idx1 = a
val2,idx2 = b
if val1 == val2:
return idx2-idx1
return val1-val2 from functools import cmp_to_key
l = [] # l中存元组(val, idx)
l.sort(reverse=True, key=cmp_to_key(Solution.mcmp))

代码如下

class Solution:
# 利用functools中的cmp_to_key重载排序:
def mcmp(a, b):
val1,idx1 = a
val2,idx2 = b
if val1 == val2:
return idx2-idx1
return val1-val2 def trap(self, height: List[int]) -> int:
if len(height) == 0:
return 0
from functools import cmp_to_key
l = []
# 将(val, idx)存入l = []中
for i in range(0, len(height)):
l.append((height[i],i)) ans = 0 # 往左找
l.sort(reverse=True)
h, con = l[0]
for i in range(1, len(l)):
val, left = l[i]
if left < con:
for j in range(left+1, con):
ans += val - height[j]
con = left # 往右找
h, con = l[0]
l.sort(reverse=True, key=cmp_to_key(Solution.mcmp))
for i in range(1, len(l)):
val, right = l[i]
if right > con:
for j in range(con+1, right):
ans += val - height[j]
con = right
return ans

C++解法

解题思路:

本思路与Python类似,需找到最高点左右遍历,时间复杂度O(nlogn)以下为向左遍历的过程。差别在于C++是存map,无须手动排序。

// m中first存高度val,second存索引集合。
// set<int> s = m[2]表示高度为2的点的位置集合。
map<int, set<int>> m;

实现代码如下:

class Solution {
public:
int trap(vector<int>& height) {
if(height.size() == 0) return 0;
int ans = 0;
// m中first存高度val,second存索引集合。
// set<int> s = m[2]表示高度为2的点的位置集合。
map<int, set<int>> m;
// itm表示m的一个迭代器
map<int, set<int>>::iterator itm;
set<int> s;
set<int>::iterator its;
//将(val, idx)存入map
int sz = height.size();
for(int i = 0;i < sz; ++i){
m[height[i]].insert(i);
} //向左找
itm = --m.end();
s = itm->second;
int h = itm->first;
int left = *s.begin();
//每次找val小,idx小的
while(left != -1 && left >= 0){
s = m[h];
its = s.lower_bound(left);
if(its == s.begin()){
if(itm == m.begin()){
left = -1;
}else{
--itm;
h = itm->first;
}
}else{
--its;
for(int i = *its+1;i < left; ++i){
ans += h-height[i];
}
left = *its;
continue;
}
} //向右找
itm = --m.end();
s = itm->second;
h = itm->first;
int right = *s.begin();
//每次找val小,idx大的
while(right != -1 && right < sz){
s = m[h];
its = s.upper_bound(right);
if(its == s.end()){
if(itm == m.begin()){
right = -1;
}else{
--itm;
h = itm->first;
}
}else{
for(int i = right+1;i < *its; ++i){
ans += h-height[i];
}
right = *its;
continue;
}
}
return ans;
}
};

LeetCode 42. Trapping Rain Water 【两种解法】(python排序遍历,C++ STL map存索引,时间复杂度O(nlogn))的更多相关文章

  1. leetcode#42 Trapping rain water的五种解法详解

    leetcode#42 Trapping rain water 这道题十分有意思,可以用很多方法做出来,每种方法的思想都值得让人细细体会. 42. Trapping Rain WaterGiven n ...

  2. [array] leetcode - 42. Trapping Rain Water - Hard

    leetcode - 42. Trapping Rain Water - Hard descrition Given n non-negative integers representing an e ...

  3. LeetCode - 42. Trapping Rain Water

    42. Trapping Rain Water Problem's Link ------------------------------------------------------------- ...

  4. [LeetCode] 42. Trapping Rain Water 收集雨水

    Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...

  5. leetCode 42.Trapping Rain Water(凹槽的雨水) 解题思路和方法

    Trapping Rain Water Given n non-negative integers representing an elevation map where the width of e ...

  6. [leetcode]42. Trapping Rain Water雨水积水问题

    Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...

  7. [LeetCode] 42. Trapping Rain Water 解题思路

    Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...

  8. Java [Leetcode 42]Trapping Rain Water

    题目描述: Given n non-negative integers representing an elevation map where the width of each bar is 1, ...

  9. LeetCode 42 Trapping Rain Water(积水体积)

    题目链接: https://leetcode.com/problems/trapping-rain-water/?tab=Description   Problem: 根据所给数组的值,按照上图的示意 ...

随机推荐

  1. 【剑指Offer】9、变态跳台阶

      题目描述:   一只青蛙一次可以跳上1级台阶,也可以跳上2级--它也可以跳上n级.求该青蛙跳上一个n级的台阶总共有多少种跳法.   解题思路:   当只有一级台阶时,f(1)=1:当有两级台阶时, ...

  2. html第五节课

    格式布局 一.position:fixed 锁定位置(相对于浏览器的位置),例如有些网站的右下角的弹出窗口. 示例: 二.position:absolute 1.外层没有position:absolu ...

  3. mysql字符集和排序规则

    1.关于字符集和排序规则所为字符集,就是用来定义字符在数据库中的编码的集合.常见的字符集有:utf8(支持中文)和AccIS(不支持中文) 数据库中的排序规则用来定义字符在进行排序和比较的时候的一种规 ...

  4. springcloud(一):初识springcloud

    研究了一段时间Spring Boot了准备向Spring Cloud进发,公司架构和项目也全面拥抱了Spring Cloud.在使用了一段时间后发现Spring Cloud从技术架构上降低了对大型系统 ...

  5. CodeForces - 340 C - Tourist Problem

    先上题目: A - Tourist Problem Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & ...

  6. CGLib与JDK的动态代理

    一.CGLib 简单介绍 CGLib (Code Generation Library) 是一个强大的,高性能,高质量的Code生成类库. 它能够在执行期扩展Java类与实现Java接口. Hiber ...

  7. golang自己定义数据类型查询与插入postgresql中point数据

    golang自己定义数据类型查询与插入postgresql中point数据 详细代码例如以下: package main import ( "bytes" "databa ...

  8. java生成一张图片

    public class CreateImage { public static void main(String[] args) throws Exception{ int width = 100; ...

  9. Linux命令(四)——文件权限管理

    文件权限是指对文件的访问控制,即哪些用户或群组可以访问文件以及执行什么样的操作. 一.文件的权限 1.Linux文件类型 (1)普通文件:文本文件+数据文件+可执行的二进制文件. (2)目录文件:即文 ...

  10. 火云开发课堂 - 《使用Cocos2d-x 开发3D游戏》系列 第一节:3D时代来临!

    <使用Cocos2d-x 开发3D游戏>系列在线课程 第一节:3D时代来临.Cocos2d-x程序猿的机遇和挑战! 视频地址:http://edu.csdn.net/course/deta ...