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.

Revisions

  1. skryl revised this gist Mar 6, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion min_len.rb
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    def min_len(a)
    h = Hash.new { |h,k| h[k] = [] }
    array.each.with_object(h)
    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] }
  2. skryl created this gist Mar 6, 2018.
    12 changes: 12 additions & 0 deletions min_len.rb
    Original 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