Skip to content

Commit 0c1e43d

Browse files
authored
feat: add swift implementation to lcci problem: No.17.08 (doocs#2780)
1 parent 593670e commit 0c1e43d

File tree

3 files changed

+183
-0
lines changed

3 files changed

+183
-0
lines changed

lcci/17.08.Circus Tower/README.md

+62
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,68 @@ func bestSeqAtIndex(height []int, weight []int) int {
239239
}
240240
```
241241

242+
```swift
243+
class BinaryIndexedTree {
244+
private var n: Int
245+
private var c: [Int]
246+
247+
init(_ n: Int) {
248+
self.n = n
249+
self.c = [Int](repeating: 0, count: n + 1)
250+
}
251+
252+
func update(_ x: Int, _ val: Int) {
253+
var x = x
254+
while x <= n {
255+
c[x] = max(c[x], val)
256+
x += x & -x
257+
}
258+
}
259+
260+
func query(_ x: Int) -> Int {
261+
var x = x
262+
var s = 0
263+
while x > 0 {
264+
s = max(s, c[x])
265+
x -= x & -x
266+
}
267+
return s
268+
}
269+
}
270+
271+
class Solution {
272+
func bestSeqAtIndex(_ height: [Int], _ weight: [Int]) -> Int {
273+
let n = height.count
274+
var arr: [(Int, Int)] = []
275+
for i in 0..<n {
276+
arr.append((height[i], weight[i]))
277+
}
278+
arr.sort {
279+
if $0.0 == $1.0 {
280+
return $1.1 < $0.1
281+
}
282+
return $0.0 < $1.0
283+
}
284+
285+
let weights = Set(arr.map { $1 })
286+
let sortedWeights = Array(weights).sorted()
287+
let m = sortedWeights.enumerated().reduce(into: [Int: Int]()) {
288+
$0[$1.element] = $1.offset + 1
289+
}
290+
291+
let tree = BinaryIndexedTree(sortedWeights.count)
292+
var ans = 1
293+
for (_, w) in arr {
294+
let x = m[w]!
295+
let t = tree.query(x - 1) + 1
296+
ans = max(ans, t)
297+
tree.update(x, t)
298+
}
299+
return ans
300+
}
301+
}
302+
```
303+
242304
<!-- tabs:end -->
243305

244306
<!-- end -->

lcci/17.08.Circus Tower/README_EN.md

+62
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,68 @@ func bestSeqAtIndex(height []int, weight []int) int {
242242
}
243243
```
244244

245+
```swift
246+
class BinaryIndexedTree {
247+
private var n: Int
248+
private var c: [Int]
249+
250+
init(_ n: Int) {
251+
self.n = n
252+
self.c = [Int](repeating: 0, count: n + 1)
253+
}
254+
255+
func update(_ x: Int, _ val: Int) {
256+
var x = x
257+
while x <= n {
258+
c[x] = max(c[x], val)
259+
x += x & -x
260+
}
261+
}
262+
263+
func query(_ x: Int) -> Int {
264+
var x = x
265+
var s = 0
266+
while x > 0 {
267+
s = max(s, c[x])
268+
x -= x & -x
269+
}
270+
return s
271+
}
272+
}
273+
274+
class Solution {
275+
func bestSeqAtIndex(_ height: [Int], _ weight: [Int]) -> Int {
276+
let n = height.count
277+
var arr: [(Int, Int)] = []
278+
for i in 0..<n {
279+
arr.append((height[i], weight[i]))
280+
}
281+
arr.sort {
282+
if $0.0 == $1.0 {
283+
return $1.1 < $0.1
284+
}
285+
return $0.0 < $1.0
286+
}
287+
288+
let weights = Set(arr.map { $1 })
289+
let sortedWeights = Array(weights).sorted()
290+
let m = sortedWeights.enumerated().reduce(into: [Int: Int]()) {
291+
$0[$1.element] = $1.offset + 1
292+
}
293+
294+
let tree = BinaryIndexedTree(sortedWeights.count)
295+
var ans = 1
296+
for (_, w) in arr {
297+
let x = m[w]!
298+
let t = tree.query(x - 1) + 1
299+
ans = max(ans, t)
300+
tree.update(x, t)
301+
}
302+
return ans
303+
}
304+
}
305+
```
306+
245307
<!-- tabs:end -->
246308

247309
<!-- end -->
+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
class BinaryIndexedTree {
2+
private var n: Int
3+
private var c: [Int]
4+
5+
init(_ n: Int) {
6+
self.n = n
7+
self.c = [Int](repeating: 0, count: n + 1)
8+
}
9+
10+
func update(_ x: Int, _ val: Int) {
11+
var x = x
12+
while x <= n {
13+
c[x] = max(c[x], val)
14+
x += x & -x
15+
}
16+
}
17+
18+
func query(_ x: Int) -> Int {
19+
var x = x
20+
var s = 0
21+
while x > 0 {
22+
s = max(s, c[x])
23+
x -= x & -x
24+
}
25+
return s
26+
}
27+
}
28+
29+
class Solution {
30+
func bestSeqAtIndex(_ height: [Int], _ weight: [Int]) -> Int {
31+
let n = height.count
32+
var arr: [(Int, Int)] = []
33+
for i in 0..<n {
34+
arr.append((height[i], weight[i]))
35+
}
36+
arr.sort {
37+
if $0.0 == $1.0 {
38+
return $1.1 < $0.1
39+
}
40+
return $0.0 < $1.0
41+
}
42+
43+
let weights = Set(arr.map { $1 })
44+
let sortedWeights = Array(weights).sorted()
45+
let m = sortedWeights.enumerated().reduce(into: [Int: Int]()) {
46+
$0[$1.element] = $1.offset + 1
47+
}
48+
49+
let tree = BinaryIndexedTree(sortedWeights.count)
50+
var ans = 1
51+
for (_, w) in arr {
52+
let x = m[w]!
53+
let t = tree.query(x - 1) + 1
54+
ans = max(ans, t)
55+
tree.update(x, t)
56+
}
57+
return ans
58+
}
59+
}

0 commit comments

Comments
 (0)