Created
April 29, 2015 21:06
-
-
Save thephw/f439ee5127e4a2547a84 to your computer and use it in GitHub Desktop.
Minimum absolute slice sum
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def min_abs_slice_sum(a) | |
a.each_index.to_a.combination(2).map{|i,j| a[(i..j)].inject(&:+).abs}.min | |
end | |
def run_benchmark | |
require 'benchmark' | |
n = 3 | |
Benchmark.bm(17) do |x| | |
(0..n).each do |i| | |
len = 10**i | |
x.report("a.length = #{len}") { | |
a = len.times.map{(-10_000 + rand*20_000).floor} | |
min_abs_slice_sum(a) | |
} | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment