-
Notifications
You must be signed in to change notification settings - Fork 10
/
10-3-peaks.rb
118 lines (98 loc) · 2.71 KB
/
10-3-peaks.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
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# https://codility.com/demo/results/trainingY67YP5-4QB/
# require 'minitest/autorun' <--- for Minitest
# O(n*n). 100% correct, 20% performance.
def slow_solution(a)
return 0 if [1, 2].include?(a.size)
peaks = []
blocks = 0
length = a.size
(1..length - 2).each do |i|
if a[i] > a[i - 1] && a[i] > a[i + 1]
peaks << i
end
end
peaks.size.downto(1) do |b|
next unless length % b == 0
num_peaks = 0
bsize = length / b
b.times do |c|
if !peaks.any? {|p| p.between?(c * bsize, (c+1) * bsize - 1)}
# 0 ..1 * bsize - 1
# 1 * bsize..2 * bsize - 1
# 2 * bsize..3 * bsize - 1
break
else
num_peaks += 1
end
end
return b if num_peaks == b
end
blocks
end
# O(n*log(log n)) time. O(n) space.
def fast_solution(a)
peaks = []
blocks = 0
length = a.size
# find indices of all peaks
(1..length - 2).each do |i|
if a[i] > a[i - 1] && a[i] > a[i + 1]
peaks << i
end
end
# max possible # of blocks = # of peaks
peaks.size.downto(1) do |blocks|
next unless length % blocks == 0
block_size = length / blocks
block_no = 0
peaks.each do |peak|
if peak / block_size > block_no
# current block doesn't have peak. try smaller # of blocks.
break
end
if peak / block_size == block_no
# current block has peak. see if next block has peak.
block_no += 1
end
end
return blocks if block_no == blocks
end
end
# RSpec tests:
describe '#solution' do
it 'finds max # of blocks into which array may be divided' do
expect(solution([5, 6])).to eq(0)
expect(solution([4, 6, 2])).to eq(1)
expect(solution([1, 10, 9, 6, 20])).to eq(1)
expect(solution([1, 2, 1, 2, 1, 2])).to eq(2)
expect(solution([1, 2, 3, 4, 3, 4, 1, 2, 3, 4, 6, 2])).to eq(3)
expect(solution([1, 5, 3, 4, 3, 4, 1, 0, 1, 4, 6, 7])).to eq(1)
expect(solution([1, 5, 3, 4, 3, 4, 1, 2, 1, 4, 6, 7])).to eq(2)
end
end
# Minitest style:
# class TestPeaks < Minitest::Test
# def test_peaks
# assert_equal(0, solution([5, 6]))
# end
# end
# Your test case: [5, 6]
# Returned value: 0
# Your test case: [4, 6, 2]
# Returned value: 1
# peaks = [1]
# Your test case: [1, 10, 9, 6, 20]
# Returned value: 1
# peaks = [1]
# Your test case: [1, 2, 1, 2, 1, 2]
# Returned value: 2
# peaks = [1, 3]
# Example test: [1, 2, 3, 4, 3, 4, 1, 2, 3, 4, 6, 2]
# Returned value: 3
# Peaks = [3, 5, 10]
# Your test case: [1, 5, 3, 4, 3, 4, 1, 0, 1, 4, 6, 7]
# Returned value: 1
# peaks = [1, 3, 5]
# Your test case: [1, 5, 3, 4, 3, 4, 1, 2, 1, 4, 6, 7]
# Returned value: 2
# peaks = [1, 3, 5, 7]