Blage's Coding Blage's Coding
Home
算法
  • 手写Spring
  • SSM
  • SpringBoot
  • JavaWeb
  • JAVA基础
  • 容器
  • Netty

    • IO模型
    • Netty初级
    • Netty原理
  • JVM
  • JUC
  • Redis基础
  • 源码分析
  • 实战应用
  • 单机缓存
  • MySQL

    • 基础部分
    • 实战与处理方案
    • 面试
  • ORM框架

    • Mybatis
    • Mybatis_Plus
  • SpringCloudAlibaba
  • MQ消息队列
  • Nginx
  • Elasticsearch
  • Gateway
  • Xxl-job
  • Feign
  • Eureka
  • 面试
  • 工具
  • 项目
  • 关于
🌏本站
🧸GitHub (opens new window)
Home
算法
  • 手写Spring
  • SSM
  • SpringBoot
  • JavaWeb
  • JAVA基础
  • 容器
  • Netty

    • IO模型
    • Netty初级
    • Netty原理
  • JVM
  • JUC
  • Redis基础
  • 源码分析
  • 实战应用
  • 单机缓存
  • MySQL

    • 基础部分
    • 实战与处理方案
    • 面试
  • ORM框架

    • Mybatis
    • Mybatis_Plus
  • SpringCloudAlibaba
  • MQ消息队列
  • Nginx
  • Elasticsearch
  • Gateway
  • Xxl-job
  • Feign
  • Eureka
  • 面试
  • 工具
  • 项目
  • 关于
🌏本站
🧸GitHub (opens new window)
  • 数组

  • 链表

  • 字符串

  • 二叉树

  • 动态规划

  • 深搜回溯

  • 数学贪心

  • 堆栈队列

    • 394.字符串解码
    • 32. 最长有效括号
    • 739. 每日温度
    • 1130. 叶值的最小代价生成树
    • 84. 柱状图中最大的矩形
    • 42.接雨水
    • 85. 最大矩形
    • 239.滑动窗口最大值
    • 6891. 机器人碰撞
    • 445. 两数相加 II
    • 2762. 不间断子数组
    • 6919. 使数组中的所有元素都等于零
    • 862. 和至少为 K 的最短子数组
    • 1499. 满足不等式的最大值
    • 2856. 删除数对后的最小数组长度
    • 901. 股票价格跨度
    • 2034. 股票价格波动
    • 1488. 避免洪水泛滥
      • 1.贪心+优先级队列+TreeMap
    • 2940. 找到 Alice 和 Bob 可以相遇的建筑
    • 2454. 下一个更大元素 IV
  • 前缀和

  • 算法设计

  • 位运算

  • WA

  • 算法
  • 堆栈队列
phan
2023-10-13
目录

1488. 避免洪水泛滥

# 1488. 避免洪水泛滥 (opens new window)

# 1.贪心+优先级队列+TreeMap

基于贪心策略,每次选择“当前可抽水湖泊”中,洪水泛滥时刻最早的湖泊进行抽水,其中当前可抽水湖泊定义为,当前已经装满水的湖泊。小顶堆queue输出湖泊的泛滥时刻最提前的湖泊。

  • 第一步首先需要记录<上次降雨时刻,洪水泛滥时刻>,采用Map辅助计算。其中key为水池编号,value为上次降雨时刻。
  • 如果当前湖泊已经出现过,说明当前湖泊在第i天会发生泛滥,需要在(begintime,i)区间内完成抽水。TreeMap存储每个泛滥湖泊的起始时间点信息,key为开始时间,value[0]为结束时间,value[1]为湖泊编号。
  • 第二部贪心抽水,当前如果不降雨,则取出所有在第i天之前会发生泛滥的湖泊信息。并存入小顶堆,每次抽水都从堆顶取出泛滥时刻最邻近的湖泊。
class Solution {
    public int[] avoidFlood(int[] rains) {
        int[] res=new int[rains.length];
        Map<Integer,Integer> map=new HashMap<>();
        TreeMap<Integer,int[]> time=new TreeMap<>();
        PriorityQueue<int[]> queue=new PriorityQueue<>(new Comparator<int[]>(){
            public int compare(int[] o1,int[] o2){
                return o1[1]-o2[1];
            }
        });
        //拿到每个会发生洪水的区间
        for(int i=0;i<rains.length;i++){
            if(rains[i]!=0){
                if(map.containsKey(rains[i])){
                    int begintime=map.get(rains[i]);
                    time.put(begintime,new int[]{i,rains[i]});
                }
                map.put(rains[i],i);
                res[i]=-1;
            }
        }
        //贪心抽取最邻近要溢出的水库
        for(int i=0;i<rains.length;i++){
            if(rains[i]==0){
                while(time.size()>0&&time.firstKey()<i){
                    int start=time.firstKey();
                    int[] nums=time.get(start);
                    int end=nums[0];
                    int lake=nums[1];
                    time.remove(start);
                    queue.offer(new int[]{start,end,lake});
                }
                if(queue.size()==0){
                    res[i]=1;
                    continue;
                }
                int[] peek=queue.poll();
                if(peek[1]<=i) return new int[0];
                res[i]=peek[2];
            }
        }
        if(queue.size()>0||time.size()>0) return new int[0];
        return res;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
编辑 (opens new window)
#Leetcode#堆栈队列
上次更新: 2023/12/15, 15:49:57
2034. 股票价格波动
2940. 找到 Alice 和 Bob 可以相遇的建筑

← 2034. 股票价格波动 2940. 找到 Alice 和 Bob 可以相遇的建筑→

Theme by Vdoing | Copyright © 2023-2024 blageCoder
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式