Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
137 changes: 137 additions & 0 deletions problem-bank/judges.json
Original file line number Diff line number Diff line change
Expand Up @@ -9146,6 +9146,143 @@
10
]
]
},
{
"label": "chained overlaps: [[1,3],[2,5],[4,8]]",
Comment thread
ye144440154 marked this conversation as resolved.
Resolved
Comment thread
ye144440154 marked this conversation as resolved.
Resolved
"input": [
[
[
1,
3
],
[
2,
5
],
[
4,
8
]
]
],
"expected": [
[
1,
8
]
]
},
{
"label": "later window inside the first: [[1,10],[2,3],[4,5]]",
Comment thread
ye144440154 marked this conversation as resolved.
Resolved
"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]]",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An unsorted variant that folds each window into the first kept window it overlaps, then sorts the result, still passes every case here, since [2,4] touches only [1,3]:

const merged = [];
for (const [s, e] of intervals) {
  const w = merged.find(([a, b]) => s <= b && e >= a);
  if (w) { w[0] = Math.min(w[0], s); w[1] = Math.max(w[1], e); }
  else merged.push([s, e]);
}
return merged.sort(([a], [b]) => a - b);

It never merges two kept windows with each other, so on [[1,2],[5,6],[2,5]], where the last window bridges the first two, it returns [[1,5],[5,6]] instead of [[1,6]] (I ran it through checkAnswer against this head's web/judges/maintenance-window-consolidation.json). A case such as window joining two earlier ones: [[1,2],[5,6],[2,5]] expecting [[1,6]], inside the stated constraints, rejects it while sort-and-extend passes, and would join the labels pinned in tests/web/contract.rs with this variant as its own shortcut in the browser test.

"input": [
[
[
1,
3
],
[
5,
6
],
[
2,
4
]
]
],
"expected": [
[
1,
4
],
[
5,
6
]
]
}
],
"paramNames": [
Expand Down
127 changes: 127 additions & 0 deletions tests/browser/compiler-explorer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Comment thread
ye144440154 marked this conversation as resolved.
Resolved
// 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"];
Comment thread
ye144440154 marked this conversation as resolved.
Resolved
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
Expand Down
2 changes: 1 addition & 1 deletion tests/golden/task-posed.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
5 changes: 5 additions & 0 deletions tests/web/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
Loading
Loading