Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

No.3015.Count the Number of Houses at a Certain Distance II #2259

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 19 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -72,19 +72,127 @@
<!-- tabs:start -->

```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<long long> countOfPairs(int n, int x, int y) {
--x, --y;
if (x > y) {
swap(x, y);
}
vector<long long> 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
}
```

<!-- tabs:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,19 +68,127 @@
<!-- tabs:start -->

```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<long long> countOfPairs(int n, int x, int y) {
--x, --y;
if (x > y) {
swap(x, y);
}
vector<long long> 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
}
```

<!-- tabs:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class Solution {
public:
vector<long long> countOfPairs(int n, int x, int y) {
--x, --y;
if (x > y) {
swap(x, y);
}
vector<long long> 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;
}
};
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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