当前位置:网站首页>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
边栏推荐
- Cmake introductory study notes
- MYSQL主从复制
- SVN server construction + SVN client + TeamCity integrated environment construction + VS2019 development
- Js replication
- GDB source code analysis series of articles five: dynamic library delay breakpoint implementation mechanism
- 类和对象:上
- Binary tree traversal non-recursive program -- using stack to simulate system stack
- Key Points Estimation and Point Instance
- cobaltstrike
- 500 miles
猜你喜欢
随机推荐
STK8321 I2C(昇佳-加速度传感器)示例
vector的基本实现
leetcode:1648. 销售价值减少的颜色球【二分找边界】
[Reading Notes -> Data Analysis] 02 Data Analysis Preparation
MYSQL二阶段提交
Web API 介绍和类型
虚继承的原理
声称AI存在意识,谷歌工程师遭解雇:违反保密协议
vim的基本使用概念
Modern Enterprise Architecture Framework 1
[Microservice] Distributed Transaction Solution - Seata
【 】 today in history: on July 31, "brains in vats" the birth of the participant;The father of wi-fi was born;USB 3.1 standard
OSF一分钟了解敏捷开发模式
Classes and Objects: Medium
MVCC总结
RTL8762DK Lighting/LED (3)
500 miles
RTL8762DK WDG(六)
Blueprint: Yang Hui's Triangular Arrangement
Key Points Estimation and Point Instance








