当前位置: 首页 > news >正文

Leetcode 刷题笔记1 单调栈part02

leetcode 42 接雨水

本题用双指针法更为浅显易懂

双指针法:

class Solution:
    def trap(self, height: List[int]) -> int:
        leftheight, rightheight = [0] * len(height), [0] * len(height)
        ans = 0
        leftheight[0] = height[0]
        for i in range(1, len(height)):
            leftheight[i] = max(height[i], leftheight[i - 1])
        rightheight[-1] = height[-1]
        for i in range(len(height) - 2, -1, -1):
            rightheight[i] = max(height[i], rightheight[i + 1])
        for i in range(len(height)):
            total = min(leftheight[i], rightheight[i]) - height[i]
            ans += total
        return ans

单调栈:

class Solution:
    def trap(self, height: List[int]) -> int:
        stack = [0]
        ans = 0
        for i in range(1, len(height)):
            if height[i] < height[stack[-1]]:
                stack.append(i)
            elif height[i] == height[stack[-1]]:
                stack.pop()
                stack.append(i)
            else:
                while stack and height[i] > height[stack[-1]]:
                    mid_height = height[stack[-1]]
                    stack.pop()
                    if stack:
                        right_height = height[i]
                        left_height = height[stack[-1]]
                        h = min(right_height, left_height) - mid_height
                        w = i - stack[-1] - 1
                        ans += h * w
                stack.append(i)
        return ans

leetcode 84 柱状图的最大的矩形

class Solution:
    def largestRectangleArea(self, heights: List[int]) -> int:
        heights.insert(0, 0)
        heights.append(0)
        stack = [0]
        ans = 0
        for i in range(1, len(heights)):
            while stack and heights[i] < heights[stack[-1]]:
                mid_height = heights[stack[-1]]
                stack.pop()
                if stack:
                    area = (i - stack[-1] - 1) * mid_height
                    ans = max(area, ans)
            stack.append(i)
        return ans

双指针法:
 

class Solution:
    def largestRectangleArea(self, heights: List[int]) -> int:
        n = len(heights)
        min_left_index = [0] * n
        min_right_index = [0] * n
        min_left_index[0] = -1
        result = 0
        for i in range(1, n):
            temp = i - 1
            while temp >= 0 and heights[temp] >= heights[i]:
                temp = min_left_index[temp]
            min_left_index[i] = temp
        min_right_index[n - 1] = n
        for i in range(n - 2, -1, -1):
            temp = i + 1
            while temp < n and heights[temp] >= heights[i]:
                temp = min_right_index[temp]
            min_right_index[i] = temp
        for i in range(len(heights)):
            area = heights[i] * (min_right_index[i] - min_left_index[i] -1)
            result = max(result, area)
        return result

相关文章:

  • C# 获取Type对象的方式
  • 本周安全速报(2025.3.11~3.17)
  • 依赖倒置 DIP、依赖注入 DI、控制反转 IoC 和工厂模式
  • 算法备案全景洞察趋势解码:技术迭代、行业裂变与生态重构
  • IP关联对跨境电商的影响及如何防范措施?
  • Arduino开发ESP8266环境搭建
  • LeetCode[19]删除链表的倒数第N个节点
  • 【Go语言圣经3.1】
  • 《Python深度学习》第一讲:深度学习基础
  • uniapp APP权限弹框
  • 高级java每日一道面试题-2025年3月04日-微服务篇[Eureka篇]-Eureka是什么?
  • 卷积神经网络 - 基本概念
  • 【玩转正则表达式】Python、Go、Java正则表达式解释器的差异解析(附示例)
  • 【Mac 从 0 到 1 保姆级配置教程 08】08. 快速配置 Neovim、LazyVim 以及常用开发环境,如果之前有人这么写就好了
  • 算法刷题记录——LeetCode篇(10) [第901~1000题](持续更新)
  • MySQL:Ubuntu下安装MySQL 8.0记录
  • Maya的操作基础教学
  • Redis--渐进式遍历
  • 应急响应靶场练习-Web1
  • tensorflow与torch并行读取数据机制
  • 摩天大楼天津117大厦复工背后:停工近十年,未知挑战和压力仍在
  • 七部门联合发布《终端设备直连卫星服务管理规定》
  • 马上评丨上热搜的协和“4+4”模式,如何面对舆论审视
  • 向总书记汇报具身智能发展的“稚辉君”:从期待到兴奋再到备受鼓舞
  • 北京发布今年第四轮拟供商品住宅用地清单,共计5宗22公顷
  • 国家核准10台核电新机组,四大核电央企披露新项目进展