leetcode
leetcode 2701 ~ 2750
按距离统计房屋对数目 I

按距离统计房屋对数目 I

难度:

标签:

题目描述

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 <= 100
  • 1 <= x, y <= n

代码结果

运行时间: 40 ms, 内存: 16.1 MB


/*
 * Problem statement:
 * Given three positive integers n, x, and y.
 * There are houses numbered from 1 to n, connected by n streets.
 * Each i (1 <= i < n) has a street connecting house i and house i + 1.
 * There is another street connecting house x and house y.
 * For each k (1 <= k <= n), find the number of house pairs (house1, house2) such that the shortest street distance between house1 and house2 is k.
 * Return an array result where result[k] is the count of such house pairs.
 */

import java.util.stream.IntStream;

public class Solution {
    public int[] countPairs(int n, int x, int y) {
        int[] result = new int[n];
        IntStream.rangeClosed(1, n).forEach(i -> 
            IntStream.rangeClosed(i + 1, n).forEach(j -> {
                int dist = Math.min(j - i, Math.abs(x - i) + 1 + Math.abs(y - j));
                result[dist]++;
            })
        );
        return result;
    }
}

解释

方法:

此题解采用了前缀和的思想。首先,通过调整 x, y 的大小关系,保证 x < y。然后遍历每个房屋,对于每个房屋 i,计算从 i 到 x 和从 i 到 y 的距离,根据这两个距离的大小关系,判断房屋对是否需要通过街道 x 到 y 的中介。在确定了是否需要中介后,对结果数组进行更新,最后通过累加前缀和,计算每个距离下的房屋对数量。

时间复杂度:

O(n)

空间复杂度:

O(n)

代码细节讲解

🦆
为什么在解决问题时首先需要确保x < y,这一步调整对算法的影响是什么?
在算法中确保 x < y 的主要原因是为了简化问题处理逻辑和保持一致性。当 x < y 时,可以统一地处理所有房屋到街道 x 和 y 的距离比较,而不需要在每次比较时都考虑 x 和 y 的相对位置。这样可以减少代码的复杂性并避免出错。此外,算法中的计算步骤和更新操作可以按照固定的方向(从 x 到 y)执行,这有助于减少条件判断,使算法更加清晰和高效。
🦆
在计算从房屋i到x和从房屋i到y的距离时,你如何确定是否需要通过街道x到y的中介?这一逻辑是基于什么原理?
在题解中,我们通过比较从房屋 i 到 x 和从房屋 i 到 y 的距离来决定是否需要通过街道 x 到 y 的中介。基本原理是,如果房屋到 x 的距离加上从 x 到 y 的距离(即绕路的总距离)大于直接从房屋到 y 的距离,那么可以认为房屋 i 更倾向于直接连接到 y,而不通过 x。这种判断帮助我们理解房屋对的连接方式如何依赖于其到两条街道的相对位置。
🦆
题解中提到的`更新操作`具体是如何实现的?能否详细解释`ans[1] += 1`和`ans[n - i] -= 1`这两步操作的含义及其对结果的影响?
题解中的更新操作使用了一种差分数组的方法来高效地管理区间更新。`ans[1] += 1`意味着从距离 1 开始,房屋对数量增加。而`ans[n - i] -= 1`表示在距离 n-i 处房屋对数量减少,这是因为超出这个距离后,之前增加的房屋对不再在计数范围内。通过这种方法,我们可以在 O(1) 的时间复杂度内更新大范围的数据,最后通过累加这些差分值来还原出每个距离下的实际房屋对数量。这种技术极大地提高了数据更新的效率,特别是在处理大量数据时。

相关问题