Last active
March 6, 2018 05:44
-
-
Save skryl/7b9e5bcdcb7109ca21ec95a049df592c to your computer and use it in GitHub Desktop.
This file contains hidden or 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_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