File tree Expand file tree Collapse file tree 1 file changed +46
-0
lines changed
Greedy/2055.Plates-Between-Candles Expand file tree Collapse file tree 1 file changed +46
-0
lines changed Original file line number Diff line number Diff line change 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+ };
You can’t perform that action at this time.
0 commit comments