Skip to content

Commit 078995b

Browse files
committed
feat: add solutions to lc problem: No.0683
No.0683.K Empty Slots
1 parent d74125f commit 078995b

File tree

6 files changed

+529
-1
lines changed

6 files changed

+529
-1
lines changed

solution/0600-0699/0683.K Empty Slots/README.md

Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,22 +54,206 @@ k: 1
5454

5555
<!-- 这里可写通用的实现逻辑 -->
5656

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+
5766
<!-- tabs:start -->
5867

5968
### **Python3**
6069

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

6372
```python
73+
class BinaryIndexedTree:
74+
def __init__(self, n):
75+
self.n = n
76+
self.c = [0] * (n + 1)
6477

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
65105
```
66106

67107
### **Java**
68108

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

71111
```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+
}
72243
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+
}
73257
```
74258

75259
### **...**

0 commit comments

Comments
 (0)