Git Product home page Git Product logo

max_sequence_sum's Introduction

max_sequence_sum

max_sequence_sum is a library extension to Array that finds the sequence with the largest sum inside an array (subarray). It has methods to return the sum, the subarray and its initial and final positions.

Instructions

Run bundle install first.

Installation

require_relative "lib/max_sequence_sum"

Usage

  • Array#max_slice_sum
# returns sum of subarray with largest sum
[2, -4, 6, 8, -10, 100, -6, 5].max_slice_sum
 => 104
  • Array#max_sum_slice
# returns subarray with largest sum
[2, -4, 6, 8, -10, 100, -6, 5].max_sum_slice
 => [6, 8, -10, 100]
  • Array#max_sum_slice_positions
# returns positions of subarray with largest sum
[2, -4, 6, 8, -10, 100, -6, 5].max_sum_slice_positions
 => [2,5]

History

  1. A different approach for each method: 9f173e2…
  2. A common approach for the methods: HEAD
  3. Optimized version: optimizations branch

Rationale

It's an exhaustive solution. It runs on O(n^2), it's really not efficient for large arrays.

Take [ 10, -99, 5, 8 ] as an example array.

  1. All subarrays are generated
[ [10], [-99], [5], [8], [10, -99], [-99, 5], [5, 8], [10, -99, 5], [-99, 5, 8], [10, -99, 5, 8] ]
  1. Then we compute the sum for the subarrays
[ [10], [-99], [5], [8], [10, -99], [-99, 5], [5, 8], [10, -99, 5], [-99, 5, 8], [10, -99, 5, 8] ]
    |     |     |    |       |          |        |          |            |              |
    10   -99    5    8      -89        -94       13        -84          -86            -76
  1. Next we select the one with the largest sum
[  [5, 8]  ]
       |
       13
  1. Finally we return it, along with its positions and sum
subarray  = [5, 8]
positions = [2,4]
sum       = 13

max_sequence_sum's People

Contributors

ijverig avatar

Watchers

 avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.