当前位置:网站首页>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
总结
强悍模拟 + 端点记录 + 区间范围合并大法
边栏推荐
- Blueprint: Yang Hui's Triangular Arrangement
- 500 miles
- pycaret source code analysis: download dataset\Lib\site-packages\pycaret\datasets.py
- 考研备考方案
- pycaret源码分析:下载数据集\Lib\site-packages\pycaret\datasets.py
- Nmap Operation Manual - Full Version
- WeChat applet page syntax
- 如何设计高可用高性能中间件 - 作业
- [Microservice] Distributed Transaction Solution - Seata
- WindowInsetsControllerCompat简单使用
猜你喜欢
cobaltstrike
Redis五种数据类型简介
[MATLAB project combat] LDPC-BP channel coding
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
Super like the keyboard made from zero, IT people love it
一体化步进电机在无人机自动机场的应用
Likou Binary Tree
How to Design High Availability and High Performance Middleware - Homework
Kyoto University: Masaki Waga | Dynamic Masking for Reinforcement Learning in Black Box Environments
两院院士直言:不要迷信院士
随机推荐
WindowInsetsControllerCompat is simple to use
Xinao Learning Plan The Road to Informatics Competition (2022.07.31)
Carefully organize 16 MySQL usage specifications to reduce problems by 80% and recommend sharing with the team
Rainbow share | how to use moving targets defense technology to guard against the unknown
Carefully summarize thirteen suggestions to help you create more suitable MySQL indexes
WAASAP WebClient UI页面标签的决定逻辑介绍
MYSQL事务
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
Likou Binary Tree
Interview Question: Implementing Deadlocks
vector的基本实现
Team of Professor Chen Jianyu of Tsinghua University | Contact Safety Reinforcement Learning Framework Based on Contact-rich Robot Operation
Rasa 3.x Study Series - Rasa - Issues 4898 Study Notes
Notes on how to use zeno
虚继承的原理
Interview Blitz 69: Is TCP Reliable?Why?
ECCV2022 Workshop | 复杂环境中的多目标跟踪和分割
qlib量化源码分析:qlib/qlib/contrib/model/gbdt.py
网关gateway跨域
TFC CTF 2022 WEB Diamand WriteUp