From aaf0c8d2210be56a11f8ef2c16daf635f2e98a17 Mon Sep 17 00:00:00 2001 From: Sandarbh Singhal <123533242+Nothing-avil@users.noreply.github.com> Date: Wed, 24 Jan 2024 17:36:04 +0530 Subject: [PATCH 01/19] feat: add solutions to lc problem: No.3017 --- .../README.md | 133 +++++++++++++++++- 1 file changed, 129 insertions(+), 4 deletions(-) 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..a580b6fbb596f 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,144 @@ ```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 - +import ( + "fmt" + "math" +) + +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 // go left and right + 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 // i -> x -> y <- x + A[r+int64((y-x+1)/2)] -= 1 // i -> y -> x <- y + } + for i := 1; i < n; i++ { + A[i] += A[i-1] + } + return A +} + +func min(a, b int64) int64 { + if a < b { + return a + } + return b +} + +func max(a, b int64) int64 { + if a > b { + return a + } + return b +} ``` From 99ad881527f187dd9ea1c9b70d86387f2fc42b6a Mon Sep 17 00:00:00 2001 From: Sandarbh Singhal <123533242+Nothing-avil@users.noreply.github.com> Date: Wed, 24 Jan 2024 17:42:03 +0530 Subject: [PATCH 02/19] feat: add solutions to lc problem: No.3017 --- .../README_EN.md | 134 +++++++++++++++++- 1 file changed, 130 insertions(+), 4 deletions(-) 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..acd541181eda4 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,145 @@ ```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 - +import ( + "fmt" + "math" +) + +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 +} + +func min(a, b int64) int64 { + if a < b { + return a + } + return b +} + +func max(a, b int64) int64 { + if a > b { + return a + } + return b +} ``` From 7c7155db0d7684c621c375072d7016ebf30518af Mon Sep 17 00:00:00 2001 From: Sandarbh Singhal <123533242+Nothing-avil@users.noreply.github.com> Date: Wed, 24 Jan 2024 17:42:30 +0530 Subject: [PATCH 03/19] feat: add solutions to lc problem: No.3017 --- .../README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) 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 a580b6fbb596f..fa04e68ab2742 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 @@ -182,14 +182,14 @@ func countOfPairs(n int, x int, y int) []int64 { } A := make([]int64, n) for i := 1; i <= n; i++ { - A[0] += 2 // go left and right + 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 // i -> x -> y <- x - A[r+int64((y-x+1)/2)] -= 1 // i -> y -> x <- y + 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] From 571d48016769210a4cc04e828a1a757356d3ba68 Mon Sep 17 00:00:00 2001 From: Sandarbh Singhal <123533242+Nothing-avil@users.noreply.github.com> Date: Wed, 24 Jan 2024 17:43:31 +0530 Subject: [PATCH 04/19] feat: add solutions to lc problem: No.3017 --- .../Solution.cpp | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 solution/3000-3099/3017.Count the Number of Houses at a Certain Distance II/Solution.cpp 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..9d8b6d13c5aed --- /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; + } +}; From 02111c610f9617c6a3b5ebb3020e013c7b000f98 Mon Sep 17 00:00:00 2001 From: Sandarbh Singhal <123533242+Nothing-avil@users.noreply.github.com> Date: Wed, 24 Jan 2024 17:45:00 +0530 Subject: [PATCH 05/19] feat: add solutions to lc problem: No.3017 --- .../Solution.py | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 solution/3000-3099/3017.Count the Number of Houses at a Certain Distance II/Solution.py 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..730f74c166e72 --- /dev/null +++ b/solution/3000-3099/3017.Count the Number of Houses at a Certain Distance II/Solution.py @@ -0,0 +1,41 @@ +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 From 4e5fb83d98b4a149cabfb6cfda8822e27b6982f0 Mon Sep 17 00:00:00 2001 From: Sandarbh Singhal <123533242+Nothing-avil@users.noreply.github.com> Date: Wed, 24 Jan 2024 17:45:41 +0530 Subject: [PATCH 06/19] feat: add solutions to lc problem: No.3017 --- .../Solution.java | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 solution/3000-3099/3017.Count the Number of Houses at a Certain Distance II/Solution.java 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..682a67f129795 --- /dev/null +++ b/solution/3000-3099/3017.Count the Number of Houses at a Certain Distance II/Solution.java @@ -0,0 +1,26 @@ +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; + } +} From cb73474c060f65e9467423918f1483dab2e8bf98 Mon Sep 17 00:00:00 2001 From: Sandarbh Singhal <123533242+Nothing-avil@users.noreply.github.com> Date: Wed, 24 Jan 2024 17:47:18 +0530 Subject: [PATCH 07/19] feat: add solutions to lc problem: No.3017 --- .../Solution.go | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 solution/3000-3099/3017.Count the Number of Houses at a Certain Distance II/Solution.go 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..5e48f477f10cb --- /dev/null +++ b/solution/3000-3099/3017.Count the Number of Houses at a Certain Distance II/Solution.go @@ -0,0 +1,39 @@ +import ( + "fmt" + "math" +) +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 +} + +func min(a, b int64) int64 { + if a < b { + return a + } + return b +} + +func max(a, b int64) int64 { + if a > b { + return a + } + return b +} From 9b50675df4a59d1ba943d1e4f29cb4824ed11c26 Mon Sep 17 00:00:00 2001 From: Sandarbh Singhal <123533242+Nothing-avil@users.noreply.github.com> Date: Wed, 24 Jan 2024 18:07:33 +0530 Subject: [PATCH 08/19] feat: add solutions to lc problem: No.3017 --- .../Solution.cpp | 38 +++++++++---------- 1 file changed, 19 insertions(+), 19 deletions(-) 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 index 9d8b6d13c5aed..52443a9fa868a 100644 --- 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 @@ -1,23 +1,23 @@ class Solution { public: - vector countOfPairs(int n, int x, int y) { - --x, --y; - if (x > y) { - swap(x, y); + 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; } - 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; - } }; From 3a8732ee6afcd1af3d94349ab1fdd4906a512dc1 Mon Sep 17 00:00:00 2001 From: Sandarbh Singhal <123533242+Nothing-avil@users.noreply.github.com> Date: Wed, 24 Jan 2024 18:09:28 +0530 Subject: [PATCH 09/19] feat: add solutions to lc problem: No.3017 --- .../Solution.java | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) 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 index 682a67f129795..8acefd81d90d0 100644 --- 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 @@ -3,23 +3,23 @@ public long[] countOfPairs(int n, int x, int y) { --x; --y; if (x > y) { - int temp = x; - x = y; - y = temp; + 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]; + 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]; + diff[i + 1] += diff[i]; } return diff; } From c676f1d603365aa023728954f9b61df18b7cb2a9 Mon Sep 17 00:00:00 2001 From: Sandarbh Singhal <123533242+Nothing-avil@users.noreply.github.com> Date: Wed, 24 Jan 2024 18:11:53 +0530 Subject: [PATCH 10/19] feat: add solutions to lc problem: No.3017 --- .../Solution.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) 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 index 8acefd81d90d0..b6f6cb63e5da8 100644 --- 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 @@ -13,8 +13,7 @@ public long[] countOfPairs(int n, int x, int y) { ++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.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]; } From 0449cce218665b0ad294586c04f07b5a04ad7b7e Mon Sep 17 00:00:00 2001 From: Sandarbh Singhal <123533242+Nothing-avil@users.noreply.github.com> Date: Wed, 24 Jan 2024 18:17:52 +0530 Subject: [PATCH 11/19] feat: add solutions to lc problem: No.3017 --- .../Solution.py | 3 +++ 1 file changed, 3 insertions(+) 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 index 730f74c166e72..b4098764ac955 100644 --- 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 @@ -1,5 +1,6 @@ class Solution: def countOfPairs(self, n: int, x: int, y: int) -> List[int]: + #print() if abs(x - y) <= 1: return [2 * x for x in reversed(range(n))] cycle_len = abs(x - y) + 1 @@ -11,8 +12,10 @@ def countOfPairs(self, n: int, x: int, y: int) -> List[int]: 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 From d39842075064e20d7c39c2ef1ac4047013640f85 Mon Sep 17 00:00:00 2001 From: Sandarbh Singhal <123533242+Nothing-avil@users.noreply.github.com> Date: Wed, 24 Jan 2024 18:27:39 +0530 Subject: [PATCH 12/19] feat: add solutions to lc problem: No.3017 --- .../Solution.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) 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 index b4098764ac955..fd6c264e888e3 100644 --- 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 @@ -1,6 +1,7 @@ class Solution: def countOfPairs(self, n: int, x: int, y: int) -> List[int]: - #print() + # print() + if abs(x - y) <= 1: return [2 * x for x in reversed(range(n))] cycle_len = abs(x - y) + 1 @@ -12,17 +13,17 @@ def countOfPairs(self, n: int, x: int, y: int) -> List[int]: 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)) + 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 @@ -36,7 +37,7 @@ def countOfPairs(self, n: int, x: int, y: int) -> List[int]: for i in range(1, tail + 1): res3[i] += 2 if not cycle_len & 1: - mn = (cycle_len >> 1) + mn = cycle_len >> 1 for i in range(mn, mn + tail): res3[i] += 2 for i in range(len(res3)): From 8bfc2675ce06fd6ea3b5070b458a8f40f4d3adb9 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Thu, 25 Jan 2024 08:23:50 +0800 Subject: [PATCH 13/19] Update Solution.py --- .../Solution.py | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) 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 index fd6c264e888e3..730f74c166e72 100644 --- 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 @@ -1,7 +1,5 @@ class Solution: def countOfPairs(self, n: int, x: int, y: int) -> List[int]: - # print() - if abs(x - y) <= 1: return [2 * x for x in reversed(range(n))] cycle_len = abs(x - y) + 1 @@ -13,7 +11,6 @@ def countOfPairs(self, n: int, x: int, y: int) -> List[int]: 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: @@ -21,9 +18,8 @@ def countOfPairs(self, n: int, x: int, y: int) -> List[int]: tail1 = x - 1 tail2 = n - y for tail in (tail1, tail2): - if not tail: - continue - i_mx = tail + (cycle_len >> 1) + 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 @@ -37,7 +33,7 @@ def countOfPairs(self, n: int, x: int, y: int) -> List[int]: for i in range(1, tail + 1): res3[i] += 2 if not cycle_len & 1: - mn = cycle_len >> 1 + mn = (cycle_len >> 1) for i in range(mn, mn + tail): res3[i] += 2 for i in range(len(res3)): From 2afad64fed07c3bbf936d77f3bdc286e73cc61fb Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Thu, 25 Jan 2024 08:27:01 +0800 Subject: [PATCH 14/19] Update Solution.go --- .../Solution.go | 18 ------------------ 1 file changed, 18 deletions(-) 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 index 5e48f477f10cb..34ecbd05b65b1 100644 --- 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 @@ -1,7 +1,3 @@ -import ( - "fmt" - "math" -) func countOfPairs(n int, x int, y int) []int64 { if x > y { x, y = y, x @@ -23,17 +19,3 @@ func countOfPairs(n int, x int, y int) []int64 { return A } - -func min(a, b int64) int64 { - if a < b { - return a - } - return b -} - -func max(a, b int64) int64 { - if a > b { - return a - } - return b -} From 2e72815f79241e7732b4ef45c213ee00264f9b21 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Thu, 25 Jan 2024 08:27:27 +0800 Subject: [PATCH 15/19] Update README.md --- .../README.md | 20 +------------------ 1 file changed, 1 insertion(+), 19 deletions(-) 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 fa04e68ab2742..4fd4698ba9a12 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 @@ -171,11 +171,6 @@ public: ``` ```go -import ( - "fmt" - "math" -) - func countOfPairs(n int, x int, y int) []int64 { if x > y { x, y = y, x @@ -194,21 +189,8 @@ func countOfPairs(n int, x int, y int) []int64 { for i := 1; i < n; i++ { A[i] += A[i-1] } - return A -} - -func min(a, b int64) int64 { - if a < b { - return a - } - return b -} -func max(a, b int64) int64 { - if a > b { - return a - } - return b + return A } ``` From c6b84294796dc3389c27021ff0005580c79b8e9f Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Thu, 25 Jan 2024 08:27:52 +0800 Subject: [PATCH 16/19] Update README_EN.md --- .../README_EN.md | 19 ------------------- 1 file changed, 19 deletions(-) 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 acd541181eda4..18656f2c9e551 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 @@ -167,11 +167,6 @@ public: ``` ```go -import ( - "fmt" - "math" -) - func countOfPairs(n int, x int, y int) []int64 { if x > y { x, y = y, x @@ -193,20 +188,6 @@ func countOfPairs(n int, x int, y int) []int64 { return A } - -func min(a, b int64) int64 { - if a < b { - return a - } - return b -} - -func max(a, b int64) int64 { - if a > b { - return a - } - return b -} ``` From f881f462f9ff639f905bae6f9618e032379b724c Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Thu, 25 Jan 2024 08:30:05 +0800 Subject: [PATCH 17/19] Update README.md --- .../README.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) 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 4fd4698ba9a12..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 @@ -92,8 +92,9 @@ class Solution: tail1 = x - 1 tail2 = n - y for tail in (tail1, tail2): - if not tail: continue - i_mx = (tail + (cycle_len >> 1)) + 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 @@ -107,7 +108,7 @@ class Solution: for i in range(1, tail + 1): res3[i] += 2 if not cycle_len & 1: - mn = (cycle_len >> 1) + mn = cycle_len >> 1 for i in range(mn, mn + tail): res3[i] += 2 for i in range(len(res3)): From 4f6e88c65893fd9a1996657af9f24dedeb552434 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Thu, 25 Jan 2024 08:30:20 +0800 Subject: [PATCH 18/19] Update README_EN.md --- .../README_EN.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) 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 18656f2c9e551..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 @@ -88,8 +88,9 @@ class Solution: tail1 = x - 1 tail2 = n - y for tail in (tail1, tail2): - if not tail: continue - i_mx = (tail + (cycle_len >> 1)) + 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 @@ -103,7 +104,7 @@ class Solution: for i in range(1, tail + 1): res3[i] += 2 if not cycle_len & 1: - mn = (cycle_len >> 1) + mn = cycle_len >> 1 for i in range(mn, mn + tail): res3[i] += 2 for i in range(len(res3)): From f6f1e9ba555fdb4376695768955d2efc658b07a5 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Thu, 25 Jan 2024 08:30:34 +0800 Subject: [PATCH 19/19] Update Solution.py --- .../Solution.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) 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 index 730f74c166e72..0e06fedbb9656 100644 --- 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 @@ -18,8 +18,9 @@ def countOfPairs(self, n: int, x: int, y: int) -> List[int]: tail1 = x - 1 tail2 = n - y for tail in (tail1, tail2): - if not tail: continue - i_mx = (tail + (cycle_len >> 1)) + 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 @@ -33,7 +34,7 @@ def countOfPairs(self, n: int, x: int, y: int) -> List[int]: for i in range(1, tail + 1): res3[i] += 2 if not cycle_len & 1: - mn = (cycle_len >> 1) + mn = cycle_len >> 1 for i in range(mn, mn + tail): res3[i] += 2 for i in range(len(res3)):