当前位置:网站首页>[Title brushing] avoid flooding
[Title brushing] avoid flooding
2022-06-30 13:31:00 【m0_ sixty million six hundred and thirty-one thousand three hun】
Catalog
One 、 subject
Two 、 Answer key
2.1 General train of thought

2.2 Detailed implementation

2.3 Source code
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]; // If flooding cannot be avoided , Returns this empty array
//map Used to store , When will it rain in a lake
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;
}
}
边栏推荐
- Data Lake (11): Iceberg table data organization and query
- Kaniko official documents - build images in kubernetes
- MySQL queries the data within the radius according to the longitude and latitude, and draws a circle to query the database
- Golang Basics - string and int, Int64 inter conversion
- Introduction to the new source code of substrat: fix the memory leak of the mission engine of beefy, and optimize the smart contract deletion queue
- On the simplification and acceleration of join operation
- Basic syntax of unity script (2) -record time in unity
- 服务线上治理
- visualstudio 和sql
- Postman automatically generates curl code snippets
猜你喜欢
![[kali] Kali system, software update (with image source)](/img/ac/43a3f81d50ab6866271b500b142252.png)
[kali] Kali system, software update (with image source)

损失函数:DIOU loss手写实现

Goods and services - platform properties

Js根据相同值将数组转换为二维数组

Open source of xinzhibao applet

Kaniko official documents - build images in kubernetes

资源变现小程序开通微信官方小商店教程

WTM重大更新,多租户和单点登录

Rk356x u-boot Institute (command section) 3.3 env related command usage

防火墙基础之总部双机热备与分支基础配置
随机推荐
Resource realization applet opening traffic main tutorial
Publicity of the fourth batch of shortlisted Enterprises - annual Top100 smart Internet supplier selection
SQL programming problem, test case failed
mqtt-ros模拟发布一个自定义消息类型
Idea 2021.3 golang error: rning: undefined behavior version of delve is too old for go version 1.18
顺应媒体融合趋势,中科闻歌携手美摄打造数智媒宣
Package tronapi wave field interface based on thinkphp5 PHP version -- interface document attached -20220627
In line with the trend of media integration, Zhongke Wenge and Meishe jointly create digital intelligence media publicity
A keepalived high availability accident made me learn it again!
华为帐号多端协同,打造美好互联生活
【刷题篇】避免洪水泛滥
Postman automatically generates curl code snippets
mysql拒绝访问、管理员身份打开的
我如何才能保护我的私钥?
An interesting thing happened in the project
How to handle ZABBIX server startup failure
exlipse同时操作多行。比如同时在多行同列输入相同的文字
波卡跨链通信源码探秘: 要素篇
PG Basics - logical structure management (table inheritance, partition table)
uniapp支付之APP微信支付unicloud版(附源码)