/** * Beispiel aus * * - Algorithmen und Datenstrukturen für Dummies * - von Andreas Gogol-Döring und Thomas Letschert * - Verlag Wiley-VCH; Oktober 2019 * - Kapitel 13, Dynamisches Programmieren * * @author A. Gogol-Döring, Th. Letschert */ import Foundation struct AuD_13_07_MaxSubSumKadane { static func kadane(_ a: [Int]) -> Int { var S = a[0] var S_end = a[0] let n = a.count-1 for i in 1 ... n { S_end = max(a[i], S_end + a[i]) S = max(S_end, S) } return S } static let a = [1, -4, 3, 18, 1, -8, 2, -1, 10, -5, -80, 20, 3, -2] static func run () { print(kadane(a)) // 25 } }