UP | HOME

find max subsequence

The following code shows how to find max subsequence

#lang racket/base

(define (max-subsquence lst)
  (define maxsofar 0)
  (define maxendinghere 0)
  (define maxelement -inf.0)
  (for ([e lst])
    (set! maxelement (max maxelement e))
    (set! maxendinghere (max (+ maxendinghere e) 0))
    (set! maxsofar (max maxsofar maxendinghere)))
  (if (= maxsofar 0)
      maxelement
      maxsofar))
Date: 2021-07-03 Sat 00:00
Author: Lîm Tsú-thuàn