Created
January 2, 2023 12:22
-
-
Save exegeteio/99d7618b893a87810e0139c30cb38595 to your computer and use it in GitHub Desktop.
Cassidoo newsletter - 2023 - Week 0
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# frozen_string_literal: true | |
def max_subarray(haystack, target_length) | |
cons = haystack.each_cons(target_length).to_a | |
sums = cons.collect(&:sum) | |
cons[sums.index(sums.max)] | |
end | |
puts max_subarray([-4, 2, -5, 1, 2, 3, 6, -5, 1], 4).inspect | |
puts max_subarray([1, 2, 0, 5], 2).inspect | |
puts max_subarray([10, 20, 30, 50, -10, 90], 2).inspect |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment