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

@problem@discussion
#Breadth-First Search#Graph#Prefix Sum



1/**
2 * [3015] Count the Number of Houses at a Certain Distance I
3 *
4 * You are given three positive integers n, x, and y.
5 * 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.
6 * 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.
7 * 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.
8 * Note that x and y can be equal.
9 *  
10 * <strong class="example">Example 1:
11 * <img alt="" src="https://assets.leetcode.com/uploads/2023/12/20/example2.png" style="width: 474px; height: 197px;" />
12 * Input: n = 3, x = 1, y = 3
13 * Output: [6,0,0]
14 * Explanation: Let's look at each pair of houses:
15 * - For the pair (1, 2), we can go from house 1 to house 2 directly.
16 * - For the pair (2, 1), we can go from house 2 to house 1 directly.
17 * - For the pair (1, 3), we can go from house 1 to house 3 directly.
18 * - For the pair (3, 1), we can go from house 3 to house 1 directly.
19 * - For the pair (2, 3), we can go from house 2 to house 3 directly.
20 * - For the pair (3, 2), we can go from house 3 to house 2 directly.
21 * 
22 * <strong class="example">Example 2:
23 * <img alt="" src="https://assets.leetcode.com/uploads/2023/12/20/example3.png" style="width: 668px; height: 174px;" />
24 * Input: n = 5, x = 2, y = 4
25 * Output: [10,8,2,0,0]
26 * Explanation: For each distance k the pairs are:
27 * - 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).
28 * - For k == 2, the pairs are (1, 3), (3, 1), (1, 4), (4, 1), (2, 5), (5, 2), (3, 5), and (5, 3).
29 * - For k == 3, the pairs are (1, 5), and (5, 1).
30 * - For k == 4 and k == 5, there are no pairs.
31 * 
32 * <strong class="example">Example 3:
33 * <img alt="" src="https://assets.leetcode.com/uploads/2023/12/20/example5.png" style="width: 544px; height: 130px;" />
34 * Input: n = 4, x = 1, y = 1
35 * Output: [6,4,2,0]
36 * Explanation: For each distance k the pairs are:
37 * - For k == 1, the pairs are (1, 2), (2, 1), (2, 3), (3, 2), (3, 4), and (4, 3).
38 * - For k == 2, the pairs are (1, 3), (3, 1), (2, 4), and (4, 2).
39 * - For k == 3, the pairs are (1, 4), and (4, 1).
40 * - For k == 4, there are no pairs.
41 * 
42 *  
43 * Constraints:
44 * 
45 * 	2 <= n <= 100
46 * 	1 <= x, y <= n
47 * 
48 */
49pub struct Solution {}
50
51// problem: https://leetcode.com/problems/count-the-number-of-houses-at-a-certain-distance-i/
52// discuss: https://leetcode.com/problems/count-the-number-of-houses-at-a-certain-distance-i/discuss/?currentPage=1&orderBy=most_votes&query=
53
54// submission codes start here
55
56impl Solution {
57    pub fn count_of_pairs(n: i32, x: i32, y: i32) -> Vec<i32> {
58        vec![]
59    }
60}
61
62// submission codes end
63
64#[cfg(test)]
65mod tests {
66    use super::*;
67
68    #[test]
69    fn test_3015() {
70    }
71}
72


Back
© 2025 bowen.ge All Rights Reserved.