-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path9-CountNonDivisible.rb
60 lines (54 loc) · 1.08 KB
/
9-CountNonDivisible.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# O(N2)
# def solution(a)
# result = []
# a.each { |x|
# count = 0
# a.each { |y|
# count += 1 if x % y == 0
# }
# result << a.count - count
# }
# result
# end
# O(N2)
# def solution(a)
# numbers = [0] * (2 * a.count + 1)
# a.each { |x|
# k = x
# while k <= 2 * a.count
# numbers[k] += 1
# k += x
# end
# }
# result = []
# a.each { |x|
# result << a.count - numbers[x]
# }
# result
# end
# finally :)
def solution(a)
counts = [0] * (2 * a.count + 1)
a.each { |x|
counts[x] += 1
}
divisors = [0] * (2 * a.count + 1)
counts.each_with_index { |count, x|
if count > 0
k = x
while k <= 2 * a.count
divisors[k] += count
k += x
end
end
}
result = []
a.each { |x|
result << a.count - divisors[x]
}
result
end
#input = [3, 1, 2, 3, 6]
input = [1] * 50_000
#input = (1..50000).to_a
puts solution(input).inspect