Skip to content

feat: add swift implementation to lcci problem: No.17.08 #2780

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions lcci/17.08.Circus Tower/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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..<n {
arr.append((height[i], weight[i]))
}
arr.sort {
if $0.0 == $1.0 {
return $1.1 < $0.1
}
return $0.0 < $1.0
}

let weights = Set(arr.map { $1 })
let sortedWeights = Array(weights).sorted()
let m = sortedWeights.enumerated().reduce(into: [Int: Int]()) {
$0[$1.element] = $1.offset + 1
}

let tree = BinaryIndexedTree(sortedWeights.count)
var ans = 1
for (_, w) in arr {
let x = m[w]!
let t = tree.query(x - 1) + 1
ans = max(ans, t)
tree.update(x, t)
}
return ans
}
}
```

<!-- tabs:end -->

<!-- end -->
62 changes: 62 additions & 0 deletions lcci/17.08.Circus Tower/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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..<n {
arr.append((height[i], weight[i]))
}
arr.sort {
if $0.0 == $1.0 {
return $1.1 < $0.1
}
return $0.0 < $1.0
}

let weights = Set(arr.map { $1 })
let sortedWeights = Array(weights).sorted()
let m = sortedWeights.enumerated().reduce(into: [Int: Int]()) {
$0[$1.element] = $1.offset + 1
}

let tree = BinaryIndexedTree(sortedWeights.count)
var ans = 1
for (_, w) in arr {
let x = m[w]!
let t = tree.query(x - 1) + 1
ans = max(ans, t)
tree.update(x, t)
}
return ans
}
}
```

<!-- tabs:end -->

<!-- end -->
59 changes: 59 additions & 0 deletions lcci/17.08.Circus Tower/Solution.swift
Original file line number Diff line number Diff line change
@@ -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..<n {
arr.append((height[i], weight[i]))
}
arr.sort {
if $0.0 == $1.0 {
return $1.1 < $0.1
}
return $0.0 < $1.0
}

let weights = Set(arr.map { $1 })
let sortedWeights = Array(weights).sorted()
let m = sortedWeights.enumerated().reduce(into: [Int: Int]()) {
$0[$1.element] = $1.offset + 1
}

let tree = BinaryIndexedTree(sortedWeights.count)
var ans = 1
for (_, w) in arr {
let x = m[w]!
let t = tree.query(x - 1) + 1
ans = max(ans, t)
tree.update(x, t)
}
return ans
}
}