当前位置:网站首页>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
总结
二分和贪心
边栏推荐
- 华为云数据治理生产线DataArts,让“数据‘慧’说话”
- Goland opens file saving and automatically formats
- (1) Cloud computing technology learning - virtualized vSphere learning
- Chapter 5 Advanced SQL Processing
- Security business revenue growth rate exceeds 70% 360 builds digital security leader
- data storage
- 【SOC】Classic output hello world
- Rounding out the most practical way of several DLL injection
- 云厂商做生态需要“真连接、真赋能”,用“技术+真金实银”发展伙伴
- Leetcode 119. Yang Hui's Triangle II
猜你喜欢
随机推荐
Login Module Debugging - Getting Started with Software Debugging
lotus 1.16.0 最小快照导出 导入
《痞子衡嵌入式半月刊》 第 59 期
lotus 爆块失败
@Bean注解详解
归一化与标准化
云风:不加班、不炫技,把复杂的问题简单化
Large-scale integrated office management system source code (OA+HR+CRM) source code sharing for free
初识二叉搜索树
PyQt5快速开发与实战 9.2 数据库处理
字符串加千分位符与递归数组求和
Gorilla Mux 和 GORM 的使用方法
MySQL索引常见面试题(2022版)
MySql统计函数COUNT详解
Explore CSAPP Experiment 2-bomb lab-Section 1
Nervegrowold d2l (7) kaggle housing forecast model, numerical stability and the initialization and activation function
node.js中怎么连接redis?
torch.optim.Adam() 函数用法
微信小程序picker滚动选择器使用详解
swagger使用教程——快速使用swagger









