LC.P1375[二进制字符串前缀一致的次数]

方法一:维护最大值

1
2
3
4
5
6
7
8
9
10
class Solution {
public int numTimesAllBlue(int[] flips) {
int ans = 0, max = 0, n = flips.length;
for (int i = 0; i < n; ++i) {
max = Math.max(max, flips[i]);
if (max == i + 1) ++ans;
}
return ans;
}
}
  • 时间复杂度:$O(n)$
  • 空间复杂度:$O(1)$