mirror of
https://github.com/trekhleb/javascript-algorithms.git
synced 2026-03-13 08:51:02 +08:00
Add a Divide and Conquer version of the MaxSubArray problem.
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
# Maximum subarray problem
|
||||
|
||||
The maximum subarray problem is the task of finding the contiguous
|
||||
subarray within a one-dimensional array, `a[1...n]`, of numbers
|
||||
The maximum subarray problem is the task of finding the contiguous
|
||||
subarray within a one-dimensional array, `a[1...n]`, of numbers
|
||||
which has the largest sum, where,
|
||||
|
||||

|
||||
@@ -10,11 +10,17 @@ which has the largest sum, where,
|
||||
|
||||
## Example
|
||||
|
||||
The list usually contains both positive and negative numbers along
|
||||
with `0`. For example, for the array of
|
||||
values `−2, 1, −3, 4, −1, 2, 1, −5, 4` the contiguous subarray
|
||||
The list usually contains both positive and negative numbers along
|
||||
with `0`. For example, for the array of
|
||||
values `−2, 1, −3, 4, −1, 2, 1, −5, 4` the contiguous subarray
|
||||
with the largest sum is `4, −1, 2, 1`, with sum `6`.
|
||||
|
||||
## Solutions
|
||||
|
||||
- Brute Force solution `O(n^2)`: [bfMaximumSubarray.js](./bfMaximumSubarray.js)
|
||||
- Divide and Conquer solution `O(n^2)`: [dcMaximumSubarraySum.js](./dcMaximumSubarraySum.js)
|
||||
- Dynamic Programming solution `O(n)`: [dpMaximumSubarray.js](./dpMaximumSubarray.js)
|
||||
|
||||
## References
|
||||
|
||||
- [Wikipedia](https://en.wikipedia.org/wiki/Maximum_subarray_problem)
|
||||
|
||||
Reference in New Issue
Block a user