diff --git a/lcci/17.08.Circus Tower/README.md b/lcci/17.08.Circus Tower/README.md index 7dc55088a28c3..24f5cecfecf83 100644 --- a/lcci/17.08.Circus Tower/README.md +++ b/lcci/17.08.Circus Tower/README.md @@ -239,6 +239,68 @@ func bestSeqAtIndex(height []int, weight []int) int { } ``` +```swift +class BinaryIndexedTree { + private var n: Int + private var c: [Int] + + init(_ n: Int) { + self.n = n + self.c = [Int](repeating: 0, count: n + 1) + } + + func update(_ x: Int, _ val: Int) { + var x = x + while x <= n { + c[x] = max(c[x], val) + x += x & -x + } + } + + func query(_ x: Int) -> Int { + var x = x + var s = 0 + while x > 0 { + s = max(s, c[x]) + x -= x & -x + } + return s + } +} + +class Solution { + func bestSeqAtIndex(_ height: [Int], _ weight: [Int]) -> Int { + let n = height.count + var arr: [(Int, Int)] = [] + for i in 0.. diff --git a/lcci/17.08.Circus Tower/README_EN.md b/lcci/17.08.Circus Tower/README_EN.md index 98c6e9671528e..dc6d34efff6c4 100644 --- a/lcci/17.08.Circus Tower/README_EN.md +++ b/lcci/17.08.Circus Tower/README_EN.md @@ -242,6 +242,68 @@ func bestSeqAtIndex(height []int, weight []int) int { } ``` +```swift +class BinaryIndexedTree { + private var n: Int + private var c: [Int] + + init(_ n: Int) { + self.n = n + self.c = [Int](repeating: 0, count: n + 1) + } + + func update(_ x: Int, _ val: Int) { + var x = x + while x <= n { + c[x] = max(c[x], val) + x += x & -x + } + } + + func query(_ x: Int) -> Int { + var x = x + var s = 0 + while x > 0 { + s = max(s, c[x]) + x -= x & -x + } + return s + } +} + +class Solution { + func bestSeqAtIndex(_ height: [Int], _ weight: [Int]) -> Int { + let n = height.count + var arr: [(Int, Int)] = [] + for i in 0.. diff --git a/lcci/17.08.Circus Tower/Solution.swift b/lcci/17.08.Circus Tower/Solution.swift new file mode 100644 index 0000000000000..5d880cde599b7 --- /dev/null +++ b/lcci/17.08.Circus Tower/Solution.swift @@ -0,0 +1,59 @@ +class BinaryIndexedTree { + private var n: Int + private var c: [Int] + + init(_ n: Int) { + self.n = n + self.c = [Int](repeating: 0, count: n + 1) + } + + func update(_ x: Int, _ val: Int) { + var x = x + while x <= n { + c[x] = max(c[x], val) + x += x & -x + } + } + + func query(_ x: Int) -> Int { + var x = x + var s = 0 + while x > 0 { + s = max(s, c[x]) + x -= x & -x + } + return s + } +} + +class Solution { + func bestSeqAtIndex(_ height: [Int], _ weight: [Int]) -> Int { + let n = height.count + var arr: [(Int, Int)] = [] + for i in 0..