Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions archive/r/ruby/maximum-subarray.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# frozen_string_literal: true

USAGE = 'Usage: Please provide a list of integers in the format: "1, 2, 3, 4, 5"'

def usage!
warn USAGE
exit 1
end

def parse_list(str)
str.split(",").map { Integer(it.strip) }
rescue ArgumentError, NoMethodError
usage!
end

def max_subarray_sum(arr)
return arr.first if arr.length == 1
return arr.sum if arr.none?(&:negative?)

current = max = arr.first

arr[1..].each do |x|
current = [x, current + x].max
max = [max, current].max
end

max
end

raw = ARGV.first
usage! if raw.nil? || raw.strip.empty?

arr = parse_list(raw)
usage! if arr.length < 1

puts max_subarray_sum(arr)
Loading