-
Notifications
You must be signed in to change notification settings - Fork 29
/
Contents.swift
47 lines (40 loc) · 1.31 KB
/
Contents.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
import UIKit
/*
This problem was asked by Google.
You are given an array of arrays of integers, where each array corresponds to a row in a triangle of numbers. For example, [[1], [2, 3], [1, 5, 1]] represents the triangle:
1
2 3
1 5 1
We define a path in the triangle to start at the top and go down one row at a time to an adjacent value, eventually ending with an entry on the bottom row. For example, 1 -> 3 -> 5. The weight of the path is the sum of the entries.
Write a program that returns the weight of the maximum weight path.
*/
func maxWeightPath(row: Int = 0, idx: Int = 0, triangle: [[Int]], cache: inout [[Int]]) -> Int {
if row > triangle.count - 1 { return 0 }
let value = triangle[row][idx]
let result: Int
if cache[row][idx] != 0 {
result = cache[row][idx]
}
else {
result = value + max(
maxWeightPath(row: row + 1, idx: idx, triangle: triangle, cache: &cache),
maxWeightPath(row: row + 1, idx: idx + 1, triangle: triangle, cache: &cache)
)
}
cache[row][idx] = result
return result
}
let triangle =
[
[1],
[2,3],
[1,5,1],
[2,3,1,9],
[3,6,7,-5,4]
]
var cache = triangle.map { (row) in
return row.map { _ in
return 0
}
}
maxWeightPath(triangle: triangle, cache: &cache)