-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbulb.py
More file actions
83 lines (66 loc) · 2.49 KB
/
bulb.py
File metadata and controls
83 lines (66 loc) · 2.49 KB
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
# Still pending...
class Solution(object):
def bulbSwitch(self, n):
"""
:type n: int
:rtype: int
"""
# Logic 4:
# * Taking divisibility test in logic 3 to another level
# * This solution has interesting math property --> Only perfect squares have odd number of divisibility and only odd leads to bulb being on
# https://leetcode.com/problems/bulb-switcher/discuss/327486/Python-one-line-solution
# * Lets do a O(N) iteration instead of a one line solution
result = 0
for i in range(int(math.sqrt(n))+1):
if float(math.sqrt(i+1)).is_integer():
result += 1
return result
# Logic 3: Divisibility test logic
"""
def is_divisible(number):
import math
result = 2 # 1 and itself being divisible
if number == 1:
return 1
for i in range(2, (number//2)+1):#int(math.sqrt(number))+1):
if number%i == 0:
result += 1
return result
switches_with_one = 0
for i in range(n):
div_test = is_divisible(i+1)
#print(i+1, div_test)
if div_test%2 != 0:
switches_with_one += 1
return switches_with_one
"""
# Logic 2: Naive bruteforce
"""
# Lets assume 1 in ON and 0 is OFF
# * First steps (1st iteration) also includes constructing the dictionary
# * So, all are ON
# Initial condition and first round
switches = [1]*n #[0]*n
# N Rounds with N switches
for i in range(2, n+1):
for j in range(i, n+1, i):
switches[j-1] = switches[j-1]^1
#print(switches)
return switches.count(1)
"""
# Logic 1: an attempt before 7 month
"""
import collections
# Lets assume 1 in ON and 0 is OFF
# * First steps (1st iteration) also includes constructing the dictionary
# * So, all are ON
switches = collections.Counter(range(1,n+1))
for i in range(2, n+1):
if i == 2:
for k in range(i, n+1, i):
switches[k] = 0
else:
for k in range(i, n+1, i):
switches[k] = switches[k]^1
return sum(switches.values())
"""