• + 0 comments

    And you can easily make solution "sparse" to conserve memory on big arrays, just replace

    arr = Array.new(N + 1, 0)

    with

    arr = Hash.new(0)

    and

    arr.each do |value|

    with

    1.upto(N) do |n| value = arr[n]