惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
WordPress大学
WordPress大学
宝玉的分享
宝玉的分享
人人都是产品经理
人人都是产品经理
博客园 - 聂微东
IT之家
IT之家
V
V2EX
Jina AI
Jina AI
V
Visual Studio Blog
有赞技术团队
有赞技术团队
博客园 - 司徒正美
博客园 - 叶小钗
The Cloudflare Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
小众软件
小众软件
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
博客园 - 三生石上(FineUI控件)
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Google DeepMind News
Google DeepMind News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
腾讯CDC
Google Online Security Blog
Google Online Security Blog
博客园 - 【当耐特】
Apple Machine Learning Research
Apple Machine Learning Research
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
N
News and Events Feed by Topic
N
News and Events Feed by Topic
The Last Watchdog
The Last Watchdog
W
WeLiveSecurity
月光博客
月光博客
Security Archives - TechRepublic
Security Archives - TechRepublic
Webroot Blog
Webroot Blog
SecWiki News
SecWiki News
博客园_首页
罗磊的独立博客
量子位
Latest news
Latest news
I
Intezer
V
Vulnerabilities – Threatpost
A
Arctic Wolf
Last Week in AI
Last Week in AI
Recent Commits to openclaw:main
Recent Commits to openclaw:main
S
SegmentFault 最新的问题
S
Security Affairs
阮一峰的网络日志
阮一峰的网络日志
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
酷 壳 – CoolShell
酷 壳 – CoolShell
P
Palo Alto Networks Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
N
News | PayPal Newsroom

静水深流's blog

探索 SSE:服务器推送技术的魅力与应用 | 静水深流 图解DIFF算法介绍 | 静水深流 如何使用javascript实现复制出的文案带链接? | 静水深流 基于vuepress2搭建专属自己的博客,并集成各种常用功能 | 静水深流 听说你至今不晓得缓存淘汰算法?实现LRU、LFU和FIFO? | 静水深流 最长递增子序列及vue3.0中diff算法 | 静水深流 二进制之入门到应用实践 | 静水深流 常见算法学习 | 静水深流 CSS 形状的实现 | 静水深流 ajax取消接口请求 | 静水深流 前端常见的安全问题 | 静水深流 关于http服务端的学习&总结 | 静水深流 前端面试题总结 | 静水深流 javascript原生代码实现及代码总结 | 静水深流 LeetCode算法学习总结-简单 | 静水深流 LeetCode算法学习总结- 中等 | 静水深流 排序算法总结 | 静水深流 扫码登录的实现原理 | 静水深流 Javascript之常见类型判断汇总 | 静水深流 JavaScript各种继承方式和优缺点 | 静水深流 webpack开发、使用及优化总结 | 静水深流 从JavaScript中的拷贝开始思考 | 静水深流 vue原理、使用及面试方面的总结 | 静水深流 前端发展及选择 | 静水深流 css面试总结 | 静水深流 JavaScript 数组展开(扁平化)和underscore的 flatten | 静水深流 首页 | 静水深流 学习网站收藏 | 静水深流 文章列表 | 静水深流
LeetCode算法学习总结-困难 | 静水深流
shaolibao · 2021-08-23 · via 静水深流's blog

接雨水问题leetcode42open in new window

从左侧看,找到每个对应可以看到最高的柱子,右侧也是,然后遍历数组,只有当当前的柱子低于左侧和右侧的柱子时才能接住雨水(否则有一侧就会漏水),最后将所有的雨水加起来

var trap = function(height) {
  const len = height.length
  let volumn = 0
  if (len < 2) {
    return 0
  }
  const leftMax = []
  const rightMax = []
  let max = 0
  // 从左侧看找到最每个地方对应能看到最高的柱子[ 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3 ]
  for(let i=0; i<len; i++) {
    leftMax[i] = max = Math.max(max, height[i])
  }
  
  max = 0
  // 从右侧看,找到每个地方能看到的对应最高的柱子[ 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 2, 1 ]
  for (let i = len - 1; i >=0; i--) {
    rightMax[i] = max = Math.max(max, height[i])
  }

  for (let i = 0; i < len-1; i++) {
    // 站在当前柱子上,必须要低于左侧和右侧最高的柱子,此处才能存住雨水
    if (leftMax[i] > height[i] && rightMax[i] > height[i]) {
      // 左侧和右侧最高柱子中最小的那个-当前柱子的高度=当前柱子能够存放的雨水
      volumn += Math.min(leftMax[i], rightMax[i]) - height[i]
    }
    
  }
  return volumn
};
console.log(trap([0,1,0,2,1,0,1,3,2,1,2,1]));

盛最多水的容器leetcode11open in new window

借用双指针来减少搜索空间,参考open in new window

  • 如果左边低,将i右移
  • 如果右边低,将j左移
const maxArea = function(height) {
    let max = 0; // 最大容纳水量
    let left = 0; // 左
    let right = height.length - 1; // 右
    
    while (left < right) {
        // 计算当前水量
        let cur = (right - left) * Math.min(height[left], height[right]);
        max = Math.max(cur, max);
        height[left] < height[right] ? left ++ : right --;
    }

    return max;
};