|
72 | 72 | <!-- tabs:start -->
|
73 | 73 |
|
74 | 74 | ```python
|
75 |
| - |
| 75 | +class Solution: |
| 76 | + def countOfPairs(self, n: int, x: int, y: int) -> List[int]: |
| 77 | + if abs(x - y) <= 1: |
| 78 | + return [2 * x for x in reversed(range(n))] |
| 79 | + cycle_len = abs(x - y) + 1 |
| 80 | + n2 = n - cycle_len + 2 |
| 81 | + res = [2 * x for x in reversed(range(n2))] |
| 82 | + while len(res) < n: |
| 83 | + res.append(0) |
| 84 | + res2 = [cycle_len * 2] * (cycle_len >> 1) |
| 85 | + if not cycle_len & 1: |
| 86 | + res2[-1] = cycle_len |
| 87 | + res2[0] -= 2 |
| 88 | + for i in range(len(res2)): |
| 89 | + res[i] += res2[i] |
| 90 | + if x > y: |
| 91 | + x, y = y, x |
| 92 | + tail1 = x - 1 |
| 93 | + tail2 = n - y |
| 94 | + for tail in (tail1, tail2): |
| 95 | + if not tail: |
| 96 | + continue |
| 97 | + i_mx = tail + (cycle_len >> 1) |
| 98 | + val_mx = 4 * min((cycle_len - 3) >> 1, tail) |
| 99 | + i_mx2 = i_mx - (1 - (cycle_len & 1)) |
| 100 | + res3 = [val_mx] * i_mx |
| 101 | + res3[0] = 0 |
| 102 | + res3[1] = 0 |
| 103 | + if not cycle_len & 1: |
| 104 | + res3[-1] = 0 |
| 105 | + for i, j in enumerate(range(4, val_mx, 4)): |
| 106 | + res3[i + 2] = j |
| 107 | + res3[i_mx2 - i - 1] = j |
| 108 | + for i in range(1, tail + 1): |
| 109 | + res3[i] += 2 |
| 110 | + if not cycle_len & 1: |
| 111 | + mn = cycle_len >> 1 |
| 112 | + for i in range(mn, mn + tail): |
| 113 | + res3[i] += 2 |
| 114 | + for i in range(len(res3)): |
| 115 | + res[i] += res3[i] |
| 116 | + return res |
76 | 117 | ```
|
77 | 118 |
|
78 | 119 | ```java
|
79 |
| - |
| 120 | +class Solution { |
| 121 | + public long[] countOfPairs(int n, int x, int y) { |
| 122 | + --x; |
| 123 | + --y; |
| 124 | + if (x > y) { |
| 125 | + int temp = x; |
| 126 | + x = y; |
| 127 | + y = temp; |
| 128 | + } |
| 129 | + long[] diff = new long[n]; |
| 130 | + for (int i = 0; i < n; ++i) { |
| 131 | + diff[0] += 1 + 1; |
| 132 | + ++diff[Math.min(Math.abs(i - x), Math.abs(i - y) + 1)]; |
| 133 | + ++diff[Math.min(Math.abs(i - y), Math.abs(i - x) + 1)]; |
| 134 | + --diff[Math.min(Math.abs(i - 0), Math.abs(i - y) + 1 + Math.abs(x - 0))]; |
| 135 | + --diff[Math.min(Math.abs(i - (n - 1)), |
| 136 | + Math.abs(i - x) + 1 + Math.abs(y - (n - 1)))]; |
| 137 | + --diff[Math.max(x - i, 0) + Math.max(i - y, 0) + ((y - x) + 0) / 2]; |
| 138 | + --diff[Math.max(x - i, 0) + Math.max(i - y, 0) + ((y - x) + 1) / 2]; |
| 139 | + } |
| 140 | + for (int i = 0; i + 1 < n; ++i) { |
| 141 | + diff[i + 1] += diff[i]; |
| 142 | + } |
| 143 | + return diff; |
| 144 | + } |
| 145 | +} |
80 | 146 | ```
|
81 | 147 |
|
82 | 148 | ```cpp
|
83 |
| - |
| 149 | +class Solution { |
| 150 | +public: |
| 151 | + vector<long long> countOfPairs(int n, int x, int y) { |
| 152 | + --x, --y; |
| 153 | + if (x > y) { |
| 154 | + swap(x, y); |
| 155 | + } |
| 156 | + vector<long long> diff(n); |
| 157 | + for (int i = 0; i < n; ++i) { |
| 158 | + diff[0] += 1 + 1; |
| 159 | + ++diff[min(abs(i - x), abs(i - y) + 1)]; |
| 160 | + ++diff[min(abs(i - y), abs(i - x) + 1)]; |
| 161 | + --diff[min(abs(i - 0), abs(i - y) + 1 + abs(x - 0))]; |
| 162 | + --diff[min(abs(i - (n - 1)), abs(i - x) + 1 + abs(y - (n - 1)))]; |
| 163 | + --diff[max(x - i, 0) + max(i - y, 0) + ((y - x) + 0) / 2]; |
| 164 | + --diff[max(x - i, 0) + max(i - y, 0) + ((y - x) + 1) / 2]; |
| 165 | + } |
| 166 | + for (int i = 0; i + 1 < n; ++i) { |
| 167 | + diff[i + 1] += diff[i]; |
| 168 | + } |
| 169 | + return diff; |
| 170 | + } |
| 171 | +}; |
84 | 172 | ```
|
85 | 173 |
|
86 | 174 | ```go
|
87 |
| - |
| 175 | +func countOfPairs(n int, x int, y int) []int64 { |
| 176 | + if x > y { |
| 177 | + x, y = y, x |
| 178 | + } |
| 179 | + A := make([]int64, n) |
| 180 | + for i := 1; i <= n; i++ { |
| 181 | + A[0] += 2 |
| 182 | + A[min(int64(i-1), int64(math.Abs(float64(i-y)))+int64(x))] -= 1 |
| 183 | + A[min(int64(n-i), int64(math.Abs(float64(i-x)))+1+int64(n-y))] -= 1 |
| 184 | + A[min(int64(math.Abs(float64(i-x))), int64(math.Abs(float64(y-i)))+1)] += 1 |
| 185 | + A[min(int64(math.Abs(float64(i-x)))+1, int64(math.Abs(float64(y-i))))] += 1 |
| 186 | + r := max(int64(x-i), 0) + max(int64(i-y), 0) |
| 187 | + A[r+int64((y-x+0)/2)] -= 1 |
| 188 | + A[r+int64((y-x+1)/2)] -= 1 |
| 189 | + } |
| 190 | + for i := 1; i < n; i++ { |
| 191 | + A[i] += A[i-1] |
| 192 | + } |
| 193 | +
|
| 194 | + return A |
| 195 | +} |
88 | 196 | ```
|
89 | 197 |
|
90 | 198 | <!-- tabs:end -->
|
|
0 commit comments