Skip to content

Commit b40888d

Browse files
Add min max divide and conquer in Ruby
1 parent 0b6e77b commit b40888d

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1310,8 +1310,8 @@ In order to achieve greater coverage and encourage more people to contribute to
13101310
</a>
13111311
</td>
13121312
<td> <!-- Ruby -->
1313-
<a href="./CONTRIBUTING.md">
1314-
<img align="center" height="25" src="./logos/github.svg" />
1313+
<a href="./src/ruby/min_max_dc.rb">
1314+
<img align="center" height="25" src="./logos/ruby.svg" />
13151315
</a>
13161316
</td>
13171317
<td> <!-- JavaScript -->

src/ruby/min_max_dc.rb

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
require 'test/unit/assertions'
2+
include Test::Unit::Assertions
3+
4+
def min_max_dc(vector, start_index, end_index)
5+
if start_index == end_index
6+
return [vector[start_index], vector[start_index]]
7+
end
8+
9+
middle = (start_index + end_index) / 2
10+
left_min, left_max = min_max_dc(vector, start_index, middle)
11+
right_min, right_max = min_max_dc(vector, middle + 1, end_index)
12+
13+
min = left_min < right_min ? left_min : right_min
14+
max = left_max > right_max ? left_max : right_max
15+
16+
[min, max]
17+
end
18+
19+
assert_equal([1, 5], min_max_dc([1, 2, 3, 4, 5], 0, 4))
20+
assert_equal([14, 55], min_max_dc([13, 55, 42, 14, 15], 1, 3))
21+
assert_equal([13, 13], min_max_dc([13, 55, 42, 14, 15], 0, 0))
22+
assert_equal([14, 15], min_max_dc([13, 55, 42, 14, 15], 3, 4))

0 commit comments

Comments
 (0)