当前位置:网站首页>【刷题篇】避免洪水泛滥
【刷题篇】避免洪水泛滥
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;
}
}
边栏推荐
- Golang foundation -- slicing several declaration methods
- Dark horse notes - common date API
- rxjs Observable 两大类操作符简介
- How can I protect my private key?
- Today's sleep quality record 80 points
- [deep anatomy of C language] storage principle of float variable in memory & comparison between pointer variable and "zero value"
- 【精选】资源变现资讯、新闻、自媒体、博客小程序(可引流,开通流量主,带pc后台管理)
- kaniko官方文档 - Build Images In Kubernetes
- 随着产业互联网的发展,有关互联网的落地和应用也就变得宽阔了起来
- 数据库表为什么写不进数据了
猜你喜欢

正则系列之断言Assertions

Open source of xinzhibao applet

为基础性语言摇旗呐喊
![[Select] resource realization information, news, we media, blog applet (can be drained, open traffic master, with PC background management)](/img/e7/1c34d8aa364b944688ec2ffb4feb7c.jpg)
[Select] resource realization information, news, we media, blog applet (can be drained, open traffic master, with PC background management)

【招聘(广州)】成功易(广州).Net Core中高级开发工程师

QT read / write excel--qxlsx worksheet display / hide status setting 4

Dark horse notes - common date API

SQL考勤统计月报表

uniapp支付之APP微信支付unicloud版(附源码)

Wechat applet reports an error: typeerror: cannot read property 'SetData' of undefined
随机推荐
Discussion on JMeter operation principle
[kali] Kali system, software update (with image source)
【招聘(广州)】成功易(广州).Net Core中高级开发工程师
知识传播不能取代专业学习!
腾讯二面:@Bean 与 @Component 用在同一个类上,会怎么样?
Resource realization applet opening wechat official small store tutorial
Yousi College: Six Sigma is not just statistics!
A keepalived high availability accident made me learn it again!
navicat数据库建表是没有utf8选项。
In line with the trend of media integration, Zhongke Wenge and Meishe jointly create digital intelligence media publicity
postman 自动生成 curl 代码片段
华为帐号多端协同,打造美好互联生活
On the simplification and acceleration of join operation
mysql拒绝访问、管理员身份打开的
数字时代,XDR(扩展检测与响应)的无限可能
MySQL如何将列合并?
Data Lake (11): Iceberg table data organization and query
The independent station is Web3.0. The national "14th five year plan" requires enterprises to build digital websites!
Paper interpretation (AGC) attributed graph clustering via adaptive graph revolution
Mysql根据经纬度查询半径多少以内的数据,画个圈圈查数据库