Skip to content

Instantly share code, notes, and snippets.

@thephw
Created April 29, 2015 21:06
Show Gist options
  • Save thephw/f439ee5127e4a2547a84 to your computer and use it in GitHub Desktop.
Save thephw/f439ee5127e4a2547a84 to your computer and use it in GitHub Desktop.
Minimum absolute slice sum
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