Simple O(n) ruby solution


  • 0
    T
    def max_sub_array(nums)
        @test = false
        max_subs = []
        max = -1.0/0.0
        (0...nums.length).each do |i|
            max_subs[i] =  [(max_subs[i - 1] || 0) + nums[i], nums[i]].max
            max = [max_subs[i], max].max
        end
        return max
    end
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.