This commit is contained in:
commit
568474d52f
1591 files changed
+528213
No files matched your search
@@ -0,0 +1,16 @@
|
||||
// Kadane: find the best, with an early return for the empty case.
|
||||
// EXPECT: 6
|
||||
// EXPECT: -1
|
||||
FUNCTION MAX_SUBARRAY(Xs : LIST OF INTEGER) RETURNS INTEGER
|
||||
REQUIRE LENGTH(Xs) >= 1
|
||||
DECLARE Best, Here : INTEGER
|
||||
Best <- Xs[0]
|
||||
Here <- Xs[0]
|
||||
FOR I <- 1 TO LENGTH(Xs)
|
||||
Here <- MAX(Xs[I], Here + Xs[I])
|
||||
Best <- MAX(Best, Here)
|
||||
NEXT I
|
||||
RETURN Best
|
||||
ENDFUNCTION
|
||||
OUTPUT MAX_SUBARRAY([-2, 1, -3, 4, -1, 2, 1, -5, 4])
|
||||
OUTPUT MAX_SUBARRAY([-1])
|
||||
Reference in new issue
Block a user