蜡烛之间的盘子
难度:
标签:
题目描述
给你一个长桌子,桌子上盘子和蜡烛排成一列。给你一个下标从 0 开始的字符串 s
,它只包含字符 '*'
和 '|'
,其中 '*'
表示一个 盘子 ,'|'
表示一支 蜡烛 。
同时给你一个下标从 0 开始的二维整数数组 queries
,其中 queries[i] = [lefti, righti]
表示 子字符串 s[lefti...righti]
(包含左右端点的字符)。对于每个查询,你需要找到 子字符串中 在 两支蜡烛之间 的盘子的 数目 。如果一个盘子在 子字符串中 左边和右边 都 至少有一支蜡烛,那么这个盘子满足在 两支蜡烛之间 。
- 比方说,
s = "||**||**|*"
,查询[3, 8]
,表示的是子字符串"*||**|"
。子字符串中在两支蜡烛之间的盘子数目为2
,子字符串中右边两个盘子在它们左边和右边 都 至少有一支蜡烛。
请你返回一个整数数组 answer
,其中 answer[i]
是第 i
个查询的答案。
示例 1:
输入:s = "**|**|***|", queries = [[2,5],[5,9]] 输出:[2,3] 解释: - queries[0] 有两个盘子在蜡烛之间。 - queries[1] 有三个盘子在蜡烛之间。
示例 2:
输入:s = "***|**|*****|**||**|*", queries = [[1,17],[4,5],[14,17],[5,11],[15,16]] 输出:[9,0,0,0,0] 解释: - queries[0] 有 9 个盘子在蜡烛之间。 - 另一个查询没有盘子在蜡烛之间。
提示:
3 <= s.length <= 105
s
只包含字符'*'
和'|'
。1 <= queries.length <= 105
queries[i].length == 2
0 <= lefti <= righti < s.length
代码结果
运行时间: 164 ms, 内存: 47.7 MB
// 思路:使用Java Stream API进行处理,我们可以通过stream的方式进行字符串的处理和数据的计算,但考虑到效率问题,主要逻辑不变。
import java.util.Arrays;
public int[] platesBetweenCandles(String s, int[][] queries) {
int n = s.length();
int[] leftCandle = new int[n];
int[] rightCandle = new int[n];
int[] platesCount = new int[n];
int[] result = new int[queries.length];
Arrays.fill(leftCandle, -1);
Arrays.fill(rightCandle, -1);
// Finding leftmost candles
Arrays.stream(s.split(""))
.forEachIndexed((i, c) -> {
if (c.equals("|")) {
leftCandle[i] = i;
} else if (i > 0) {
leftCandle[i] = leftCandle[i - 1];
}
});
// Finding rightmost candles
Arrays.stream(s.split(""))
.collect(Collectors.toCollection(LinkedList::new))
.descendingIterator()
.forEachRemaining((c, i) -> {
if (c.equals("|")) {
rightCandle[i] = i;
} else if (i < n - 1) {
rightCandle[i] = rightCandle[i + 1];
}
});
// Counting plates
Arrays.stream(s.split(""))
.forEachIndexed((i, c) -> {
if (c.equals("*")) {
platesCount[i] = platesCount[i - 1] + 1;
} else if (i > 0) {
platesCount[i] = platesCount[i - 1];
}
});
// Calculating results
for (int i = 0; i < queries.length; i++) {
int leftIndex = rightCandle[queries[i][0]];
int rightIndex = leftCandle[queries[i][1]];
if (leftIndex != -1 && rightIndex != -1 && leftIndex <= rightIndex) {
result[i] = platesCount[rightIndex] - platesCount[leftIndex];
}
}
return result;
}
解释
方法:
此题解采用了预处理数组的方法来优化查询。首先,使用前缀和数组 preSum 来记录从字符串开头到当前位置的盘子数量。其次,使用两个数组 left 和 right 分别存储每个位置左侧最近的蜡烛位置和右侧最近的蜡烛位置。通过这种方式,对于每一个查询,可以快速找到查询范围内左侧和右侧的第一个蜡烛,并计算这两个蜡烛之间的盘子数量。
时间复杂度:
O(n + q)
空间复杂度:
O(n)
代码细节讲解
🦆
为什么需要使用前缀和数组来记录盘子的数量?直接在处理查询时统计不可以吗?
▷🦆
在构建left和right数组时,如果在s字符串的起始或结束位置很长一段没有蜡烛,这种情况下如何保证算法的正确性?
▷🦆
请问为什么要从右向左再次遍历字符串s来构建right数组?
▷🦆
在处理查询时,如果查询区间内没有蜡烛怎么处理?
▷