-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbulbs.py
92 lines (87 loc) · 1.83 KB
/
bulbs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# -----------------------------------------------
# Author : Mohit Chaudhari
# Created Date : 08/07/21
# -----------------------------------------------
# Bulbs
# Problem Description
#
# N light bulbs are connected by a wire.
#
# Each bulb has a switch associated with it,
# however due to faulty wiring, a switch also changes the state of all the bulbs to the right of current bulb.
#
# Given an initial state of all bulbs, find the minimum number of switches you have to press to turn on all the bulbs.
#
# You can press the same switch multiple times.
#
# Note: 0 represents the bulb is off and 1 represents the bulb is on.
#
#
#
# Problem Constraints
# 1 <= N <= 5*105
# 0 <= A[i] <= 1
#
#
#
# Input Format
# The first and the only argument contains an integer array A, of size N.
#
#
#
# Output Format
# Return an integer representing the minimum number of switches required.
#
#
#
# Example Input
# Input 1:
#
# A = [0, 1, 0, 1]
# Input 2:
#
# A = [1, 1, 1, 1]
#
#
# Example Output
# Output 1:
#
# 4
# Output 2:
#
# 0
#
#
# Example Explanation
# Explanation 1:
#
# press switch 0 : [1 0 1 0]
# press switch 1 : [1 1 0 1]
# press switch 2 : [1 1 1 0]
# press switch 3 : [1 1 1 1]
# Explanation 2:
#
# There is no need to turn any switches as all the bulbs are already on.
class Solution:
# @param A : list of integers
# @return an integer
def bulbs(self, A):
n = len(A)
if n == 0:
return 0
cnt = 0
cnd = A[0]
addition = 1 if A[0] == 0 else 0
for i in range(n):
if cnd == 0:
if A[i] == 1:
cnd = 1
cnt += 1
elif cnd == 1:
if A[i] == 0:
cnd = 0
cnt += 1
return cnt + addition
s = Solution()
print(s.bulbs([1, 1, 1, 1]))
# OUTPUT : 0