Skip to content

3017. Count the Number of Houses at a Certain Distance II

Description

You are given three positive integers n, x, and y.

In a city, there exist houses numbered 1 to n connected by n streets. There is a street connecting the house numbered i with the house numbered i + 1 for all 1 <= i <= n - 1 . An additional street connects the house numbered x with the house numbered y.

For each k, such that 1 <= k <= n, you need to find the number of pairs of houses (house1, house2) such that the minimum number of streets that need to be traveled to reach house2 from house1 is k.

Return a 1-indexed array result of length n where result[k] represents the total number of pairs of houses such that the minimum streets required to reach one house from the other is k.

Note that x and y can be equal.

 

Example 1:

Input: n = 3, x = 1, y = 3
Output: [6,0,0]
Explanation: Let's look at each pair of houses:
- For the pair (1, 2), we can go from house 1 to house 2 directly.
- For the pair (2, 1), we can go from house 2 to house 1 directly.
- For the pair (1, 3), we can go from house 1 to house 3 directly.
- For the pair (3, 1), we can go from house 3 to house 1 directly.
- For the pair (2, 3), we can go from house 2 to house 3 directly.
- For the pair (3, 2), we can go from house 3 to house 2 directly.

Example 2:

Input: n = 5, x = 2, y = 4
Output: [10,8,2,0,0]
Explanation: For each distance k the pairs are:
- For k == 1, the pairs are (1, 2), (2, 1), (2, 3), (3, 2), (2, 4), (4, 2), (3, 4), (4, 3), (4, 5), and (5, 4).
- For k == 2, the pairs are (1, 3), (3, 1), (1, 4), (4, 1), (2, 5), (5, 2), (3, 5), and (5, 3).
- For k == 3, the pairs are (1, 5), and (5, 1).
- For k == 4 and k == 5, there are no pairs.

Example 3:

Input: n = 4, x = 1, y = 1
Output: [6,4,2,0]
Explanation: For each distance k the pairs are:
- For k == 1, the pairs are (1, 2), (2, 1), (2, 3), (3, 2), (3, 4), and (4, 3).
- For k == 2, the pairs are (1, 3), (3, 1), (2, 4), and (4, 2).
- For k == 3, the pairs are (1, 4), and (4, 1).
- For k == 4, there are no pairs.

 

Constraints:

  • 2 <= n <= 105
  • 1 <= x, y <= n

 

Solutions

Solution: Prefix Sum

  • Time complexity: O(n)
  • Space complexity: O(n)

 

JavaScript

js
/**
 * @param {number} n
 * @param {number} x
 * @param {number} y
 * @return {number[]}
 */
const countOfPairs = function (n, x, y) {
  if (x > y) {
    return countOfPairs(n, y, x);
  }

  const result = Array.from({ length: n }, () => 0);
  const leftDis = x - 1;
  const rightDis = n - y;
  const ringDis = y - x + 1;

  combineVectors(result, bothInRing(n, ringDis));
  combineVectors(result, bothInSameLine(n, leftDis));
  combineVectors(result, bothInSameLine(n, rightDis));
  combineVectors(result, lineToRing(n, leftDis, ringDis));
  combineVectors(result, lineToRing(n, rightDis, ringDis));
  combineVectors(result, lineToLine(n, x, y, leftDis, rightDis));

  return result.map(val => val * 2);
};

function bothInRing(n, ringDis) {
  const result = Array.from({ length: n }, () => 0);
  const center = Math.floor((ringDis - 1) / 2);

  for (let k = 1; k <= center; k++) {
    result[k - 1] += ringDis;
  }

  if (ringDis % 2 === 0) {
    const halfRing = ringDis / 2;

    result[halfRing - 1] += halfRing;
  }

  return result;
}

function bothInSameLine(n, dis) {
  const result = Array.from({ length: n }, () => 0);

  for (let k = 1; k <= dis; k++) {
    result[k - 1] += dis - k;
  }

  return result;
}

function lineToRing(n, dis, ringDis) {
  const result = Array.from({ length: n }, () => 0);
  const halfRing = Math.floor(ringDis / 2);

  for (let k = 1; k <= dis + ringDis; k++) {
    const maxRingDis = Math.min(k - 1, halfRing);
    const minRingDis = Math.max(0, k - dis);

    if (minRingDis > maxRingDis) continue;

    result[k - 1] += (maxRingDis - minRingDis + 1) * 2;

    if (minRingDis === 0) {
      result[k - 1] -= 1;
    }

    if (maxRingDis * 2 === ringDis) {
      result[k - 1] -= 1;
    }
  }

  return result;
}

function lineToLine(n, x, y, leftDis, ringDis) {
  const result = Array.from({ length: n }, () => 0);
  const hasBridge = x === y ? 0 : 1;

  for (let k = 1; k <= leftDis + ringDis + 2; k++) {
    const maxLeftDis = Math.min(leftDis, k - 1 - hasBridge);
    const minLeftDis = Math.max(1, k - ringDis - hasBridge);

    if (minLeftDis <= maxLeftDis) {
      result[k - 1] += maxLeftDis - minLeftDis + 1;
    }
  }

  return result;
}

function combineVectors(a, b) {
  const n = a.length;

  for (let index = 0; index < n; index++) {
    a[index] += b[index];
  }
}

Released under the MIT license