Skip to content

Commit d6547bd

Browse files
committed
feat: add solutions to lc problem: No.1375
No.1375.Number of Times Binary String Is Prefix-Aligned
1 parent bab55a2 commit d6547bd

File tree

6 files changed

+548
-0
lines changed

6 files changed

+548
-0
lines changed

solution/1300-1399/1375.Number of Times Binary String Is Prefix-Aligned/README.md

Lines changed: 254 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,22 +56,276 @@
5656

5757
<!-- 这里可写通用的实现逻辑 -->
5858

59+
**方法一:直接遍历**
60+
61+
遍历数组 $flips$,记录最大值,将最大值与当前遍历到的下标 $i$ 比较,若相等,答案累加。
62+
63+
时间复杂度 $O(n)$。
64+
65+
**方法二:树状数组**
66+
67+
时间复杂度 $O(nlogn)$。
68+
5969
<!-- tabs:start -->
6070

6171
### **Python3**
6272

6373
<!-- 这里可写当前语言的特殊实现逻辑 -->
6474

6575
```python
76+
class Solution:
77+
def numTimesAllBlue(self, flips: List[int]) -> int:
78+
ans = mx = 0
79+
for i, v in enumerate(flips, 1):
80+
mx = max(mx, v)
81+
if mx == i:
82+
ans += 1
83+
return ans
84+
```
6685

86+
```python
87+
class BinaryIndexedTree:
88+
def __init__(self, n):
89+
self.n = n
90+
self.c = [0] * (n + 1)
91+
92+
@staticmethod
93+
def lowbit(x):
94+
return x & -x
95+
96+
def update(self, x, delta):
97+
while x <= self.n:
98+
self.c[x] += delta
99+
x += BinaryIndexedTree.lowbit(x)
100+
101+
def query(self, x):
102+
s = 0
103+
while x > 0:
104+
s += self.c[x]
105+
x -= BinaryIndexedTree.lowbit(x)
106+
return s
107+
108+
109+
class Solution:
110+
def numTimesAllBlue(self, flips: List[int]) -> int:
111+
n = len(flips)
112+
tree = BinaryIndexedTree(n)
113+
ans = mx = 0
114+
for v in flips:
115+
mx = max(mx, v)
116+
tree.update(v, 1)
117+
if tree.query(mx) == mx:
118+
ans += 1
119+
return ans
67120
```
68121

69122
### **Java**
70123

71124
<!-- 这里可写当前语言的特殊实现逻辑 -->
72125

73126
```java
127+
class Solution {
128+
public int numTimesAllBlue(int[] flips) {
129+
int ans = 0;
130+
int mx = 0;
131+
for (int i = 1; i <= flips.length; ++i) {
132+
mx = Math.max(mx, flips[i - 1]);
133+
if (mx == i) {
134+
++ans;
135+
}
136+
}
137+
return ans;
138+
}
139+
}
140+
```
141+
142+
```java
143+
class BinaryIndexedTree {
144+
private int n;
145+
private int[] c;
146+
147+
public BinaryIndexedTree(int n) {
148+
this.n = n;
149+
this.c = new int[n + 1];
150+
}
151+
152+
public static int lowbit(int x) {
153+
return x & -x;
154+
}
155+
156+
public void update(int x, int delta) {
157+
while (x <= n) {
158+
c[x] += delta;
159+
x += lowbit(x);
160+
}
161+
}
162+
163+
public int query(int x) {
164+
int s = 0;
165+
while (x > 0) {
166+
s += c[x];
167+
x -= lowbit(x);
168+
}
169+
return s;
170+
}
171+
}
172+
173+
class Solution {
174+
public int numTimesAllBlue(int[] flips) {
175+
int n = flips.length;
176+
BinaryIndexedTree tree = new BinaryIndexedTree(n);
177+
int ans = 0;
178+
int mx = 0;
179+
for (int v : flips) {
180+
mx = Math.max(mx, v);
181+
tree.update(v, 1);
182+
if (tree.query(mx) == mx) {
183+
++ans;
184+
}
185+
}
186+
return ans;
187+
}
188+
}
189+
```
190+
191+
### **C++**
192+
193+
```cpp
194+
class Solution {
195+
public:
196+
int numTimesAllBlue(vector<int>& flips) {
197+
int ans = 0, mx = 0;
198+
for (int i = 1; i <= flips.size(); ++i)
199+
{
200+
mx = max(mx, flips[i - 1]);
201+
ans += mx == i;
202+
}
203+
return ans;
204+
}
205+
};
206+
```
207+
208+
```cpp
209+
class BinaryIndexedTree {
210+
public:
211+
int n;
212+
vector<int> c;
213+
214+
BinaryIndexedTree(int _n): n(_n), c(_n + 1){}
215+
216+
void update(int x, int delta) {
217+
while (x <= n)
218+
{
219+
c[x] += delta;
220+
x += lowbit(x);
221+
}
222+
}
223+
224+
int query(int x) {
225+
int s = 0;
226+
while (x > 0)
227+
{
228+
s += c[x];
229+
x -= lowbit(x);
230+
}
231+
return s;
232+
}
233+
234+
int lowbit(int x) {
235+
return x & -x;
236+
}
237+
};
238+
239+
class Solution {
240+
public:
241+
int numTimesAllBlue(vector<int>& flips) {
242+
int n = flips.size();
243+
BinaryIndexedTree* tree = new BinaryIndexedTree(n);
244+
int ans = 0, mx = 0;
245+
for (int v : flips)
246+
{
247+
mx = max(mx, v);
248+
tree->update(v, 1);
249+
ans += tree->query(mx) == mx;
250+
}
251+
return ans;
252+
}
253+
};
254+
```
255+
256+
### **Go**
257+
258+
```go
259+
func numTimesAllBlue(flips []int) int {
260+
ans, mx := 0, 0
261+
for i := 1; i <= len(flips); i++ {
262+
mx = max(mx, flips[i-1])
263+
if mx == i {
264+
ans++
265+
}
266+
}
267+
return ans
268+
}
269+
270+
func max(a, b int) int {
271+
if a > b {
272+
return a
273+
}
274+
return b
275+
}
276+
```
74277

278+
```go
279+
type BinaryIndexedTree struct {
280+
n int
281+
c []int
282+
}
283+
284+
func newBinaryIndexedTree(n int) *BinaryIndexedTree {
285+
c := make([]int, n+1)
286+
return &BinaryIndexedTree{n, c}
287+
}
288+
289+
func (this *BinaryIndexedTree) lowbit(x int) int {
290+
return x & -x
291+
}
292+
293+
func (this *BinaryIndexedTree) update(x, delta int) {
294+
for x <= this.n {
295+
this.c[x] += delta
296+
x += this.lowbit(x)
297+
}
298+
}
299+
300+
func (this *BinaryIndexedTree) query(x int) int {
301+
s := 0
302+
for x > 0 {
303+
s += this.c[x]
304+
x -= this.lowbit(x)
305+
}
306+
return s
307+
}
308+
309+
func numTimesAllBlue(flips []int) int {
310+
n := len(flips)
311+
tree := newBinaryIndexedTree(n)
312+
ans, mx := 0, 0
313+
for _, v := range flips {
314+
mx = max(mx, v)
315+
tree.update(v, 1)
316+
if tree.query(mx) == mx {
317+
ans++
318+
}
319+
}
320+
return ans
321+
}
322+
323+
func max(a, b int) int {
324+
if a > b {
325+
return a
326+
}
327+
return b
328+
}
75329
```
76330

77331
### **TypeScript**

0 commit comments

Comments
 (0)