当前位置:网站首页>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
总结
二分和贪心
边栏推荐
- C语言学习之旅 【函数(二)】
- Test Management and Specification
- Invalid or corrupt jarfile xxx.jar
- The service already exists! Solution
- 对话框 QDialog ( 详解 )
- Login Module Debugging - Getting Started with Software Debugging
- Horizontal Pod Autoscaler(HPA)
- SwiftUI SQLite教程之带有历史的搜索栏List App (教程含完整代码)
- 【SOC FPGA】Peripheral KEY LED
- LeetCode167:有序数组两数之和
猜你喜欢
![[TypeScript] Introduction, Development Environment Construction, Basic Types](/img/d7/b3175ab538906ac1b658a9f361ba44.png)
[TypeScript] Introduction, Development Environment Construction, Basic Types

MySql统计函数COUNT详解

DTSE Tech Talk丨第2期:1小时深度解读SaaS应用系统设计

3D激光SLAM:LeGO-LOAM论文解读---系统概述部分

数据库的三大范式

基于STM32F407使用ADC采集电压实验

Discuz杂志/新闻报道模板(jeavi_line)UTF8-GBK模板

Leetcode 118. Yanghui Triangle

DTSE Tech Talk丨第2期:1小时深度解读SaaS应用系统设计

李沐d2l(七)kaggle房价预测+数值稳定性+模型初始化和激活函数
随机推荐
Lotus explodes the block failed
(一)云计算技术学习--虚拟化vSphere学习
Test Management and Specification
Gorilla Mux 和 GORM 的使用方法
为人处世之道,与君共勉!
OpenCV形状检测
游戏窗口化的逆向分析
DTSE Tech Talk丨第2期:1小时深度解读SaaS应用系统设计
数据的存储
详解最实用的几种dll注入方式
CMake库搜索函数居然不搜索LD_LIBRARY_PATH
MySQL超详细安装教程 手把手教你安装MySQL到使用MySQL 最简单的MySQL安装方式,这种方式装,卸载也简单
vivo announced to extend the product warranty period, the system launched a variety of functional services
报错500,“message“: “nested exception is org.apache.ibatis.binding.BindingException: 解决记录
onenote使用
gvim命令记录
测试管理与规范
Public Key Retrieval is not allowed error solution
What does a good resume look like in the eyes of a big factory interviewer?
How to use Redis for distributed applications in Golang