@@ -9,15 +9,15 @@ class Solution {
9
9
val m = 2 * n
10
10
val events = createEvents(squares, m)
11
11
val xsRaw = createXsRaw(squares, m)
12
- events.sortWith<Event ? > { a, b: Event ? -> a!! .y.compareTo(b!! .y) }
12
+ events.sortWith<Event > { a, b: Event -> a.y.compareTo(b.y) }
13
13
val xs = compress(xsRaw)
14
14
val totalUnionArea = calculateTotalUnionArea(events, xs, m)
15
15
val target = totalUnionArea / 2.0
16
16
return findSplitPoint(events, xs, m, target)
17
17
}
18
18
19
- private fun createEvents (squares : Array <IntArray >, m : Int ): Array <Event ? > {
20
- val events = arrayOfNulls< Event > (m)
19
+ private fun createEvents (squares : Array <IntArray >, m : Int ): Array <Event > {
20
+ val events = Array (m) { Event ( 0.0 , 0.0 , 0.0 , 0 ) }
21
21
var idx = 0
22
22
for (sq in squares) {
23
23
val x = sq[0 ].toDouble()
@@ -43,34 +43,34 @@ class Solution {
43
43
return xsRaw
44
44
}
45
45
46
- private fun calculateTotalUnionArea (events : Array <Event ? >, xs : DoubleArray , m : Int ): Double {
46
+ private fun calculateTotalUnionArea (events : Array <Event >, xs : DoubleArray , m : Int ): Double {
47
47
val segTree = SegmentTree (xs)
48
48
var totalUnionArea = 0.0
49
- var lastY = events[0 ]!! .y
49
+ var lastY = events[0 ].y
50
50
var i = 0
51
51
while (i < m) {
52
- val curY = events[i]!! .y
52
+ val curY = events[i].y
53
53
if (curY > lastY) {
54
54
val unionX = segTree.query()
55
55
totalUnionArea + = unionX * (curY - lastY)
56
56
lastY = curY
57
57
}
58
- while (i < m && events[i]!! .y == curY) {
59
- val indices = findIndices(xs, events[i]!! )
60
- segTree.update(1 , 0 , xs.size - 1 , indices[0 ], indices[1 ], events[i]!! .type)
58
+ while (i < m && events[i].y == curY) {
59
+ val indices = findIndices(xs, events[i])
60
+ segTree.update(1 , 0 , xs.size - 1 , indices[0 ], indices[1 ], events[i].type)
61
61
i++
62
62
}
63
63
}
64
64
return totalUnionArea
65
65
}
66
66
67
- private fun findSplitPoint (events : Array <Event ? >, xs : DoubleArray , m : Int , target : Double ): Double {
67
+ private fun findSplitPoint (events : Array <Event >, xs : DoubleArray , m : Int , target : Double ): Double {
68
68
val segTree = SegmentTree (xs)
69
- var lastY = events[0 ]!! .y
69
+ var lastY = events[0 ].y
70
70
var cumArea = 0.0
71
71
var i = 0
72
72
while (i < m) {
73
- val curY = events[i]!! .y
73
+ val curY = events[i].y
74
74
if (curY > lastY) {
75
75
val unionX = segTree.query()
76
76
val dy = curY - lastY
@@ -80,9 +80,9 @@ class Solution {
80
80
cumArea + = unionX * dy
81
81
lastY = curY
82
82
}
83
- while (i < m && events[i]!! .y == curY) {
84
- val indices = findIndices(xs, events[i]!! )
85
- segTree.update(1 , 0 , xs.size - 1 , indices[0 ], indices[1 ], events[i]!! .type)
83
+ while (i < m && events[i].y == curY) {
84
+ val indices = findIndices(xs, events[i])
85
+ segTree.update(1 , 0 , xs.size - 1 , indices[0 ], indices[1 ], events[i].type)
86
86
i++
87
87
}
88
88
}
0 commit comments