diff --git a/solution/3000-3099/3017.Count the Number of Houses at a Certain Distance II/README.md b/solution/3000-3099/3017.Count the Number of Houses at a Certain Distance II/README.md index a6a492ac5ba61..6fcca45503d45 100644 --- a/solution/3000-3099/3017.Count the Number of Houses at a Certain Distance II/README.md +++ b/solution/3000-3099/3017.Count the Number of Houses at a Certain Distance II/README.md @@ -72,19 +72,127 @@ ```python - +class Solution: + def countOfPairs(self, n: int, x: int, y: int) -> List[int]: + if abs(x - y) <= 1: + return [2 * x for x in reversed(range(n))] + cycle_len = abs(x - y) + 1 + n2 = n - cycle_len + 2 + res = [2 * x for x in reversed(range(n2))] + while len(res) < n: + res.append(0) + res2 = [cycle_len * 2] * (cycle_len >> 1) + if not cycle_len & 1: + res2[-1] = cycle_len + res2[0] -= 2 + for i in range(len(res2)): + res[i] += res2[i] + if x > y: + x, y = y, x + tail1 = x - 1 + tail2 = n - y + for tail in (tail1, tail2): + if not tail: + continue + i_mx = tail + (cycle_len >> 1) + val_mx = 4 * min((cycle_len - 3) >> 1, tail) + i_mx2 = i_mx - (1 - (cycle_len & 1)) + res3 = [val_mx] * i_mx + res3[0] = 0 + res3[1] = 0 + if not cycle_len & 1: + res3[-1] = 0 + for i, j in enumerate(range(4, val_mx, 4)): + res3[i + 2] = j + res3[i_mx2 - i - 1] = j + for i in range(1, tail + 1): + res3[i] += 2 + if not cycle_len & 1: + mn = cycle_len >> 1 + for i in range(mn, mn + tail): + res3[i] += 2 + for i in range(len(res3)): + res[i] += res3[i] + return res ``` ```java - +class Solution { + public long[] countOfPairs(int n, int x, int y) { + --x; + --y; + if (x > y) { + int temp = x; + x = y; + y = temp; + } + long[] diff = new long[n]; + for (int i = 0; i < n; ++i) { + diff[0] += 1 + 1; + ++diff[Math.min(Math.abs(i - x), Math.abs(i - y) + 1)]; + ++diff[Math.min(Math.abs(i - y), Math.abs(i - x) + 1)]; + --diff[Math.min(Math.abs(i - 0), Math.abs(i - y) + 1 + Math.abs(x - 0))]; + --diff[Math.min(Math.abs(i - (n - 1)), + Math.abs(i - x) + 1 + Math.abs(y - (n - 1)))]; + --diff[Math.max(x - i, 0) + Math.max(i - y, 0) + ((y - x) + 0) / 2]; + --diff[Math.max(x - i, 0) + Math.max(i - y, 0) + ((y - x) + 1) / 2]; + } + for (int i = 0; i + 1 < n; ++i) { + diff[i + 1] += diff[i]; + } + return diff; + } +} ``` ```cpp - +class Solution { +public: + vector countOfPairs(int n, int x, int y) { + --x, --y; + if (x > y) { + swap(x, y); + } + vector diff(n); + for (int i = 0; i < n; ++i) { + diff[0] += 1 + 1; + ++diff[min(abs(i - x), abs(i - y) + 1)]; + ++diff[min(abs(i - y), abs(i - x) + 1)]; + --diff[min(abs(i - 0), abs(i - y) + 1 + abs(x - 0))]; + --diff[min(abs(i - (n - 1)), abs(i - x) + 1 + abs(y - (n - 1)))]; + --diff[max(x - i, 0) + max(i - y, 0) + ((y - x) + 0) / 2]; + --diff[max(x - i, 0) + max(i - y, 0) + ((y - x) + 1) / 2]; + } + for (int i = 0; i + 1 < n; ++i) { + diff[i + 1] += diff[i]; + } + return diff; + } +}; ``` ```go - +func countOfPairs(n int, x int, y int) []int64 { + if x > y { + x, y = y, x + } + A := make([]int64, n) + for i := 1; i <= n; i++ { + A[0] += 2 + A[min(int64(i-1), int64(math.Abs(float64(i-y)))+int64(x))] -= 1 + A[min(int64(n-i), int64(math.Abs(float64(i-x)))+1+int64(n-y))] -= 1 + A[min(int64(math.Abs(float64(i-x))), int64(math.Abs(float64(y-i)))+1)] += 1 + A[min(int64(math.Abs(float64(i-x)))+1, int64(math.Abs(float64(y-i))))] += 1 + r := max(int64(x-i), 0) + max(int64(i-y), 0) + A[r+int64((y-x+0)/2)] -= 1 + A[r+int64((y-x+1)/2)] -= 1 + } + for i := 1; i < n; i++ { + A[i] += A[i-1] + } + + return A +} ``` diff --git a/solution/3000-3099/3017.Count the Number of Houses at a Certain Distance II/README_EN.md b/solution/3000-3099/3017.Count the Number of Houses at a Certain Distance II/README_EN.md index 39456681a3e8a..22279e0b01841 100644 --- a/solution/3000-3099/3017.Count the Number of Houses at a Certain Distance II/README_EN.md +++ b/solution/3000-3099/3017.Count the Number of Houses at a Certain Distance II/README_EN.md @@ -68,19 +68,127 @@ ```python - +class Solution: + def countOfPairs(self, n: int, x: int, y: int) -> List[int]: + if abs(x - y) <= 1: + return [2 * x for x in reversed(range(n))] + cycle_len = abs(x - y) + 1 + n2 = n - cycle_len + 2 + res = [2 * x for x in reversed(range(n2))] + while len(res) < n: + res.append(0) + res2 = [cycle_len * 2] * (cycle_len >> 1) + if not cycle_len & 1: + res2[-1] = cycle_len + res2[0] -= 2 + for i in range(len(res2)): + res[i] += res2[i] + if x > y: + x, y = y, x + tail1 = x - 1 + tail2 = n - y + for tail in (tail1, tail2): + if not tail: + continue + i_mx = tail + (cycle_len >> 1) + val_mx = 4 * min((cycle_len - 3) >> 1, tail) + i_mx2 = i_mx - (1 - (cycle_len & 1)) + res3 = [val_mx] * i_mx + res3[0] = 0 + res3[1] = 0 + if not cycle_len & 1: + res3[-1] = 0 + for i, j in enumerate(range(4, val_mx, 4)): + res3[i + 2] = j + res3[i_mx2 - i - 1] = j + for i in range(1, tail + 1): + res3[i] += 2 + if not cycle_len & 1: + mn = cycle_len >> 1 + for i in range(mn, mn + tail): + res3[i] += 2 + for i in range(len(res3)): + res[i] += res3[i] + return res ``` ```java - +class Solution { + public long[] countOfPairs(int n, int x, int y) { + --x; + --y; + if (x > y) { + int temp = x; + x = y; + y = temp; + } + long[] diff = new long[n]; + for (int i = 0; i < n; ++i) { + diff[0] += 1 + 1; + ++diff[Math.min(Math.abs(i - x), Math.abs(i - y) + 1)]; + ++diff[Math.min(Math.abs(i - y), Math.abs(i - x) + 1)]; + --diff[Math.min(Math.abs(i - 0), Math.abs(i - y) + 1 + Math.abs(x - 0))]; + --diff[Math.min(Math.abs(i - (n - 1)), + Math.abs(i - x) + 1 + Math.abs(y - (n - 1)))]; + --diff[Math.max(x - i, 0) + Math.max(i - y, 0) + ((y - x) + 0) / 2]; + --diff[Math.max(x - i, 0) + Math.max(i - y, 0) + ((y - x) + 1) / 2]; + } + for (int i = 0; i + 1 < n; ++i) { + diff[i + 1] += diff[i]; + } + return diff; + } +} ``` ```cpp - +class Solution { +public: + vector countOfPairs(int n, int x, int y) { + --x, --y; + if (x > y) { + swap(x, y); + } + vector diff(n); + for (int i = 0; i < n; ++i) { + diff[0] += 1 + 1; + ++diff[min(abs(i - x), abs(i - y) + 1)]; + ++diff[min(abs(i - y), abs(i - x) + 1)]; + --diff[min(abs(i - 0), abs(i - y) + 1 + abs(x - 0))]; + --diff[min(abs(i - (n - 1)), abs(i - x) + 1 + abs(y - (n - 1)))]; + --diff[max(x - i, 0) + max(i - y, 0) + ((y - x) + 0) / 2]; + --diff[max(x - i, 0) + max(i - y, 0) + ((y - x) + 1) / 2]; + } + for (int i = 0; i + 1 < n; ++i) { + diff[i + 1] += diff[i]; + } + return diff; + } +}; ``` ```go - +func countOfPairs(n int, x int, y int) []int64 { + if x > y { + x, y = y, x + } + A := make([]int64, n) + for i := 1; i <= n; i++ { + A[0] += 2 + A[min(int64(i-1), int64(math.Abs(float64(i-y)))+int64(x))] -= 1 + A[min(int64(n-i), int64(math.Abs(float64(i-x)))+1+int64(n-y))] -= 1 + A[min(int64(math.Abs(float64(i-x))), int64(math.Abs(float64(y-i)))+1)] += 1 + A[min(int64(math.Abs(float64(i-x)))+1, int64(math.Abs(float64(y-i))))] += 1 + r := max(int64(x-i), 0) + max(int64(i-y), 0) + A[r+int64((y-x+0)/2)] -= 1 + A[r+int64((y-x+1)/2)] -= 1 + } + for i := 1; i < n; i++ { + A[i] += A[i-1] + } + + return A +} ``` diff --git a/solution/3000-3099/3017.Count the Number of Houses at a Certain Distance II/Solution.cpp b/solution/3000-3099/3017.Count the Number of Houses at a Certain Distance II/Solution.cpp new file mode 100644 index 0000000000000..52443a9fa868a --- /dev/null +++ b/solution/3000-3099/3017.Count the Number of Houses at a Certain Distance II/Solution.cpp @@ -0,0 +1,23 @@ +class Solution { +public: + vector countOfPairs(int n, int x, int y) { + --x, --y; + if (x > y) { + swap(x, y); + } + vector diff(n); + for (int i = 0; i < n; ++i) { + diff[0] += 1 + 1; + ++diff[min(abs(i - x), abs(i - y) + 1)]; + ++diff[min(abs(i - y), abs(i - x) + 1)]; + --diff[min(abs(i - 0), abs(i - y) + 1 + abs(x - 0))]; + --diff[min(abs(i - (n - 1)), abs(i - x) + 1 + abs(y - (n - 1)))]; + --diff[max(x - i, 0) + max(i - y, 0) + ((y - x) + 0) / 2]; + --diff[max(x - i, 0) + max(i - y, 0) + ((y - x) + 1) / 2]; + } + for (int i = 0; i + 1 < n; ++i) { + diff[i + 1] += diff[i]; + } + return diff; + } +}; diff --git a/solution/3000-3099/3017.Count the Number of Houses at a Certain Distance II/Solution.go b/solution/3000-3099/3017.Count the Number of Houses at a Certain Distance II/Solution.go new file mode 100644 index 0000000000000..34ecbd05b65b1 --- /dev/null +++ b/solution/3000-3099/3017.Count the Number of Houses at a Certain Distance II/Solution.go @@ -0,0 +1,21 @@ +func countOfPairs(n int, x int, y int) []int64 { + if x > y { + x, y = y, x + } + A := make([]int64, n) + for i := 1; i <= n; i++ { + A[0] += 2 + A[min(int64(i-1), int64(math.Abs(float64(i-y)))+int64(x))] -= 1 + A[min(int64(n-i), int64(math.Abs(float64(i-x)))+1+int64(n-y))] -= 1 + A[min(int64(math.Abs(float64(i-x))), int64(math.Abs(float64(y-i)))+1)] += 1 + A[min(int64(math.Abs(float64(i-x)))+1, int64(math.Abs(float64(y-i))))] += 1 + r := max(int64(x-i), 0) + max(int64(i-y), 0) + A[r+int64((y-x+0)/2)] -= 1 + A[r+int64((y-x+1)/2)] -= 1 + } + for i := 1; i < n; i++ { + A[i] += A[i-1] + } + + return A +} diff --git a/solution/3000-3099/3017.Count the Number of Houses at a Certain Distance II/Solution.java b/solution/3000-3099/3017.Count the Number of Houses at a Certain Distance II/Solution.java new file mode 100644 index 0000000000000..b6f6cb63e5da8 --- /dev/null +++ b/solution/3000-3099/3017.Count the Number of Houses at a Certain Distance II/Solution.java @@ -0,0 +1,25 @@ +class Solution { + public long[] countOfPairs(int n, int x, int y) { + --x; + --y; + if (x > y) { + int temp = x; + x = y; + y = temp; + } + long[] diff = new long[n]; + for (int i = 0; i < n; ++i) { + diff[0] += 1 + 1; + ++diff[Math.min(Math.abs(i - x), Math.abs(i - y) + 1)]; + ++diff[Math.min(Math.abs(i - y), Math.abs(i - x) + 1)]; + --diff[Math.min(Math.abs(i - 0), Math.abs(i - y) + 1 + Math.abs(x - 0))]; + --diff[Math.min(Math.abs(i - (n - 1)), Math.abs(i - x) + 1 + Math.abs(y - (n - 1)))]; + --diff[Math.max(x - i, 0) + Math.max(i - y, 0) + ((y - x) + 0) / 2]; + --diff[Math.max(x - i, 0) + Math.max(i - y, 0) + ((y - x) + 1) / 2]; + } + for (int i = 0; i + 1 < n; ++i) { + diff[i + 1] += diff[i]; + } + return diff; + } +} diff --git a/solution/3000-3099/3017.Count the Number of Houses at a Certain Distance II/Solution.py b/solution/3000-3099/3017.Count the Number of Houses at a Certain Distance II/Solution.py new file mode 100644 index 0000000000000..0e06fedbb9656 --- /dev/null +++ b/solution/3000-3099/3017.Count the Number of Houses at a Certain Distance II/Solution.py @@ -0,0 +1,42 @@ +class Solution: + def countOfPairs(self, n: int, x: int, y: int) -> List[int]: + if abs(x - y) <= 1: + return [2 * x for x in reversed(range(n))] + cycle_len = abs(x - y) + 1 + n2 = n - cycle_len + 2 + res = [2 * x for x in reversed(range(n2))] + while len(res) < n: + res.append(0) + res2 = [cycle_len * 2] * (cycle_len >> 1) + if not cycle_len & 1: + res2[-1] = cycle_len + res2[0] -= 2 + for i in range(len(res2)): + res[i] += res2[i] + if x > y: + x, y = y, x + tail1 = x - 1 + tail2 = n - y + for tail in (tail1, tail2): + if not tail: + continue + i_mx = tail + (cycle_len >> 1) + val_mx = 4 * min((cycle_len - 3) >> 1, tail) + i_mx2 = i_mx - (1 - (cycle_len & 1)) + res3 = [val_mx] * i_mx + res3[0] = 0 + res3[1] = 0 + if not cycle_len & 1: + res3[-1] = 0 + for i, j in enumerate(range(4, val_mx, 4)): + res3[i + 2] = j + res3[i_mx2 - i - 1] = j + for i in range(1, tail + 1): + res3[i] += 2 + if not cycle_len & 1: + mn = cycle_len >> 1 + for i in range(mn, mn + tail): + res3[i] += 2 + for i in range(len(res3)): + res[i] += res3[i] + return res