当前位置:网站首页>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
边栏推荐
- Rasa 3.x Study Series - Rasa - Issues 4898 Study Notes
- GDB source code analysis series of articles five: dynamic library delay breakpoint implementation mechanism
- 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
- qlib量化源码分析:qlib/qlib/contrib/model/gbdt.py
- 二叉树遍历非递归程序 -- 使用栈模拟系统栈
- Cmake introductory study notes
- 开源好用的 流程图绘制工具 drawio
- ECCV2022 Workshop | Multi-Object Tracking and Segmentation in Complex Environments
- NIO编程
- GDB 源码分析系列文章五:动态库延迟断点实现机制
猜你喜欢

VPGNet
![[AMEX] LGBM Optuna American Express Credit Card Fraud Contest kaggle](/img/64/55af53a3d9dc1162490d613fe8a436.png)
[AMEX] LGBM Optuna American Express Credit Card Fraud Contest kaggle

Notes on how to use zeno

Classes and Objects: Medium

Rainbow share | how to use moving targets defense technology to guard against the unknown
![[微服务]分布式事务解决方案-Seata](/img/a8/fc6c24e4d42dfb635bad786cc02164.png)
[微服务]分布式事务解决方案-Seata

从零造键盘的键盘超级喜欢,IT人最爱

谷歌『云开发者速查表』;清华3D人体数据集;商汤『通用视觉框架』公开课;Web3极简入门指南;高效深度学习免费书;前沿论文 | ShowMeAI资讯日报

一行代码解决CoreData托管对象属性变更在SwiftUI中无动画效果的问题

Blueprint: Yang Hui's Triangular Arrangement
随机推荐
[Microservice] Distributed Transaction Solution - Seata
JQESAP系统里的胖接口Fat interface
Force buckle 2326, 197
Xinao Learning Plan The Road to Informatics Competition (2022.07.31)
RTL8762DK RTC(五)
WebApi 打个Attribute 统一处理异常
Pylint检查规则中文版
TFC CTF 2022 WEB Diamand WriteUp
Key Points Estimation and Point Instance
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
ECCV2022 Workshop | Multi-Object Tracking and Segmentation in Complex Environments
[AMEX] LGBM Optuna American Express Credit Card Fraud Contest kaggle
SVN server construction + SVN client + TeamCity integrated environment construction + VS2019 development
设计消息队列存储消息数据的MySQL表格
Item 36: Specify std::launch::async if asynchronicity is essential.
OSF一分钟了解敏捷开发模式
Classes and Objects: Medium
Matlab/ArcGIS processing GPM global monthly precipitation data
GDB source code analysis series of articles five: dynamic library delay breakpoint implementation mechanism
To help the construction of digital government, the three parties of China Science and Technology build a domain name security system