当前位置:网站首页>leetcode:1488. 避免洪水泛滥【二分 + 贪心】
leetcode:1488. 避免洪水泛滥【二分 + 贪心】
2022-07-30 16:48:00 【白速龙王的回眸】
分析
先把可以抽干的位置记录下来,用sortedlist的q,方便删除
然后用dict记录某个湖之前是否出现过
如果出现过肯定有pre和now的位置
然后根据pre和now找到q中对应的idx,直接改变这个idx对应的湖即可
模拟贪心二分吧
ac code
from sortedcontainers import SortedList
class Solution:
def avoidFlood(self, rains: List[int]) -> List[int]:
get_drys = SortedList()
n = len(rains)
ans = [-1] * n
for i in range(n):
if rains[i] == 0:
get_drys.add(i)
d = defaultdict(int)
for i in range(1, n + 1):
if rains[i - 1] > 0:
if d[rains[i - 1]] == 0:
d[rains[i - 1]] = i
else:
pre, now = d[rains[i - 1]] - 1, i - 1
idx1 = bisect_left(get_drys, pre)
idx2 = bisect_left(get_drys, now)
if idx1 == idx2: # no dry option
return []
else:
# pick the first one
idx3 = get_drys[idx1]
ans[idx3] = rains[i - 1]
get_drys.discard(idx3)
d[rains[i - 1]] = i
for i in range(n):
if rains[i] == 0 and ans[i] == -1:
ans[i] = 1
return ans
总结
二分和贪心
边栏推荐
- 代码越写越乱?那是因为你没用责任链
- [AGC] Quality Service 2 - Performance Management Example
- 镜像站收集
- 数据的存储
- Leetcode 119. 杨辉三角 II
- 基于STM32F407使用ADC采集电压实验
- Chapter 5 Advanced SQL Processing
- MySQL详细学习教程(建议收藏)
- The service already exists!解决办法
- Large-scale integrated office management system source code (OA+HR+CRM) source code sharing for free
猜你喜欢
Discuz magazine/news report template (jeavi_line) UTF8-GBK template
【SOC】经典输出hello world
huato hot update environment construction (DLL method hot update C# code)
【SOC FPGA】外设KEY点LED
论文阅读 (63):Get To The Point: Summarization with Pointer-Generator Networks
华为云数据治理生产线DataArts,让“数据‘慧’说话”
JVM学习----垃圾回收
【Linux操作系统】 虚拟文件系统 | 文件缓存
Gorilla Mux 和 GORM 的使用方法
华为云数据治理生产线DataArts,让“数据'慧'说话”
随机推荐
The first time I used debug query and found that this was empty, does it mean that the database has not been obtained yet?please help.
MySQL索引常见面试题(2022版)
PHP留言反馈管理系统源码
PyQt5快速开发与实战 9.2 数据库处理
阿里巴巴中国站获得1688商品分类 API
Visual Studio编辑器 2019:scanf函数返回值被忽略(C4996)报错及解决办法
论文阅读 (63):Get To The Point: Summarization with Pointer-Generator Networks
[NCTF2019] Fake XML cookbook-1|XXE vulnerability|XXE information introduction
归一化与标准化
Chapter 5 Advanced SQL Processing
哎,这要人老命的缓存一致问题啊
data storage
Goland opens file saving and automatically formats
游戏多开检测的几种实现方法及破解方法参考
《痞子衡嵌入式半月刊》 第 59 期
You are a first-class loser, you become a first-class winner
hcip--ospf综合实验
完美绕开CRC32检测的无痕hook
Qt 容器控件Tool Box 使用详解
3D激光SLAM:LeGO-LOAM论文解读---激光雷达里程计与建图