Skip to content

Commit 3142bc6

Browse files
authored
Create 2055.Plates-Between-Candles.cpp
1 parent d8ee01d commit 3142bc6

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
class Solution {
2+
public:
3+
vector<int> platesBetweenCandles(string s, vector<vector<int>>& queries)
4+
{
5+
int n = s.size();
6+
vector<int>presum(n);
7+
int count = 0;
8+
for (int i=0; i<n; i++)
9+
{
10+
count += (s[i]=='*');
11+
presum[i] = count;
12+
}
13+
14+
vector<int>last(n);
15+
int t = -1;
16+
for (int i=0; i<n; i++)
17+
{
18+
if (s[i]=='|')
19+
t = i;
20+
last[i] = t;
21+
}
22+
23+
vector<int>next(n);
24+
t = n;
25+
for (int i=n-1; i>=0; i--)
26+
{
27+
if (s[i]=='|')
28+
t = i;
29+
next[i] = t;
30+
}
31+
32+
vector<int>rets;
33+
for (auto q: queries)
34+
{
35+
int a = q[0], b = q[1];
36+
int x = next[a], y = last[b];
37+
38+
if (x<=y && x>=a && y<=b)
39+
rets.push_back(presum[y] - presum[x]);
40+
else
41+
rets.push_back(0);
42+
}
43+
44+
return rets;
45+
}
46+
};

0 commit comments

Comments
 (0)