leetcode在力扣 App 中打开
调试中...
调试中...
题目描述
题目描述
题解
题解
提交记录
提交记录
代码
代码
测试用例
测试用例
测试结果
测试结果
困难
相关标签
相关企业
提示

给你一个整数数组 colors 和一个二维整数数组 queriescolors表示一个由红色和蓝色瓷砖组成的环,第 i 块瓷砖的颜色为 colors[i] :

  • colors[i] == 0 表示第 i 块瓷砖的颜色是 红色 。
  • colors[i] == 1 表示第 i 块瓷砖的颜色是 蓝色 。

环中连续若干块瓷砖的颜色如果是 交替 颜色(也就是说这组瓷砖中除了第一块和最后一块瓷砖以外,中间瓷砖的颜色与它 左边 和 右边 的颜色都不同),那么它被称为一个 交替组

你需要处理两种类型的查询:

  • queries[i] = [1, sizei],确定大小为sizei 交替组 的数量。
  • queries[i] = [2, indexi, colori],将colors[indexi]更改为colori

返回数组 answer,数组中按顺序包含第一种类型查询的结果。

注意 ,由于 colors 表示一个  ,第一块 瓷砖和 最后一块 瓷砖是相邻的。

 

示例 1:

输入:colors = [0,1,1,0,1], queries = [[2,1,0],[1,4]]

输出:[2]

解释:

第一次查询:

colors[1] 改为 0。

第二次查询:

统计大小为 4 的交替组的数量:

示例 2:

输入:colors = [0,0,1,0,1,1], queries = [[1,3],[2,3,0],[1,5]]

输出:[2,0]

解释:

第一次查询:

统计大小为 3 的交替组的数量。

第二次查询:colors不变。

第三次查询:不存在大小为 5 的交替组。

 

提示:

  • 4 <= colors.length <= 5 * 104
  • 0 <= colors[i] <= 1
  • 1 <= queries.length <= 5 * 104
  • queries[i][0] == 1queries[i][0] == 2
  • 对于所有的i
    • queries[i][0] == 1queries[i].length == 2, 3 <= queries[i][1] <= colors.length - 1
    • queries[i][0] == 2queries[i].length == 3, 0 <= queries[i][1] <= colors.length - 1, 0 <= queries[i][2] <= 1
通过次数
880
提交次数
3.5K
通过率
25.0%

相关标签

相关企业

提示 1
Try using a segment tree to store the maximal alternating groups.

提示 2
Store the sizes of these maximal alternating groups in another data structure.

提示 3
Find the count of the alternating groups of size k with having the count of maximal alternating groups with size greater than or equal to k and the sum of their sizes.

评论 (0)

贡献者
© 2025 领扣网络(上海)有限公司
0 人在线
行 1,列 1
colors =
[0,1,1,0,1]
queries =
[[2,1,0],[1,4]]
Source