当前位置:网站首页>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
总结
强悍模拟 + 端点记录 + 区间范围合并大法
边栏推荐
- LeetCode--The problem of robbery
- [微服务]分布式事务解决方案-Seata
- Rasa 3.x Study Series - Rasa - Issues 4898 Study Notes
- Southern University of Science and Technology: Xiaoying Tang | AADG: Automatic Enhancement for Generalization in the Field of Retinal Image Segmentation
- Design the message queue storage MySQL form of message data
- vim的基本使用概念
- Matlab / ArcGIS 处理GPM全球月均降水数据
- pycaret源码分析:下载数据集\Lib\site-packages\pycaret\datasets.py
- 二叉树遍历非递归程序 -- 使用栈模拟系统栈
- js 实现复制功能
猜你喜欢
Automated machine learning pycaret: PyCaret Basic Auto Classification LightGBM
Key Points Estimation and Point Instance
类和对象:上
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
ROS2系列知识(4): 理解【服务】的概念
Matlab / ArcGIS 处理GPM全球月均降水数据
How to Design High Availability and High Performance Middleware - Homework
声称AI存在意识,谷歌工程师遭解雇:违反保密协议
[MATLAB project combat] LDPC-BP channel coding
【Cryptography/Cryptanalysis】Cryptanalysis method based on TMTO
随机推荐
WeChat applet page syntax
Force buckle 2326, 197
MVCC总结
RTL8762DK UART(二)
力扣2326、197
从零造键盘的键盘超级喜欢,IT人最爱
Blueprint: Yang Hui's Triangular Arrangement
MYSQL事务
SC7A20(士兰微-加速度传感器)示例
Js replication
Classes and Objects: Medium
date命令
/usr/local/bin和/usr/bin的区别
Introduction to the five data types of Redis
[Reading Notes -> Data Analysis] 02 Data Analysis Preparation
RTL8762DK 使用DebugAnalyzer(四)
Carefully organize 16 MySQL usage specifications to reduce problems by 80% and recommend sharing with the team
LeetCode--打家劫舍问题
Matlab / ArcGIS 处理GPM全球月均降水数据
zeno使用方法笔记