题目链接:1488. 避免洪水泛滥

Your country has an infinite number of lakes. Initially, all the lakes are empty, but when it rains over the nth lake, the nth lake becomes full of water. If it rains over a lake which is full of water, there will be a flood. Your goal is to avoid the flood in any lake.

Given an integer array rains where:

  • rains[i] > 0 means there will be rains over the rains[i] lake.
  • rains[i] == 0 means there are no rains this day and you can choose one lake this day and dry it.

Return an array ans where:

  • ans.length == rains.length
  • ans[i] == -1 if rains[i] > 0.
  • ans[i] is the lake you choose to dry in the ith day if rains[i] == 0.

If there are multiple valid answers return any of them. If it is impossible to avoid flood return an empty array.

Notice that if you chose to dry a full lake, it becomes empty, but if you chose to dry an empty lake, nothing changes. (see example 4)

Example 1:

Input: rains = [1,2,3,4]
Output: [-1,-1,-1,-1]
Explanation: After the first day full lakes are [1]
After the second day full lakes are [1,2]
After the third day full lakes are [1,2,3]
After the fourth day full lakes are [1,2,3,4]
There’s no day to dry any lake and there is no flood in any lake.

Example 2:

Input: rains = [1,2,0,0,2,1]
Output: [-1,-1,2,1,-1,-1]
Explanation: After the first day full lakes are [1]
After the second day full lakes are [1,2]
After the third day, we dry lake 2. Full lakes are [1]
After the fourth day, we dry lake 1. There is no full lakes.
After the fifth day, full lakes are [2].
After the sixth day, full lakes are [1,2].
It is easy that this scenario is flood-free. [-1,-1,1,2,-1,-1] is another acceptable scenario.

Example 3:

Input: rains = [1,2,0,1,2]
Output: []
Explanation: After the second day, full lakes are [1,2]. We have to dry one lake in the third day.
After that, it will rain over lakes [1,2]. It’s easy to prove that no matter which lake you choose to dry in the 3rd day, the other one will flood.

Example 4:

Input: rains = [69,0,0,0,69]
Output: [-1,69,1,1,-1]
Explanation: Any solution on one of the forms [-1,69,x,y,-1], [-1,x,69,y,-1] or [-1,x,y,69,-1] is acceptable where 1 <= x,y <= 10^9

Example 5:

Input: rains = [10,20,20]
Output: []
Explanation: It will rain over lake 20 two consecutive days. There is no chance to dry any lake.

Constraints:
  • 1 <= rains.length <= 10^5
  • 0 <= rains[i] <= 10^9
题解

不考虑代码,直接面对问题,可以得出此结论。尽可能的先抽干最有可能发洪水的湖泊。什么样的湖泊最先发洪水呢,当然是已经装满水并且未来最先下雨的容易发洪水。先排除这些湖泊的隐患不就解决了吗?

所以需要用到优先队列,此处用PriorityQueue实现。

Java代码
/*** <p>创建日期:2020-06-21 12:35:10</p>*/
class Solution {public int[] avoidFlood(int[] rains) {// 天数int days = rains.length;Map<Integer, Integer> map = new HashMap<>(days);// next[i]=k表示第rains[i]个湖泊下一次下雨在第k天int[] next = new int[days];// 逆序遍历每天的下雨情况for (int i = days - 1; i >= 0; i--) {// 将第rains[i]个湖泊下雨的日期保存到next数组中next[i] = map.getOrDefault(rains[i], days);// 更新第rains[i]个湖泊的下雨的最近的日期map.put(rains[i], i);}int[] res = new int[days];Arrays.fill(res, -1);// 装满水的湖泊队列,按照未来最先下雨的顺序排列,因为装满水的情况下,先下雨可能发洪水,优先抽干Queue<Lake> full = new PriorityQueue<>(days, Comparator.comparingInt(lake -> lake.nextDay));// 顺序遍历每天的下雨情况for (int i = 0; i < days; i++) {if (rains[i] == 0) {// 没有湖泊下雨if (full.isEmpty()) {// 没有湖泊装满水res[i] = 1;} else {// 将第k个湖泊抽干,k为下面等号右边的值res[i] = full.poll().index;}} else {// 第rains[i]个湖泊Lake lake = new Lake();// 湖泊索引lake.index = rains[i];// 此湖泊未来最近的下雨日期lake.nextDay = next[i];// 添加到优先队列中full.offer(lake);}if (!full.isEmpty() && full.peek().nextDay <= i) {// 如果有装满水的湖泊并且该湖泊在下次下雨之前已经没有机会抽干,会发洪水return new int[0];}}return res;}/*** 湖泊*/private static class Lake {/*** 湖泊索引*/int index;/*** 此湖泊未来最近的下雨日期*/int nextDay;}
}

LeetCode 1488. Avoid Flood in The City - Java - 优先队列相关推荐

  1. Leetcode 1488. Avoid Flood in The City(python)

    题目 解法:贪心 总体思想是把0的位置储存下来.每当遇到已经满的湖泊,查看两个问题:1)是否能够干燥他 2)能干燥他的符合条件的最早位置 不能干燥有两种情况,一种是现在就没有保存好的0,第二种是,保存 ...

  2. LeetCode (12.整数转罗马数字)JAVA StringBuffer

    LeetCode (12.整数转罗马数字)JAVA StringBuffer 罗马数字包含以下七种字符: I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5 X 10 L 50 C 1 ...

  3. LeetCode(13.罗马数字转整数) JAVA Hashmap

    LeetCode(13.罗马数字转整数) JAVA Hashmap 罗马数字包含以下七种字符: I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5 X 10 L 50 C 100 D ...

  4. LeetCode 09:回文数(Java实现)

    LeetCode 09:回文数(Java实现) 题目 判断一个整数是否是回文数.回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数. 示例 1:输入: 121 输出: true 示例 2:输 ...

  5. [LeetCode]187. 重复的DNA序列(java实现)暴力 + 哈希

    [LeetCode]187. 重复的DNA序列(java实现) 1. 题目 2. 读题(需要重点注意的东西) 3. 解法 4. 可能有帮助的前置习题 5. 所用到的数据结构与算法思想 6. 总结 1. ...

  6. 2020-12-17 【 java优先队列的用法】

    java优先队列的用法 peek是top() 只取最上面的元素 poll是pop() 弹出 add是添加 push 类的比较器,相对于仿函数 struct cmp{ bool operator ()( ...

  7. java 优先队列从小到大,优先队列(Java)

    优先队列(底层结构为最大堆) 普通的队列是一种先进先出的数据结构,元素在队列尾追加,而从队列头删除.在优先队列中,元素被赋予优先级.当访问元素时,具有最高优先级的元素最先删除.优先队列具有最高级先出 ...

  8. java 优先队列_优先队列Java

    java 优先队列 Every now and then we need to process items of a queue in a particular order. Priority que ...

  9. LeetCode 674. Longest Continuous Increasing Subsequence--python,Java,C++解法

    此题链接:Longest Continuous Increasing Subsequence - LeetCode Given an unsorted array of integers, find ...

最新文章

  1. Python:Scrapy 框架简单介绍
  2. 怎么注册今日头条?哪里可以直接购置?
  3. 主流TTLCMOS电平介绍
  4. 配置Ubuntu Server高速apt-get源
  5. UIButton 上的标题添加下划线效果
  6. 蘋果春季發布會一周之後
  7. 2018蓝桥杯A组:分数(3种方法 循环累称 快速幂运算 移位运算)
  8. 系统科学专业 计算机,2018年北京市培养单位数学与系统科学研究院863计算机学科综合(专业)之计算机操作系统考研核心题库...
  9. linux下使用小票打印
  10. Echarts官方文档!
  11. linux查看其他用户计划任务,Linux计划任务(crond、atd)
  12. 4-渔夫打鱼晒网问题
  13. 关于MyEclipse的servers和WTPservers
  14. python 经纬度 地址批量转换
  15. 如何删除word空白页技巧汇总
  16. 天龙八部兵圣奇阵称号
  17. 大数据培训哪家好?大数据都学什么?
  18. 基于RTP协议的H.264视频传输系统:实现
  19. .NET之对接口和抽象类
  20. 新零售新趋势?丨工信部:我国5G研发推动全球统一标准

热门文章

  1. python精典习题——输入某年某月某日,判断这一天是这一年的第几天?
  2. 【资讯】福布斯:金融科技革命开端的尾声
  3. asdl能够连接成功,不能打开网页,qq超时登陆
  4. Java内存分配(四种内存类型)
  5. 桌面图标有蓝色阴影怎么去掉
  6. 软件无线电SDR应用(1):MATLAB信号产生
  7. 用户积分和积分排行榜功能微服务实现
  8. 美女接电话是怎么样被气死的
  9. W3C 代码标准规范
  10. 视觉SLAM中的数学基础 第三篇 李群与李代数