当前位置:网站首页>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
边栏推荐
- Item 36: Specify std::launch::async if asynchronicity is essential.
- Binary tree traversal non-recursive program -- using stack to simulate system stack
- cobaltstrike
- cmake入门学习笔记
- Compose principle - the view and the principle of two-way data binding
- date命令
- [AMEX] LGBM Optuna American Express Credit Card Fraud Contest kaggle
- 500 miles
- MYSQL查询截取优化分析
- Interview Question: Implementing Deadlocks
猜你喜欢
![[Reading Notes -> Data Analysis] 02 Data Analysis Preparation](/img/e7/258daf851746cb043f301437ee3bbe.png)
[Reading Notes -> Data Analysis] 02 Data Analysis Preparation

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

推荐系统:常用评价指标总结【准确率、精确率、召回率、命中率、(归一化折损累计增益)NDCG、平均倒数排名(MRR)、ROC曲线、AUC(ROC曲线下的面积)、P-R曲线、A/B测试】

SVN server construction + SVN client + TeamCity integrated environment construction + VS2019 development

ROS2系列知识(4): 理解【服务】的概念

如何设计高可用高性能中间件 - 作业

RTL8762DK WDG(六)

你需要知道的 TCP 四次挥手

欧拉系统(euleros):升级Mysql

Classes and Objects: Medium
随机推荐
虚继承的原理
Modern Enterprise Architecture Framework 1
vector的基本实现
二叉树遍历非递归程序 -- 使用栈模拟系统栈
Matlab/ArcGIS processing GPM global monthly precipitation data
2022年最新重庆建筑八大员(电气施工员)模拟题库及答案
NIO编程
Likou Binary Tree
设计消息队列存储消息数据的MySQL表格
Key Points Estimation and Point Instance
Exam preparation plan
力扣二叉树
【历史上的今天】7 月 31 日:“缸中之脑”的提出者诞生;Wi-Fi 之父出生;USB 3.1 标准发布
Force buckle 2326, 197
Keil nRF52832 download failed
[1161. The maximum sum of elements in the layer]
WindowInsetsControllerCompat简单使用
Rasa 3.x 学习系列- Rasa - Issues 4898 学习笔记
七月集训(第31天) —— 状态压缩
南方科技大学:Xiaoying Tang | AADG:视网膜图像分割领域泛化的自动增强