From 47a64facfd46f37cc2763966aba0c8718799eb81 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Thu, 18 Jun 2026 16:25:30 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20Bolt:=20O(N^3)=20to=20O(N^2)=20nest?= =?UTF-8?q?ed=20loop=20optimization=20in=20SubjectTrendLineChart?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: alvin000009238 <107313913+alvin000009238@users.noreply.github.com> --- .../main/java/com/clhs/score/ui/SubjectTrendLineChart.kt | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/android/app/src/main/java/com/clhs/score/ui/SubjectTrendLineChart.kt b/android/app/src/main/java/com/clhs/score/ui/SubjectTrendLineChart.kt index ccbe1b8..ae18e4d 100644 --- a/android/app/src/main/java/com/clhs/score/ui/SubjectTrendLineChart.kt +++ b/android/app/src/main/java/com/clhs/score/ui/SubjectTrendLineChart.kt @@ -135,10 +135,14 @@ fun SubjectTrendLineChart( val currentPoints = pointsByIndex[indices[i]]!! val nextPoints = pointsByIndex[indices[i+1]]!! + // Optimization: Precompute subjects to avoid O(N^3) complexity + val currentSubjects = currentPoints.map { it.third }.toSet() + val nextSubjects = nextPoints.map { it.third }.toSet() + currentPoints.forEach { p1 -> nextPoints.forEach { p2 -> - val p1HasSuccessor = nextPoints.any { it.third == p1.third } - val p2HasPredecessor = currentPoints.any { it.third == p2.third } + val p1HasSuccessor = nextSubjects.contains(p1.third) + val p2HasPredecessor = currentSubjects.contains(p2.third) val isDifferentSubjectConnection = (!p1HasSuccessor || !p2HasPredecessor) && p1.third != p2.third val isSameSubjectGap = p1.third == p2.third && (p2.first - p1.first > 1)