Skip to content

Commit 47a64fa

Browse files
⚡ Bolt: O(N^3) to O(N^2) nested loop optimization in SubjectTrendLineChart
Co-authored-by: alvin000009238 <107313913+alvin000009238@users.noreply.github.com>
1 parent fd03a1f commit 47a64fa

1 file changed

Lines changed: 6 additions & 2 deletions

File tree

android/app/src/main/java/com/clhs/score/ui/SubjectTrendLineChart.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,14 @@ fun SubjectTrendLineChart(
135135
val currentPoints = pointsByIndex[indices[i]]!!
136136
val nextPoints = pointsByIndex[indices[i+1]]!!
137137

138+
// Optimization: Precompute subjects to avoid O(N^3) complexity
139+
val currentSubjects = currentPoints.map { it.third }.toSet()
140+
val nextSubjects = nextPoints.map { it.third }.toSet()
141+
138142
currentPoints.forEach { p1 ->
139143
nextPoints.forEach { p2 ->
140-
val p1HasSuccessor = nextPoints.any { it.third == p1.third }
141-
val p2HasPredecessor = currentPoints.any { it.third == p2.third }
144+
val p1HasSuccessor = nextSubjects.contains(p1.third)
145+
val p2HasPredecessor = currentSubjects.contains(p2.third)
142146

143147
val isDifferentSubjectConnection = (!p1HasSuccessor || !p2HasPredecessor) && p1.third != p2.third
144148
val isSameSubjectGap = p1.third == p2.third && (p2.first - p1.first > 1)

0 commit comments

Comments
 (0)