inputs
stringlengths 50
14k
| targets
stringlengths 4
655k
|
|---|---|
There is a room with n bulbs, numbered from 1 to n, arranged in a row from left to right. Initially, all the bulbs are turned off.
At moment k (for k from 0 to n - 1), we turn on the light[k] bulb. A bulb change color to blue only if it is on and all the previous bulbs (to the left) are turned on too.
Return the number of moments in which all turned on bulbs are blue.
Example 1:
Input: light = [2,1,3,5,4]
Output: 3
Explanation: All bulbs turned on, are blue at the moment 1, 2 and 4.
Example 2:
Input: light = [3,2,4,1,5]
Output: 2
Explanation: All bulbs turned on, are blue at the moment 3, and 4 (index-0).
Example 3:
Input: light = [4,1,2,3]
Output: 1
Explanation: All bulbs turned on, are blue at the moment 3 (index-0).
Bulb 4th changes to blue at the moment 3.
Example 4:
Input: light = [2,1,4,3,6,5]
Output: 3
Example 5:
Input: light = [1,2,3,4,5,6]
Output: 6
Constraints:
n == light.length
1 <= n <= 5 * 10^4
light is a permutation of [1, 2, ..., n]
|
class Solution:
def numTimesAllBlue(self, light: List[int]) -> int:
right = result = 0
for i, a in enumerate(light, 1):
right = max(right, a)
result += right == i
return result
|
There is a room with n bulbs, numbered from 1 to n, arranged in a row from left to right. Initially, all the bulbs are turned off.
At moment k (for k from 0 to n - 1), we turn on the light[k] bulb. A bulb change color to blue only if it is on and all the previous bulbs (to the left) are turned on too.
Return the number of moments in which all turned on bulbs are blue.
Example 1:
Input: light = [2,1,3,5,4]
Output: 3
Explanation: All bulbs turned on, are blue at the moment 1, 2 and 4.
Example 2:
Input: light = [3,2,4,1,5]
Output: 2
Explanation: All bulbs turned on, are blue at the moment 3, and 4 (index-0).
Example 3:
Input: light = [4,1,2,3]
Output: 1
Explanation: All bulbs turned on, are blue at the moment 3 (index-0).
Bulb 4th changes to blue at the moment 3.
Example 4:
Input: light = [2,1,4,3,6,5]
Output: 3
Example 5:
Input: light = [1,2,3,4,5,6]
Output: 6
Constraints:
n == light.length
1 <= n <= 5 * 10^4
light is a permutation of [1, 2, ..., n]
|
class Solution:
def numTimesAllBlue(self, light: List[int]) -> int:
c=0
mx=0
for i in range(len(light)):
a = light[i]
mx=max(mx,a)
if mx==i+1:
c+=1
return c
|
There is a room with n bulbs, numbered from 1 to n, arranged in a row from left to right. Initially, all the bulbs are turned off.
At moment k (for k from 0 to n - 1), we turn on the light[k] bulb. A bulb change color to blue only if it is on and all the previous bulbs (to the left) are turned on too.
Return the number of moments in which all turned on bulbs are blue.
Example 1:
Input: light = [2,1,3,5,4]
Output: 3
Explanation: All bulbs turned on, are blue at the moment 1, 2 and 4.
Example 2:
Input: light = [3,2,4,1,5]
Output: 2
Explanation: All bulbs turned on, are blue at the moment 3, and 4 (index-0).
Example 3:
Input: light = [4,1,2,3]
Output: 1
Explanation: All bulbs turned on, are blue at the moment 3 (index-0).
Bulb 4th changes to blue at the moment 3.
Example 4:
Input: light = [2,1,4,3,6,5]
Output: 3
Example 5:
Input: light = [1,2,3,4,5,6]
Output: 6
Constraints:
n == light.length
1 <= n <= 5 * 10^4
light is a permutation of [1, 2, ..., n]
|
#
class Solution:
def numTimesAllBlue(self, light: List[int]) -> int:
# index for lights that should be on but is currently off
pending_on = set()
# index for lights that is turned on but not yet blue
already_on = set()
count = 0
for idx, lgt in enumerate(light):
lgt -= 1 # change from 1-base to 0-base
if idx in already_on:
already_on.remove(idx)
else:
pending_on.add(idx)
if lgt in pending_on:
pending_on.remove(lgt)
else:
already_on.add(lgt)
if len(pending_on) == 0:
count += 1
return count
|
There is a room with n bulbs, numbered from 1 to n, arranged in a row from left to right. Initially, all the bulbs are turned off.
At moment k (for k from 0 to n - 1), we turn on the light[k] bulb. A bulb change color to blue only if it is on and all the previous bulbs (to the left) are turned on too.
Return the number of moments in which all turned on bulbs are blue.
Example 1:
Input: light = [2,1,3,5,4]
Output: 3
Explanation: All bulbs turned on, are blue at the moment 1, 2 and 4.
Example 2:
Input: light = [3,2,4,1,5]
Output: 2
Explanation: All bulbs turned on, are blue at the moment 3, and 4 (index-0).
Example 3:
Input: light = [4,1,2,3]
Output: 1
Explanation: All bulbs turned on, are blue at the moment 3 (index-0).
Bulb 4th changes to blue at the moment 3.
Example 4:
Input: light = [2,1,4,3,6,5]
Output: 3
Example 5:
Input: light = [1,2,3,4,5,6]
Output: 6
Constraints:
n == light.length
1 <= n <= 5 * 10^4
light is a permutation of [1, 2, ..., n]
|
##we need to find out for indices i which ones such that a[0],a[1],...,a[i] are permutations of 1,2,...,i+1.
class Solution:
def numTimesAllBlue(self, light: List[int]) -> int:
minimum = len(light)+1
maximum = 0
moments = 0
for index,num in enumerate(light):
minimum = min(num,minimum)
maximum = max(num,maximum)
if minimum == 1 and maximum == index+1:
moments += 1
return moments
|
There is a room with n bulbs, numbered from 1 to n, arranged in a row from left to right. Initially, all the bulbs are turned off.
At moment k (for k from 0 to n - 1), we turn on the light[k] bulb. A bulb change color to blue only if it is on and all the previous bulbs (to the left) are turned on too.
Return the number of moments in which all turned on bulbs are blue.
Example 1:
Input: light = [2,1,3,5,4]
Output: 3
Explanation: All bulbs turned on, are blue at the moment 1, 2 and 4.
Example 2:
Input: light = [3,2,4,1,5]
Output: 2
Explanation: All bulbs turned on, are blue at the moment 3, and 4 (index-0).
Example 3:
Input: light = [4,1,2,3]
Output: 1
Explanation: All bulbs turned on, are blue at the moment 3 (index-0).
Bulb 4th changes to blue at the moment 3.
Example 4:
Input: light = [2,1,4,3,6,5]
Output: 3
Example 5:
Input: light = [1,2,3,4,5,6]
Output: 6
Constraints:
n == light.length
1 <= n <= 5 * 10^4
light is a permutation of [1, 2, ..., n]
|
class Solution:
def numTimesAllBlue(self, light: List[int]) -> int:
yellow, blue = set(), set()
moment = 0
for bulb in light:
if bulb == 1 or bulb - 1 in blue:
blue.add(bulb)
t = bulb+1
while t in yellow:
yellow.remove(t)
blue.add(t)
t += 1
else:
yellow.add(bulb)
if not yellow:
moment += 1
return moment
|
There is a room with n bulbs, numbered from 1 to n, arranged in a row from left to right. Initially, all the bulbs are turned off.
At moment k (for k from 0 to n - 1), we turn on the light[k] bulb. A bulb change color to blue only if it is on and all the previous bulbs (to the left) are turned on too.
Return the number of moments in which all turned on bulbs are blue.
Example 1:
Input: light = [2,1,3,5,4]
Output: 3
Explanation: All bulbs turned on, are blue at the moment 1, 2 and 4.
Example 2:
Input: light = [3,2,4,1,5]
Output: 2
Explanation: All bulbs turned on, are blue at the moment 3, and 4 (index-0).
Example 3:
Input: light = [4,1,2,3]
Output: 1
Explanation: All bulbs turned on, are blue at the moment 3 (index-0).
Bulb 4th changes to blue at the moment 3.
Example 4:
Input: light = [2,1,4,3,6,5]
Output: 3
Example 5:
Input: light = [1,2,3,4,5,6]
Output: 6
Constraints:
n == light.length
1 <= n <= 5 * 10^4
light is a permutation of [1, 2, ..., n]
|
class Solution:
def numTimesAllBlue(self, light: List[int]) -> int:
n = len(light)
ans = 0
bulbs = [0 for _ in range(n)]
zero_i, one_i = 0, 0
for i in light:
bulbs[i-1] = 1
if zero_i == i-1:
j = i-1
while j < n and bulbs[j] == 1:
j += 1
zero_i = j
one_i = max(one_i, i-1)
if one_i < zero_i:
ans += 1
return ans
|
There is a room with n bulbs, numbered from 1 to n, arranged in a row from left to right. Initially, all the bulbs are turned off.
At moment k (for k from 0 to n - 1), we turn on the light[k] bulb. A bulb change color to blue only if it is on and all the previous bulbs (to the left) are turned on too.
Return the number of moments in which all turned on bulbs are blue.
Example 1:
Input: light = [2,1,3,5,4]
Output: 3
Explanation: All bulbs turned on, are blue at the moment 1, 2 and 4.
Example 2:
Input: light = [3,2,4,1,5]
Output: 2
Explanation: All bulbs turned on, are blue at the moment 3, and 4 (index-0).
Example 3:
Input: light = [4,1,2,3]
Output: 1
Explanation: All bulbs turned on, are blue at the moment 3 (index-0).
Bulb 4th changes to blue at the moment 3.
Example 4:
Input: light = [2,1,4,3,6,5]
Output: 3
Example 5:
Input: light = [1,2,3,4,5,6]
Output: 6
Constraints:
n == light.length
1 <= n <= 5 * 10^4
light is a permutation of [1, 2, ..., n]
|
class Solution:
def numTimesAllBlue(self, light: List[int]) -> int:
min_seen, max_seen = float('inf'), float('-inf')
times = 0
for i, moment in enumerate(light):
min_seen = min(min_seen, moment)
max_seen = max(max_seen, moment)
times += (max_seen - min_seen == i) and min_seen == 1 and max_seen == i + 1
return times
|
There is a room with n bulbs, numbered from 1 to n, arranged in a row from left to right. Initially, all the bulbs are turned off.
At moment k (for k from 0 to n - 1), we turn on the light[k] bulb. A bulb change color to blue only if it is on and all the previous bulbs (to the left) are turned on too.
Return the number of moments in which all turned on bulbs are blue.
Example 1:
Input: light = [2,1,3,5,4]
Output: 3
Explanation: All bulbs turned on, are blue at the moment 1, 2 and 4.
Example 2:
Input: light = [3,2,4,1,5]
Output: 2
Explanation: All bulbs turned on, are blue at the moment 3, and 4 (index-0).
Example 3:
Input: light = [4,1,2,3]
Output: 1
Explanation: All bulbs turned on, are blue at the moment 3 (index-0).
Bulb 4th changes to blue at the moment 3.
Example 4:
Input: light = [2,1,4,3,6,5]
Output: 3
Example 5:
Input: light = [1,2,3,4,5,6]
Output: 6
Constraints:
n == light.length
1 <= n <= 5 * 10^4
light is a permutation of [1, 2, ..., n]
|
class Solution:
def numTimesAllBlueQuad(self, light: List[int]) -> int:
blue = 0
lights = [0 for l in light]
for l in light:
lights[l-1] = 1
if all([x == 1 for x in lights[:l-1]]):
blue += 1
print(l)
return blue
def numTimesAllBlue(self, light: List[int]) -> int:
count = 0
sums = [0] * (len(light) + 1)
for idx, l in enumerate(light):
sums[idx+1] = sums[idx] + l
print(sums)
for idx, l in enumerate(light):
if sums[idx+1] == self.required(idx+1):
count += 1
return count
@staticmethod
def required(n):
return ((n+1)*n)//2
|
There is a room with n bulbs, numbered from 1 to n, arranged in a row from left to right. Initially, all the bulbs are turned off.
At moment k (for k from 0 to n - 1), we turn on the light[k] bulb. A bulb change color to blue only if it is on and all the previous bulbs (to the left) are turned on too.
Return the number of moments in which all turned on bulbs are blue.
Example 1:
Input: light = [2,1,3,5,4]
Output: 3
Explanation: All bulbs turned on, are blue at the moment 1, 2 and 4.
Example 2:
Input: light = [3,2,4,1,5]
Output: 2
Explanation: All bulbs turned on, are blue at the moment 3, and 4 (index-0).
Example 3:
Input: light = [4,1,2,3]
Output: 1
Explanation: All bulbs turned on, are blue at the moment 3 (index-0).
Bulb 4th changes to blue at the moment 3.
Example 4:
Input: light = [2,1,4,3,6,5]
Output: 3
Example 5:
Input: light = [1,2,3,4,5,6]
Output: 6
Constraints:
n == light.length
1 <= n <= 5 * 10^4
light is a permutation of [1, 2, ..., n]
|
class Solution:
def numTimesAllBlue(self, light: List[int]) -> int:
n = len(light)
rMin = [light[-1]]*n
for i in range(n-2,-1,-1):
rMin[i] = min(rMin[i+1],light[i])
out = 1
lmax = 0
for i in range(n-1):
lmax = max(lmax,light[i])
if lmax <= rMin[i+1]:
out+=1
return out
|
There is a room with n bulbs, numbered from 1 to n, arranged in a row from left to right. Initially, all the bulbs are turned off.
At moment k (for k from 0 to n - 1), we turn on the light[k] bulb. A bulb change color to blue only if it is on and all the previous bulbs (to the left) are turned on too.
Return the number of moments in which all turned on bulbs are blue.
Example 1:
Input: light = [2,1,3,5,4]
Output: 3
Explanation: All bulbs turned on, are blue at the moment 1, 2 and 4.
Example 2:
Input: light = [3,2,4,1,5]
Output: 2
Explanation: All bulbs turned on, are blue at the moment 3, and 4 (index-0).
Example 3:
Input: light = [4,1,2,3]
Output: 1
Explanation: All bulbs turned on, are blue at the moment 3 (index-0).
Bulb 4th changes to blue at the moment 3.
Example 4:
Input: light = [2,1,4,3,6,5]
Output: 3
Example 5:
Input: light = [1,2,3,4,5,6]
Output: 6
Constraints:
n == light.length
1 <= n <= 5 * 10^4
light is a permutation of [1, 2, ..., n]
|
class Solution:
def numTimesAllBlue(self, light: List[int]) -> int:
if len(light) == 0:
return 0
dic = {}
count = 0
flag = True
Max = light[0]
for i, item in enumerate(light):
dic[item] = True
Max = max(light[i], Max)
if Max == len(dic):
count += 1
continue
elif Max > len(dic):
flag = False
else:
for j in range(item, 0, -1):
if j not in dic:
flag = False
if flag:
count += 1
flag = True
return count
|
There is a room with n bulbs, numbered from 1 to n, arranged in a row from left to right. Initially, all the bulbs are turned off.
At moment k (for k from 0 to n - 1), we turn on the light[k] bulb. A bulb change color to blue only if it is on and all the previous bulbs (to the left) are turned on too.
Return the number of moments in which all turned on bulbs are blue.
Example 1:
Input: light = [2,1,3,5,4]
Output: 3
Explanation: All bulbs turned on, are blue at the moment 1, 2 and 4.
Example 2:
Input: light = [3,2,4,1,5]
Output: 2
Explanation: All bulbs turned on, are blue at the moment 3, and 4 (index-0).
Example 3:
Input: light = [4,1,2,3]
Output: 1
Explanation: All bulbs turned on, are blue at the moment 3 (index-0).
Bulb 4th changes to blue at the moment 3.
Example 4:
Input: light = [2,1,4,3,6,5]
Output: 3
Example 5:
Input: light = [1,2,3,4,5,6]
Output: 6
Constraints:
n == light.length
1 <= n <= 5 * 10^4
light is a permutation of [1, 2, ..., n]
|
class Solution:
def numTimesAllBlue(self, light: List[int]) -> int:
res = hi = on = 0
for l in light:
on += 1
if l>hi:
hi = l
if on==hi:
res+=1
return res
|
There is a room with n bulbs, numbered from 1 to n, arranged in a row from left to right. Initially, all the bulbs are turned off.
At moment k (for k from 0 to n - 1), we turn on the light[k] bulb. A bulb change color to blue only if it is on and all the previous bulbs (to the left) are turned on too.
Return the number of moments in which all turned on bulbs are blue.
Example 1:
Input: light = [2,1,3,5,4]
Output: 3
Explanation: All bulbs turned on, are blue at the moment 1, 2 and 4.
Example 2:
Input: light = [3,2,4,1,5]
Output: 2
Explanation: All bulbs turned on, are blue at the moment 3, and 4 (index-0).
Example 3:
Input: light = [4,1,2,3]
Output: 1
Explanation: All bulbs turned on, are blue at the moment 3 (index-0).
Bulb 4th changes to blue at the moment 3.
Example 4:
Input: light = [2,1,4,3,6,5]
Output: 3
Example 5:
Input: light = [1,2,3,4,5,6]
Output: 6
Constraints:
n == light.length
1 <= n <= 5 * 10^4
light is a permutation of [1, 2, ..., n]
|
class Solution:
def numTimesAllBlue(self, light: List[int]) -> int:
b=0
ans=0
k=0
l=[0 for i in range(len(light))]
for i in range(len(light)):
a=light[i]-1
if b==a:
l[a]=2
c=a+1
#print(l,a,c)
b=c
while c<len(light) and l[c]==1:
l[c]=2
b=c+1
k-=1
c+=1
else:
if a>0 and l[a-1]==2:
l[a]=2
b=l[a]+1
else:
l[a]=1
k+=1
#print(i,l,k,a,b)
if k==0:
ans+=1
return ans
|
There is a room with n bulbs, numbered from 1 to n, arranged in a row from left to right. Initially, all the bulbs are turned off.
At moment k (for k from 0 to n - 1), we turn on the light[k] bulb. A bulb change color to blue only if it is on and all the previous bulbs (to the left) are turned on too.
Return the number of moments in which all turned on bulbs are blue.
Example 1:
Input: light = [2,1,3,5,4]
Output: 3
Explanation: All bulbs turned on, are blue at the moment 1, 2 and 4.
Example 2:
Input: light = [3,2,4,1,5]
Output: 2
Explanation: All bulbs turned on, are blue at the moment 3, and 4 (index-0).
Example 3:
Input: light = [4,1,2,3]
Output: 1
Explanation: All bulbs turned on, are blue at the moment 3 (index-0).
Bulb 4th changes to blue at the moment 3.
Example 4:
Input: light = [2,1,4,3,6,5]
Output: 3
Example 5:
Input: light = [1,2,3,4,5,6]
Output: 6
Constraints:
n == light.length
1 <= n <= 5 * 10^4
light is a permutation of [1, 2, ..., n]
|
class Solution:
# 2:00
'''
- light yellow
- if left is blue
- keep on marking everything to the right blue
- if in the end number of yello = 0, increment ans
- key thing to notice is that the blue only start propogating when number 1 bulb is lighted
'''
def numTimesAllBlue(self, light: List[int]) -> int:
nyellow = 0
nblue = 0
dx = {}
YELLOW = 0
BLUE = 1
OFF = 2
for b in light:
dx[b] = OFF
ans = 0
for i in range(len(light)):
curr = light[i]
dx[curr] = YELLOW
nyellow += 1
check = False
if curr == 1 or dx[curr - 1] == BLUE:
check = True
if check:
for j in range(curr, len(light)+1):
if dx[j] == OFF:
break
else:
nyellow -= 1
nblue += 1
dx[j] = BLUE
# print(light[i], nyellow)
if nyellow == 0:
ans += 1
return ans
|
There is a room with n bulbs, numbered from 1 to n, arranged in a row from left to right. Initially, all the bulbs are turned off.
At moment k (for k from 0 to n - 1), we turn on the light[k] bulb. A bulb change color to blue only if it is on and all the previous bulbs (to the left) are turned on too.
Return the number of moments in which all turned on bulbs are blue.
Example 1:
Input: light = [2,1,3,5,4]
Output: 3
Explanation: All bulbs turned on, are blue at the moment 1, 2 and 4.
Example 2:
Input: light = [3,2,4,1,5]
Output: 2
Explanation: All bulbs turned on, are blue at the moment 3, and 4 (index-0).
Example 3:
Input: light = [4,1,2,3]
Output: 1
Explanation: All bulbs turned on, are blue at the moment 3 (index-0).
Bulb 4th changes to blue at the moment 3.
Example 4:
Input: light = [2,1,4,3,6,5]
Output: 3
Example 5:
Input: light = [1,2,3,4,5,6]
Output: 6
Constraints:
n == light.length
1 <= n <= 5 * 10^4
light is a permutation of [1, 2, ..., n]
|
class Solution:
def numTimesAllBlue(self, light: List[int]) -> int:
n = max(light)
bulb_status = ['off'] * (n + 1)
count = 0
num_on = 0
num_blue = 0
for i in light:
bulb_status[i] = 'on'
num_on += 1
while i <= n and (i == 1 or bulb_status[i - 1] == 'blue'):
if bulb_status[i] == 'off':
break
bulb_status[i] = 'blue'
num_blue += 1
num_on -= 1
i += 1
if num_on == 0 and num_blue > 0:
count += 1
return count
|
There is a room with n bulbs, numbered from 1 to n, arranged in a row from left to right. Initially, all the bulbs are turned off.
At moment k (for k from 0 to n - 1), we turn on the light[k] bulb. A bulb change color to blue only if it is on and all the previous bulbs (to the left) are turned on too.
Return the number of moments in which all turned on bulbs are blue.
Example 1:
Input: light = [2,1,3,5,4]
Output: 3
Explanation: All bulbs turned on, are blue at the moment 1, 2 and 4.
Example 2:
Input: light = [3,2,4,1,5]
Output: 2
Explanation: All bulbs turned on, are blue at the moment 3, and 4 (index-0).
Example 3:
Input: light = [4,1,2,3]
Output: 1
Explanation: All bulbs turned on, are blue at the moment 3 (index-0).
Bulb 4th changes to blue at the moment 3.
Example 4:
Input: light = [2,1,4,3,6,5]
Output: 3
Example 5:
Input: light = [1,2,3,4,5,6]
Output: 6
Constraints:
n == light.length
1 <= n <= 5 * 10^4
light is a permutation of [1, 2, ..., n]
|
class Solution:
def numTimesAllBlue(self, light: List[int]) -> int:
maxBulb = 0
maxBlue = 0
count = 0
moments = [False for i in range(len(light))]
for idx, bulb in enumerate(light):
maxBulb = max(maxBulb, bulb)
moments[bulb-1] = True
if bulb == 1 or maxBlue == bulb-1:
maxBlue = bulb
i = bulb+1
while(i <= len(light) and moments[i-1]):
maxBlue = i
i+=1
if maxBlue == maxBulb:
count+=1
return count
|
There is a room with n bulbs, numbered from 1 to n, arranged in a row from left to right. Initially, all the bulbs are turned off.
At moment k (for k from 0 to n - 1), we turn on the light[k] bulb. A bulb change color to blue only if it is on and all the previous bulbs (to the left) are turned on too.
Return the number of moments in which all turned on bulbs are blue.
Example 1:
Input: light = [2,1,3,5,4]
Output: 3
Explanation: All bulbs turned on, are blue at the moment 1, 2 and 4.
Example 2:
Input: light = [3,2,4,1,5]
Output: 2
Explanation: All bulbs turned on, are blue at the moment 3, and 4 (index-0).
Example 3:
Input: light = [4,1,2,3]
Output: 1
Explanation: All bulbs turned on, are blue at the moment 3 (index-0).
Bulb 4th changes to blue at the moment 3.
Example 4:
Input: light = [2,1,4,3,6,5]
Output: 3
Example 5:
Input: light = [1,2,3,4,5,6]
Output: 6
Constraints:
n == light.length
1 <= n <= 5 * 10^4
light is a permutation of [1, 2, ..., n]
|
class Solution:
def numTimesAllBlue(self, light: List[int]) -> int:
# use a min and max counter when min == max => all bulbs are blue
min_counter = -1
max_counter = -1
turned_on = [0] * len(light)
moments = 0
for bulb in light:
bulb = bulb-1
max_counter = max(max_counter, bulb)
turned_on[bulb] = 1
while min_counter+1 < len(turned_on) and turned_on[min_counter+1] == 1:
min_counter += 1
if max_counter == min_counter:
moments += 1
return moments
|
There is a room with n bulbs, numbered from 1 to n, arranged in a row from left to right. Initially, all the bulbs are turned off.
At moment k (for k from 0 to n - 1), we turn on the light[k] bulb. A bulb change color to blue only if it is on and all the previous bulbs (to the left) are turned on too.
Return the number of moments in which all turned on bulbs are blue.
Example 1:
Input: light = [2,1,3,5,4]
Output: 3
Explanation: All bulbs turned on, are blue at the moment 1, 2 and 4.
Example 2:
Input: light = [3,2,4,1,5]
Output: 2
Explanation: All bulbs turned on, are blue at the moment 3, and 4 (index-0).
Example 3:
Input: light = [4,1,2,3]
Output: 1
Explanation: All bulbs turned on, are blue at the moment 3 (index-0).
Bulb 4th changes to blue at the moment 3.
Example 4:
Input: light = [2,1,4,3,6,5]
Output: 3
Example 5:
Input: light = [1,2,3,4,5,6]
Output: 6
Constraints:
n == light.length
1 <= n <= 5 * 10^4
light is a permutation of [1, 2, ..., n]
|
class Solution:
def numTimesAllBlue(self, light: List[int]) -> int:
if len(light) == 1:
return 1
mx, mi, s = float('-inf'), float('inf'), 0
res = 0
for i, num in enumerate(light):
s += num
mx = max(mx, num)
mi = min(mi, num)
if mi == 1 and mx == i+1 and s == (i+1)*(i+2)//2:
res += 1
return res
|
There is a room with n bulbs, numbered from 1 to n, arranged in a row from left to right. Initially, all the bulbs are turned off.
At moment k (for k from 0 to n - 1), we turn on the light[k] bulb. A bulb change color to blue only if it is on and all the previous bulbs (to the left) are turned on too.
Return the number of moments in which all turned on bulbs are blue.
Example 1:
Input: light = [2,1,3,5,4]
Output: 3
Explanation: All bulbs turned on, are blue at the moment 1, 2 and 4.
Example 2:
Input: light = [3,2,4,1,5]
Output: 2
Explanation: All bulbs turned on, are blue at the moment 3, and 4 (index-0).
Example 3:
Input: light = [4,1,2,3]
Output: 1
Explanation: All bulbs turned on, are blue at the moment 3 (index-0).
Bulb 4th changes to blue at the moment 3.
Example 4:
Input: light = [2,1,4,3,6,5]
Output: 3
Example 5:
Input: light = [1,2,3,4,5,6]
Output: 6
Constraints:
n == light.length
1 <= n <= 5 * 10^4
light is a permutation of [1, 2, ..., n]
|
class Solution:
def numTimesAllBlue(self, light: List[int]) -> int:
light = [i-1 for i in light]
bulbs = [0 for _ in range(len(light))]
on,blue = set(),set()
res = 0
#print(bulbs)
#print(light)
n = len(light)
total = 0
for moment in light:
#print(bulbs,moment)
if moment == 0:
bulbs[0] = 2
blue.add(0)
elif bulbs[moment-1] == 2:
bulbs[moment] = 2
blue.add(moment)
else:
bulbs[moment] = 1
on.add(moment)
if bulbs[moment] == 2:
#print('turning things blue',bulbs[moment+1] if moment+1 < n else -1)
i = moment+1
while i < n and bulbs[i] == 1:
bulbs[i] = 2
blue.add(on.remove(i))
i += 1
res += 1 if blue and not on else 0
return res
|
There is a room with n bulbs, numbered from 1 to n, arranged in a row from left to right. Initially, all the bulbs are turned off.
At moment k (for k from 0 to n - 1), we turn on the light[k] bulb. A bulb change color to blue only if it is on and all the previous bulbs (to the left) are turned on too.
Return the number of moments in which all turned on bulbs are blue.
Example 1:
Input: light = [2,1,3,5,4]
Output: 3
Explanation: All bulbs turned on, are blue at the moment 1, 2 and 4.
Example 2:
Input: light = [3,2,4,1,5]
Output: 2
Explanation: All bulbs turned on, are blue at the moment 3, and 4 (index-0).
Example 3:
Input: light = [4,1,2,3]
Output: 1
Explanation: All bulbs turned on, are blue at the moment 3 (index-0).
Bulb 4th changes to blue at the moment 3.
Example 4:
Input: light = [2,1,4,3,6,5]
Output: 3
Example 5:
Input: light = [1,2,3,4,5,6]
Output: 6
Constraints:
n == light.length
1 <= n <= 5 * 10^4
light is a permutation of [1, 2, ..., n]
|
class Solution:
def numTimesAllBlue(self, light: List[int]) -> int:
# Moments at which max light is last consec on
max_light_on = -1
last_consecutive_on = -1
all_blue = 0
for light_bulb in light:
# Turn this light on by negating it
light_index = abs(light_bulb) - 1
light[light_index] = -light[light_index]
# Check if this is the max light on
max_light_on = max(max_light_on, light_index)
# Check if this is the last consecutive light on starting from 0
while last_consecutive_on < len(light) - 1 and light[last_consecutive_on + 1] < 0:
last_consecutive_on += 1
if max_light_on == last_consecutive_on:
all_blue += 1
return all_blue
|
There is a room with n bulbs, numbered from 1 to n, arranged in a row from left to right. Initially, all the bulbs are turned off.
At moment k (for k from 0 to n - 1), we turn on the light[k] bulb. A bulb change color to blue only if it is on and all the previous bulbs (to the left) are turned on too.
Return the number of moments in which all turned on bulbs are blue.
Example 1:
Input: light = [2,1,3,5,4]
Output: 3
Explanation: All bulbs turned on, are blue at the moment 1, 2 and 4.
Example 2:
Input: light = [3,2,4,1,5]
Output: 2
Explanation: All bulbs turned on, are blue at the moment 3, and 4 (index-0).
Example 3:
Input: light = [4,1,2,3]
Output: 1
Explanation: All bulbs turned on, are blue at the moment 3 (index-0).
Bulb 4th changes to blue at the moment 3.
Example 4:
Input: light = [2,1,4,3,6,5]
Output: 3
Example 5:
Input: light = [1,2,3,4,5,6]
Output: 6
Constraints:
n == light.length
1 <= n <= 5 * 10^4
light is a permutation of [1, 2, ..., n]
|
class Solution:
def numTimesAllBlue(self, light: List[int]) -> int:
sum = total = count = 0
for i in range(len(light)):
sum += i+1
total += light[i]
if sum == total:
count+=1
return count
|
There is a room with n bulbs, numbered from 1 to n, arranged in a row from left to right. Initially, all the bulbs are turned off.
At moment k (for k from 0 to n - 1), we turn on the light[k] bulb. A bulb change color to blue only if it is on and all the previous bulbs (to the left) are turned on too.
Return the number of moments in which all turned on bulbs are blue.
Example 1:
Input: light = [2,1,3,5,4]
Output: 3
Explanation: All bulbs turned on, are blue at the moment 1, 2 and 4.
Example 2:
Input: light = [3,2,4,1,5]
Output: 2
Explanation: All bulbs turned on, are blue at the moment 3, and 4 (index-0).
Example 3:
Input: light = [4,1,2,3]
Output: 1
Explanation: All bulbs turned on, are blue at the moment 3 (index-0).
Bulb 4th changes to blue at the moment 3.
Example 4:
Input: light = [2,1,4,3,6,5]
Output: 3
Example 5:
Input: light = [1,2,3,4,5,6]
Output: 6
Constraints:
n == light.length
1 <= n <= 5 * 10^4
light is a permutation of [1, 2, ..., n]
|
class Solution:
def numTimesAllBlue(self, light: List[int]) -> int:
min_off = 1
max_on = float('-inf')
count = 0
blue = [0]*(len(light)+1)
for i in range(len(light)):
blue[light[i]] = 1
max_on = max(max_on,light[i])
if light[i] == min_off:
while min_off <= len(light) and blue[min_off] == 1:
min_off +=1
if max_on < min_off:
count += 1
return count
|
There is a room with n bulbs, numbered from 1 to n, arranged in a row from left to right. Initially, all the bulbs are turned off.
At moment k (for k from 0 to n - 1), we turn on the light[k] bulb. A bulb change color to blue only if it is on and all the previous bulbs (to the left) are turned on too.
Return the number of moments in which all turned on bulbs are blue.
Example 1:
Input: light = [2,1,3,5,4]
Output: 3
Explanation: All bulbs turned on, are blue at the moment 1, 2 and 4.
Example 2:
Input: light = [3,2,4,1,5]
Output: 2
Explanation: All bulbs turned on, are blue at the moment 3, and 4 (index-0).
Example 3:
Input: light = [4,1,2,3]
Output: 1
Explanation: All bulbs turned on, are blue at the moment 3 (index-0).
Bulb 4th changes to blue at the moment 3.
Example 4:
Input: light = [2,1,4,3,6,5]
Output: 3
Example 5:
Input: light = [1,2,3,4,5,6]
Output: 6
Constraints:
n == light.length
1 <= n <= 5 * 10^4
light is a permutation of [1, 2, ..., n]
|
class Solution:
def numTimesAllBlue(self, light: List[int]) -> int:
light = [i-1 for i in light]
bulbs = [0 for _ in range(len(light))]
on,blue = set(),set()
res = 0
print(bulbs)
print(light)
n = len(light)
total = 0
for moment in light:
#print(bulbs,moment)
if moment == 0:
bulbs[0] = 2
blue.add(0)
elif bulbs[moment-1] == 2:
bulbs[moment] = 2
blue.add(moment)
else:
bulbs[moment] = 1
on.add(moment)
if bulbs[moment] == 2:
#print('turning things blue',bulbs[moment+1] if moment+1 < n else -1)
i = moment+1
while i < n and bulbs[i] == 1:
bulbs[i] = 2
blue.add(on.remove(i))
i += 1
res += 1 if blue and not on else 0
return res
|
There is a room with n bulbs, numbered from 1 to n, arranged in a row from left to right. Initially, all the bulbs are turned off.
At moment k (for k from 0 to n - 1), we turn on the light[k] bulb. A bulb change color to blue only if it is on and all the previous bulbs (to the left) are turned on too.
Return the number of moments in which all turned on bulbs are blue.
Example 1:
Input: light = [2,1,3,5,4]
Output: 3
Explanation: All bulbs turned on, are blue at the moment 1, 2 and 4.
Example 2:
Input: light = [3,2,4,1,5]
Output: 2
Explanation: All bulbs turned on, are blue at the moment 3, and 4 (index-0).
Example 3:
Input: light = [4,1,2,3]
Output: 1
Explanation: All bulbs turned on, are blue at the moment 3 (index-0).
Bulb 4th changes to blue at the moment 3.
Example 4:
Input: light = [2,1,4,3,6,5]
Output: 3
Example 5:
Input: light = [1,2,3,4,5,6]
Output: 6
Constraints:
n == light.length
1 <= n <= 5 * 10^4
light is a permutation of [1, 2, ..., n]
|
class Solution:
def numTimesAllBlue(self, light: List[int]) -> int:
light = sorted([(light[i], i) for i in range(len(light))], key=lambda t: t[0])
go_over = list([t[1] for t in light])
smallest_distance_left_until_blue = 0
count = 0
for i in range(len(go_over)):
smallest_distance_left_until_blue = max(smallest_distance_left_until_blue - 1, 0, go_over[i] - i)
if smallest_distance_left_until_blue == 0:
count += 1
return count
|
There is a room with n bulbs, numbered from 1 to n, arranged in a row from left to right. Initially, all the bulbs are turned off.
At moment k (for k from 0 to n - 1), we turn on the light[k] bulb. A bulb change color to blue only if it is on and all the previous bulbs (to the left) are turned on too.
Return the number of moments in which all turned on bulbs are blue.
Example 1:
Input: light = [2,1,3,5,4]
Output: 3
Explanation: All bulbs turned on, are blue at the moment 1, 2 and 4.
Example 2:
Input: light = [3,2,4,1,5]
Output: 2
Explanation: All bulbs turned on, are blue at the moment 3, and 4 (index-0).
Example 3:
Input: light = [4,1,2,3]
Output: 1
Explanation: All bulbs turned on, are blue at the moment 3 (index-0).
Bulb 4th changes to blue at the moment 3.
Example 4:
Input: light = [2,1,4,3,6,5]
Output: 3
Example 5:
Input: light = [1,2,3,4,5,6]
Output: 6
Constraints:
n == light.length
1 <= n <= 5 * 10^4
light is a permutation of [1, 2, ..., n]
|
import collections
class Solution:
def numTimesAllBlue(self, light: List[int]) -> int:
n = len(light)
ans = count_blue = 0
state = collections.defaultdict(int)
for j, bulb in enumerate(light):
#if prev bulb is blue
if bulb == 1 or state[bulb-1]==2:
state[bulb] = 2
count_blue+=1
else:
# else just set to lit
state[bulb] = 1
# if curre bulb is blue, set next bulb to blue if they are lit
if state[bulb] == 2:
i = bulb+1
while i<n+1 and state[i]==1:
count_blue+=1
state[i] = 2
i+=1
if count_blue== j+1:
ans += 1
return ans
|
There is a room with n bulbs, numbered from 1 to n, arranged in a row from left to right. Initially, all the bulbs are turned off.
At moment k (for k from 0 to n - 1), we turn on the light[k] bulb. A bulb change color to blue only if it is on and all the previous bulbs (to the left) are turned on too.
Return the number of moments in which all turned on bulbs are blue.
Example 1:
Input: light = [2,1,3,5,4]
Output: 3
Explanation: All bulbs turned on, are blue at the moment 1, 2 and 4.
Example 2:
Input: light = [3,2,4,1,5]
Output: 2
Explanation: All bulbs turned on, are blue at the moment 3, and 4 (index-0).
Example 3:
Input: light = [4,1,2,3]
Output: 1
Explanation: All bulbs turned on, are blue at the moment 3 (index-0).
Bulb 4th changes to blue at the moment 3.
Example 4:
Input: light = [2,1,4,3,6,5]
Output: 3
Example 5:
Input: light = [1,2,3,4,5,6]
Output: 6
Constraints:
n == light.length
1 <= n <= 5 * 10^4
light is a permutation of [1, 2, ..., n]
|
def fillMissingSet(highest_bulb, bulbs_missing, bulbs_lit, difference):
for i in range(highest_bulb-difference,highest_bulb):
if i not in bulbs_lit:
bulbs_missing.add(i)
class Solution:
def numTimesAllBlue(self, light: List[int]) -> int:
highest_bulb = 1
difference = 0
bulbs_lit = set()
bulbs_missing = set()
instances=0
fillMissingSet(light[0], bulbs_missing, bulbs_lit, difference)
for i in range(len(light)):
difference = 0
if highest_bulb < light[i]:
difference = light[i]-highest_bulb
highest_bulb = light[i]
bulbs_lit.add(light[i])
if light[i] in bulbs_missing:
bulbs_missing.remove(light[i])
fillMissingSet(highest_bulb, bulbs_missing, bulbs_lit, difference)
if len(bulbs_missing) == 0:
instances +=1
#print(highest_bulb, bulbs_missing, bulbs_lit, instances, difference)
return instances
|
There is a room with n bulbs, numbered from 1 to n, arranged in a row from left to right. Initially, all the bulbs are turned off.
At moment k (for k from 0 to n - 1), we turn on the light[k] bulb. A bulb change color to blue only if it is on and all the previous bulbs (to the left) are turned on too.
Return the number of moments in which all turned on bulbs are blue.
Example 1:
Input: light = [2,1,3,5,4]
Output: 3
Explanation: All bulbs turned on, are blue at the moment 1, 2 and 4.
Example 2:
Input: light = [3,2,4,1,5]
Output: 2
Explanation: All bulbs turned on, are blue at the moment 3, and 4 (index-0).
Example 3:
Input: light = [4,1,2,3]
Output: 1
Explanation: All bulbs turned on, are blue at the moment 3 (index-0).
Bulb 4th changes to blue at the moment 3.
Example 4:
Input: light = [2,1,4,3,6,5]
Output: 3
Example 5:
Input: light = [1,2,3,4,5,6]
Output: 6
Constraints:
n == light.length
1 <= n <= 5 * 10^4
light is a permutation of [1, 2, ..., n]
|
import heapq
class Solution:
def numTimesAllBlue(self, light: List[int]) -> int:
q = []
res = 0
for num in light:
heapq.heappush(q, -num)
if -q[0] == len(q):
res += 1
return res
|
There is a room with n bulbs, numbered from 1 to n, arranged in a row from left to right. Initially, all the bulbs are turned off.
At moment k (for k from 0 to n - 1), we turn on the light[k] bulb. A bulb change color to blue only if it is on and all the previous bulbs (to the left) are turned on too.
Return the number of moments in which all turned on bulbs are blue.
Example 1:
Input: light = [2,1,3,5,4]
Output: 3
Explanation: All bulbs turned on, are blue at the moment 1, 2 and 4.
Example 2:
Input: light = [3,2,4,1,5]
Output: 2
Explanation: All bulbs turned on, are blue at the moment 3, and 4 (index-0).
Example 3:
Input: light = [4,1,2,3]
Output: 1
Explanation: All bulbs turned on, are blue at the moment 3 (index-0).
Bulb 4th changes to blue at the moment 3.
Example 4:
Input: light = [2,1,4,3,6,5]
Output: 3
Example 5:
Input: light = [1,2,3,4,5,6]
Output: 6
Constraints:
n == light.length
1 <= n <= 5 * 10^4
light is a permutation of [1, 2, ..., n]
|
class Solution:
def numTimesAllBlue(self, light: List[int]) -> int:
'''m = set()
flag, c = -1, 0
for i in range(len(light)):
m.add(light[i])
for j in range(i+1,0,-1):
if j not in m:
flag = 1
break
if flag == 1:
flag = -1
else:
c += 1
return c'''
res = 0
curr = -1
for p,q in enumerate(light):
curr = max(curr,q)
if curr == p+1:
res += 1
return res
|
There is a room with n bulbs, numbered from 1 to n, arranged in a row from left to right. Initially, all the bulbs are turned off.
At moment k (for k from 0 to n - 1), we turn on the light[k] bulb. A bulb change color to blue only if it is on and all the previous bulbs (to the left) are turned on too.
Return the number of moments in which all turned on bulbs are blue.
Example 1:
Input: light = [2,1,3,5,4]
Output: 3
Explanation: All bulbs turned on, are blue at the moment 1, 2 and 4.
Example 2:
Input: light = [3,2,4,1,5]
Output: 2
Explanation: All bulbs turned on, are blue at the moment 3, and 4 (index-0).
Example 3:
Input: light = [4,1,2,3]
Output: 1
Explanation: All bulbs turned on, are blue at the moment 3 (index-0).
Bulb 4th changes to blue at the moment 3.
Example 4:
Input: light = [2,1,4,3,6,5]
Output: 3
Example 5:
Input: light = [1,2,3,4,5,6]
Output: 6
Constraints:
n == light.length
1 <= n <= 5 * 10^4
light is a permutation of [1, 2, ..., n]
|
class Solution:
def numTimesAllBlue(self, light: List[int]) -> int:
n = len(light)
states = [0] * n
yellow_lights = set()
all_blue_counter = 0
for i in range(n):
bulb = light[i] - 1
if bulb == 0 or states[bulb-1] == 2:
states[bulb] = 2
elif states[bulb-1] != 2:
states[bulb] = 1
yellow_lights.add(bulb)
if states[bulb] == 2:
for j in range(bulb + 1, n):
if states[j] == 1:
yellow_lights.remove(j)
states[j] = 2
else: break
if len(yellow_lights) == 0:
all_blue_counter += 1
return all_blue_counter
|
There is a room with n bulbs, numbered from 1 to n, arranged in a row from left to right. Initially, all the bulbs are turned off.
At moment k (for k from 0 to n - 1), we turn on the light[k] bulb. A bulb change color to blue only if it is on and all the previous bulbs (to the left) are turned on too.
Return the number of moments in which all turned on bulbs are blue.
Example 1:
Input: light = [2,1,3,5,4]
Output: 3
Explanation: All bulbs turned on, are blue at the moment 1, 2 and 4.
Example 2:
Input: light = [3,2,4,1,5]
Output: 2
Explanation: All bulbs turned on, are blue at the moment 3, and 4 (index-0).
Example 3:
Input: light = [4,1,2,3]
Output: 1
Explanation: All bulbs turned on, are blue at the moment 3 (index-0).
Bulb 4th changes to blue at the moment 3.
Example 4:
Input: light = [2,1,4,3,6,5]
Output: 3
Example 5:
Input: light = [1,2,3,4,5,6]
Output: 6
Constraints:
n == light.length
1 <= n <= 5 * 10^4
light is a permutation of [1, 2, ..., n]
|
class Solution:
def numTimesAllBlue(self, light: List[int]) -> int:
moment_count = 0
lit_idx = -1
idx_map = {}
for idx, moment in enumerate(light):
idx_map[moment] = idx
for moment in range(1, len(light) + 1):
idx = idx_map[moment]
light[idx] = 'L'
if idx == lit_idx + 1:
for l_idx in range(lit_idx + 1, len(light)):
if light[l_idx] == 'L':
lit_idx += 1
else:
break
if lit_idx + 1 == moment:
moment_count += 1
return moment_count
|
There is a room with n bulbs, numbered from 1 to n, arranged in a row from left to right. Initially, all the bulbs are turned off.
At moment k (for k from 0 to n - 1), we turn on the light[k] bulb. A bulb change color to blue only if it is on and all the previous bulbs (to the left) are turned on too.
Return the number of moments in which all turned on bulbs are blue.
Example 1:
Input: light = [2,1,3,5,4]
Output: 3
Explanation: All bulbs turned on, are blue at the moment 1, 2 and 4.
Example 2:
Input: light = [3,2,4,1,5]
Output: 2
Explanation: All bulbs turned on, are blue at the moment 3, and 4 (index-0).
Example 3:
Input: light = [4,1,2,3]
Output: 1
Explanation: All bulbs turned on, are blue at the moment 3 (index-0).
Bulb 4th changes to blue at the moment 3.
Example 4:
Input: light = [2,1,4,3,6,5]
Output: 3
Example 5:
Input: light = [1,2,3,4,5,6]
Output: 6
Constraints:
n == light.length
1 <= n <= 5 * 10^4
light is a permutation of [1, 2, ..., n]
|
class Solution:
def numTimesAllBlue(self, light: List[int]) -> int:
max_light = 0
num=0
res = 0
for i,l in enumerate(light):
max_light = max(max_light,l)
print(max_light)
if i+1>=max_light:
res+=1
return res
|
There is a room with n bulbs, numbered from 1 to n, arranged in a row from left to right. Initially, all the bulbs are turned off.
At moment k (for k from 0 to n - 1), we turn on the light[k] bulb. A bulb change color to blue only if it is on and all the previous bulbs (to the left) are turned on too.
Return the number of moments in which all turned on bulbs are blue.
Example 1:
Input: light = [2,1,3,5,4]
Output: 3
Explanation: All bulbs turned on, are blue at the moment 1, 2 and 4.
Example 2:
Input: light = [3,2,4,1,5]
Output: 2
Explanation: All bulbs turned on, are blue at the moment 3, and 4 (index-0).
Example 3:
Input: light = [4,1,2,3]
Output: 1
Explanation: All bulbs turned on, are blue at the moment 3 (index-0).
Bulb 4th changes to blue at the moment 3.
Example 4:
Input: light = [2,1,4,3,6,5]
Output: 3
Example 5:
Input: light = [1,2,3,4,5,6]
Output: 6
Constraints:
n == light.length
1 <= n <= 5 * 10^4
light is a permutation of [1, 2, ..., n]
|
class Solution:
def numTimesAllBlue(self, light: List[int]) -> int:
res = 0
grid = [0]*len(light)
dicts = {}
for turn in light:
if turn == 1:
grid[turn-1] = 1
start = turn - 1
while start < len(light):
if grid[start] != 0:
if start in dicts: del dicts[start]
grid[start] = 1
start += 1
else:
break
if not dicts: res += 1
else:
if grid[turn - 2] == 1:
grid[turn - 1] = 1
start = turn - 1
breaked = False
while start < len(light):
if grid[start] != 0:
if start in dicts: del dicts[start]
grid[start] = 1
start += 1
else:
break
if not dicts: res += 1
else:
grid[turn -1] = 2
dicts[turn - 1] = 1
return res
|
There is a room with n bulbs, numbered from 1 to n, arranged in a row from left to right. Initially, all the bulbs are turned off.
At moment k (for k from 0 to n - 1), we turn on the light[k] bulb. A bulb change color to blue only if it is on and all the previous bulbs (to the left) are turned on too.
Return the number of moments in which all turned on bulbs are blue.
Example 1:
Input: light = [2,1,3,5,4]
Output: 3
Explanation: All bulbs turned on, are blue at the moment 1, 2 and 4.
Example 2:
Input: light = [3,2,4,1,5]
Output: 2
Explanation: All bulbs turned on, are blue at the moment 3, and 4 (index-0).
Example 3:
Input: light = [4,1,2,3]
Output: 1
Explanation: All bulbs turned on, are blue at the moment 3 (index-0).
Bulb 4th changes to blue at the moment 3.
Example 4:
Input: light = [2,1,4,3,6,5]
Output: 3
Example 5:
Input: light = [1,2,3,4,5,6]
Output: 6
Constraints:
n == light.length
1 <= n <= 5 * 10^4
light is a permutation of [1, 2, ..., n]
|
class Solution:
def numTimesAllBlue(self, light: List[int]) -> int:
len_lis = len(light)
lis = [[] for x in range(len_lis+2)]
tot = 0
for i in range(len_lis):
if(light[i] > 1 and lis[light[i]-1] and light[i] < len_lis and lis[light[i]+1]):
end = lis[light[i]+1][1]
start = lis[light[i]-1][0]
elif(light[i] > 1 and lis[light[i]-1]):
start = lis[light[i]-1][0]
end = light[i]
elif(light[i] < len_lis and lis[light[i]+1]):
start = light[i]
end = lis[light[i]+1][1]
else:
start = light[i]
end = light[i]
if(end-start == i and start == 1):
tot += 1
lis[end] = [start, end]
lis[start] = [start, end]
#print(lis)
return tot
|
There is a room with n bulbs, numbered from 1 to n, arranged in a row from left to right. Initially, all the bulbs are turned off.
At moment k (for k from 0 to n - 1), we turn on the light[k] bulb. A bulb change color to blue only if it is on and all the previous bulbs (to the left) are turned on too.
Return the number of moments in which all turned on bulbs are blue.
Example 1:
Input: light = [2,1,3,5,4]
Output: 3
Explanation: All bulbs turned on, are blue at the moment 1, 2 and 4.
Example 2:
Input: light = [3,2,4,1,5]
Output: 2
Explanation: All bulbs turned on, are blue at the moment 3, and 4 (index-0).
Example 3:
Input: light = [4,1,2,3]
Output: 1
Explanation: All bulbs turned on, are blue at the moment 3 (index-0).
Bulb 4th changes to blue at the moment 3.
Example 4:
Input: light = [2,1,4,3,6,5]
Output: 3
Example 5:
Input: light = [1,2,3,4,5,6]
Output: 6
Constraints:
n == light.length
1 <= n <= 5 * 10^4
light is a permutation of [1, 2, ..., n]
|
class Solution:
def numTimesAllBlue(self, light: List[int]) -> int:
ranges = {}
r = 0
for l in light:
if l + 1 not in ranges and l - 1 not in ranges:
ranges[l] = (l, l)
elif l + 1 in ranges and l - 1 not in ranges:
_, rr = ranges[l + 1]
ranges[l] = ranges[rr] = (l, rr)
if rr != l + 1:
del ranges[l + 1]
elif l - 1 in ranges and l + 1 not in ranges:
ll, _ = ranges[l - 1]
ranges[ll] = ranges[l] = (ll, l)
if ll != l - 1:
del ranges[l - 1]
else:
ll, _ = ranges[l - 1]
_, rr = ranges[l + 1]
ranges[ll] = ranges[rr] = (ll, rr)
if ll != l - 1:
del ranges[l - 1]
if rr != l + 1:
del ranges[l + 1]
# print(ranges)
if len(ranges) == 1:
r += (list(ranges.items())[0][0] == 1)
elif len(ranges) == 2:
r += (len(set(ranges.values())) == 1 and list(ranges.values())[0][0] == 1)
return r
|
There is a room with n bulbs, numbered from 1 to n, arranged in a row from left to right. Initially, all the bulbs are turned off.
At moment k (for k from 0 to n - 1), we turn on the light[k] bulb. A bulb change color to blue only if it is on and all the previous bulbs (to the left) are turned on too.
Return the number of moments in which all turned on bulbs are blue.
Example 1:
Input: light = [2,1,3,5,4]
Output: 3
Explanation: All bulbs turned on, are blue at the moment 1, 2 and 4.
Example 2:
Input: light = [3,2,4,1,5]
Output: 2
Explanation: All bulbs turned on, are blue at the moment 3, and 4 (index-0).
Example 3:
Input: light = [4,1,2,3]
Output: 1
Explanation: All bulbs turned on, are blue at the moment 3 (index-0).
Bulb 4th changes to blue at the moment 3.
Example 4:
Input: light = [2,1,4,3,6,5]
Output: 3
Example 5:
Input: light = [1,2,3,4,5,6]
Output: 6
Constraints:
n == light.length
1 <= n <= 5 * 10^4
light is a permutation of [1, 2, ..., n]
|
class Solution:
def numTimesAllBlue(self, light: List[int]) -> int:
right = res = 0
for i, v in enumerate(light):
right = max(right, v)
res += right == i + 1
return res
|
There is a room with n bulbs, numbered from 1 to n, arranged in a row from left to right. Initially, all the bulbs are turned off.
At moment k (for k from 0 to n - 1), we turn on the light[k] bulb. A bulb change color to blue only if it is on and all the previous bulbs (to the left) are turned on too.
Return the number of moments in which all turned on bulbs are blue.
Example 1:
Input: light = [2,1,3,5,4]
Output: 3
Explanation: All bulbs turned on, are blue at the moment 1, 2 and 4.
Example 2:
Input: light = [3,2,4,1,5]
Output: 2
Explanation: All bulbs turned on, are blue at the moment 3, and 4 (index-0).
Example 3:
Input: light = [4,1,2,3]
Output: 1
Explanation: All bulbs turned on, are blue at the moment 3 (index-0).
Bulb 4th changes to blue at the moment 3.
Example 4:
Input: light = [2,1,4,3,6,5]
Output: 3
Example 5:
Input: light = [1,2,3,4,5,6]
Output: 6
Constraints:
n == light.length
1 <= n <= 5 * 10^4
light is a permutation of [1, 2, ..., n]
|
class Solution:
def numTimesAllBlue(self, light: List[int]) -> int:
max_v = 0
res = 0
for i,v in enumerate(light):
max_v = max(max_v, v)
if max_v == i+1:
res += 1
return res
|
There is a room with n bulbs, numbered from 1 to n, arranged in a row from left to right. Initially, all the bulbs are turned off.
At moment k (for k from 0 to n - 1), we turn on the light[k] bulb. A bulb change color to blue only if it is on and all the previous bulbs (to the left) are turned on too.
Return the number of moments in which all turned on bulbs are blue.
Example 1:
Input: light = [2,1,3,5,4]
Output: 3
Explanation: All bulbs turned on, are blue at the moment 1, 2 and 4.
Example 2:
Input: light = [3,2,4,1,5]
Output: 2
Explanation: All bulbs turned on, are blue at the moment 3, and 4 (index-0).
Example 3:
Input: light = [4,1,2,3]
Output: 1
Explanation: All bulbs turned on, are blue at the moment 3 (index-0).
Bulb 4th changes to blue at the moment 3.
Example 4:
Input: light = [2,1,4,3,6,5]
Output: 3
Example 5:
Input: light = [1,2,3,4,5,6]
Output: 6
Constraints:
n == light.length
1 <= n <= 5 * 10^4
light is a permutation of [1, 2, ..., n]
|
class Solution:
def numTimesAllBlue(self, light: List[int]) -> int:
n = 0
highest = 0
ans = 0
for bulb in light:
n += 1
highest = max(highest, bulb)
if highest == n:
ans += 1
return ans
|
There is a room with n bulbs, numbered from 1 to n, arranged in a row from left to right. Initially, all the bulbs are turned off.
At moment k (for k from 0 to n - 1), we turn on the light[k] bulb. A bulb change color to blue only if it is on and all the previous bulbs (to the left) are turned on too.
Return the number of moments in which all turned on bulbs are blue.
Example 1:
Input: light = [2,1,3,5,4]
Output: 3
Explanation: All bulbs turned on, are blue at the moment 1, 2 and 4.
Example 2:
Input: light = [3,2,4,1,5]
Output: 2
Explanation: All bulbs turned on, are blue at the moment 3, and 4 (index-0).
Example 3:
Input: light = [4,1,2,3]
Output: 1
Explanation: All bulbs turned on, are blue at the moment 3 (index-0).
Bulb 4th changes to blue at the moment 3.
Example 4:
Input: light = [2,1,4,3,6,5]
Output: 3
Example 5:
Input: light = [1,2,3,4,5,6]
Output: 6
Constraints:
n == light.length
1 <= n <= 5 * 10^4
light is a permutation of [1, 2, ..., n]
|
class Solution:
def numTimesAllBlue(self, light: List[int]) -> int:
'''arr=[0]*(len(light)+1)
cnt=0
max_1=light[0]
i=0
while i<len(light):
arr[light[i]]=1
if max_1<light[i]:
max_1=light[i]
if sum(arr)==max_1:
cnt+=1
i+=1
return(cnt)'''
cnt=0
max_1=light[0]
for i in range(0,len(light)):
max_1=max(max_1,light[i])
#print(max_1,i+1)
if max_1==i+1:
cnt+=1
return(cnt)
|
There is a room with n bulbs, numbered from 1 to n, arranged in a row from left to right. Initially, all the bulbs are turned off.
At moment k (for k from 0 to n - 1), we turn on the light[k] bulb. A bulb change color to blue only if it is on and all the previous bulbs (to the left) are turned on too.
Return the number of moments in which all turned on bulbs are blue.
Example 1:
Input: light = [2,1,3,5,4]
Output: 3
Explanation: All bulbs turned on, are blue at the moment 1, 2 and 4.
Example 2:
Input: light = [3,2,4,1,5]
Output: 2
Explanation: All bulbs turned on, are blue at the moment 3, and 4 (index-0).
Example 3:
Input: light = [4,1,2,3]
Output: 1
Explanation: All bulbs turned on, are blue at the moment 3 (index-0).
Bulb 4th changes to blue at the moment 3.
Example 4:
Input: light = [2,1,4,3,6,5]
Output: 3
Example 5:
Input: light = [1,2,3,4,5,6]
Output: 6
Constraints:
n == light.length
1 <= n <= 5 * 10^4
light is a permutation of [1, 2, ..., n]
|
def parent(p,i):
if p[i]!=i:
p[i]=parent(p,p[i])
return p[i]
class Solution:
def numTimesAllBlue(self, light: List[int]) -> int:
p=[0]*len(light)
c=[0]*len(p)
for i in range(len(p)):
p[i]=i
x=0
ans=0
for j in light:
i=j-1
x+=1
c[i]=1
if i-1>-1 and c[i-1]!=0:
p[i]=parent(p,i-1)
c[p[i]]+=1
if i+1<len(light) and c[i+1]!=0:
c[p[i]]+=c[i+1]
p[i+1]=p[i]
if c[p[i]]==x and c[0]!=0:
#print(c)
#print(p)
#print()
ans+=1
return ans
|
There is a room with n bulbs, numbered from 1 to n, arranged in a row from left to right. Initially, all the bulbs are turned off.
At moment k (for k from 0 to n - 1), we turn on the light[k] bulb. A bulb change color to blue only if it is on and all the previous bulbs (to the left) are turned on too.
Return the number of moments in which all turned on bulbs are blue.
Example 1:
Input: light = [2,1,3,5,4]
Output: 3
Explanation: All bulbs turned on, are blue at the moment 1, 2 and 4.
Example 2:
Input: light = [3,2,4,1,5]
Output: 2
Explanation: All bulbs turned on, are blue at the moment 3, and 4 (index-0).
Example 3:
Input: light = [4,1,2,3]
Output: 1
Explanation: All bulbs turned on, are blue at the moment 3 (index-0).
Bulb 4th changes to blue at the moment 3.
Example 4:
Input: light = [2,1,4,3,6,5]
Output: 3
Example 5:
Input: light = [1,2,3,4,5,6]
Output: 6
Constraints:
n == light.length
1 <= n <= 5 * 10^4
light is a permutation of [1, 2, ..., n]
|
class Solution:
def numTimesAllBlue(self, light: List[int]) -> int:
rolling_min = light[0]
rolling_max = light[0]
r = 0
for i, l in enumerate(light):
rolling_min = min(rolling_min, l)
rolling_max = max(rolling_max, l)
r += (rolling_min == 1 and rolling_max == i + 1)
return r
|
There is a room with n bulbs, numbered from 1 to n, arranged in a row from left to right. Initially, all the bulbs are turned off.
At moment k (for k from 0 to n - 1), we turn on the light[k] bulb. A bulb change color to blue only if it is on and all the previous bulbs (to the left) are turned on too.
Return the number of moments in which all turned on bulbs are blue.
Example 1:
Input: light = [2,1,3,5,4]
Output: 3
Explanation: All bulbs turned on, are blue at the moment 1, 2 and 4.
Example 2:
Input: light = [3,2,4,1,5]
Output: 2
Explanation: All bulbs turned on, are blue at the moment 3, and 4 (index-0).
Example 3:
Input: light = [4,1,2,3]
Output: 1
Explanation: All bulbs turned on, are blue at the moment 3 (index-0).
Bulb 4th changes to blue at the moment 3.
Example 4:
Input: light = [2,1,4,3,6,5]
Output: 3
Example 5:
Input: light = [1,2,3,4,5,6]
Output: 6
Constraints:
n == light.length
1 <= n <= 5 * 10^4
light is a permutation of [1, 2, ..., n]
|
from heapq import heappush,heappop,heapify
class Solution:
def numTimesAllBlue(self, light: List[int]) -> int:
n=len(light)
a=[]
ans=0
for i in range(n):
heappush(a,-1*(light[i]))
if len(a)==i+1:
temp=-1*heappop(a)
if temp==i+1:
ans+=1
heappush(a,-1*temp)
return ans
|
There is a room with n bulbs, numbered from 1 to n, arranged in a row from left to right. Initially, all the bulbs are turned off.
At moment k (for k from 0 to n - 1), we turn on the light[k] bulb. A bulb change color to blue only if it is on and all the previous bulbs (to the left) are turned on too.
Return the number of moments in which all turned on bulbs are blue.
Example 1:
Input: light = [2,1,3,5,4]
Output: 3
Explanation: All bulbs turned on, are blue at the moment 1, 2 and 4.
Example 2:
Input: light = [3,2,4,1,5]
Output: 2
Explanation: All bulbs turned on, are blue at the moment 3, and 4 (index-0).
Example 3:
Input: light = [4,1,2,3]
Output: 1
Explanation: All bulbs turned on, are blue at the moment 3 (index-0).
Bulb 4th changes to blue at the moment 3.
Example 4:
Input: light = [2,1,4,3,6,5]
Output: 3
Example 5:
Input: light = [1,2,3,4,5,6]
Output: 6
Constraints:
n == light.length
1 <= n <= 5 * 10^4
light is a permutation of [1, 2, ..., n]
|
class Solution:
def numTimesAllBlue(self, light: List[int]) -> int:
maximum = 1
res = 0
for i, val in enumerate(light):
maximum = max(maximum, val)
if i+1 == maximum:
res += 1
return res
|
There is a room with n bulbs, numbered from 1 to n, arranged in a row from left to right. Initially, all the bulbs are turned off.
At moment k (for k from 0 to n - 1), we turn on the light[k] bulb. A bulb change color to blue only if it is on and all the previous bulbs (to the left) are turned on too.
Return the number of moments in which all turned on bulbs are blue.
Example 1:
Input: light = [2,1,3,5,4]
Output: 3
Explanation: All bulbs turned on, are blue at the moment 1, 2 and 4.
Example 2:
Input: light = [3,2,4,1,5]
Output: 2
Explanation: All bulbs turned on, are blue at the moment 3, and 4 (index-0).
Example 3:
Input: light = [4,1,2,3]
Output: 1
Explanation: All bulbs turned on, are blue at the moment 3 (index-0).
Bulb 4th changes to blue at the moment 3.
Example 4:
Input: light = [2,1,4,3,6,5]
Output: 3
Example 5:
Input: light = [1,2,3,4,5,6]
Output: 6
Constraints:
n == light.length
1 <= n <= 5 * 10^4
light is a permutation of [1, 2, ..., n]
|
class Solution:
def numTimesAllBlue(self, light: List[int]) -> int:
res, mx = 0, 0
for i, v in enumerate(light, 1):
mx = max(v, mx)
res += (i == mx)
return res
|
There is a room with n bulbs, numbered from 1 to n, arranged in a row from left to right. Initially, all the bulbs are turned off.
At moment k (for k from 0 to n - 1), we turn on the light[k] bulb. A bulb change color to blue only if it is on and all the previous bulbs (to the left) are turned on too.
Return the number of moments in which all turned on bulbs are blue.
Example 1:
Input: light = [2,1,3,5,4]
Output: 3
Explanation: All bulbs turned on, are blue at the moment 1, 2 and 4.
Example 2:
Input: light = [3,2,4,1,5]
Output: 2
Explanation: All bulbs turned on, are blue at the moment 3, and 4 (index-0).
Example 3:
Input: light = [4,1,2,3]
Output: 1
Explanation: All bulbs turned on, are blue at the moment 3 (index-0).
Bulb 4th changes to blue at the moment 3.
Example 4:
Input: light = [2,1,4,3,6,5]
Output: 3
Example 5:
Input: light = [1,2,3,4,5,6]
Output: 6
Constraints:
n == light.length
1 <= n <= 5 * 10^4
light is a permutation of [1, 2, ..., n]
|
class Solution:
def numTimesAllBlue(self, light: List[int]) -> int:
right = res = 0
for i, a in enumerate(light, 1):
right = max(right, a)
res += right == i
return res
|
There is a room with n bulbs, numbered from 1 to n, arranged in a row from left to right. Initially, all the bulbs are turned off.
At moment k (for k from 0 to n - 1), we turn on the light[k] bulb. A bulb change color to blue only if it is on and all the previous bulbs (to the left) are turned on too.
Return the number of moments in which all turned on bulbs are blue.
Example 1:
Input: light = [2,1,3,5,4]
Output: 3
Explanation: All bulbs turned on, are blue at the moment 1, 2 and 4.
Example 2:
Input: light = [3,2,4,1,5]
Output: 2
Explanation: All bulbs turned on, are blue at the moment 3, and 4 (index-0).
Example 3:
Input: light = [4,1,2,3]
Output: 1
Explanation: All bulbs turned on, are blue at the moment 3 (index-0).
Bulb 4th changes to blue at the moment 3.
Example 4:
Input: light = [2,1,4,3,6,5]
Output: 3
Example 5:
Input: light = [1,2,3,4,5,6]
Output: 6
Constraints:
n == light.length
1 <= n <= 5 * 10^4
light is a permutation of [1, 2, ..., n]
|
class Solution:
# 2:00
'''
- light yellow
- if left is blue
- keep on marking everything to the right blue
- if in the end number of yello = 0, increment ans
- key thing to notice is that the blue only start propogating when number 1 bulb is lighted
'''
def numTimesAllBlue(self, light: List[int]) -> int:
nyellow = 0
nblue = 0
dx = {}
YELLOW = 0
BLUE = 1
OFF = 2
for b in light:
dx[b] = OFF
ans = 0
for i in range(len(light)):
curr = light[i]
dx[curr] = YELLOW
nyellow += 1
check = False
if curr == 1 or dx[curr - 1] == BLUE:
check = True
if check:
for j in range(curr, len(light)+1):
if dx[j] == OFF:
break
else:
nyellow -= 1
nblue += 1
dx[j] = BLUE
print((light[i], nyellow))
if nyellow == 0:
ans += 1
return ans
|
There is a room with n bulbs, numbered from 1 to n, arranged in a row from left to right. Initially, all the bulbs are turned off.
At moment k (for k from 0 to n - 1), we turn on the light[k] bulb. A bulb change color to blue only if it is on and all the previous bulbs (to the left) are turned on too.
Return the number of moments in which all turned on bulbs are blue.
Example 1:
Input: light = [2,1,3,5,4]
Output: 3
Explanation: All bulbs turned on, are blue at the moment 1, 2 and 4.
Example 2:
Input: light = [3,2,4,1,5]
Output: 2
Explanation: All bulbs turned on, are blue at the moment 3, and 4 (index-0).
Example 3:
Input: light = [4,1,2,3]
Output: 1
Explanation: All bulbs turned on, are blue at the moment 3 (index-0).
Bulb 4th changes to blue at the moment 3.
Example 4:
Input: light = [2,1,4,3,6,5]
Output: 3
Example 5:
Input: light = [1,2,3,4,5,6]
Output: 6
Constraints:
n == light.length
1 <= n <= 5 * 10^4
light is a permutation of [1, 2, ..., n]
|
class Solution:
def numTimesAllBlue(self, light: List[int]) -> int:
total = 0
count = 0
for k in range(len(light)):
total += light[k]
if total == ((k + 1) * (k + 2)) // 2:
count += 1
return count
|
There is a room with n bulbs, numbered from 1 to n, arranged in a row from left to right. Initially, all the bulbs are turned off.
At moment k (for k from 0 to n - 1), we turn on the light[k] bulb. A bulb change color to blue only if it is on and all the previous bulbs (to the left) are turned on too.
Return the number of moments in which all turned on bulbs are blue.
Example 1:
Input: light = [2,1,3,5,4]
Output: 3
Explanation: All bulbs turned on, are blue at the moment 1, 2 and 4.
Example 2:
Input: light = [3,2,4,1,5]
Output: 2
Explanation: All bulbs turned on, are blue at the moment 3, and 4 (index-0).
Example 3:
Input: light = [4,1,2,3]
Output: 1
Explanation: All bulbs turned on, are blue at the moment 3 (index-0).
Bulb 4th changes to blue at the moment 3.
Example 4:
Input: light = [2,1,4,3,6,5]
Output: 3
Example 5:
Input: light = [1,2,3,4,5,6]
Output: 6
Constraints:
n == light.length
1 <= n <= 5 * 10^4
light is a permutation of [1, 2, ..., n]
|
class Solution:
def numTimesAllBlue(self, light: List[int]) -> int:
res = 0
n = len(light)
counter = collections.Counter()
for i in range(n):
counter[light[i]] += 1
if counter[light[i]] == 0:
counter.pop(light[i])
counter[i+1] -= 1
if counter[i+1] == 0:
counter.pop(i+1)
if not counter:
res += 1
return res
|
There is a room with n bulbs, numbered from 1 to n, arranged in a row from left to right. Initially, all the bulbs are turned off.
At moment k (for k from 0 to n - 1), we turn on the light[k] bulb. A bulb change color to blue only if it is on and all the previous bulbs (to the left) are turned on too.
Return the number of moments in which all turned on bulbs are blue.
Example 1:
Input: light = [2,1,3,5,4]
Output: 3
Explanation: All bulbs turned on, are blue at the moment 1, 2 and 4.
Example 2:
Input: light = [3,2,4,1,5]
Output: 2
Explanation: All bulbs turned on, are blue at the moment 3, and 4 (index-0).
Example 3:
Input: light = [4,1,2,3]
Output: 1
Explanation: All bulbs turned on, are blue at the moment 3 (index-0).
Bulb 4th changes to blue at the moment 3.
Example 4:
Input: light = [2,1,4,3,6,5]
Output: 3
Example 5:
Input: light = [1,2,3,4,5,6]
Output: 6
Constraints:
n == light.length
1 <= n <= 5 * 10^4
light is a permutation of [1, 2, ..., n]
|
class Solution:
def numTimesAllBlue(self, light: List[int]) -> int:
maxv = 0
result =0
for i, num in enumerate(light):
maxv = max(maxv, num)
if maxv== i+1: result+=1
return result
|
There is a room with n bulbs, numbered from 1 to n, arranged in a row from left to right. Initially, all the bulbs are turned off.
At moment k (for k from 0 to n - 1), we turn on the light[k] bulb. A bulb change color to blue only if it is on and all the previous bulbs (to the left) are turned on too.
Return the number of moments in which all turned on bulbs are blue.
Example 1:
Input: light = [2,1,3,5,4]
Output: 3
Explanation: All bulbs turned on, are blue at the moment 1, 2 and 4.
Example 2:
Input: light = [3,2,4,1,5]
Output: 2
Explanation: All bulbs turned on, are blue at the moment 3, and 4 (index-0).
Example 3:
Input: light = [4,1,2,3]
Output: 1
Explanation: All bulbs turned on, are blue at the moment 3 (index-0).
Bulb 4th changes to blue at the moment 3.
Example 4:
Input: light = [2,1,4,3,6,5]
Output: 3
Example 5:
Input: light = [1,2,3,4,5,6]
Output: 6
Constraints:
n == light.length
1 <= n <= 5 * 10^4
light is a permutation of [1, 2, ..., n]
|
class Solution:
def numTimesAllBlue(self, light: List[int]) -> int:
status = [0]*(len(light)+1)
k = 0
number_of_blues = 0
count = 0
while(k < len(light)):
idx = light[k]
status[idx] = 1
if idx == 1:
status[1] = 2
number_of_blues += 1
for i in range(idx,len(light)+1):
if i == idx and status[i] == 2:
continue
if status[i] == 1 and status[i-1] == 2:
status[i] = 2
number_of_blues += 1
else:
break
if number_of_blues == k+1:
count += 1
#print(status)
k += 1
return count
|
There is a room with n bulbs, numbered from 1 to n, arranged in a row from left to right. Initially, all the bulbs are turned off.
At moment k (for k from 0 to n - 1), we turn on the light[k] bulb. A bulb change color to blue only if it is on and all the previous bulbs (to the left) are turned on too.
Return the number of moments in which all turned on bulbs are blue.
Example 1:
Input: light = [2,1,3,5,4]
Output: 3
Explanation: All bulbs turned on, are blue at the moment 1, 2 and 4.
Example 2:
Input: light = [3,2,4,1,5]
Output: 2
Explanation: All bulbs turned on, are blue at the moment 3, and 4 (index-0).
Example 3:
Input: light = [4,1,2,3]
Output: 1
Explanation: All bulbs turned on, are blue at the moment 3 (index-0).
Bulb 4th changes to blue at the moment 3.
Example 4:
Input: light = [2,1,4,3,6,5]
Output: 3
Example 5:
Input: light = [1,2,3,4,5,6]
Output: 6
Constraints:
n == light.length
1 <= n <= 5 * 10^4
light is a permutation of [1, 2, ..., n]
|
class Solution:
def numTimesAllBlue(self, light: List[int]) -> int:
res = 0
grid = [0]*len(light)
dicts = {}
for turn in light:
if turn == 1:
grid[turn-1] = 1
start = turn
while start < len(light):
if grid[start] != 0:
if start in dicts: del dicts[start]
grid[start] = 1
start += 1
else:
break
if not dicts: res += 1
else:
if grid[turn - 2] == 1:
grid[turn - 1] = 1
start = turn
breaked = False
while start < len(light):
if grid[start] != 0:
if start in dicts: del dicts[start]
grid[start] = 1
start += 1
else:
break
if not dicts: res += 1
else:
grid[turn -1] = 2
dicts[turn - 1] = 1
return res
|
There is a room with n bulbs, numbered from 1 to n, arranged in a row from left to right. Initially, all the bulbs are turned off.
At moment k (for k from 0 to n - 1), we turn on the light[k] bulb. A bulb change color to blue only if it is on and all the previous bulbs (to the left) are turned on too.
Return the number of moments in which all turned on bulbs are blue.
Example 1:
Input: light = [2,1,3,5,4]
Output: 3
Explanation: All bulbs turned on, are blue at the moment 1, 2 and 4.
Example 2:
Input: light = [3,2,4,1,5]
Output: 2
Explanation: All bulbs turned on, are blue at the moment 3, and 4 (index-0).
Example 3:
Input: light = [4,1,2,3]
Output: 1
Explanation: All bulbs turned on, are blue at the moment 3 (index-0).
Bulb 4th changes to blue at the moment 3.
Example 4:
Input: light = [2,1,4,3,6,5]
Output: 3
Example 5:
Input: light = [1,2,3,4,5,6]
Output: 6
Constraints:
n == light.length
1 <= n <= 5 * 10^4
light is a permutation of [1, 2, ..., n]
|
class Solution:
def numTimesAllBlue(self, light: List[int]) -> int:
res = 0
n = len(light)
# Actual turned on light (positive)
# Required turned on light (negative)
counter = collections.Counter()
for i in range(n):
counter[light[i]] += 1
if counter[light[i]] == 0:
counter.pop(light[i])
counter[i+1] -= 1
if counter[i+1] == 0:
counter.pop(i+1)
if not counter:
res += 1
return res
|
There is a room with n bulbs, numbered from 1 to n, arranged in a row from left to right. Initially, all the bulbs are turned off.
At moment k (for k from 0 to n - 1), we turn on the light[k] bulb. A bulb change color to blue only if it is on and all the previous bulbs (to the left) are turned on too.
Return the number of moments in which all turned on bulbs are blue.
Example 1:
Input: light = [2,1,3,5,4]
Output: 3
Explanation: All bulbs turned on, are blue at the moment 1, 2 and 4.
Example 2:
Input: light = [3,2,4,1,5]
Output: 2
Explanation: All bulbs turned on, are blue at the moment 3, and 4 (index-0).
Example 3:
Input: light = [4,1,2,3]
Output: 1
Explanation: All bulbs turned on, are blue at the moment 3 (index-0).
Bulb 4th changes to blue at the moment 3.
Example 4:
Input: light = [2,1,4,3,6,5]
Output: 3
Example 5:
Input: light = [1,2,3,4,5,6]
Output: 6
Constraints:
n == light.length
1 <= n <= 5 * 10^4
light is a permutation of [1, 2, ..., n]
|
class Solution:
def numTimesAllBlue(self, light: List[int]) -> int:
s = 0
max_index = 0
count = 0
for i in range(len(light)):
s += 1
if light[i] > max_index:
max_index = light[i]
#print(f\"s: {s} max_index: {max_index} i: {i}\")
if i+1 == max_index:
count += 1
return count
|
There is a room with n bulbs, numbered from 1 to n, arranged in a row from left to right. Initially, all the bulbs are turned off.
At moment k (for k from 0 to n - 1), we turn on the light[k] bulb. A bulb change color to blue only if it is on and all the previous bulbs (to the left) are turned on too.
Return the number of moments in which all turned on bulbs are blue.
Example 1:
Input: light = [2,1,3,5,4]
Output: 3
Explanation: All bulbs turned on, are blue at the moment 1, 2 and 4.
Example 2:
Input: light = [3,2,4,1,5]
Output: 2
Explanation: All bulbs turned on, are blue at the moment 3, and 4 (index-0).
Example 3:
Input: light = [4,1,2,3]
Output: 1
Explanation: All bulbs turned on, are blue at the moment 3 (index-0).
Bulb 4th changes to blue at the moment 3.
Example 4:
Input: light = [2,1,4,3,6,5]
Output: 3
Example 5:
Input: light = [1,2,3,4,5,6]
Output: 6
Constraints:
n == light.length
1 <= n <= 5 * 10^4
light is a permutation of [1, 2, ..., n]
|
class Solution:
def numTimesAllBlue(self, light: List[int]) -> int:
curr_max = 0
res = 0
for idx, val in enumerate(light):
curr_max = max(curr_max, val)
if curr_max == idx + 1:
res += 1
return res
|
There is a room with n bulbs, numbered from 1 to n, arranged in a row from left to right. Initially, all the bulbs are turned off.
At moment k (for k from 0 to n - 1), we turn on the light[k] bulb. A bulb change color to blue only if it is on and all the previous bulbs (to the left) are turned on too.
Return the number of moments in which all turned on bulbs are blue.
Example 1:
Input: light = [2,1,3,5,4]
Output: 3
Explanation: All bulbs turned on, are blue at the moment 1, 2 and 4.
Example 2:
Input: light = [3,2,4,1,5]
Output: 2
Explanation: All bulbs turned on, are blue at the moment 3, and 4 (index-0).
Example 3:
Input: light = [4,1,2,3]
Output: 1
Explanation: All bulbs turned on, are blue at the moment 3 (index-0).
Bulb 4th changes to blue at the moment 3.
Example 4:
Input: light = [2,1,4,3,6,5]
Output: 3
Example 5:
Input: light = [1,2,3,4,5,6]
Output: 6
Constraints:
n == light.length
1 <= n <= 5 * 10^4
light is a permutation of [1, 2, ..., n]
|
class Solution:
def numTimesAllBlue(self, light: List[int]) -> int:
max_ind = 0
res = 0
for t in range(len(light)):
max_ind = max(max_ind, light[t])
if max_ind == t+1:
res += 1
return res
|
There is a room with n bulbs, numbered from 1 to n, arranged in a row from left to right. Initially, all the bulbs are turned off.
At moment k (for k from 0 to n - 1), we turn on the light[k] bulb. A bulb change color to blue only if it is on and all the previous bulbs (to the left) are turned on too.
Return the number of moments in which all turned on bulbs are blue.
Example 1:
Input: light = [2,1,3,5,4]
Output: 3
Explanation: All bulbs turned on, are blue at the moment 1, 2 and 4.
Example 2:
Input: light = [3,2,4,1,5]
Output: 2
Explanation: All bulbs turned on, are blue at the moment 3, and 4 (index-0).
Example 3:
Input: light = [4,1,2,3]
Output: 1
Explanation: All bulbs turned on, are blue at the moment 3 (index-0).
Bulb 4th changes to blue at the moment 3.
Example 4:
Input: light = [2,1,4,3,6,5]
Output: 3
Example 5:
Input: light = [1,2,3,4,5,6]
Output: 6
Constraints:
n == light.length
1 <= n <= 5 * 10^4
light is a permutation of [1, 2, ..., n]
|
class Solution:
def numTimesAllBlue(self, light: List[int]) -> int:
max_on = 0
count = 0
for idx, bulb in enumerate(light):
max_on = max(max_on, bulb)
if max_on == idx+1:
count += 1
return count
|
There is a room with n bulbs, numbered from 1 to n, arranged in a row from left to right. Initially, all the bulbs are turned off.
At moment k (for k from 0 to n - 1), we turn on the light[k] bulb. A bulb change color to blue only if it is on and all the previous bulbs (to the left) are turned on too.
Return the number of moments in which all turned on bulbs are blue.
Example 1:
Input: light = [2,1,3,5,4]
Output: 3
Explanation: All bulbs turned on, are blue at the moment 1, 2 and 4.
Example 2:
Input: light = [3,2,4,1,5]
Output: 2
Explanation: All bulbs turned on, are blue at the moment 3, and 4 (index-0).
Example 3:
Input: light = [4,1,2,3]
Output: 1
Explanation: All bulbs turned on, are blue at the moment 3 (index-0).
Bulb 4th changes to blue at the moment 3.
Example 4:
Input: light = [2,1,4,3,6,5]
Output: 3
Example 5:
Input: light = [1,2,3,4,5,6]
Output: 6
Constraints:
n == light.length
1 <= n <= 5 * 10^4
light is a permutation of [1, 2, ..., n]
|
class Solution:
def numTimesAllBlue(self, light: List[int]) -> int:
res = right = 0
for i, n in enumerate(light):
# update right most bulb's number
right = max(right, n)
# right most bulb's number equals to number of processed bulbs
# meaning all bulbs to the left are ON
res += 1 if right == i + 1 else 0
return res
|
There is a room with n bulbs, numbered from 1 to n, arranged in a row from left to right. Initially, all the bulbs are turned off.
At moment k (for k from 0 to n - 1), we turn on the light[k] bulb. A bulb change color to blue only if it is on and all the previous bulbs (to the left) are turned on too.
Return the number of moments in which all turned on bulbs are blue.
Example 1:
Input: light = [2,1,3,5,4]
Output: 3
Explanation: All bulbs turned on, are blue at the moment 1, 2 and 4.
Example 2:
Input: light = [3,2,4,1,5]
Output: 2
Explanation: All bulbs turned on, are blue at the moment 3, and 4 (index-0).
Example 3:
Input: light = [4,1,2,3]
Output: 1
Explanation: All bulbs turned on, are blue at the moment 3 (index-0).
Bulb 4th changes to blue at the moment 3.
Example 4:
Input: light = [2,1,4,3,6,5]
Output: 3
Example 5:
Input: light = [1,2,3,4,5,6]
Output: 6
Constraints:
n == light.length
1 <= n <= 5 * 10^4
light is a permutation of [1, 2, ..., n]
|
class Solution:
def numTimesAllBlue(self, light: List[int]) -> int:
right = -1
res = 0
for i in range(len(light)):
right = max(light[i], right)
if right == i+1:
res += 1
return res
|
There is a room with n bulbs, numbered from 1 to n, arranged in a row from left to right. Initially, all the bulbs are turned off.
At moment k (for k from 0 to n - 1), we turn on the light[k] bulb. A bulb change color to blue only if it is on and all the previous bulbs (to the left) are turned on too.
Return the number of moments in which all turned on bulbs are blue.
Example 1:
Input: light = [2,1,3,5,4]
Output: 3
Explanation: All bulbs turned on, are blue at the moment 1, 2 and 4.
Example 2:
Input: light = [3,2,4,1,5]
Output: 2
Explanation: All bulbs turned on, are blue at the moment 3, and 4 (index-0).
Example 3:
Input: light = [4,1,2,3]
Output: 1
Explanation: All bulbs turned on, are blue at the moment 3 (index-0).
Bulb 4th changes to blue at the moment 3.
Example 4:
Input: light = [2,1,4,3,6,5]
Output: 3
Example 5:
Input: light = [1,2,3,4,5,6]
Output: 6
Constraints:
n == light.length
1 <= n <= 5 * 10^4
light is a permutation of [1, 2, ..., n]
|
class Solution:
def numTimesAllBlue(self, light: List[int]) -> int:
bulb_ranges = {}
lights_on = 0
result = 0
for l in light:
lights_on += 1
left = right = None
if l - 1 not in bulb_ranges and l + 1 not in bulb_ranges:
left = l
right = l
else:
if l - 1 in bulb_ranges:
cur_left, cur_right = bulb_ranges[l - 1]
left = cur_left
right = l
del bulb_ranges[l - 1]
if l + 1 in bulb_ranges:
cur_left, cur_right = bulb_ranges[l + 1]
if left is None:
left = l
right = cur_right
del bulb_ranges[l + 1]
bulb_ranges[left] = (left, right)
bulb_ranges[right] = (left, right)
if left == 1 and right - left + 1 == lights_on:
result += 1
return result
|
There is a room with n bulbs, numbered from 1 to n, arranged in a row from left to right. Initially, all the bulbs are turned off.
At moment k (for k from 0 to n - 1), we turn on the light[k] bulb. A bulb change color to blue only if it is on and all the previous bulbs (to the left) are turned on too.
Return the number of moments in which all turned on bulbs are blue.
Example 1:
Input: light = [2,1,3,5,4]
Output: 3
Explanation: All bulbs turned on, are blue at the moment 1, 2 and 4.
Example 2:
Input: light = [3,2,4,1,5]
Output: 2
Explanation: All bulbs turned on, are blue at the moment 3, and 4 (index-0).
Example 3:
Input: light = [4,1,2,3]
Output: 1
Explanation: All bulbs turned on, are blue at the moment 3 (index-0).
Bulb 4th changes to blue at the moment 3.
Example 4:
Input: light = [2,1,4,3,6,5]
Output: 3
Example 5:
Input: light = [1,2,3,4,5,6]
Output: 6
Constraints:
n == light.length
1 <= n <= 5 * 10^4
light is a permutation of [1, 2, ..., n]
|
class Solution:
def numTimesAllBlue(self, light: List[int]) -> int:
res = nums_on = last_pos = 0
for pos in light:
nums_on += 1
if pos > last_pos:
last_pos = pos
if nums_on == last_pos:
res += 1
return res
|
There is a room with n bulbs, numbered from 1 to n, arranged in a row from left to right. Initially, all the bulbs are turned off.
At moment k (for k from 0 to n - 1), we turn on the light[k] bulb. A bulb change color to blue only if it is on and all the previous bulbs (to the left) are turned on too.
Return the number of moments in which all turned on bulbs are blue.
Example 1:
Input: light = [2,1,3,5,4]
Output: 3
Explanation: All bulbs turned on, are blue at the moment 1, 2 and 4.
Example 2:
Input: light = [3,2,4,1,5]
Output: 2
Explanation: All bulbs turned on, are blue at the moment 3, and 4 (index-0).
Example 3:
Input: light = [4,1,2,3]
Output: 1
Explanation: All bulbs turned on, are blue at the moment 3 (index-0).
Bulb 4th changes to blue at the moment 3.
Example 4:
Input: light = [2,1,4,3,6,5]
Output: 3
Example 5:
Input: light = [1,2,3,4,5,6]
Output: 6
Constraints:
n == light.length
1 <= n <= 5 * 10^4
light is a permutation of [1, 2, ..., n]
|
class Solution:
def numTimesAllBlue(self, light: List[int]) -> int:
last_on = 0
heap = []
heapq.heapify(heap)
result = 0
for i, num in enumerate(light):
if num == last_on + 1:
last_on = num
if not heap: result += 1
while(heap):
top = heapq.heappop(heap)
if top == last_on+1:
last_on += 1
else:
heapq.heappush(heap, top)
break
if not heap:
result+=1
else:
heapq.heappush(heap, num)
return result
|
There is a room with n bulbs, numbered from 1 to n, arranged in a row from left to right. Initially, all the bulbs are turned off.
At moment k (for k from 0 to n - 1), we turn on the light[k] bulb. A bulb change color to blue only if it is on and all the previous bulbs (to the left) are turned on too.
Return the number of moments in which all turned on bulbs are blue.
Example 1:
Input: light = [2,1,3,5,4]
Output: 3
Explanation: All bulbs turned on, are blue at the moment 1, 2 and 4.
Example 2:
Input: light = [3,2,4,1,5]
Output: 2
Explanation: All bulbs turned on, are blue at the moment 3, and 4 (index-0).
Example 3:
Input: light = [4,1,2,3]
Output: 1
Explanation: All bulbs turned on, are blue at the moment 3 (index-0).
Bulb 4th changes to blue at the moment 3.
Example 4:
Input: light = [2,1,4,3,6,5]
Output: 3
Example 5:
Input: light = [1,2,3,4,5,6]
Output: 6
Constraints:
n == light.length
1 <= n <= 5 * 10^4
light is a permutation of [1, 2, ..., n]
|
class Solution:
def numTimesAllBlue(self, A):
right = res = 0
for i, a in enumerate(A, 1):
right = max(right, a)
res += right == i
return res
|
There is a room with n bulbs, numbered from 1 to n, arranged in a row from left to right. Initially, all the bulbs are turned off.
At moment k (for k from 0 to n - 1), we turn on the light[k] bulb. A bulb change color to blue only if it is on and all the previous bulbs (to the left) are turned on too.
Return the number of moments in which all turned on bulbs are blue.
Example 1:
Input: light = [2,1,3,5,4]
Output: 3
Explanation: All bulbs turned on, are blue at the moment 1, 2 and 4.
Example 2:
Input: light = [3,2,4,1,5]
Output: 2
Explanation: All bulbs turned on, are blue at the moment 3, and 4 (index-0).
Example 3:
Input: light = [4,1,2,3]
Output: 1
Explanation: All bulbs turned on, are blue at the moment 3 (index-0).
Bulb 4th changes to blue at the moment 3.
Example 4:
Input: light = [2,1,4,3,6,5]
Output: 3
Example 5:
Input: light = [1,2,3,4,5,6]
Output: 6
Constraints:
n == light.length
1 <= n <= 5 * 10^4
light is a permutation of [1, 2, ..., n]
|
class Solution:
def numTimesAllBlue(self, light: List[int]) -> int:
_min = len(light)+1
_max = 0
missing = set()
num_moments = 0
for i in light:
if i > _max:
if _max != 0:
for j in range(_max+1, i):
missing.add(j)
_max = i
if i < _min:
if _min != len(light)+1:
for j in range(i+1, _min):
missing.add(j)
_min = i
if i in missing:
missing.remove(i)
if len(missing) == 0 and _min < 2:
num_moments+=1
return num_moments
# number_of_moments = 0
# max_seen_value = 0
# min_seen_index = len(light)+1
# for i in range(len(light)):
# if abs(max_seen - light[i]) == 1 and min_seen_index == 0:
# number_of_moments += 1
# if light[i] > max_seen:
# max_seen = light[i]-1
# if min_seen_index < light[i] - 1:
# min_seen_index == light[i] - 1
# return number_of_moments
# max = 3, index = 2
# max = 3, index = 1
# max = 4, index = 1
# max = 4, index = 0
# light_on = [0]*len(light)
# number_of_moments = 0
# farthest_pos = 0
# for i in range(len(light)):
# all_blue = True
# light_on[light[i]-1] = 1
# if light[i] - 1 > farthest_pos:
# farthest_pos = light[i] - 1
# for j in range(farthest_pos):
# if light_on[j] == 0:
# all_blue = False
# break;
# if all_blue:
# number_of_moments += 1
# return number_of_moments
|
There is a room with n bulbs, numbered from 1 to n, arranged in a row from left to right. Initially, all the bulbs are turned off.
At moment k (for k from 0 to n - 1), we turn on the light[k] bulb. A bulb change color to blue only if it is on and all the previous bulbs (to the left) are turned on too.
Return the number of moments in which all turned on bulbs are blue.
Example 1:
Input: light = [2,1,3,5,4]
Output: 3
Explanation: All bulbs turned on, are blue at the moment 1, 2 and 4.
Example 2:
Input: light = [3,2,4,1,5]
Output: 2
Explanation: All bulbs turned on, are blue at the moment 3, and 4 (index-0).
Example 3:
Input: light = [4,1,2,3]
Output: 1
Explanation: All bulbs turned on, are blue at the moment 3 (index-0).
Bulb 4th changes to blue at the moment 3.
Example 4:
Input: light = [2,1,4,3,6,5]
Output: 3
Example 5:
Input: light = [1,2,3,4,5,6]
Output: 6
Constraints:
n == light.length
1 <= n <= 5 * 10^4
light is a permutation of [1, 2, ..., n]
|
class Solution:
def numTimesAllBlue(self, light: List[int]) -> int:
max_ = 0
output = 0
for i in range(len(light)):
max_ = max(max_, light[i])
if max_ == i+1:
output += 1
return output
|
There is a room with n bulbs, numbered from 1 to n, arranged in a row from left to right. Initially, all the bulbs are turned off.
At moment k (for k from 0 to n - 1), we turn on the light[k] bulb. A bulb change color to blue only if it is on and all the previous bulbs (to the left) are turned on too.
Return the number of moments in which all turned on bulbs are blue.
Example 1:
Input: light = [2,1,3,5,4]
Output: 3
Explanation: All bulbs turned on, are blue at the moment 1, 2 and 4.
Example 2:
Input: light = [3,2,4,1,5]
Output: 2
Explanation: All bulbs turned on, are blue at the moment 3, and 4 (index-0).
Example 3:
Input: light = [4,1,2,3]
Output: 1
Explanation: All bulbs turned on, are blue at the moment 3 (index-0).
Bulb 4th changes to blue at the moment 3.
Example 4:
Input: light = [2,1,4,3,6,5]
Output: 3
Example 5:
Input: light = [1,2,3,4,5,6]
Output: 6
Constraints:
n == light.length
1 <= n <= 5 * 10^4
light is a permutation of [1, 2, ..., n]
|
class Solution:
def numTimesAllBlue(self, light: List[int]) -> int:
res = 0
high = 0
on = 0
for l in light:
on += 1
if l > high:
high = l
if on == high:
res += 1
return res
|
There is a room with n bulbs, numbered from 1 to n, arranged in a row from left to right. Initially, all the bulbs are turned off.
At moment k (for k from 0 to n - 1), we turn on the light[k] bulb. A bulb change color to blue only if it is on and all the previous bulbs (to the left) are turned on too.
Return the number of moments in which all turned on bulbs are blue.
Example 1:
Input: light = [2,1,3,5,4]
Output: 3
Explanation: All bulbs turned on, are blue at the moment 1, 2 and 4.
Example 2:
Input: light = [3,2,4,1,5]
Output: 2
Explanation: All bulbs turned on, are blue at the moment 3, and 4 (index-0).
Example 3:
Input: light = [4,1,2,3]
Output: 1
Explanation: All bulbs turned on, are blue at the moment 3 (index-0).
Bulb 4th changes to blue at the moment 3.
Example 4:
Input: light = [2,1,4,3,6,5]
Output: 3
Example 5:
Input: light = [1,2,3,4,5,6]
Output: 6
Constraints:
n == light.length
1 <= n <= 5 * 10^4
light is a permutation of [1, 2, ..., n]
|
class Solution:
def numTimesAllBlue(self, light: List[int]) -> int:
# count times when all the lights on the left are on
# which is: when the max idx of the lights on == the the number of lights on.
max_idx = float('-inf')
cnt = 0
for i in range(len(light)):
max_idx = max(max_idx, light[i])
if max_idx == i + 1:
cnt += 1
return cnt
|
There is a room with n bulbs, numbered from 1 to n, arranged in a row from left to right. Initially, all the bulbs are turned off.
At moment k (for k from 0 to n - 1), we turn on the light[k] bulb. A bulb change color to blue only if it is on and all the previous bulbs (to the left) are turned on too.
Return the number of moments in which all turned on bulbs are blue.
Example 1:
Input: light = [2,1,3,5,4]
Output: 3
Explanation: All bulbs turned on, are blue at the moment 1, 2 and 4.
Example 2:
Input: light = [3,2,4,1,5]
Output: 2
Explanation: All bulbs turned on, are blue at the moment 3, and 4 (index-0).
Example 3:
Input: light = [4,1,2,3]
Output: 1
Explanation: All bulbs turned on, are blue at the moment 3 (index-0).
Bulb 4th changes to blue at the moment 3.
Example 4:
Input: light = [2,1,4,3,6,5]
Output: 3
Example 5:
Input: light = [1,2,3,4,5,6]
Output: 6
Constraints:
n == light.length
1 <= n <= 5 * 10^4
light is a permutation of [1, 2, ..., n]
|
class Solution:
def numTimesAllBlue(self, light: List[int]) -> int:
_max = 0
_min = 1
num_moments = 0
for i in range(len(light)):
if _max < light[i]:
_max = light[i]
if _max - _min == i:
num_moments += 1
return num_moments
# _min = len(light)+1
# _max = 0
# missing = set()
# num_moments = 0
# for i in light:
# if i > _max:
# if _max != 0:
# for j in range(_max+1, i):
# missing.add(j)
# _max = i
# if i < _min:
# if _min != len(light)+1:
# for j in range(i+1, _min):
# missing.add(j)
# _min = i
# if i in missing:
# missing.remove(i)
# if len(missing) == 0 and _min < 2:
# num_moments+=1
# return num_moments
# number_of_moments = 0
# max_seen_value = 0
# min_seen_index = len(light)+1
# for i in range(len(light)):
# if abs(max_seen - light[i]) == 1 and min_seen_index == 0:
# number_of_moments += 1
# if light[i] > max_seen:
# max_seen = light[i]-1
# if min_seen_index < light[i] - 1:
# min_seen_index == light[i] - 1
# return number_of_moments
# max = 3, index = 2
# max = 3, index = 1
# max = 4, index = 1
# max = 4, index = 0
# light_on = [0]*len(light)
# number_of_moments = 0
# farthest_pos = 0
# for i in range(len(light)):
# all_blue = True
# light_on[light[i]-1] = 1
# if light[i] - 1 > farthest_pos:
# farthest_pos = light[i] - 1
# for j in range(farthest_pos):
# if light_on[j] == 0:
# all_blue = False
# break;
# if all_blue:
# number_of_moments += 1
# return number_of_moments
|
There is a room with n bulbs, numbered from 1 to n, arranged in a row from left to right. Initially, all the bulbs are turned off.
At moment k (for k from 0 to n - 1), we turn on the light[k] bulb. A bulb change color to blue only if it is on and all the previous bulbs (to the left) are turned on too.
Return the number of moments in which all turned on bulbs are blue.
Example 1:
Input: light = [2,1,3,5,4]
Output: 3
Explanation: All bulbs turned on, are blue at the moment 1, 2 and 4.
Example 2:
Input: light = [3,2,4,1,5]
Output: 2
Explanation: All bulbs turned on, are blue at the moment 3, and 4 (index-0).
Example 3:
Input: light = [4,1,2,3]
Output: 1
Explanation: All bulbs turned on, are blue at the moment 3 (index-0).
Bulb 4th changes to blue at the moment 3.
Example 4:
Input: light = [2,1,4,3,6,5]
Output: 3
Example 5:
Input: light = [1,2,3,4,5,6]
Output: 6
Constraints:
n == light.length
1 <= n <= 5 * 10^4
light is a permutation of [1, 2, ..., n]
|
class Solution:
def numTimesAllBlue(self, light: List[int]) -> int:
moment = 0
bulbsOn = 0
for i in range(len(light)):
bulbsOn = max(bulbsOn, light[i])
if bulbsOn == i + 1:
moment += 1
return moment
|
There is a room with n bulbs, numbered from 1 to n, arranged in a row from left to right. Initially, all the bulbs are turned off.
At moment k (for k from 0 to n - 1), we turn on the light[k] bulb. A bulb change color to blue only if it is on and all the previous bulbs (to the left) are turned on too.
Return the number of moments in which all turned on bulbs are blue.
Example 1:
Input: light = [2,1,3,5,4]
Output: 3
Explanation: All bulbs turned on, are blue at the moment 1, 2 and 4.
Example 2:
Input: light = [3,2,4,1,5]
Output: 2
Explanation: All bulbs turned on, are blue at the moment 3, and 4 (index-0).
Example 3:
Input: light = [4,1,2,3]
Output: 1
Explanation: All bulbs turned on, are blue at the moment 3 (index-0).
Bulb 4th changes to blue at the moment 3.
Example 4:
Input: light = [2,1,4,3,6,5]
Output: 3
Example 5:
Input: light = [1,2,3,4,5,6]
Output: 6
Constraints:
n == light.length
1 <= n <= 5 * 10^4
light is a permutation of [1, 2, ..., n]
|
class Solution:
def numTimesAllBlue(self, light: List[int]) -> int:
res = 0
numBlue = 0
status = [0] * len(light)
for i in range(len(light)):
idx = light[i] - 1
if idx == 0 or status[idx - 1] == 2:
status[idx] = 2
numBlue += 1
idx += 1
while idx != len(light) and status[idx] == 1:
status[idx] = 2
numBlue += 1
idx += 1
else:
status[idx] = 1
# print(status)
# print(numBlue)
# print(i + 1)
if numBlue == i + 1:
res += 1
return res
|
There is a room with n bulbs, numbered from 1 to n, arranged in a row from left to right. Initially, all the bulbs are turned off.
At moment k (for k from 0 to n - 1), we turn on the light[k] bulb. A bulb change color to blue only if it is on and all the previous bulbs (to the left) are turned on too.
Return the number of moments in which all turned on bulbs are blue.
Example 1:
Input: light = [2,1,3,5,4]
Output: 3
Explanation: All bulbs turned on, are blue at the moment 1, 2 and 4.
Example 2:
Input: light = [3,2,4,1,5]
Output: 2
Explanation: All bulbs turned on, are blue at the moment 3, and 4 (index-0).
Example 3:
Input: light = [4,1,2,3]
Output: 1
Explanation: All bulbs turned on, are blue at the moment 3 (index-0).
Bulb 4th changes to blue at the moment 3.
Example 4:
Input: light = [2,1,4,3,6,5]
Output: 3
Example 5:
Input: light = [1,2,3,4,5,6]
Output: 6
Constraints:
n == light.length
1 <= n <= 5 * 10^4
light is a permutation of [1, 2, ..., n]
|
class Solution:
def numTimesAllBlue(self, light: List[int]) -> int:
res = 0
right = 0
for idx in range(len(light)):
right = max(right, light[idx])
if right == idx + 1:
res += 1
return res
|
There is a room with n bulbs, numbered from 1 to n, arranged in a row from left to right. Initially, all the bulbs are turned off.
At moment k (for k from 0 to n - 1), we turn on the light[k] bulb. A bulb change color to blue only if it is on and all the previous bulbs (to the left) are turned on too.
Return the number of moments in which all turned on bulbs are blue.
Example 1:
Input: light = [2,1,3,5,4]
Output: 3
Explanation: All bulbs turned on, are blue at the moment 1, 2 and 4.
Example 2:
Input: light = [3,2,4,1,5]
Output: 2
Explanation: All bulbs turned on, are blue at the moment 3, and 4 (index-0).
Example 3:
Input: light = [4,1,2,3]
Output: 1
Explanation: All bulbs turned on, are blue at the moment 3 (index-0).
Bulb 4th changes to blue at the moment 3.
Example 4:
Input: light = [2,1,4,3,6,5]
Output: 3
Example 5:
Input: light = [1,2,3,4,5,6]
Output: 6
Constraints:
n == light.length
1 <= n <= 5 * 10^4
light is a permutation of [1, 2, ..., n]
|
class Solution:
def numTimesAllBlue(self, light: List[int]) -> int:
count = 0
max_idx = 0
res = 0
for p in light:
max_idx = max(max_idx, p-1)
count += 1
if count == max_idx + 1:
res += 1
return res
|
There is a room with n bulbs, numbered from 1 to n, arranged in a row from left to right. Initially, all the bulbs are turned off.
At moment k (for k from 0 to n - 1), we turn on the light[k] bulb. A bulb change color to blue only if it is on and all the previous bulbs (to the left) are turned on too.
Return the number of moments in which all turned on bulbs are blue.
Example 1:
Input: light = [2,1,3,5,4]
Output: 3
Explanation: All bulbs turned on, are blue at the moment 1, 2 and 4.
Example 2:
Input: light = [3,2,4,1,5]
Output: 2
Explanation: All bulbs turned on, are blue at the moment 3, and 4 (index-0).
Example 3:
Input: light = [4,1,2,3]
Output: 1
Explanation: All bulbs turned on, are blue at the moment 3 (index-0).
Bulb 4th changes to blue at the moment 3.
Example 4:
Input: light = [2,1,4,3,6,5]
Output: 3
Example 5:
Input: light = [1,2,3,4,5,6]
Output: 6
Constraints:
n == light.length
1 <= n <= 5 * 10^4
light is a permutation of [1, 2, ..., n]
|
class Solution:
def numTimesAllBlue(self, light: List[int]) -> int:
right = res = 0
for i, a in enumerate(light, 1):
right = max(right, a)
res += right == i
return res
|
There is a room with n bulbs, numbered from 1 to n, arranged in a row from left to right. Initially, all the bulbs are turned off.
At moment k (for k from 0 to n - 1), we turn on the light[k] bulb. A bulb change color to blue only if it is on and all the previous bulbs (to the left) are turned on too.
Return the number of moments in which all turned on bulbs are blue.
Example 1:
Input: light = [2,1,3,5,4]
Output: 3
Explanation: All bulbs turned on, are blue at the moment 1, 2 and 4.
Example 2:
Input: light = [3,2,4,1,5]
Output: 2
Explanation: All bulbs turned on, are blue at the moment 3, and 4 (index-0).
Example 3:
Input: light = [4,1,2,3]
Output: 1
Explanation: All bulbs turned on, are blue at the moment 3 (index-0).
Bulb 4th changes to blue at the moment 3.
Example 4:
Input: light = [2,1,4,3,6,5]
Output: 3
Example 5:
Input: light = [1,2,3,4,5,6]
Output: 6
Constraints:
n == light.length
1 <= n <= 5 * 10^4
light is a permutation of [1, 2, ..., n]
|
class Solution:
def numTimesAllBlue(self, light: List[int]) -> int:
def cumulative_sum(input_list):
curSum, cumSum = 0, []
for i in input_list:
curSum += i
cumSum.append(curSum)
return cumSum
cumSum = cumulative_sum(light)
cumSum_target = cumulative_sum(list(range(1, len(light)+1)))
return sum([cumSum[i]==cumSum_target[i] for i in range(len(cumSum))])
|
There is a room with n bulbs, numbered from 1 to n, arranged in a row from left to right. Initially, all the bulbs are turned off.
At moment k (for k from 0 to n - 1), we turn on the light[k] bulb. A bulb change color to blue only if it is on and all the previous bulbs (to the left) are turned on too.
Return the number of moments in which all turned on bulbs are blue.
Example 1:
Input: light = [2,1,3,5,4]
Output: 3
Explanation: All bulbs turned on, are blue at the moment 1, 2 and 4.
Example 2:
Input: light = [3,2,4,1,5]
Output: 2
Explanation: All bulbs turned on, are blue at the moment 3, and 4 (index-0).
Example 3:
Input: light = [4,1,2,3]
Output: 1
Explanation: All bulbs turned on, are blue at the moment 3 (index-0).
Bulb 4th changes to blue at the moment 3.
Example 4:
Input: light = [2,1,4,3,6,5]
Output: 3
Example 5:
Input: light = [1,2,3,4,5,6]
Output: 6
Constraints:
n == light.length
1 <= n <= 5 * 10^4
light is a permutation of [1, 2, ..., n]
|
class Solution:
def numTimesAllBlue(self, light: List[int]) -> int:
moments = 0
pair = [light[0], light[0] - 1]
for i in range(1, len(light)):
if pair[1] == 0:
moments += 1
if pair[0] > light[i]:
pair[1] -= 1
else:
pair[1] = light[i] - pair[0] + pair[1] - 1
pair[0] = light[i]
if pair[1] == 0:
moments += 1
return moments
|
There is a room with n bulbs, numbered from 1 to n, arranged in a row from left to right. Initially, all the bulbs are turned off.
At moment k (for k from 0 to n - 1), we turn on the light[k] bulb. A bulb change color to blue only if it is on and all the previous bulbs (to the left) are turned on too.
Return the number of moments in which all turned on bulbs are blue.
Example 1:
Input: light = [2,1,3,5,4]
Output: 3
Explanation: All bulbs turned on, are blue at the moment 1, 2 and 4.
Example 2:
Input: light = [3,2,4,1,5]
Output: 2
Explanation: All bulbs turned on, are blue at the moment 3, and 4 (index-0).
Example 3:
Input: light = [4,1,2,3]
Output: 1
Explanation: All bulbs turned on, are blue at the moment 3 (index-0).
Bulb 4th changes to blue at the moment 3.
Example 4:
Input: light = [2,1,4,3,6,5]
Output: 3
Example 5:
Input: light = [1,2,3,4,5,6]
Output: 6
Constraints:
n == light.length
1 <= n <= 5 * 10^4
light is a permutation of [1, 2, ..., n]
|
class Solution:
def numTimesAllBlue(self, light: List[int]) -> int:
if not light: return int()
answer = right = int()
for i, light in enumerate(light):
right = max(right, light)
if i + 1 == right:
answer += 1
return answer
|
Given an integer array of digits, return the largest multiple of three that can be formed by concatenating some of the given digits in any order.
Since the answer may not fit in an integer data type, return the answer as a string.
If there is no answer return an empty string.
Example 1:
Input: digits = [8,1,9]
Output: "981"
Example 2:
Input: digits = [8,6,7,1,0]
Output: "8760"
Example 3:
Input: digits = [1]
Output: ""
Example 4:
Input: digits = [0,0,0,0,0,0]
Output: "0"
Constraints:
1 <= digits.length <= 10^4
0 <= digits[i] <= 9
The returning answer must not contain unnecessary leading zeros.
|
class Solution:
def largestMultipleOfThree(self, digits: List[int]) -> str:
counts = Counter(digits)
m = sum(digits) % 3
if m:
if counts[m] + counts[m+3] + counts[m+6]:
counts[min([m+i for i in [0,3,6] if counts[m+i]])] -= 1
else:
counts[min([i-m for i in [3,6,9] if counts[i-m]])] -= 1
counts[min([i-m for i in [3,6,9] if counts[i-m]])] -= 1
ans = ''
for i in range(9, -1, -1):
if not ans and not counts[i]:
continue
ans += str(i) * counts[i]
if ans:
return ans.lstrip('0') or '0'
return ''
|
Given an integer array of digits, return the largest multiple of three that can be formed by concatenating some of the given digits in any order.
Since the answer may not fit in an integer data type, return the answer as a string.
If there is no answer return an empty string.
Example 1:
Input: digits = [8,1,9]
Output: "981"
Example 2:
Input: digits = [8,6,7,1,0]
Output: "8760"
Example 3:
Input: digits = [1]
Output: ""
Example 4:
Input: digits = [0,0,0,0,0,0]
Output: "0"
Constraints:
1 <= digits.length <= 10^4
0 <= digits[i] <= 9
The returning answer must not contain unnecessary leading zeros.
|
class Solution:
def largestMultipleOfThree(self, digits: List[int]) -> str:
heaps, total = [[], [], []], 0
for digit in digits:
total += digit
heapq.heappush(heaps[digit % 3], str(digit))
if r := (total % 3):
if heaps[r]:
heapq.heappop(heaps[r])
elif len(heaps[-r]) > 1:
heapq.heappop(heaps[-r])
heapq.heappop(heaps[-r])
if any(heaps):
return str(int(''.join(sorted(sum(heaps, []), reverse=True))))
return ''
|
Given an integer array of digits, return the largest multiple of three that can be formed by concatenating some of the given digits in any order.
Since the answer may not fit in an integer data type, return the answer as a string.
If there is no answer return an empty string.
Example 1:
Input: digits = [8,1,9]
Output: "981"
Example 2:
Input: digits = [8,6,7,1,0]
Output: "8760"
Example 3:
Input: digits = [1]
Output: ""
Example 4:
Input: digits = [0,0,0,0,0,0]
Output: "0"
Constraints:
1 <= digits.length <= 10^4
0 <= digits[i] <= 9
The returning answer must not contain unnecessary leading zeros.
|
class Solution:
def largestMultipleOfThree(self, d: List[int]) -> str:
d1 = sorted([i for i in d if i%3 ==1])
d2 = sorted([i for i in d if i%3 ==2])
d3 = [i for i in d if i%3 ==0]
if sum(d) % 3 == 1:
if len(d1) != 0:
res = d1[1:] + d2 + d3
else:
res = d2[2:]+ d3
elif sum(d) % 3 == 2:
if len(d2) != 0:
res = d1 + d2[1:] + d3
else:
res = d1[2:] +d3
else:
res = d
res.sort(reverse = True)
if not res: return ''
return str(int(''.join([str(i) for i in res])))
|
Given an integer array of digits, return the largest multiple of three that can be formed by concatenating some of the given digits in any order.
Since the answer may not fit in an integer data type, return the answer as a string.
If there is no answer return an empty string.
Example 1:
Input: digits = [8,1,9]
Output: "981"
Example 2:
Input: digits = [8,6,7,1,0]
Output: "8760"
Example 3:
Input: digits = [1]
Output: ""
Example 4:
Input: digits = [0,0,0,0,0,0]
Output: "0"
Constraints:
1 <= digits.length <= 10^4
0 <= digits[i] <= 9
The returning answer must not contain unnecessary leading zeros.
|
class Solution:
def largestMultipleOfThree(self, digits: List[int]) -> str:
mod_gap = sum(digits) % 3
if mod_gap == 0:
answer = ''.join([str(i) for i in sorted(digits, reverse=True)])
mult3 = [i for i in digits if i % 3==0]
non3 = [i for i in digits if i % 3!=0]
non3 = sorted(non3, reverse=True)
if mod_gap == 1:
last_odd_idx = [i for i in range(len(non3)) if non3[i] & 1]
if len(last_odd_idx) > 0:
last_odd_idx = last_odd_idx[-1]
non3 = non3[:last_odd_idx] + non3[last_odd_idx+1:]
elif mod_gap==2:
last_even_idx = [i for i in range(len(non3)) if non3[i] % 2==0]
if len(last_even_idx) > 0:
last_even_idx = last_even_idx[-1]
non3 = non3[:last_even_idx] + non3[last_even_idx+1:]
if sum(non3) % 3 != 0:
non3 = []
answer = ''.join([str(int(i)) for i in sorted(mult3+non3, reverse=True)])
while answer.startswith('0') and len(answer)>1:
answer = answer[1:]
return answer
|
Given an integer array of digits, return the largest multiple of three that can be formed by concatenating some of the given digits in any order.
Since the answer may not fit in an integer data type, return the answer as a string.
If there is no answer return an empty string.
Example 1:
Input: digits = [8,1,9]
Output: "981"
Example 2:
Input: digits = [8,6,7,1,0]
Output: "8760"
Example 3:
Input: digits = [1]
Output: ""
Example 4:
Input: digits = [0,0,0,0,0,0]
Output: "0"
Constraints:
1 <= digits.length <= 10^4
0 <= digits[i] <= 9
The returning answer must not contain unnecessary leading zeros.
|
class Solution:
def largestMultipleOfThree(self, digits: List[int]) -> str:
dp = [-1, -1, -1]
for x in sorted(digits, reverse=True):
r = x%3
r1 = 0
for y in list(dp):
if y == -1:
dp[r] = max(dp[r], x)
else:
dp[(r1+r)%3] = max(dp[(r1+r)%3], 10*y+x)
r1 += 1
if dp[0] == -1:
return ''
else:
return str(dp[0])
|
Given an integer array of digits, return the largest multiple of three that can be formed by concatenating some of the given digits in any order.
Since the answer may not fit in an integer data type, return the answer as a string.
If there is no answer return an empty string.
Example 1:
Input: digits = [8,1,9]
Output: "981"
Example 2:
Input: digits = [8,6,7,1,0]
Output: "8760"
Example 3:
Input: digits = [1]
Output: ""
Example 4:
Input: digits = [0,0,0,0,0,0]
Output: "0"
Constraints:
1 <= digits.length <= 10^4
0 <= digits[i] <= 9
The returning answer must not contain unnecessary leading zeros.
|
class Solution:
def largestMultipleOfThree(self, a: List[int]) -> str:
n, dp = len(a), [''] * 3
a.sort(reverse=True)
for i in range(n):
d, dp1 = a[i] % 3, [''] * 3
for j in range(3):
k = (j-d) % 3
dp1[j] = max([dp[k] + str(a[i]) if dp[k] or k==0 else '', dp[j]], key=lambda x: (len(x), x))
if len(dp1[j]) >= 2 and dp1[j][0] == '0': dp1[j] = dp1[j][1:]
dp = dp1
return dp[0]
|
Given an integer array of digits, return the largest multiple of three that can be formed by concatenating some of the given digits in any order.
Since the answer may not fit in an integer data type, return the answer as a string.
If there is no answer return an empty string.
Example 1:
Input: digits = [8,1,9]
Output: "981"
Example 2:
Input: digits = [8,6,7,1,0]
Output: "8760"
Example 3:
Input: digits = [1]
Output: ""
Example 4:
Input: digits = [0,0,0,0,0,0]
Output: "0"
Constraints:
1 <= digits.length <= 10^4
0 <= digits[i] <= 9
The returning answer must not contain unnecessary leading zeros.
|
class Solution:
def largestMultipleOfThree(self, digits: List[int]) -> str:
digits = collections.Counter(digits)
final_ans_digits = {i: 0 for i in range(10)}
for i in range(0, 10, 3):
final_ans_digits[i] = digits[i]
# append all digits whose quant > 3
left_numbers = list()
for i in [1,2,4,5,7,8]:
if digits[i] > 3:
undefined = 3 + (digits[i] % 3)
left_numbers += [i] * undefined
else:
left_numbers += [i] * digits[i]
mod = sum(left_numbers) % 3
# but we may need to exclude a couple of numbers
if not mod:
pass
elif not(left_numbers[0] - mod) % 3:
# have to exclude number, can exclude the smallest
digits[left_numbers[0]] -= 1
else:
# have to exclude some numbers
# option 1: exclude a single number
num1 = None
for n in left_numbers:
if not (n - mod) % 3:
num1 = n
digits[n] -= 1
break
if num1 is None:
# option 2: exclude two numbers
num2 = None
for i in range(1, len(left_numbers)):
if not (left_numbers[i] + left_numbers[0] - mod) % 3:
num2 = left_numbers[i]
digits[left_numbers[0]] -= 1
digits[num2] -= 1
if num2 is None:
return ''
ans = ''.join(str(i) * digits[i] for i in range(9, -1, -1))
if not ans:
return ''
elif ans[0] == '0':
return '0'
else:
return ans
|
Given an integer array of digits, return the largest multiple of three that can be formed by concatenating some of the given digits in any order.
Since the answer may not fit in an integer data type, return the answer as a string.
If there is no answer return an empty string.
Example 1:
Input: digits = [8,1,9]
Output: "981"
Example 2:
Input: digits = [8,6,7,1,0]
Output: "8760"
Example 3:
Input: digits = [1]
Output: ""
Example 4:
Input: digits = [0,0,0,0,0,0]
Output: "0"
Constraints:
1 <= digits.length <= 10^4
0 <= digits[i] <= 9
The returning answer must not contain unnecessary leading zeros.
|
class Solution:
def largestMultipleOfThree(self, a: List[int]) -> str:
n = len(a); a.sort(reverse=True)
dp = ['']*3
for i in range(n):
d = a[i] % 3
dp1 = ['']*3
for j in range(3):
k = (j-d) % 3
dp1[j] = max([dp[k] + str(a[i]) if dp[k] or k==0 else '', dp[j]], key=lambda x: (len(x), x))
if len(dp1[j]) >= 2 and dp1[j][0] == '0': dp1[j] = dp1[j][1:]
dp = dp1
return dp[0] or ''
|
Given an integer array of digits, return the largest multiple of three that can be formed by concatenating some of the given digits in any order.
Since the answer may not fit in an integer data type, return the answer as a string.
If there is no answer return an empty string.
Example 1:
Input: digits = [8,1,9]
Output: "981"
Example 2:
Input: digits = [8,6,7,1,0]
Output: "8760"
Example 3:
Input: digits = [1]
Output: ""
Example 4:
Input: digits = [0,0,0,0,0,0]
Output: "0"
Constraints:
1 <= digits.length <= 10^4
0 <= digits[i] <= 9
The returning answer must not contain unnecessary leading zeros.
|
class Solution:
def largestMultipleOfThree(self, a: List[int]) -> str:
n = len(a); a.sort(reverse=True)
dp = ['']*3
for i in range(n):
d = a[i] % 3
dp1 = ['']*3
for j in range(3):
k = (j-d) % 3
dp1[j] = max([dp[k] + str(a[i]) if dp[k] or k==0 else '', dp[j]], key=lambda x: (len(x), x))
if len(dp1[j]) >= 2 and dp1[j][0] == '0': dp1[j] = dp1[j][1:]
dp = dp1
return dp[0]
|
Given an integer array of digits, return the largest multiple of three that can be formed by concatenating some of the given digits in any order.
Since the answer may not fit in an integer data type, return the answer as a string.
If there is no answer return an empty string.
Example 1:
Input: digits = [8,1,9]
Output: "981"
Example 2:
Input: digits = [8,6,7,1,0]
Output: "8760"
Example 3:
Input: digits = [1]
Output: ""
Example 4:
Input: digits = [0,0,0,0,0,0]
Output: "0"
Constraints:
1 <= digits.length <= 10^4
0 <= digits[i] <= 9
The returning answer must not contain unnecessary leading zeros.
|
class Solution:
def largestMultipleOfThree(self, a: List[int]) -> str:
n, dp = len(a), [''] * 3
a.sort(reverse=True)
for i in range(n):
d, dp1 = a[i] % 3, [''] * 3
for j in range(3):
k = (j-d) % 3
dp1[j] = max([dp[k] + str(a[i]) if dp[k] or k==0 else '', dp[j]], key=lambda x: (len(x), x))
if len(dp1[j]) >= 2 and dp1[j][0] == '0': dp1[j] = dp1[j][1:]
dp = dp1
return dp[0]
|
Given an integer array of digits, return the largest multiple of three that can be formed by concatenating some of the given digits in any order.
Since the answer may not fit in an integer data type, return the answer as a string.
If there is no answer return an empty string.
Example 1:
Input: digits = [8,1,9]
Output: "981"
Example 2:
Input: digits = [8,6,7,1,0]
Output: "8760"
Example 3:
Input: digits = [1]
Output: ""
Example 4:
Input: digits = [0,0,0,0,0,0]
Output: "0"
Constraints:
1 <= digits.length <= 10^4
0 <= digits[i] <= 9
The returning answer must not contain unnecessary leading zeros.
|
import numpy as np
class Solution:
def largestMultipleOfThree(self, digits: List[int]) -> str:
mod_gap = sum(digits) % 3
if mod_gap == 0:
answer = ''.join([str(i) for i in sorted(digits, reverse=True)])
digits = np.array(digits)
cond = digits % 3 == 0
mult3 = digits[cond]
non3 = digits[~cond]
non3 = sorted(non3, reverse=True)
non3 = np.array(non3)
if mod_gap == 1:
last_odd_idx = np.ravel(np.argwhere(non3 & 1))
if len(last_odd_idx) > 0:
last_odd_idx = last_odd_idx[-1]
non3 = list(non3[:last_odd_idx]) + list(non3[last_odd_idx+1:])
elif mod_gap==2:
last_even_idx = np.ravel(np.argwhere(non3 % 2==0))
if len(last_even_idx) > 0:
last_even_idx = last_even_idx[-1]
non3 = list(non3[:last_even_idx]) + list(non3[last_even_idx+1:])
if sum(non3) % 3 != 0:
non3 = []
answer = ''.join([str(int(i)) for i in sorted (np.concatenate([mult3, non3]), reverse=True)])
while answer.startswith('0') and len(answer)>1:
answer = answer[1:]
return answer
|
Given an integer array of digits, return the largest multiple of three that can be formed by concatenating some of the given digits in any order.
Since the answer may not fit in an integer data type, return the answer as a string.
If there is no answer return an empty string.
Example 1:
Input: digits = [8,1,9]
Output: "981"
Example 2:
Input: digits = [8,6,7,1,0]
Output: "8760"
Example 3:
Input: digits = [1]
Output: ""
Example 4:
Input: digits = [0,0,0,0,0,0]
Output: "0"
Constraints:
1 <= digits.length <= 10^4
0 <= digits[i] <= 9
The returning answer must not contain unnecessary leading zeros.
|
class Solution:
def largestMultipleOfThree(self, digits: List[int]) -> str:
digits.sort(reverse=True)
N = len(digits)
dp = [[float('-inf')] * N for _ in range(3)]
for n in range(N):
for k in [0, 1, 2, 0]:
if n == 0 or (dp[k][n-1] == float('-inf') and dp[(k - digits[n]) % 3][n-1] == float('-inf')):
if digits[n] % 3 == k:
dp[k][n] = digits[n]
else:
if digits[n] % 3 == 0:
dp[k][n] = dp[k][n-1] * 10 + digits[n]
else:
dp[k][n] = max(dp[k][n-1], dp[(k - digits[n]) % 3][n-1] * 10 + digits[n])
return str(dp[0][-1]) if dp[0][-1] != float('-inf') else ''
|
Given an integer array of digits, return the largest multiple of three that can be formed by concatenating some of the given digits in any order.
Since the answer may not fit in an integer data type, return the answer as a string.
If there is no answer return an empty string.
Example 1:
Input: digits = [8,1,9]
Output: "981"
Example 2:
Input: digits = [8,6,7,1,0]
Output: "8760"
Example 3:
Input: digits = [1]
Output: ""
Example 4:
Input: digits = [0,0,0,0,0,0]
Output: "0"
Constraints:
1 <= digits.length <= 10^4
0 <= digits[i] <= 9
The returning answer must not contain unnecessary leading zeros.
|
class Solution:
def largestMultipleOfThree(self, digits: List[int]) -> str:
state2nums = defaultdict(Counter)
digits.sort(reverse=True)
for num in digits:
delta = num % 3
nstate = defaultdict(Counter)
for i in range(3):
if sum(v for k, v in list(state2nums[i].items())) == 0 and i != 0:
continue
target = (delta+i) % 3
if sum(v for k, v in list(state2nums[target].items())) < sum(v for k, v in list(state2nums[i].items()))+1:
counter = Counter(state2nums[i])
counter[num] += 1
nstate[target] = counter
for i in range(3):
if i in nstate:
state2nums[i] = nstate[i]
# print(state2nums)
print(state2nums)
nums = sorted([k for k, v in list(state2nums[0].items()) for _ in range(v) ], reverse=True)
ret = ''.join(map(str, nums))
return '0' if ret and ret[0] == '0' else ret
|
Given an integer array of digits, return the largest multiple of three that can be formed by concatenating some of the given digits in any order.
Since the answer may not fit in an integer data type, return the answer as a string.
If there is no answer return an empty string.
Example 1:
Input: digits = [8,1,9]
Output: "981"
Example 2:
Input: digits = [8,6,7,1,0]
Output: "8760"
Example 3:
Input: digits = [1]
Output: ""
Example 4:
Input: digits = [0,0,0,0,0,0]
Output: "0"
Constraints:
1 <= digits.length <= 10^4
0 <= digits[i] <= 9
The returning answer must not contain unnecessary leading zeros.
|
class Solution:
def largestMultipleOfThree(self, digits: List[int]) -> str:
n = len(digits)
f = [0] * 10
for x in digits:
f[x] += 1
M = defaultdict(int)
def upper(d, m): # largest 3k + m: 3k + m <= f[d]
if f[d] < m:
return 0
return (f[d]-m)//3*3+m
def mx(d, mod): # largest k: 0 <= k <= f[d] && k * d % 3 = mod
dm = d % 3
if dm == 0:
return 0 if mod != 0 else f[d]
if dm == 1:
return upper(d, mod)
if dm == 2:
return upper(d, (3 - mod) % 3)
for i in range(10):
for j in range(3):
M[i, j] = mx(i, j)
X = [0] * 10
R = [0] * 10
def findout(i, mod):
if i < 0:
if mod == 0:
gt = False
sx = sr = 0
for i in range(10):
sx += X[i]
sr += R[i]
if sx > sr:
gt = True
elif sx == sr:
for i in reversed(list(range(10))):
if X[i] > R[i]:
gt = True
break
elif X[i] < R[i]:
break
if gt:
for i in range(10):
R[i] = X[i]
return
for j in range(3):
if M[i, j] > 0:
X[i] = M[i, j]
findout(i-1, (mod + j) % 3)
X[i] = 0
findout(i-1, mod)
findout(9, 0)
ret = ''
for i in reversed(list(range(10))):
if R[i] > 0:
ret += R[i] * str(i)
if len(ret) > 0 and ret[0] == '0':
ret = '0'
return ret
|
Given an integer array of digits, return the largest multiple of three that can be formed by concatenating some of the given digits in any order.
Since the answer may not fit in an integer data type, return the answer as a string.
If there is no answer return an empty string.
Example 1:
Input: digits = [8,1,9]
Output: "981"
Example 2:
Input: digits = [8,6,7,1,0]
Output: "8760"
Example 3:
Input: digits = [1]
Output: ""
Example 4:
Input: digits = [0,0,0,0,0,0]
Output: "0"
Constraints:
1 <= digits.length <= 10^4
0 <= digits[i] <= 9
The returning answer must not contain unnecessary leading zeros.
|
class Solution:
def largestMultipleOfThree(self, digits: List[int]) -> str:
dp = [-1, -1, -1]
for x in sorted(digits)[::-1]:
for a in dp[:] + [0]:
y = a * 10 + x
dp[y % 3] = max(dp[y % 3], y)
return str(dp[0]) if dp[0] >= 0 else ''
|
Given an integer array of digits, return the largest multiple of three that can be formed by concatenating some of the given digits in any order.
Since the answer may not fit in an integer data type, return the answer as a string.
If there is no answer return an empty string.
Example 1:
Input: digits = [8,1,9]
Output: "981"
Example 2:
Input: digits = [8,6,7,1,0]
Output: "8760"
Example 3:
Input: digits = [1]
Output: ""
Example 4:
Input: digits = [0,0,0,0,0,0]
Output: "0"
Constraints:
1 <= digits.length <= 10^4
0 <= digits[i] <= 9
The returning answer must not contain unnecessary leading zeros.
|
class Solution:
def largestMultipleOfThree(self, digits: List[int]) -> str:
vals = {0 : 0, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0, 8: 0, 9: 0}
total = 0
res = []
for digit in digits:
vals[digit] += 1
total += int(digit)
if total % 3 == 0:
temp = 0
for i in range(9,0,-1):
temp += vals[i]
if temp == 0:
return '0'
for i in range(9,-1,-1):
for _ in range(vals[i]):
res.append(str(i))
return ''.join(res)
if total % 3 == 1:
for i in range(10):
if i%3 == 1 and vals[i] > 0:
vals[i] -= 1
for i in range(9,-1,-1):
for _ in range(vals[i]):
res.append(str(i))
return ''.join(res)
count = 0
i = 0
while i < 10:
if count == 2:
for i in range(9,-1,-1):
for _ in range(vals[i]):
res.append(str(i))
return ''.join(res)
elif i%3 == 2 and vals[i] > 0:
vals[i] -= 1
count += 1
else:
i += 1
else:
for i in range(10):
if i%3 == 2 and vals[i] > 0:
vals[i] -= 1
for i in range(9,-1,-1):
for _ in range(vals[i]):
res.append(str(i))
return ''.join(res)
count = 0
i = 0
while i < 10:
if count == 2:
for i in range(9,-1,-1):
for _ in range(vals[i]):
res.append(str(i))
return ''.join(res)
elif i%3 == 1 and vals[i] > 0:
vals[i] -= 1
count += 1
else:
i += 1
return ''.join(res)
|
Given an integer array of digits, return the largest multiple of three that can be formed by concatenating some of the given digits in any order.
Since the answer may not fit in an integer data type, return the answer as a string.
If there is no answer return an empty string.
Example 1:
Input: digits = [8,1,9]
Output: "981"
Example 2:
Input: digits = [8,6,7,1,0]
Output: "8760"
Example 3:
Input: digits = [1]
Output: ""
Example 4:
Input: digits = [0,0,0,0,0,0]
Output: "0"
Constraints:
1 <= digits.length <= 10^4
0 <= digits[i] <= 9
The returning answer must not contain unnecessary leading zeros.
|
class Solution:
def largestMultipleOfThree(self, digits: List[int]) -> str:
r_to_nums = {i:list() for i in range(3)}
for x in digits:
r = x % 3
r_to_nums[r].append(x)
for i in range(3):
r_to_nums[i].sort()
counter = collections.Counter()
for x in digits:
counter[str(x)] += 1
r = sum(digits)%3
if r == 1:
if r_to_nums[1]:
y = str(r_to_nums[1][0])
counter[y] -= 1
elif len(r_to_nums[2]) >= 2:
y = str(r_to_nums[2][0])
counter[y] -= 1
y = str(r_to_nums[2][1])
counter[y] -= 1
else:
return ''
elif r == 2:
if r_to_nums[2]:
y = str(r_to_nums[2][0])
counter[y] -= 1
elif len(r_to_nums[1]) >= 2:
y = str(r_to_nums[1][0])
counter[y] -= 1
y = str(r_to_nums[1][1])
counter[y] -= 1
else:
return ''
res = ''
for x in sorted(counter, reverse=True):
cnt = counter[x]
res += x*cnt
if not res:
return res
return str(int(res))
|
Given an integer array of digits, return the largest multiple of three that can be formed by concatenating some of the given digits in any order.
Since the answer may not fit in an integer data type, return the answer as a string.
If there is no answer return an empty string.
Example 1:
Input: digits = [8,1,9]
Output: "981"
Example 2:
Input: digits = [8,6,7,1,0]
Output: "8760"
Example 3:
Input: digits = [1]
Output: ""
Example 4:
Input: digits = [0,0,0,0,0,0]
Output: "0"
Constraints:
1 <= digits.length <= 10^4
0 <= digits[i] <= 9
The returning answer must not contain unnecessary leading zeros.
|
class Solution:
def largestMultipleOfThree(self, digits: List[int]) -> str:
dic=defaultdict(list)
for d in digits:dic[d%3].append(d)
arr=[]
n1=len(dic[1])
n2=len(dic[2])
arr+=dic[0]
if n1%3==n2%3==0 or n1==n2:arr+=dic[1]+dic[2]
else:
dic[1].sort(reverse=1)
dic[2].sort(reverse=1)
l1=l2=0
if n1%3==2 and n2==3:
while n2>3:
l2+=3
n2-=3
l1+=n1//3*3
l1+=2
l2+=2
elif n2%3==2 and n1==3:
while n1>3:
l1+=3
n1-=3
l2+=n2//3*3
l2+=2
l1+=2
else:
if n1>2:
l1+=n1//3*3
n1-=n1//3*3
if n2>2:
l2+=n2//3*3
n2-=n2//3*3
if n1!=0 and n2!=0:
min1=min(n1,n2)
l1+=min1
l2+=min1
arr+=dic[1][:l1]+dic[2][:l2]
arr.sort(reverse=1)
if arr and arr[0]==0:return '0'
return ''.join(str(i) for i in arr)
|
The set [1,2,3,...,n] contains a total of n! unique permutations.
By listing and labeling all of the permutations in order, we get the following sequence for n = 3:
"123"
"132"
"213"
"231"
"312"
"321"
Given n and k, return the kth permutation sequence.
Note:
Given n will be between 1 and 9 inclusive.
Given k will be between 1 and n! inclusive.
Example 1:
Input: n = 3, k = 3
Output: "213"
Example 2:
Input: n = 4, k = 9
Output: "2314"
|
class Solution:
def getPermutation(self, n, k):
"""
:type n: int
:type k: int
:rtype: str
"""
nums = list("123456789")
k -= 1
factor = 1
for i in range(1, n):
factor *= i
res = []
for i in reversed(list(range(n))):
res.append(nums[k//factor])
nums.remove(nums[k//factor])
if i:
k %= factor
factor //= i
return "".join(res)
|
The set [1,2,3,...,n] contains a total of n! unique permutations.
By listing and labeling all of the permutations in order, we get the following sequence for n = 3:
"123"
"132"
"213"
"231"
"312"
"321"
Given n and k, return the kth permutation sequence.
Note:
Given n will be between 1 and 9 inclusive.
Given k will be between 1 and n! inclusive.
Example 1:
Input: n = 3, k = 3
Output: "213"
Example 2:
Input: n = 4, k = 9
Output: "2314"
|
class Solution:
def getPermutation(self, n, k):
"""
:type n: int
:type k: int
:rtype: str
"""
def factorial(n):
if n == 0:
return 1
m = 1
while n>0:
m = m*n
n -= 1
return m
ans = ""
digits = [i+1 for i in range(n)]
print(("digits: ",digits))
total = factorial(n)
while(len(digits)>0):
rem = k%factorial(n-1)
digit = digits[(k//factorial(n-1) + 1*(rem>0)) -1]
ans+=str(digit)
digits.remove(digit)
k = rem
n -= 1
return ans
|
The set [1,2,3,...,n] contains a total of n! unique permutations.
By listing and labeling all of the permutations in order, we get the following sequence for n = 3:
"123"
"132"
"213"
"231"
"312"
"321"
Given n and k, return the kth permutation sequence.
Note:
Given n will be between 1 and 9 inclusive.
Given k will be between 1 and n! inclusive.
Example 1:
Input: n = 3, k = 3
Output: "213"
Example 2:
Input: n = 4, k = 9
Output: "2314"
|
class Solution:
import math
def getPermutation(self, n, k):
"""
:type n: int
:type k: int
:rtype: str
"""
a = 0
options = ""
for i in range(n):
options += str(i + 1)
ans = ""
while n > 0:
x = math.factorial(n - 1)
while k > x:
k -= x
a += 1
ans += options[a]
options = options[:a] + options[a + 1:]
a = 0
n -= 1
return ans
|
The set [1,2,3,...,n] contains a total of n! unique permutations.
By listing and labeling all of the permutations in order, we get the following sequence for n = 3:
"123"
"132"
"213"
"231"
"312"
"321"
Given n and k, return the kth permutation sequence.
Note:
Given n will be between 1 and 9 inclusive.
Given k will be between 1 and n! inclusive.
Example 1:
Input: n = 3, k = 3
Output: "213"
Example 2:
Input: n = 4, k = 9
Output: "2314"
|
class Solution:
def getPermutation(self, n, k):
"""
:type n: int
:type k: int
:rtype: str
"""
import math
nums = list(range(1, n+1))
ans = ''
k = k - 1 #make sure the divmod result fail in [0, n-1] instead of [1, n]
while n > 0:
n = n - 1
index, k = divmod(k, math.factorial(n))
ans = ans + str(nums[index])
nums.remove(nums[index])
return ans
|
The set [1,2,3,...,n] contains a total of n! unique permutations.
By listing and labeling all of the permutations in order, we get the following sequence for n = 3:
"123"
"132"
"213"
"231"
"312"
"321"
Given n and k, return the kth permutation sequence.
Note:
Given n will be between 1 and 9 inclusive.
Given k will be between 1 and n! inclusive.
Example 1:
Input: n = 3, k = 3
Output: "213"
Example 2:
Input: n = 4, k = 9
Output: "2314"
|
class Solution:
def getPermutation(self, n, k):
"""
Input:
n: int
k: int
Output:
str
"""
return self.__getPerm(set(range(1, n+1)), k)
def __getPerm(self, set_n, k):
"""
Input:
set_n: set: set of available number
k: int: k-th permuation
Output:
str
"""
if not set_n:
return ''
arr_n = sorted(set_n)
n = len(arr_n)
i = 0
fac = 1 # i!
while i+1 < n:
i += 1
fac *= i
# fac = (n-1)!
i_digit = (k-1)//fac
k = k - i_digit*fac
digit = arr_n[i_digit]
set_n.remove(digit)
return str(digit) + self.__getPerm(set_n, k)
|
The set [1,2,3,...,n] contains a total of n! unique permutations.
By listing and labeling all of the permutations in order, we get the following sequence for n = 3:
"123"
"132"
"213"
"231"
"312"
"321"
Given n and k, return the kth permutation sequence.
Note:
Given n will be between 1 and 9 inclusive.
Given k will be between 1 and n! inclusive.
Example 1:
Input: n = 3, k = 3
Output: "213"
Example 2:
Input: n = 4, k = 9
Output: "2314"
|
class Solution:
def getPermutation(self, n, k):
"""
:type n: int
:type k: int
:rtype: str
"""
nums = [i + 1 for i in range(0, n)]
def nextnum(nn):
cnt = 0
for i in range(0, len(nums)):
if nums[i] == 0:
continue
cnt += 1
if cnt == nn:
ret = nums[i]
nums[i] = 0
return ret
facts = [1]
for i in range(1, n+2):
facts.append(facts[i-1] * i)
perm = []
while (n > 1):
nk = (k - 1) // facts[n-1] + 1
rk = (k - 1) % facts[n-1] + 1
perm.append(nextnum(nk))
k = rk
n -= 1
perm.append(nextnum(k))
return "".join([str(x) for x in perm])
|
The set [1,2,3,...,n] contains a total of n! unique permutations.
By listing and labeling all of the permutations in order, we get the following sequence for n = 3:
"123"
"132"
"213"
"231"
"312"
"321"
Given n and k, return the kth permutation sequence.
Note:
Given n will be between 1 and 9 inclusive.
Given k will be between 1 and n! inclusive.
Example 1:
Input: n = 3, k = 3
Output: "213"
Example 2:
Input: n = 4, k = 9
Output: "2314"
|
class Solution:
def factorial(self, n):
if n <= 1:
return 1
return self.factorial(n-1) * n
def getPermutation(self, n, k):
"""
:type n: int
:type k: int
:rtype: str
"""
r = ''
nums = [i+1 for i in range(n)]
m = n
while m:
np = self.factorial(m-1)
i = (k-1)//np
k -= i*np
r += str(nums[i])
del nums[i]
m = len(nums)
return r
|
The set [1,2,3,...,n] contains a total of n! unique permutations.
By listing and labeling all of the permutations in order, we get the following sequence for n = 3:
"123"
"132"
"213"
"231"
"312"
"321"
Given n and k, return the kth permutation sequence.
Note:
Given n will be between 1 and 9 inclusive.
Given k will be between 1 and n! inclusive.
Example 1:
Input: n = 3, k = 3
Output: "213"
Example 2:
Input: n = 4, k = 9
Output: "2314"
|
class Solution:
def getPermutation(self, n, k):
"""
:type n: int
:type k: int
:rtype: str
"""
import math
if k > math.factorial(n):
return None
else:
nums = [str(i) for i in range(1,n+1)]
ans = ''
while k % math.factorial(len(nums)) != 0:
i = math.ceil(k / math.factorial(len(nums)-1))
k %= math.factorial(len(nums)-1)
ans += nums[i-1]
nums.pop(i-1)
ans += ''.join(nums[::-1])
return ans
|
The set [1,2,3,...,n] contains a total of n! unique permutations.
By listing and labeling all of the permutations in order, we get the following sequence for n = 3:
"123"
"132"
"213"
"231"
"312"
"321"
Given n and k, return the kth permutation sequence.
Note:
Given n will be between 1 and 9 inclusive.
Given k will be between 1 and n! inclusive.
Example 1:
Input: n = 3, k = 3
Output: "213"
Example 2:
Input: n = 4, k = 9
Output: "2314"
|
class Solution:
def getPermutation(self, n, k):
"""
:type n: int
:type k: int
:rtype: str
"""
ans = ""
nums = list(range(1,n+1))
m = self.factorial(n-1)
while len(nums)>1:
print("n=",n, ", k=",k, " m=", m)
remainer = k % m
if remainer==0:
remainer = m
index = (k-remainer) // m
print(index)
ans += str(nums[index])
nums.remove(nums[index])
k = remainer
n -= 1
m = m // n
ans += str(nums[0])
return ans
def factorial(self, n):
if n==1 or n==0:
return 1
else:
return n*self.factorial(n-1)
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.