当前位置:网站首页>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
边栏推荐
- Carefully organize 16 MySQL usage specifications to reduce problems by 80% and recommend sharing with the team
- Interview Question: Implementing Deadlocks
- RTL8762DK PWM(七)
- [Reading Notes -> Data Analysis] 02 Data Analysis Preparation
- vim的基本使用-底行模式
- Xinao Learning Plan The Road to Informatics Competition (2022.07.31)
- 精心总结十三条建议,帮你创建更合适的MySQL索引
- 两院院士直言:不要迷信院士
- 南方科技大学:Xiaoying Tang | AADG:视网膜图像分割领域泛化的自动增强
- 虚继承的原理
猜你喜欢
Matlab/Arcgis processing nc data
SC7A20(士兰微-加速度传感器)示例
Southern University of Science and Technology: Xiaoying Tang | AADG: Automatic Enhancement for Generalization in the Field of Retinal Image Segmentation
Rainbow share | how to use moving targets defense technology to guard against the unknown
清华大学陈建宇教授团队 | 基于接触丰富机器人操作的接触安全强化学习框架
RTL8762DK 使用DebugAnalyzer(四)
Likou Binary Tree
MYSQL-批量插入数据
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
[Cloud Residency Co-Creation] [HCSD Big Celebrity Live Broadcast] Personally teach the secrets of interviews in big factories
随机推荐
Compose principle - the view and the principle of two-way data binding
Cmake introductory study notes
When can I use PushGateway
ROS2系列知识(4): 理解【服务】的概念
cobaltstrike
Rasa 3.x 学习系列- Rasa - Issues 4918 学习笔记
Matlab/Arcgis processing nc data
Introduction to the five data types of Redis
[Cloud Residency Co-Creation] [HCSD Big Celebrity Live Broadcast] Personally teach the secrets of interviews in big factories
Team of Professor Chen Jianyu of Tsinghua University | Contact Safety Reinforcement Learning Framework Based on Contact-rich Robot Operation
sqlserver无法远程连接
VPGNet
TFC CTF 2022 WEB Diamand WriteUp
二叉树遍历非递归程序 -- 使用栈模拟系统栈
Basic use of vim - bottom line mode
[AMEX] LGBM Optuna American Express Credit Card Fraud Contest kaggle
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
蓝图:杨辉三角排列
Web3.0: Building an NFT Market (1)
Rasa 3.x Learning Series - Using Lookup Tables to Improve Entity Extraction