设置两个状态array,分别记录当前的位置之前,有多少人头非增加的天数,和此位置之后人头非减少的天数。处理后一个array时,反过来处理,其实也就是反着看security在当前位置,有多少非增加的天数。然后,再次遍历security位置,看每个位置是否满足三条件,后两个条件可以直接查两个状态array得到。
class Solution:
def goodDaysToRobBank(self, security: List[int], time: int) -> List[int]:
increasing = [0 for _ in range(len(security))]
decreasing = [0 for _ in range(len(security))]
for idx in range(1, len(security)):
if security[idx-1] >= security[idx]:
increasing[idx] = increasing[idx-1] + 1
else:
increasing[idx] = 0
for idx in range(len(security)-2, -1, -1):
if security[idx] <= security[idx+1]:
decreasing[idx] = decreasing[idx+1] + 1
else:
decreasing[idx] = 0
res = []
for idx in range(len(security)):
if idx - time >= 0 and len(security) -1 - idx >= time and increasing[idx]>= time and decreasing[idx]>= time:
res.append(idx)
print(increasing)
print(decreasing)
return res
|