当前位置:网站首页>【刷题篇】避免洪水泛滥
【刷题篇】避免洪水泛滥
2022-06-30 12:52:00 【m0_60631323】
一、题目
二、题解
2.1 大致思路
2.2 详细实现
2.3 源码
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.PriorityQueue;
public class Test1 {
public static class Work implements Comparable<Work>{
public int lake;
public int nextRain;
public Work(int l,int p) {
lake=l;
nextRain=p;
}
@Override
public int compareTo(Work o) {
return nextRain-o.nextRain;
}
}
public static int[] avoidFlood(int[] rains) {
int n= rains.length;
int[] ans=new int[n];
int[] invalid=new int[0]; //如果无法避免洪水泛滥,则返回这个空数组
//map中用来存储,某个湖泊在哪天将下雨
HashMap<Integer, LinkedList<Integer>> map=new HashMap<>();
for (int i = 0; i < n; i++) {
if(rains[i]!=0){
if(!map.containsKey(rains[i])){
map.put(rains[i],new LinkedList<>());
}
map.get(rains[i]).addLast(i);
}
}
HashSet<Integer> set=new HashSet<>();
PriorityQueue<Work> heap=new PriorityQueue<Work>();
for (int i = 0; i < n; i++) {
if(rains[i]!=0){
if(!set.contains(rains[i])){
set.add(rains[i]);
map.get(rains[i]).pollFirst();
if(!map.get(rains[i]).isEmpty()){
heap.add(new Work(rains[i],map.get(rains[i]).peekFirst()));
}
ans[i]=-1;
}else {
return invalid;
}
}else {
if(heap.isEmpty()){
ans[i]=1;
}else {
Work cur=heap.poll();
set.remove(cur.lake);
ans[i]= cur.lake;
}
}
}
return ans;
}
}
边栏推荐
- How can c write an SQL parser
- JMeter learning notes
- Tronapi wave field interface PHP version - interface document attached - package based on thinkphp5 - source code without encryption - can be opened in two - detailed guidance of the author - 11:49:56
- How to take the first step in digital transformation
- Mysql根据经纬度查询半径多少以内的数据,画个圈圈查数据库
- 你想要的异常知识点都在这里了
- Basic syntax of unity script (3) - accessing game object components
- Golang Basics - string and int, Int64 inter conversion
- There is no utf8 option for creating tables in Navicat database.
- 数字时代,XDR(扩展检测与响应)的无限可能
猜你喜欢
DeFi“钱从哪来”?一个大多数人都没搞清楚的问题
How to handle ZABBIX server startup failure
Rk356x u-boot Institute (command section) 3.3 env related command usage
visualstudio 和sql
[kali] Kali system, software update (with image source)
正则系列之断言Assertions
一篇文章读懂关于企业IM的所有知识点
【精选】资源变现资讯、新闻、自媒体、博客小程序(可引流,开通流量主,带pc后台管理)
Unity脚本的基础语法(1)-游戏对象的常用操作
Dark horse notes -- wrapper class, regular expression, arrays class
随机推荐
深度长文探讨Join运算的简化和提速
How to handle ZABBIX server startup failure
【C】深入理解指针、回调函数(介绍模拟qsort)
嵌入式开发:5个可能不再被禁止的C特征
Loss function: Diou loss handwriting implementation
Definition of variables and assignment of variables in MySQL
RK356x U-Boot研究所(命令篇)3.2 help命令的用法
Derivation of Park transformation formula for motor control
WTM major updates, multi tenancy and single sign on
Clearing TinyMCE rich text cache in elementui
Unity脚本程序的开发
Open source of xinzhibao applet
我如何才能保护我的私钥?
Exploring the source code of Boca Cross Chain Communication: Elements
DNS 解析之家庭网络接入 Public DNS 实战
杭州电子商务研究院:官网(网站)是私域的唯一形态
postman 自动生成 curl 代码片段
MySQL queries the data within the radius according to the longitude and latitude, and draws a circle to query the database
There is no utf8 option for creating tables in Navicat database.
get请求与post提交区别的简易理解