Last active
March 6, 2018 05:44
-
-
Save skryl/7b9e5bcdcb7109ca21ec95a049df592c to your computer and use it in GitHub Desktop.
Revisions
-
skryl revised this gist
Mar 6, 2018 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,6 @@ 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] } -
skryl created this gist
Mar 6, 2018 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,12 @@ def min_len(a) h = Hash.new { |h,k| h[k] = [] } array.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