-
Notifications
You must be signed in to change notification settings - Fork 0
/
StackOfBoxes.swift
66 lines (50 loc) · 1.74 KB
/
StackOfBoxes.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
59
60
61
62
63
64
65
66
//
// StackOfBoxes.swift
//
//
// Created by Stefan Jaindl on 26.08.20.
//
import Foundation
open class StackOfBoxes {
public init() { }
open func maxHeight(of stack: [Box]) -> Int {
if stack.isEmpty {
return 0
}
let sorted = stack.sorted(by: >)
var maxHeight = 0
for (index, box) in sorted.enumerated() {
if box.height > maxHeight {
maxHeight = box.height
}
for previous in 0 ..< index {
if isValid(first: box, second: sorted[previous]) && sorted[previous].maxHeight + box.height > box.maxHeight {
box.maxHeight = box.height + sorted[previous].maxHeight
maxHeight = max(maxHeight, box.maxHeight)
}
}
}
return maxHeight
}
private func isValid(first: Box, second: Box) -> Bool {
return first.width < second.width && first.height < second.height && first.depth < second.depth
}
}
open class Box: Equatable, Comparable {
public let width: Int
public let height: Int
public let depth: Int
public var maxHeight: Int
public init(width: Int, height: Int, depth: Int) {
self.width = width
self.height = height
self.depth = depth
maxHeight = height //max height together with other boxes is at least == height of the box itself
}
public static func < (lhs: Box, rhs: Box) -> Bool {
return lhs.height < rhs.height
}
public static func == (lhs: Box, rhs: Box) -> Bool {
return lhs.width == rhs.width && lhs.height == rhs.height && lhs.depth == rhs.depth
}
}