Skip to content

Commit 6931406

Browse files
Added task 2055.
1 parent 89bf689 commit 6931406

File tree

3 files changed

+115
-0
lines changed

3 files changed

+115
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package g2001_2100.s2055_plates_between_candles;
2+
3+
// #Medium #Array #String #Binary_Search #Prefix_Sum
4+
// Time_10_ms_(92.49%)_Space_164.8_MB_(5.53%)
5+
6+
public class Solution {
7+
public int[] platesBetweenCandles(String s, int[][] queries) {
8+
char[] sa = s.toCharArray();
9+
int n = sa.length;
10+
int[] nextCandle = new int[n + 1];
11+
nextCandle[n] = -1;
12+
for (int i = n - 1; i >= 0; i--) {
13+
nextCandle[i] = sa[i] == '|' ? i : nextCandle[i + 1];
14+
}
15+
int[] prevCandle = new int[n];
16+
int[] prevPlates = new int[n];
17+
int countPlate = 0;
18+
if (sa[0] == '*') {
19+
countPlate = 1;
20+
prevCandle[0] = -1;
21+
}
22+
for (int i = 1; i < n; i++) {
23+
if (sa[i] == '|') {
24+
prevCandle[i] = i;
25+
prevPlates[i] = countPlate;
26+
} else {
27+
prevCandle[i] = prevCandle[i - 1];
28+
countPlate++;
29+
}
30+
}
31+
int len = queries.length;
32+
int[] res = new int[len];
33+
for (int i = 0; i < len; i++) {
34+
int[] query = queries[i];
35+
int start = query[0];
36+
int end = query[1];
37+
int curPlates = 0;
38+
int endCandle = prevCandle[end];
39+
if (endCandle >= start) {
40+
int startCandle = nextCandle[start];
41+
42+
curPlates = prevPlates[endCandle] - prevPlates[startCandle];
43+
}
44+
res[i] = curPlates;
45+
}
46+
return res;
47+
}
48+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2055\. Plates Between Candles
2+
3+
Medium
4+
5+
There is a long table with a line of plates and candles arranged on top of it. You are given a **0-indexed** string `s` consisting of characters `'*'` and `'|'` only, where a `'*'` represents a **plate** and a `'|'` represents a **candle**.
6+
7+
You are also given a **0-indexed** 2D integer array `queries` where <code>queries[i] = [left<sub>i</sub>, right<sub>i</sub>]</code> denotes the **substring** <code>s[left<sub>i</sub>...right<sub>i</sub>]</code> (**inclusive**). For each query, you need to find the **number** of plates **between candles** that are **in the substring**. A plate is considered **between candles** if there is at least one candle to its left **and** at least one candle to its right **in the substring**.
8+
9+
* For example, `s = "||**||**|*"`, and a query `[3, 8]` denotes the substring `"*||******|"`. The number of plates between candles in this substring is `2`, as each of the two plates has at least one candle **in the substring** to its left **and** right.
10+
11+
Return _an integer array_ `answer` _where_ `answer[i]` _is the answer to the_ <code>i<sup>th</sup></code> _query_.
12+
13+
**Example 1:**
14+
15+
![ex-1](https://assets.leetcode.com/uploads/2021/10/04/ex-1.png)
16+
17+
**Input:** s = "\*\*|\*\*|\*\*\*|", queries = [[2,5],[5,9]]
18+
19+
**Output:** [2,3]
20+
21+
**Explanation:** - queries[0] has two plates between candles. - queries[1] has three plates between candles.
22+
23+
**Example 2:**
24+
25+
![ex-2](https://assets.leetcode.com/uploads/2021/10/04/ex-2.png)
26+
27+
**Input:** s = "\*\*\*|\*\*|\*\*\*\*\*|\*\*||\*\*|\*", queries = [[1,17],[4,5],[14,17],[5,11],[15,16]]
28+
29+
**Output:** [9,0,0,0,0]
30+
31+
**Explanation:** - queries[0] has nine plates between candles. - The other queries have zero plates between candles.
32+
33+
**Constraints:**
34+
35+
* <code>3 <= s.length <= 10<sup>5</sup></code>
36+
* `s` consists of `'*'` and `'|'` characters.
37+
* <code>1 <= queries.length <= 10<sup>5</sup></code>
38+
* `queries[i].length == 2`
39+
* <code>0 <= left<sub>i</sub> <= right<sub>i</sub> < s.length</code>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package g2001_2100.s2055_plates_between_candles;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import com_github_leetcode.CommonUtils;
7+
import org.junit.jupiter.api.Test;
8+
9+
class SolutionTest {
10+
@Test
11+
void platesBetweenCandles() {
12+
int[][] queries =
13+
CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray("[2,5],[5,9]");
14+
assertThat(
15+
new Solution().platesBetweenCandles("**|**|***|", queries),
16+
equalTo(new int[] {2, 3}));
17+
}
18+
19+
@Test
20+
void platesBetweenCandles2() {
21+
int[][] queries =
22+
CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray(
23+
"[1,17],[4,5],[14,17],[5,11],[15,16]");
24+
assertThat(
25+
new Solution().platesBetweenCandles("***|**|*****|**||**|*", queries),
26+
equalTo(new int[] {9, 0, 0, 0, 0}));
27+
}
28+
}

0 commit comments

Comments
 (0)