> -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= > > by Harlan > > Given an array of integers, find the sub-array with maximum sum. For example: > > array: [-1, 2, 5, -1, 3, -2, 1] > maximum sub-array: [2, 5, -1, 3] > > Extra Credit: > > Given a matrix of integers, find the rectangle with maximum sum. > > # # Here is my solution. # If there are multiple sub arrays that equal max sum, it prints all of them. require 'enumerator' arr, s = [1,5,3,-9,9], [] (1..arr.length).each{|q| arr.each_cons(q) {|x| s << x}} big = s.max {|x,y| x.inject(0) {|a,b| a+b} <=> y.inject(0) {|c,d| c+d}} p s.select {|r| r.inject(0) {|a,b| a+b} == big.inject(0) {|c,d| c+d}} # Harry -- A Look into Japanese Ruby List in English http://www.kakueki.com/