|
63 | 63 |
|
64 | 64 | ## Solutions
|
65 | 65 |
|
66 |
| -### Solution 1 |
| 66 | +### Solution 1: Enumeration |
| 67 | + |
| 68 | +We can enumerate each pair of points $(i, j)$. The shortest distance from $i$ to $j$ is $min(|i - j|, |i - x| + 1 + |j - y|, |i - y| + 1 + |j - x|)$. We add $2$ to the count of this distance because both $(i, j)$ and $(j, i)$ are valid pairs of points. |
| 69 | + |
| 70 | +The time complexity is $O(n^2)$, where $n$ is the $n$ given in the problem. Ignoring the space consumption of the answer array, the space complexity is $O(1)$. |
67 | 71 |
|
68 | 72 | <!-- tabs:start -->
|
69 | 73 |
|
70 | 74 | ```python
|
71 |
| - |
| 75 | +class Solution: |
| 76 | + def countOfPairs(self, n: int, x: int, y: int) -> List[int]: |
| 77 | + x, y = x - 1, y - 1 |
| 78 | + ans = [0] * n |
| 79 | + for i in range(n): |
| 80 | + for j in range(i + 1, n): |
| 81 | + a = j - i |
| 82 | + b = abs(i - x) + 1 + abs(j - y) |
| 83 | + c = abs(i - y) + 1 + abs(j - x) |
| 84 | + ans[min(a, b, c) - 1] += 2 |
| 85 | + return ans |
72 | 86 | ```
|
73 | 87 |
|
74 | 88 | ```java
|
75 |
| - |
| 89 | +class Solution { |
| 90 | + public int[] countOfPairs(int n, int x, int y) { |
| 91 | + int[] ans = new int[n]; |
| 92 | + x--; |
| 93 | + y--; |
| 94 | + for (int i = 0; i < n; ++i) { |
| 95 | + for (int j = i + 1; j < n; ++j) { |
| 96 | + int a = j - i; |
| 97 | + int b = Math.abs(i - x) + 1 + Math.abs(j - y); |
| 98 | + int c = Math.abs(i - y) + 1 + Math.abs(j - x); |
| 99 | + ans[Math.min(a, Math.min(b, c)) - 1] += 2; |
| 100 | + } |
| 101 | + } |
| 102 | + return ans; |
| 103 | + } |
| 104 | +} |
76 | 105 | ```
|
77 | 106 |
|
78 | 107 | ```cpp
|
79 |
| - |
| 108 | +class Solution { |
| 109 | +public: |
| 110 | + vector<int> countOfPairs(int n, int x, int y) { |
| 111 | + vector<int> ans(n); |
| 112 | + x--; |
| 113 | + y--; |
| 114 | + for (int i = 0; i < n; ++i) { |
| 115 | + for (int j = i + 1; j < n; ++j) { |
| 116 | + int a = j - i; |
| 117 | + int b = abs(x - i) + abs(y - j) + 1; |
| 118 | + int c = abs(y - i) + abs(x - j) + 1; |
| 119 | + ans[min({a, b, c}) - 1] += 2; |
| 120 | + } |
| 121 | + } |
| 122 | + return ans; |
| 123 | + } |
| 124 | +}; |
80 | 125 | ```
|
81 | 126 |
|
82 | 127 | ```go
|
| 128 | +func countOfPairs(n int, x int, y int) []int { |
| 129 | + ans := make([]int, n) |
| 130 | + x, y = x-1, y-1 |
| 131 | + for i := 0; i < n; i++ { |
| 132 | + for j := i + 1; j < n; j++ { |
| 133 | + a := j - i |
| 134 | + b := abs(x-i) + abs(y-j) + 1 |
| 135 | + c := abs(x-j) + abs(y-i) + 1 |
| 136 | + ans[min(a, min(b, c))-1] += 2 |
| 137 | + } |
| 138 | + } |
| 139 | + return ans |
| 140 | +} |
| 141 | +
|
| 142 | +func abs(x int) int { |
| 143 | + if x < 0 { |
| 144 | + return -x |
| 145 | + } |
| 146 | + return x |
| 147 | +} |
| 148 | +``` |
83 | 149 |
|
| 150 | +```ts |
| 151 | +function countOfPairs(n: number, x: number, y: number): number[] { |
| 152 | + const ans: number[] = Array(n).fill(0); |
| 153 | + x--; |
| 154 | + y--; |
| 155 | + for (let i = 0; i < n; ++i) { |
| 156 | + for (let j = i + 1; j < n; ++j) { |
| 157 | + const a = j - i; |
| 158 | + const b = Math.abs(x - i) + Math.abs(y - j) + 1; |
| 159 | + const c = Math.abs(y - i) + Math.abs(x - j) + 1; |
| 160 | + ans[Math.min(a, b, c) - 1] += 2; |
| 161 | + } |
| 162 | + } |
| 163 | + return ans; |
| 164 | +} |
84 | 165 | ```
|
85 | 166 |
|
86 | 167 | <!-- tabs:end -->
|
|
0 commit comments