-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.swift
58 lines (55 loc) · 1.75 KB
/
main.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
//
// main.swift
// SwiftAlgorithm
//
// Created by Lee Myeonghwan on 2023/01/31.
//
import Foundation
func solution(_ arr: [String]) -> Int {
let n = arr.count / 2 + 1
var dpMax = [[Int]](repeating: [Int](repeating: -Int.max, count: n), count: n)
var dpMin = [[Int]](repeating: [Int](repeating: Int.max, count: n), count: n)
for i in 0..<n {
dpMax[i][i] = Int(arr[i*2])!
dpMin[i][i] = Int(arr[i*2])!
}
for l in 2...n {
for i in 0...n-l {
let j = i + l - 1
for k in i..<j {
if arr[k*2+1] == "+" {
dpMax[i][j] = max(dpMax[i][j], dpMax[i][k] + dpMax[k+1][j])
dpMin[i][j] = min(dpMin[i][j], dpMin[i][k] + dpMin[k+1][j])
} else {
dpMax[i][j] = max(dpMax[i][j], dpMax[i][k] - dpMin[k+1][j])
dpMin[i][j] = min(dpMin[i][j], dpMin[i][k] - dpMax[k+1][j])
}
}
}
}
return dpMax[0][n-1]
}
/* Solution referenced by other's code
func solution(_ input_array:[String]) -> Int {
let arr = input_array
var minValue = 0
var maxValue = 0
var sumValue = 0
for idx in stride(from: arr.count - 1, to: -1, by: -1) {
if arr[idx] == "+" {
continue
} else if arr[idx] == "-" {
let tempMinValue = minValue
let tempMaxValue = maxValue
minValue = min(-(sumValue + tempMaxValue), -sumValue + tempMinValue)
let minusValue = Int(arr[idx + 1])!
maxValue = max(-(sumValue + tempMinValue), -minusValue + (sumValue-minusValue) + tempMaxValue)
sumValue = 0
} else {
sumValue += Int(arr[idx])!
}
}
maxValue += sumValue
return maxValue
}
*/