forked from wisdompeak/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2055.Plates-Between-Candles.cpp
46 lines (41 loc) · 1.03 KB
/
2055.Plates-Between-Candles.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
class Solution {
public:
vector<int> platesBetweenCandles(string s, vector<vector<int>>& queries)
{
int n = s.size();
vector<int>presum(n);
int count = 0;
for (int i=0; i<n; i++)
{
count += (s[i]=='*');
presum[i] = count;
}
vector<int>last(n);
int t = -1;
for (int i=0; i<n; i++)
{
if (s[i]=='|')
t = i;
last[i] = t;
}
vector<int>next(n);
t = n;
for (int i=n-1; i>=0; i--)
{
if (s[i]=='|')
t = i;
next[i] = t;
}
vector<int>rets;
for (auto q: queries)
{
int a = q[0], b = q[1];
int x = next[a], y = last[b];
if (x<=y && x>=a && y<=b)
rets.push_back(presum[y] - presum[x]);
else
rets.push_back(0);
}
return rets;
}
};