From f0394c0ebe8433b59fc59648dc9b0b09c2d2a36a Mon Sep 17 00:00:00 2001 From: ye144440154 <80632267+ye144440154@users.noreply.github.com> Date: Wed, 25 Nov 2026 21:04:30 +0800 Subject: [PATCH] Add consolidation cases to merge-intervals Five wrong solutions passed all five Maintenance Window Consolidation cases. One merges a window with only the next one and then skips past both: on [[1,3],[2,5],[4,8]] it returns [[1,5],[4,8]] instead of [[1,8]]. Widening that to the next two windows still passes every chain of three, and returns [[1,8],[7,10]] for [[1,3],[2,5],[4,8],[7,10]]. One compares each window with the previous input window instead of the merged one, and returns [[1,10],[4,5]] for [[1,10],[2,3],[4,5]]. Every merge in the cases lands in the first output window, so comparing with the first merged window instead of the last passed, and returns [[1,2],[4,6],[5,7]] for [[1,2],[4,6],[5,7]]. The only unsorted case has its overlapping windows next to each other, so merging in input order and sorting only the result passed, and returns [[1,3],[2,4],[5,6]] for [[1,3],[5,6],[2,4]]. Those five inputs become new cases. A test in tests/browser/compiler-explorer.test.js runs each solution against the case that rejects it, looked up by label, and fails against the cases on main; the labels join the tricky-case list in tests/web/contract.rs so the cases stay, and the task preview golden takes the judge's new hash. Close #844 Change-Id: I761b4027f69f1bc7f99e698fcf8d693999424308 --- problem-bank/judges.json | 137 ++++++++++++++++++ tests/browser/compiler-explorer.test.js | 127 ++++++++++++++++ tests/golden/task-posed.json | 2 +- tests/web/contract.rs | 5 + .../maintenance-window-consolidation.json | 137 ++++++++++++++++++ 5 files changed, 407 insertions(+), 1 deletion(-) diff --git a/problem-bank/judges.json b/problem-bank/judges.json index 2af030fff..3ef90cd46 100644 --- a/problem-bank/judges.json +++ b/problem-bank/judges.json @@ -9146,6 +9146,143 @@ 10 ] ] + }, + { + "label": "chained overlaps: [[1,3],[2,5],[4,8]]", + "input": [ + [ + [ + 1, + 3 + ], + [ + 2, + 5 + ], + [ + 4, + 8 + ] + ] + ], + "expected": [ + [ + 1, + 8 + ] + ] + }, + { + "label": "later window inside the first: [[1,10],[2,3],[4,5]]", + "input": [ + [ + [ + 1, + 10 + ], + [ + 2, + 3 + ], + [ + 4, + 5 + ] + ] + ], + "expected": [ + [ + 1, + 10 + ] + ] + }, + { + "label": "four windows in one chain: [[1,3],[2,5],[4,8],[7,10]]", + "input": [ + [ + [ + 1, + 3 + ], + [ + 2, + 5 + ], + [ + 4, + 8 + ], + [ + 7, + 10 + ] + ] + ], + "expected": [ + [ + 1, + 10 + ] + ] + }, + { + "label": "overlap after a separate first window: [[1,2],[4,6],[5,7]]", + "input": [ + [ + [ + 1, + 2 + ], + [ + 4, + 6 + ], + [ + 5, + 7 + ] + ] + ], + "expected": [ + [ + 1, + 2 + ], + [ + 4, + 7 + ] + ] + }, + { + "label": "overlapping windows apart in the input: [[1,3],[5,6],[2,4]]", + "input": [ + [ + [ + 1, + 3 + ], + [ + 5, + 6 + ], + [ + 2, + 4 + ] + ] + ], + "expected": [ + [ + 1, + 4 + ], + [ + 5, + 6 + ] + ] } ], "paramNames": [ diff --git a/tests/browser/compiler-explorer.test.js b/tests/browser/compiler-explorer.test.js index 23d6a54bb..80d8e7877 100644 --- a/tests/browser/compiler-explorer.test.js +++ b/tests/browser/compiler-explorer.test.js @@ -727,6 +727,133 @@ test("remove-element cases reject skipping a last unchecked target", () => { } }); +test("merge-intervals cases reject merges that skip the merged window", () => { + const sorted = (intervals) => + structuredClone(intervals).sort(([a], [b]) => a - b); + const consolidate = (intervals) => { + const merged = []; + for (const [start, end] of sorted(intervals)) { + const last = merged.at(-1); + if (last && start <= last[1]) last[1] = Math.max(last[1], end); + else merged.push([start, end]); + } + return merged; + }; + // Merges a window with the next one, then skips both, so a merged window + // is never compared with the window after it. + const onePair = (intervals) => { + const windows = sorted(intervals); + const merged = []; + for (let i = 0; i < windows.length; i += 1) { + let [start, end] = windows[i]; + if (i + 1 < windows.length && windows[i + 1][0] <= end) { + end = Math.max(end, windows[i + 1][1]); + i += 1; + } + merged.push([start, end]); + } + return merged; + }; + // Compares each window with the previous input window, not the merged one, + // so a window inside an earlier long one starts a window of its own. + const previousInput = (intervals) => { + const windows = sorted(intervals); + const merged = []; + windows.forEach(([start, end], i) => { + if (i > 0 && start <= windows[i - 1][1]) { + merged.at(-1)[1] = Math.max(merged.at(-1)[1], end); + } else { + merged.push([start, end]); + } + }); + return merged; + }; + // Absorbs at most two following windows, so a longer chain is cut short. + const twoNeighbours = (intervals) => { + const windows = sorted(intervals); + const merged = []; + for (let i = 0; i < windows.length; i += 1) { + let [start, end] = windows[i]; + for (let n = 0; n < 2; n += 1) { + if (i + 1 < windows.length && windows[i + 1][0] <= end) { + i += 1; + end = Math.max(end, windows[i][1]); + } + } + merged.push([start, end]); + } + return merged; + }; + // Compares each window with the first merged window, not the last one. + const firstMerged = (intervals) => { + const merged = []; + for (const [start, end] of sorted(intervals)) { + if (merged.length > 0 && start <= merged[0][1]) { + merged[0][1] = Math.max(merged[0][1], end); + } else { + merged.push([start, end]); + } + } + return merged; + }; + // Merges neighbours in input order and sorts only the result, so two + // overlapping windows that are apart in the input stay apart. + const inputOrder = (intervals) => { + const merged = []; + for (const [start, end] of structuredClone(intervals)) { + const last = merged.at(-1); + if (last && start <= last[1] && end >= last[0]) { + last[0] = Math.min(last[0], start); + last[1] = Math.max(last[1], end); + } else { + merged.push([start, end]); + } + } + return merged.sort(([a], [b]) => a - b); + }; + const judge = judges["merge-intervals"]; + for (const testCase of judge.cases) { + assert.ok( + checkAnswer(judge, testCase, consolidate(...testCase.input)), + `"${testCase.label}" must expect the consolidated windows`, + ); + } + for (const [name, shortcut, label] of [ + [ + "merging only one neighbouring pair", + onePair, + "chained overlaps: [[1,3],[2,5],[4,8]]", + ], + [ + "comparing with the previous input window", + previousInput, + "later window inside the first: [[1,10],[2,3],[4,5]]", + ], + [ + "absorbing at most two following windows", + twoNeighbours, + "four windows in one chain: [[1,3],[2,5],[4,8],[7,10]]", + ], + [ + "comparing with the first merged window", + firstMerged, + "overlap after a separate first window: [[1,2],[4,6],[5,7]]", + ], + [ + "merging in input order and sorting only the result", + inputOrder, + "overlapping windows apart in the input: [[1,3],[5,6],[2,4]]", + ], + ]) { + const pinned = pinnedCase("merge-intervals", label); + assert.equal( + checkAnswer(judge, pinned, shortcut(...structuredClone(pinned.input))), + false, + `"${label}" must reject ${name}`, + ); + } +}); + test("basic-calculator cases reject parsers without a unary minus", () => { // One parser for the wrong ones, which differ only in when a "-" that starts // a term is read as a sign, and in what that sign negates. `sign` sees the diff --git a/tests/golden/task-posed.json b/tests/golden/task-posed.json index 288083e16..eebe6a2e9 100644 --- a/tests/golden/task-posed.json +++ b/tests/golden/task-posed.json @@ -80,7 +80,7 @@ "maximum-subarray": "13541dc094827af71440c146c9df2f03d7b8b14cb5deaa35f5278c9fc46c9d8f", "maximum-sum-circular-subarray": "d6e02b3ef10897b17c47d15cabf60b6b9a9caf13d64e20a1c8d6f84c2287fc4b", "median-of-two-sorted-arrays": "1ccb5ebfb19f82ff761e3d85816eccc17a4ecc3cf91d74128ccde2e819cc199e", - "merge-intervals": "bdb31219cf9b02aecca0ebc43d0a7644568a6d9597852b7812a92e0a03c9f7f4", + "merge-intervals": "6074320a040010d6c5c0a31761d5152c138b0024143b2ab1832a05828ce33c03", "merge-k-sorted-lists": "1024f0eaa94b0484dc5bf6eef5241c9234f0722fa3dafb76706e5a0bd954decd", "merge-sorted-array": "8d8e57bad76afef5f9dd55e85a45ea2fc5eec73075ac6aecdca10a86a64ddc93", "merge-two-sorted-lists": "40902f036da5f1ad1ba9ea4b92a17627ebecb97cd834567ccaba8fe6324c0ade", diff --git a/tests/web/contract.rs b/tests/web/contract.rs index f751de79e..401d10c45 100644 --- a/tests/web/contract.rs +++ b/tests/web/contract.rs @@ -746,6 +746,11 @@ fn static_problem_bank_and_judges_cover_each_problem() { "two values allow either root", ), ("merge-intervals", "unsorted"), + ("merge-intervals", "chained overlaps"), + ("merge-intervals", "later window inside the first"), + ("merge-intervals", "four windows in one chain"), + ("merge-intervals", "overlap after a separate first window"), + ("merge-intervals", "overlapping windows apart in the input"), ("valid-parentheses", "interleaved"), ("triangle", "cheapest values not connected"), ("triangle", "cheaper step misses the deep saving"), diff --git a/web/judges/maintenance-window-consolidation.json b/web/judges/maintenance-window-consolidation.json index 0555d975f..3bcff2b43 100644 --- a/web/judges/maintenance-window-consolidation.json +++ b/web/judges/maintenance-window-consolidation.json @@ -127,6 +127,143 @@ 10 ] ] + }, + { + "label": "chained overlaps: [[1,3],[2,5],[4,8]]", + "input": [ + [ + [ + 1, + 3 + ], + [ + 2, + 5 + ], + [ + 4, + 8 + ] + ] + ], + "expected": [ + [ + 1, + 8 + ] + ] + }, + { + "label": "later window inside the first: [[1,10],[2,3],[4,5]]", + "input": [ + [ + [ + 1, + 10 + ], + [ + 2, + 3 + ], + [ + 4, + 5 + ] + ] + ], + "expected": [ + [ + 1, + 10 + ] + ] + }, + { + "label": "four windows in one chain: [[1,3],[2,5],[4,8],[7,10]]", + "input": [ + [ + [ + 1, + 3 + ], + [ + 2, + 5 + ], + [ + 4, + 8 + ], + [ + 7, + 10 + ] + ] + ], + "expected": [ + [ + 1, + 10 + ] + ] + }, + { + "label": "overlap after a separate first window: [[1,2],[4,6],[5,7]]", + "input": [ + [ + [ + 1, + 2 + ], + [ + 4, + 6 + ], + [ + 5, + 7 + ] + ] + ], + "expected": [ + [ + 1, + 2 + ], + [ + 4, + 7 + ] + ] + }, + { + "label": "overlapping windows apart in the input: [[1,3],[5,6],[2,4]]", + "input": [ + [ + [ + 1, + 3 + ], + [ + 5, + 6 + ], + [ + 2, + 4 + ] + ] + ], + "expected": [ + [ + 1, + 4 + ], + [ + 5, + 6 + ] + ] } ], "paramNames": [