LeetCode 三月份每日一题

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
from typing import List


class Solution:
def platesBetweenCandles(self, s: str, queries: List[List[int]]) -> List[int]:
res = [0] * len(queries)
preSum, preSumArray = 0, [0] * len(s)
leftCandle, leftArray = -1, [0] * len(s)
for i in range(len(s)):
if s[i] == '*':
preSum += 1
else:
leftCandle = i
preSumArray[i] = preSum
leftArray[i] = leftCandle

rightCandle, rightArray = len(s) - 1, [0] * len(s)
for i in range(len(s) - 1, -1, -1):
if s[i] == '|':
rightCandle = i
rightArray[i] = rightCandle

for i in range(len(queries)):
leftIndex = rightArray[queries[i][0]]
rightIndex = leftArray[queries[i][1]]
if leftIndex < rightIndex:
res[i] = preSumArray[rightIndex] - preSumArray[leftIndex]

return res


if __name__ == '__main__':
solu = Solution()
str1 = "**|**|***|"
print(solu.platesBetweenCandles(str1, [[2, 5], [5, 9]]))


版权声明: 本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!