当前位置:网站首页>leetcode:1562. 查找大小为 M 的最新分组【模拟 + 端点记录 + 范围合并】
leetcode:1562. 查找大小为 M 的最新分组【模拟 + 端点记录 + 范围合并】
2022-08-01 00:55:00 【白速龙王的回眸】

分析
建立数字points
每个元素代表的是第几个位置的含1的连续段的开头和结尾
然后每加入一个新的相当于从0变1,考虑和左边和右边的合并
可能会剪掉两个合理的块
以及可能会加上一个合理的块
最后如果存在M的1块,可以计入结果
最后更新两个端点的left和right即可
因为中间的不可能再被用到~
ac code
class Solution:
def findLatestStep(self, arr: List[int], m: int) -> int:
# 模拟
n = len(arr)
# fuck[i]表示包含i在内的连续1的最左侧和最右侧
# [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
总结
强悍模拟 + 端点记录 + 区间范围合并大法
边栏推荐
猜你喜欢

MYSQL主从复制

Notes on how to use zeno

In 2022, the latest eight Chongqing construction members (electrical construction workers) simulation question bank and answers

Application of integrated stepper motor in UAV automatic airport

Super like the keyboard made from zero, IT people love it

Recommendation system: Summary of common evaluation indicators [accuracy rate, precision rate, recall rate, hit rate, (normalized depreciation cumulative gain) NDCG, mean reciprocal ranking (MRR), ROC

Rainbow share | how to use moving targets defense technology to guard against the unknown

TFC CTF 2022 WEB Diamand WriteUp

Southern University of Science and Technology: Xiaoying Tang | AADG: Automatic Enhancement for Generalization in the Field of Retinal Image Segmentation

一体化步进电机在无人机自动机场的应用
随机推荐
如何设计高可用高性能中间件 - 作业
现代企业架构框架1
Rasa 3.x 学习系列-使用查找表改进实体提取
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
500 miles
MYSQL查询截取优化分析
简单的vim配置
Xinao Learning Plan The Road to Informatics Competition (2022.07.31)
Kyoto University: Masaki Waga | Dynamic Masking for Reinforcement Learning in Black Box Environments
Exam preparation plan
值传递还是引用传递(By Value or By Reference)
Rainbow share | how to use moving targets defense technology to guard against the unknown
命名实体识别-模型:BERT-MRC
RTL8762DK WDG(六)
Notes on how to use zeno
/usr/sbin/vmware-authdlauncher: error while loading shared libraries: libssl.so.1.0.2*解决办法
Web API Introduction and Types
MYSQL逻辑架构
[1161. The maximum sum of elements in the layer]
从零造键盘的键盘超级喜欢,IT人最爱