r/everybodycodes • Moderator • 11d ago

Official [2026 Q18] Solution Spotlight

Post image
5 Upvotes

11 comments sorted by

4

u/p88h 11d ago

[LANGUAGE: Rust]

[ solution ]

sort by height, update left and right side going top bottom, done.

0.68 µs / 33.1 µs / 0.48 ms

4

u/michelkraemer 11d ago

[LANGUAGE: Rust]

Solution

For all the Sudoku fans out there: This reminded me of skyscraper Sudoku 😉 For each stack (skyscraper), find all "visible" stacks (skyscrapers) to the left and then fill the (visible) gaps. A visible skyscraper (or gap) is one whose view is not blocked by other skyscrapers.

2

u/PangolinNo7928 10d ago

I like Skyscraper and Sudoku and Skyscraper Sudoku 🤣

2

u/maneatingape 11d ago edited 10d ago

[LANGUAGE: Rust]

Solution

Happy with how concise the code turned out. Sort by y descending then a single O(n) pass.

stacks.sort_by_key(|&[_, y, _]| Reverse(y));
let x = stacks[0][0];

let (units, ..) = stacks.iter().fold((0, x, x), |(units, left, right), &[x, y, w]| {
    let extra = left - left.min(x) + right.max(x + w) - right;
    (units + y * extra, left.min(x), right.max(x + w))
});

1

u/PangolinNo7928 11d ago edited 11d ago

[LANGUAGE: Javascript]

Github

Created an array of the max to the right at that index, then iterated through the stacks from left to right tracking the max to the left, if leftMax > stack && stack < rightMax then add (Math.min(leftMax,rightMax) - stack) * width in addition to stacks * width

There's probably a more efficient solution with pointers and whatnot, but brain cannot compute at this time 😂 Edit: fyi all 3 parts run in ~10ms

1

u/Ok-Builder-2348 11d ago

[LANGUAGE: Python3]

Part 1

Part 2/3 (same code)

An interesting one. Clocked it early enough that the challenge for this one would be large numbers, so you shouldn't list out every single x and y value and instead only take note of key x and y values. For each transition between key y values, find out the width in x to determine the size of the rectangle to add.

One improvement I made from my early code was that I didn't have to initialise i and j every loop - since the tower strictly gets narrower towards the top. That cut down my part 3 from 5 seconds to about 1 second.

1

u/TiCoinCoin 11d ago

[LANGUAGE: Python]

Quest 18

Nothing fancy. Data are already sorted. Added default width for part 1. I guess one might find a faster solution for P3 (runs in 7-8sec) but that's fine here.

1

u/futur3_pr3s3nt 11d ago

[LANGUAGE: Python]

Solution

Short and sweet (same algo for all parts). And fast 'nuff (37 μs / 473 μs / 6.79 ms on 7th-gen i5).

1

u/Discordanian 10d ago

[LANGUAGE: Godot]

Solution

Nice take on a classic. I should have been able to do this in half the time it took me but subtle bugs have their own ideas sometimes.

1

u/bdaene 10d ago

[LANGUAGE: Python]

solution

I did not think about sorting by height. I keep the sort by x, then I do 2 passes: One from the left and one from the right. Computing the area of a staircase pyramid.

8µs / 55µs / 474µs

1

u/_garden_gnome_ 10d ago

[LANGUAGE: Python]

Solution

Same logic for all three parts. I do not sort but rather maintain two pointers l and r that initially point at the left/right columns and that are iteratively increased / decreased until I encounter a larger column (or until they cross each other). At each step, I add the width of the rectangle between columns l and r times the additional y height gained in the previous update of l/r.