-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathCountingBitsDP.py
More file actions
50 lines (44 loc) · 855 Bytes
/
CountingBitsDP.py
File metadata and controls
50 lines (44 loc) · 855 Bytes
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
'''
Problem Statement:
**INTERVIEW QUESTION**
For i = 0 to N
return a list of number of one's in binary representation of i
5
[0,1,1,2,1,2]
(0)000 -> 0
(1)001 -> 1
(2)010 -> 1
(3)011 -> 2
(4)100 -> 1
(5)101 -> 2
'''
# TIME COMPLEXITY = O(N^2) => N FOR LOOP * N FOR .count() = N*N = N^2
def approach1(n):
res = []
for i in range(n+1):
binary = bin(i)[2:]
res.append(binary.count('1'))
return res
def cntone(x):
cnt = 0
while x:
cnt+=1
x = x & (x-1)
return cnt
# TIME COMPLEXITY = O(NLOGN) => N FOR LOOP * LOGN FOR CNTONE = N*LOGN = NLOGN
def approach2(n):
res = []
for i in range(n+1):
res.append(cntone(i))
return res
# TIME COMPLEXITY = O(N) => FOR LOOP ONLY
def approach3(n):
res = []
res = [0]
for i in range(1,n+1):
res.append(res[i//2]+i%2)
return res
N = 5
print(approach1(N))
print(approach2(N))
print(approach3(N))