-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathC-Natrium2014.rb
111 lines (97 loc) · 2.67 KB
/
C-Natrium2014.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
# slow
# def solution(a)
# beginnings = []
# max = 0
# min = nil
# a.each_with_index { |v, i|
# if not min or v < min
# min = v
# beginnings << [v, i]
# else
# beginnings.each { |b|
# break if i - b[1] <= max
# if v >= b[0]
# max = i - b[1]
# break
# end
# }
# end
# }
# max
# end
# slow :)
# def solution(a)
# distance = 0
# a.each_with_index { |x, p|
# a.each_with_index { |y, q|
# if p <= q && x <= y
# distance = [distance, q - p].max
# end
# }
# }
# distance
# end
def solution(a)
candidates = []
a.each_with_index { |v, i|
if candidates.none? or v < candidates.last[0]
candidates << [v, i]
end
}
max = 0
(a.count - 1).downto(0) { |i|
while candidates.any? and a[i] >= candidates.last[0]
max = [max, i - candidates.last[1]].max
candidates.pop # O(N) because of this
end
}
max
end
require "minitest/autorun"
require "byebug"
require "timeout"
# byebug
# solution(nil)
# exit
class TestSolution < Minitest::Test
def test_default
assert_equal 3, solution([5, 3, 6, 3, 4, 2] )
end
def test_simple
assert_equal 1, solution([1, 2])
assert_equal 2, solution([1, 2, 3])
assert_equal 3, solution([1, 2, 3, 4])
assert_equal 2, solution([1, 1, 2])
assert_equal 3, solution([1, 2, 2, 3])
assert_equal 4, solution([1, 2, 3, 3, 4])
assert_equal 2, solution([1, 2, 1])
assert_equal 3, solution([1, 2, 3, 2])
assert_equal 5, solution([1, 2, 3, 4, 3, 5])
assert_equal 4, solution([1, 2, 3, 0, 4])
assert_equal 3, solution([1, 2, 3, 4, 0])
assert_equal 4, solution([7, 6, 1, 2, 3, 0, 4])
assert_equal 6, solution([1, 2, 3, 0, -5, -1, -1, -2, -3, -4, -5])
assert_equal 5, solution([1, 2, 3, 0, -5, -4, -3, -2, -2, -1])
end
def test_negative
assert_equal 2, solution([-3, -2, -1])
assert_equal 0, solution([-1, -2, -3])
end
def test_small
assert_equal 0, solution([1])
end
def test_large
Timeout::timeout(6) {
assert_equal 299_999, solution((1..300_000).to_a)
}
Timeout::timeout(6) {
assert_equal 0, solution((1..300_000).to_a.reverse)
}
Timeout::timeout(6) {
assert_equal 299_999, solution([1] * 300_000)
}
Timeout::timeout(6) {
solution((1..300_000).to_a.shuffle)
}
end
end