The fence on the street
- O(n²) · Very hard
- Full plan
- Python
- JavaScript
- comparisons
- loops
- lists
Problem
The neighbors on the cul-de-sac split up the long fence to paint it on Saturday, and each one wrote in a notebook which boards were theirs, from one board to another. Nobody looked at anyone else's stretch. Oscar, the painter who checks the work, knows that paint cracks wherever two coats or more land, and he wants to know how many boards he will have to sand. You get length (a whole number from 0 up; the boards go from 0 to length - 1) and start and end, two lists of whole numbers of the same size that may be empty: neighbor i painted from board start[i] to board end[i], both included, with 0 <= start[i] <= end[i] < length. Return, as a whole number, how many boards got paint from 2 neighbors or more; a board with three coats or more counts only once.
With length 10, start [0, 3, 5] and end [4, 6, 5]: neighbor 0 paints boards 0 to 4, neighbor 1 boards 3 to 6 and neighbor 2 only board 5. Boards 3 and 4 are painted by neighbors 0 and 1, and board 5 by neighbors 1 and 2: you return 3.
With no neighbors, or if no stretch overlaps another, you return 0.
Examples
The example
10, [0, 3, 5], [4, 6, 5] → 3
Stretches that only touch
8, [0, 3, 6], [2, 5, 7] → 0
A single neighbor
5, [1], [3] → 0
Three coats count once
6, [0, 1, 2], [5, 4, 3] → 4
Both ends of the fence
7, [0, 6, 0], [3, 6, 6] → 5
Besides these, the challenge has hidden tests that are revealed when you submit your solution.
You start with this
Python
def to_sand(length, start, end):
passJavaScript
function toSand(length, start, end) {
}It opens in your browser, with the editor and the tests. This challenge is part of the full plan; the O(1) and O(log n) ones are free.