September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
|
|
@ -0,0 +1,11 @@
|
|||
def subarray_sum:
|
||||
. as $arr
|
||||
| reduce range(0; length) as $i
|
||||
( {"first": length, "last": 0, "curr": 0, "curr_first": 0, "max": 0};
|
||||
$arr[$i] as $e
|
||||
| (.curr + $e) as $curr
|
||||
| . + (if $e > $curr then {"curr": $e, "curr_first": $i} else {"curr": $curr} end)
|
||||
| if .curr > .max then . + {"max": $curr, "first": .curr_first, "last": $i}
|
||||
else .
|
||||
end)
|
||||
| [ .max, $arr[ .first : (1 + .last)] ];
|
||||
|
|
@ -0,0 +1 @@
|
|||
[1, 2, 3, 4, 5, -8, -9, -20, 40, 25, -5] | subarray_sum
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
$ jq -c -n -f Greatest_subsequential_sum.jq
|
||||
[65,[40,25]]
|
||||
Loading…
Add table
Add a link
Reference in a new issue