当前位置:网站首页>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
总结
二分和贪心
边栏推荐
猜你喜欢

huato hot update environment construction (DLL method hot update C# code)

win下搭建php环境的方法

大型综合办公管理系统源码(OA+HR+CRM)源码免费分享

你是一流的输家,你因此成为一流的赢家

【HMS core】【FAQ】A collection of typical questions about Account, IAP, Location Kit and HarmonyOS 1

Visual Studio编辑器 2019:scanf函数返回值被忽略(C4996)报错及解决办法
![[NCTF2019]Fake XML cookbook-1|XXE漏洞|XXE信息介绍](/img/29/92b9d52d17a203b8bdead3eb2c902e.png)
[NCTF2019]Fake XML cookbook-1|XXE漏洞|XXE信息介绍

Nervegrowold d2l (7) kaggle housing forecast model, numerical stability and the initialization and activation function

归一化与标准化

如何写一份高可读性的软件工程设计文档
随机推荐
归一化与标准化
京东获取推荐商品列表 API
DTSE Tech Talk丨第2期:1小时深度解读SaaS应用系统设计
lotus 1.16.0 最小快照导出 导入
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.
Qt 容器控件Tool Box 使用详解
对话框 QDialog ( 详解 )
PHP留言反馈管理系统源码
You are a first-class loser, you become a first-class winner
MySQL详细学习教程(建议收藏)
Mysql进阶优化篇01——四万字详解数据库性能分析工具(深入、全面、详细,收藏备用)
Large-scale integrated office management system source code (OA+HR+CRM) source code sharing for free
探究CSAPP实验二-bomb lab-第一节
Gvim order record
The case of five little pigs (five little pigs compare the size of the body weight)
Leetcode 119. Yang Hui's Triangle II
23. Please talk about the difference between IO synchronization, asynchronous, blocking and non-blocking
vivo announced to extend the product warranty period, the system launched a variety of functional services
Google Cloud Spanner的实践经验
李沐d2l(七)kaggle房价预测+数值稳定性+模型初始化和激活函数