問題 TapeEquilibrium
- 使用言語:Swift3
ソースコード
public func solution3_3(_ A : inout [Int]) -> Int {
if A.count == 2 {
return abs(A[0] - A[1])
}
var sum = 0
for i in 0...A.count - 1 {
sum = sum + A[i]
}
var pMns = A[0]
var pPls = sum - pMns
var minDif = abs(pPls - pMns)
for i in 1...A.count - 2 {
pMns = pMns + A[i]
pPls = sum - pMns
let dif = abs(pPls - pMns)
if dif - minDif < 0 {
minDif = dif
}
}
return minDif
}