@@ -54,22 +54,206 @@ k: 1
54
54
55
55
<!-- 这里可写通用的实现逻辑 -->
56
56
57
+ 树状数组。
58
+
59
+ 树状数组,也称作“二叉索引树”(Binary Indexed Tree)或 Fenwick 树。 它可以高效地实现如下两个操作:
60
+
61
+ 1 . ** 单点更新** ` update(x, delta) ` : 把序列 x 位置的数加上一个值 delta;
62
+ 1 . ** 前缀和查询** ` query(x) ` :查询序列 ` [1,...x] ` 区间的区间和,即位置 x 的前缀和。
63
+
64
+ 这两个操作的时间复杂度均为 ` O(log n) ` 。
65
+
57
66
<!-- tabs:start -->
58
67
59
68
### ** Python3**
60
69
61
70
<!-- 这里可写当前语言的特殊实现逻辑 -->
62
71
63
72
``` python
73
+ class BinaryIndexedTree :
74
+ def __init__ (self , n ):
75
+ self .n = n
76
+ self .c = [0 ] * (n + 1 )
64
77
78
+ @ staticmethod
79
+ def lowbit (x ):
80
+ return x & - x
81
+
82
+ def update (self , x , delta ):
83
+ while x <= self .n:
84
+ self .c[x] += delta
85
+ x += BinaryIndexedTree.lowbit(x)
86
+
87
+ def query (self , x ):
88
+ s = 0
89
+ while x > 0 :
90
+ s += self .c[x]
91
+ x -= BinaryIndexedTree.lowbit(x)
92
+ return s
93
+
94
+ class Solution :
95
+ def kEmptySlots (self , bulbs : List[int ], k : int ) -> int :
96
+ n = len (bulbs)
97
+ tree = BinaryIndexedTree(n)
98
+ for i, x in enumerate (bulbs, 1 ):
99
+ tree.update(x, 1 )
100
+ case1 = x - k - 1 > 0 and tree.query(x - k - 1 ) - tree.query(x - k - 2 ) == 1 and tree.query(x - 1 ) - tree.query(x - k - 1 ) == 0
101
+ case2 = x + k + 1 <= n and tree.query(x + k + 1 ) - tree.query(x + k) == 1 and tree.query(x + k) - tree.query(x) == 0
102
+ if case1 or case2:
103
+ return i
104
+ return - 1
65
105
```
66
106
67
107
### ** Java**
68
108
69
109
<!-- 这里可写当前语言的特殊实现逻辑 -->
70
110
71
111
``` java
112
+ class Solution {
113
+ public int kEmptySlots (int [] bulbs , int k ) {
114
+ int n = bulbs. length;
115
+ BinaryIndexedTree tree = new BinaryIndexedTree (n);
116
+ for (int i = 0 ; i < n; ++ i) {
117
+ int x = bulbs[i];
118
+ tree. update(x, 1 );
119
+ boolean case1 = x - k - 1 > 0 && tree. query(x - k - 1 ) - tree. query(x - k - 2 ) == 1 && tree. query(x - 1 ) - tree. query(x - k - 1 ) == 0 ;
120
+ boolean case2 = x + k + 1 <= n && tree. query(x + k + 1 ) - tree. query(x + k) == 1 && tree. query(x + k) - tree. query(x) == 0 ;
121
+ if (case1 || case2) {
122
+ return i + 1 ;
123
+ }
124
+ }
125
+ return - 1 ;
126
+ }
127
+ }
128
+
129
+ class BinaryIndexedTree {
130
+ private int n;
131
+ private int [] c;
132
+
133
+ public BinaryIndexedTree (int n ) {
134
+ this . n = n;
135
+ c = new int [n + 1 ];
136
+ }
137
+
138
+ public void update (int x , int delta ) {
139
+ while (x <= n) {
140
+ c[x] += delta;
141
+ x += lowbit(x);
142
+ }
143
+ }
144
+
145
+ public int query (int x ) {
146
+ int s = 0 ;
147
+ while (x > 0 ) {
148
+ s += c[x];
149
+ x -= lowbit(x);
150
+ }
151
+ return s;
152
+ }
153
+
154
+ public static int lowbit (int x ) {
155
+ return x & - x;
156
+ }
157
+ }
158
+ ```
159
+
160
+ ### ** C++**
161
+
162
+ ``` cpp
163
+ class BinaryIndexedTree {
164
+ public:
165
+ int n;
166
+ vector<int > c;
167
+
168
+ BinaryIndexedTree(int _n): n(_n), c(_n + 1){}
169
+
170
+ void update (int x, int delta) {
171
+ while (x <= n)
172
+ {
173
+ c[ x] += delta;
174
+ x += lowbit(x);
175
+ }
176
+ }
177
+
178
+ int query(int x) {
179
+ int s = 0;
180
+ while (x > 0)
181
+ {
182
+ s += c[x];
183
+ x -= lowbit(x);
184
+ }
185
+ return s;
186
+ }
187
+
188
+ int lowbit(int x) {
189
+ return x & -x;
190
+ }
191
+ };
192
+
193
+ class Solution {
194
+ public:
195
+ int kEmptySlots(vector<int >& bulbs, int k) {
196
+ int n = bulbs.size();
197
+ BinaryIndexedTree* tree = new BinaryIndexedTree(n);
198
+ for (int i = 0; i < n; ++i)
199
+ {
200
+ int x = bulbs[ i] ;
201
+ tree->update(x, 1);
202
+ bool case1 = x - k - 1 > 0 && tree->query(x - k - 1) - tree->query(x - k - 2) == 1 && tree->query(x - 1) - tree->query(x - k - 1) == 0;
203
+ bool case2 = x + k + 1 <= n && tree->query(x + k + 1) - tree->query(x + k) == 1 && tree->query(x + k) - tree->query(x) == 0;
204
+ if (case1 || case2) return i + 1;
205
+ }
206
+ return -1;
207
+ }
208
+ };
209
+ ```
210
+
211
+ ### **Go**
212
+
213
+ ```go
214
+ type BinaryIndexedTree struct {
215
+ n int
216
+ c []int
217
+ }
218
+
219
+ func newBinaryIndexedTree(n int) *BinaryIndexedTree {
220
+ c := make([]int, n+1)
221
+ return &BinaryIndexedTree{n, c}
222
+ }
223
+
224
+ func (this *BinaryIndexedTree) lowbit(x int) int {
225
+ return x & -x
226
+ }
227
+
228
+ func (this *BinaryIndexedTree) update(x, delta int) {
229
+ for x <= this.n {
230
+ this.c[x] += delta
231
+ x += this.lowbit(x)
232
+ }
233
+ }
234
+
235
+ func (this *BinaryIndexedTree) query(x int) int {
236
+ s := 0
237
+ for x > 0 {
238
+ s += this.c[x]
239
+ x -= this.lowbit(x)
240
+ }
241
+ return s
242
+ }
72
243
244
+ func kEmptySlots(bulbs []int, k int) int {
245
+ n := len(bulbs)
246
+ tree := newBinaryIndexedTree(n)
247
+ for i, x := range bulbs {
248
+ tree.update(x, 1)
249
+ case1 := x-k-1 > 0 && tree.query(x-k-1)-tree.query(x-k-2) == 1 && tree.query(x-1)-tree.query(x-k-1) == 0
250
+ case2 := x+k+1 <= n && tree.query(x+k+1)-tree.query(x+k) == 1 && tree.query(x+k)-tree.query(x) == 0
251
+ if case1 || case2 {
252
+ return i + 1
253
+ }
254
+ }
255
+ return -1
256
+ }
73
257
```
74
258
75
259
### ** ...**
0 commit comments