You have n
bulbs in a row numbered from 1
to n
. Initially, all the bulbs are turned off. We turn on exactly one bulb every day until all bulbs are on after n
days.
You are given an array bulbs
of length n
where bulbs[i] = x
means that on the (i+1)th
day, we will turn on the bulb at position x
where i
is 0-indexed and x
is 1-indexed.
Given an integer k
, return the minimum day number such that there exists two turned on bulbs that have exactly k
bulbs between them that are all turned off. If there isn't such day, return -1
.
Example 1:
Input: bulbs = [1,3,2], k = 1 Output: 2 Explanation: On the first day: bulbs[0] = 1, first bulb is turned on: [1,0,0] On the second day: bulbs[1] = 3, third bulb is turned on: [1,0,1] On the third day: bulbs[2] = 2, second bulb is turned on: [1,1,1] We return 2 because on the second day, there were two on bulbs with one off bulb between them.
Example 2:
Input: bulbs = [1,2,3], k = 1 Output: -1
Constraints:
n == bulbs.length
1 <= n <= 2 * 104
1 <= bulbs[i] <= n
bulbs
is a permutation of numbers from1
ton
.0 <= k <= 2 * 104
Binary Indexed Tree.
class BinaryIndexedTree:
def __init__(self, n):
self.n = n
self.c = [0] * (n + 1)
@staticmethod
def lowbit(x):
return x & -x
def update(self, x, delta):
while x <= self.n:
self.c[x] += delta
x += BinaryIndexedTree.lowbit(x)
def query(self, x):
s = 0
while x > 0:
s += self.c[x]
x -= BinaryIndexedTree.lowbit(x)
return s
class Solution:
def kEmptySlots(self, bulbs: List[int], k: int) -> int:
n = len(bulbs)
tree = BinaryIndexedTree(n)
for i, x in enumerate(bulbs, 1):
tree.update(x, 1)
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
)
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
)
if case1 or case2:
return i
return -1
class Solution {
public int kEmptySlots(int[] bulbs, int k) {
int n = bulbs.length;
BinaryIndexedTree tree = new BinaryIndexedTree(n);
for (int i = 0; i < n; ++i) {
int x = bulbs[i];
tree.update(x, 1);
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;
boolean case2 = x + k + 1 <= n && tree.query(x + k + 1) - tree.query(x + k) == 1
&& tree.query(x + k) - tree.query(x) == 0;
if (case1 || case2) {
return i + 1;
}
}
return -1;
}
}
class BinaryIndexedTree {
private int n;
private int[] c;
public BinaryIndexedTree(int n) {
this.n = n;
c = new int[n + 1];
}
public void update(int x, int delta) {
while (x <= n) {
c[x] += delta;
x += lowbit(x);
}
}
public int query(int x) {
int s = 0;
while (x > 0) {
s += c[x];
x -= lowbit(x);
}
return s;
}
public static int lowbit(int x) {
return x & -x;
}
}
class BinaryIndexedTree {
public:
int n;
vector<int> c;
BinaryIndexedTree(int _n)
: n(_n)
, c(_n + 1) { }
void update(int x, int delta) {
while (x <= n) {
c[x] += delta;
x += lowbit(x);
}
}
int query(int x) {
int s = 0;
while (x > 0) {
s += c[x];
x -= lowbit(x);
}
return s;
}
int lowbit(int x) {
return x & -x;
}
};
class Solution {
public:
int kEmptySlots(vector<int>& bulbs, int k) {
int n = bulbs.size();
BinaryIndexedTree* tree = new BinaryIndexedTree(n);
for (int i = 0; i < n; ++i) {
int x = bulbs[i];
tree->update(x, 1);
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;
bool case2 = x + k + 1 <= n && tree->query(x + k + 1) - tree->query(x + k) == 1 && tree->query(x + k) - tree->query(x) == 0;
if (case1 || case2) return i + 1;
}
return -1;
}
};
type BinaryIndexedTree struct {
n int
c []int
}
func newBinaryIndexedTree(n int) *BinaryIndexedTree {
c := make([]int, n+1)
return &BinaryIndexedTree{n, c}
}
func (this *BinaryIndexedTree) lowbit(x int) int {
return x & -x
}
func (this *BinaryIndexedTree) update(x, delta int) {
for x <= this.n {
this.c[x] += delta
x += this.lowbit(x)
}
}
func (this *BinaryIndexedTree) query(x int) int {
s := 0
for x > 0 {
s += this.c[x]
x -= this.lowbit(x)
}
return s
}
func kEmptySlots(bulbs []int, k int) int {
n := len(bulbs)
tree := newBinaryIndexedTree(n)
for i, x := range bulbs {
tree.update(x, 1)
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
case2 := x+k+1 <= n && tree.query(x+k+1)-tree.query(x+k) == 1 && tree.query(x+k)-tree.query(x) == 0
if case1 || case2 {
return i + 1
}
}
return -1
}