Skip to content

Instantly share code, notes, and snippets.

@skryl
Last active March 6, 2018 05:44
Show Gist options
  • Save skryl/7b9e5bcdcb7109ca21ec95a049df592c to your computer and use it in GitHub Desktop.
Save skryl/7b9e5bcdcb7109ca21ec95a049df592c to your computer and use it in GitHub Desktop.
def min_len(a)
h = Hash.new { |h,k| h[k] = [] }
a.each.with_object(h)
.with_index { |(i, h), idx| h[i] << idx }
.values.map { |a| [a.size, a.last - a.first] }
.sort { |i1, i2| [i1.first, i1.last] <=> [i2.first, i2.last] }
.last.last
end
def min_len(a)
a.each.with_object({}).with_index{|(i,h),x|h[i]=(h[i]||[])<<x}.values.map{|a|[a.size,a[-1]-a[0]]}.sort{|a,b|[a[0],a[-1]]<=>[b[0],b[-1]]}[-1][-1]+1
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment