-
Notifications
You must be signed in to change notification settings - Fork 256
/
Copy pathcounting_bits.rb
45 lines (41 loc) · 1.07 KB
/
counting_bits.rb
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
=begin
Given an integer n, return an array ans of length n + 1 such that for each i (0 <= i <= n),
ans[i] is the number of 1's in the binary representation of i.
=end
#Solution 1 (O(n log n))
def count_bits(n)
ans = []
for i in 0..n
num_of_set_bits = 0
while i>0
pow = Math.log(i,2).to_int
i-=(2**pow)
num_of_set_bits+=1
end
ans.push(num_of_set_bits)
end
ans
end
# Solution 2 (O(n))
# Approach: Left shift or right shift doesn't change num of bits in even numbers it only halves the number
def count_bits(n)
bits_count = [0]*(n+1)
return bits_count unless n>0
bits_count[1]=1
for i in 2..n
bits_count[i] = bits_count[i/2]
bits_count[i]+=1 unless i%2==0
end
bits_count
end
#Solution 3 (O(n))
def count_bits(n)
bits_count = [0]*(n+1)
return bits_count unless n>0
bits_count[1]=1
for i in 2..n
# replaced division with right shift and condition with bitwise and op with 1
bits_count[i] = bits_count[i>>1]+(i&1)
end
bits_count
end