当前位置:网站首页>leetcode: 1562. Find latest grouping of size M [simulation + endpoint record + range merge]
leetcode: 1562. Find latest grouping of size M [simulation + endpoint record + range merge]
2022-08-01 01:03:00 【Looking Back at the White Speed Dragon King】
分析
Build numberspoints
Each element represents the containing of the first position1the beginning and end of a continuous segment
Then each adding a new equivalent slave0变1,Consider merging with left and right
Might cut two reasonable chunks
as well as possibly adding a reasonable block
最后如果存在M的1块,can be included in the results
Finally update the two endpointsleft和right即可
Because the middle one cannot be used again~
ac code
class Solution:
def findLatestStep(self, arr: List[int], m: int) -> int:
# 模拟
n = len(arr)
# fuck[i]表示包含i在内的连续1left and right
# [1..n]
fuck = [(-1, -1) for _ in range(n + 1)]
cntM = 0
ans = -1
for i in range(n):
# 初次来到
left = right = arr[i]
# 左侧1合并
if arr[i] >= 1 and fuck[arr[i] - 1][1] != -1:
# 更新left
left = fuck[arr[i] - 1][0]
# 更新cntM
if fuck[arr[i] - 1][1] - fuck[arr[i] - 1][0] + 1 == m:
cntM -= 1
# 右侧1合并
if arr[i] < n and fuck[arr[i] + 1][0] != -1:
# 更新right
right = fuck[arr[i] + 1][1]
# 更新cntM
if fuck[arr[i] + 1][1] - fuck[arr[i] + 1][0] + 1 == m:
cntM -= 1
if right - left + 1 == m:
cntM += 1
if cntM > 0:
ans = i + 1
fuck[left] = fuck[right] = (left, right)
return ans
总结
Powerful Simulation + 端点记录 + Interval range merge Dafa
边栏推荐
- 自动化机器学习pycaret: PyCaret Basic Auto Classification LightGBM
- MYSQL索引解析
- Application of integrated stepper motor in UAV automatic airport
- WindowInsetsControllerCompat is simple to use
- OSF一分钟了解敏捷开发模式
- RTL8762DK 使用DebugAnalyzer(四)
- Rasa 3.x Learning Series - Using Lookup Tables to Improve Entity Extraction
- RTL8762DK PWM(七)
- MYSQL事务
- Carefully summarize thirteen suggestions to help you create more suitable MySQL indexes
猜你喜欢
Introduction to the five data types of Redis
Super like the keyboard made from zero, IT people love it
Key Points Estimation and Point Instance
清华大学陈建宇教授团队 | 基于接触丰富机器人操作的接触安全强化学习框架
MYSQL查询截取优化分析
南方科技大学:Xiaoying Tang | AADG:视网膜图像分割领域泛化的自动增强
RTL8762DK UART(二)
【 】 today in history: on July 31, "brains in vats" the birth of the participant;The father of wi-fi was born;USB 3.1 standard
RTL8762DK Lighting/LED (3)
WeChat applet page syntax
随机推荐
pycaret源码分析:下载数据集\Lib\site-packages\pycaret\datasets.py
cmake入门学习笔记
Google "Cloud Developer Quick Checklist"; Tsinghua 3D Human Body Dataset; SenseTime "Universal Vision Framework" open class; Web3 Minimalist Getting Started Guide; Free Books for Efficient Deep Learni
命名实体识别-模型:BERT-MRC
[Microservice] Distributed Transaction Solution - Seata
Blueprint: Yang Hui's Triangular Arrangement
Qlib quantitative source analysis: qlib/qlib/contrib/model/GBDT py
Luogu P3373: 线段树
TFC CTF 2022 WEB Diamand WriteUp
Nmap 操作手册 - 完整版
TFC CTF 2022 WEB Diamand WriteUp
leetcode:1648. 销售价值减少的颜色球【二分找边界】
Academicians of the two academies speak bluntly: Don't be superstitious about academicians
Rasa 3.x Study Series - Rasa - Issues 4898 Study Notes
Redis五种数据类型简介
类和对象:上
cobaltstrike
Web3.0: Building an NFT Market (1)
[Reading Notes -> Data Analysis] 02 Data Analysis Preparation
How to Design High Availability and High Performance Middleware - Homework