Skip to content

Commit a13b1e8

Browse files
authored
feat: add swift implementation to lcci problem: No.08.13 (#2705)
1 parent 459429f commit a13b1e8

File tree

3 files changed

+87
-0
lines changed

3 files changed

+87
-0
lines changed

lcci/08.13.Pile Box/README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,36 @@ function pileBox(box: number[][]): number {
132132
}
133133
```
134134

135+
```swift
136+
class Solution {
137+
func pileBox(_ box: [[Int]]) -> Int {
138+
let boxes = box.sorted {
139+
if $0[0] == $1[0] {
140+
return $0[1] > $1[1]
141+
} else {
142+
return $0[0] < $1[0]
143+
}
144+
}
145+
146+
let n = boxes.count
147+
var f = Array(repeating: 0, count: n)
148+
var ans = 0
149+
150+
for i in 0..<n {
151+
f[i] = boxes[i][2]
152+
for j in 0..<i {
153+
if boxes[j][1] < boxes[i][1] && boxes[j][2] < boxes[i][2] {
154+
f[i] = max(f[i], f[j] + boxes[i][2])
155+
}
156+
}
157+
ans = max(ans, f[i])
158+
}
159+
160+
return ans
161+
}
162+
}
163+
```
164+
135165
<!-- tabs:end -->
136166

137167
<!-- end -->

lcci/08.13.Pile Box/README_EN.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,36 @@ function pileBox(box: number[][]): number {
138138
}
139139
```
140140

141+
```swift
142+
class Solution {
143+
func pileBox(_ box: [[Int]]) -> Int {
144+
let boxes = box.sorted {
145+
if $0[0] == $1[0] {
146+
return $0[1] > $1[1]
147+
} else {
148+
return $0[0] < $1[0]
149+
}
150+
}
151+
152+
let n = boxes.count
153+
var f = Array(repeating: 0, count: n)
154+
var ans = 0
155+
156+
for i in 0..<n {
157+
f[i] = boxes[i][2]
158+
for j in 0..<i {
159+
if boxes[j][1] < boxes[i][1] && boxes[j][2] < boxes[i][2] {
160+
f[i] = max(f[i], f[j] + boxes[i][2])
161+
}
162+
}
163+
ans = max(ans, f[i])
164+
}
165+
166+
return ans
167+
}
168+
}
169+
```
170+
141171
<!-- tabs:end -->
142172

143173
<!-- end -->

lcci/08.13.Pile Box/Solution.swift

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
func pileBox(_ box: [[Int]]) -> Int {
3+
let boxes = box.sorted {
4+
if $0[0] == $1[0] {
5+
return $0[1] > $1[1]
6+
} else {
7+
return $0[0] < $1[0]
8+
}
9+
}
10+
11+
let n = boxes.count
12+
var f = Array(repeating: 0, count: n)
13+
var ans = 0
14+
15+
for i in 0..<n {
16+
f[i] = boxes[i][2]
17+
for j in 0..<i {
18+
if boxes[j][1] < boxes[i][1] && boxes[j][2] < boxes[i][2] {
19+
f[i] = max(f[i], f[j] + boxes[i][2])
20+
}
21+
}
22+
ans = max(ans, f[i])
23+
}
24+
25+
return ans
26+
}
27+
}

0 commit comments

Comments
 (0)