当前位置:网站首页>[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;
}
}
边栏推荐
- An interesting thing happened in the project
- Postman génère automatiquement des fragments de code Curl
- 正则系列之断言Assertions
- 独立站即web3.0,国家“十四五“规划要求企业建数字化网站!
- 损失函数:DIOU loss手写实现
- Package based on thinkphp5 -tronapi- wave field interface - source code without encryption - can be opened twice - interface document attached - detailed guidance of the author - June 30, 2022 08:45:2
- Introduction to two types of rxjs observable operators
- 数据库表为什么写不进数据了
- Why can't the database table be written into data
- How does MySQL merge columns?
猜你喜欢

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

navicat数据库建表是没有utf8选项。

Multi terminal collaboration of Huawei accounts to create a better internet life

MySQL access denied, opened as Administrator

一篇文章读懂关于企业IM的所有知识点

Open source of xinzhibao applet

【刷题篇】避免洪水泛滥
![[deep anatomy of C language] storage principle of float variable in memory & comparison between pointer variable and](/img/3d/5d7fafba4ff7903afbd51d6d58dcdf.png)
[deep anatomy of C language] storage principle of float variable in memory & comparison between pointer variable and "zero value"

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

All the abnormal knowledge you want is here
随机推荐
防火墙基础之总部双机热备与分支基础配置
2022-06-23 帆软部分公式及sql生成(月份、季度取数)
一条查询SQL是如何执行的
JS converts an array to a two-dimensional array based on the same value
逆向调试入门-PE中的VA与RVA换算04/07
MySQL queries the data within the radius according to the longitude and latitude, and draws a circle to query the database
How can c write an SQL parser
WTM major updates, multi tenancy and single sign on
今日睡眠质量记录80分
SQL编程问题,测试用例不通过
服务线上治理
【C】 In depth understanding of pointers and callback functions (Introduction to simulating qsort)
In line with the trend of media integration, Zhongke Wenge and Meishe jointly create digital intelligence media publicity
Development of unity script program
Kubeedge's core philosophy
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
Data Lake (11): Iceberg table data organization and query
JS method of changing two-dimensional array to one-dimensional array
SQL考勤统计月报表
独立站即web3.0,国家“十四五“规划要求企业建数字化网站!