Search
⌘K

Leetcode 3015. Count the Number of Houses at a Certain Distance I

Given n houses in a line with one extra edge between x and y, count for each k the number of ordered house pairs whose shortest path length equals k. The challenge is to avoid O(n^2) all-pairs checks by using the fact that distances are min(|i-j|, |i-x|+1+|j-y|, |i-y|+1+|j-x|) and aggregating counts with arithmetic in O(n).


Question Timeline

See when this question was last asked and where, including any notes left by other candidates.

Comments

Your account is free and you can post anonymously if you choose.