Skip to content

Commit e3c7801

Browse files
authored
Swift Implementation for LCCI 10.10 (#2716)
1 parent bcc8b4b commit e3c7801

File tree

3 files changed

+137
-0
lines changed

3 files changed

+137
-0
lines changed

lcci/10.10.Rank from Stream/README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,52 @@ class StreamRank {
275275
*/
276276
```
277277

278+
```swift
279+
class BinaryIndexedTree {
280+
private var n: Int
281+
private var c: [Int]
282+
283+
init(_ n: Int) {
284+
self.n = n
285+
self.c = Array(repeating: 0, count: n + 1)
286+
}
287+
288+
func update(_ x: Int, _ delta: Int) {
289+
var idx = x
290+
while idx <= n {
291+
c[idx] += delta
292+
idx += (idx & -idx)
293+
}
294+
}
295+
296+
func query(_ x: Int) -> Int {
297+
var sum = 0
298+
var idx = x
299+
while idx > 0 {
300+
sum += c[idx]
301+
idx -= (idx & -idx)
302+
}
303+
return sum
304+
}
305+
}
306+
307+
class StreamRank {
308+
private var tree: BinaryIndexedTree
309+
310+
init() {
311+
tree = BinaryIndexedTree(50010)
312+
}
313+
314+
func track(_ x: Int) {
315+
tree.update(x + 1, 1)
316+
}
317+
318+
func getRankOfNumber(_ x: Int) -> Int {
319+
return tree.query(x + 1)
320+
}
321+
}
322+
```
323+
278324
<!-- tabs:end -->
279325

280326
<!-- end -->

lcci/10.10.Rank from Stream/README_EN.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,52 @@ class StreamRank {
277277
*/
278278
```
279279

280+
```swift
281+
class BinaryIndexedTree {
282+
private var n: Int
283+
private var c: [Int]
284+
285+
init(_ n: Int) {
286+
self.n = n
287+
self.c = Array(repeating: 0, count: n + 1)
288+
}
289+
290+
func update(_ x: Int, _ delta: Int) {
291+
var idx = x
292+
while idx <= n {
293+
c[idx] += delta
294+
idx += (idx & -idx)
295+
}
296+
}
297+
298+
func query(_ x: Int) -> Int {
299+
var sum = 0
300+
var idx = x
301+
while idx > 0 {
302+
sum += c[idx]
303+
idx -= (idx & -idx)
304+
}
305+
return sum
306+
}
307+
}
308+
309+
class StreamRank {
310+
private var tree: BinaryIndexedTree
311+
312+
init() {
313+
tree = BinaryIndexedTree(50010)
314+
}
315+
316+
func track(_ x: Int) {
317+
tree.update(x + 1, 1)
318+
}
319+
320+
func getRankOfNumber(_ x: Int) -> Int {
321+
return tree.query(x + 1)
322+
}
323+
}
324+
```
325+
280326
<!-- tabs:end -->
281327

282328
<!-- end -->
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
2+
class BinaryIndexedTree {
3+
private var n: Int
4+
private var c: [Int]
5+
6+
init(_ n: Int) {
7+
self.n = n
8+
self.c = Array(repeating: 0, count: n + 1)
9+
}
10+
11+
func update(_ x: Int, _ delta: Int) {
12+
var idx = x
13+
while idx <= n {
14+
c[idx] += delta
15+
idx += (idx & -idx)
16+
}
17+
}
18+
19+
func query(_ x: Int) -> Int {
20+
var sum = 0
21+
var idx = x
22+
while idx > 0 {
23+
sum += c[idx]
24+
idx -= (idx & -idx)
25+
}
26+
return sum
27+
}
28+
}
29+
30+
class StreamRank {
31+
private var tree: BinaryIndexedTree
32+
33+
init() {
34+
tree = BinaryIndexedTree(50010)
35+
}
36+
37+
func track(_ x: Int) {
38+
tree.update(x + 1, 1)
39+
}
40+
41+
func getRankOfNumber(_ x: Int) -> Int {
42+
return tree.query(x + 1)
43+
}
44+
}
45+

0 commit comments

Comments
 (0)