Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 61017ae

Browse files
feat: add solutions to lc problem: No.3017 (doocs#2259)
No.3017.Count the Number of Houses at a Certain Distance II
1 parent 1bc65ec commit 61017ae

File tree

6 files changed

+335
-8
lines changed

6 files changed

+335
-8
lines changed

‎solution/3000-3099/3017.Count the Number of Houses at a Certain Distance II/README.md‎

Lines changed: 112 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,19 +72,127 @@
7272
<!-- tabs:start -->
7373

7474
```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
76117
```
77118

78119
```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+
}
80146
```
81147

82148
```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+
};
84172
```
85173
86174
```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+
}
88196
```
89197

90198
<!-- tabs:end -->

‎solution/3000-3099/3017.Count the Number of Houses at a Certain Distance II/README_EN.md‎

Lines changed: 112 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,19 +68,127 @@
6868
<!-- tabs:start -->
6969

7070
```python
71-
71+
class Solution:
72+
def countOfPairs(self, n: int, x: int, y: int) -> List[int]:
73+
if abs(x - y) <= 1:
74+
return [2 * x for x in reversed(range(n))]
75+
cycle_len = abs(x - y) + 1
76+
n2 = n - cycle_len + 2
77+
res = [2 * x for x in reversed(range(n2))]
78+
while len(res) < n:
79+
res.append(0)
80+
res2 = [cycle_len * 2] * (cycle_len >> 1)
81+
if not cycle_len & 1:
82+
res2[-1] = cycle_len
83+
res2[0] -= 2
84+
for i in range(len(res2)):
85+
res[i] += res2[i]
86+
if x > y:
87+
x, y = y, x
88+
tail1 = x - 1
89+
tail2 = n - y
90+
for tail in (tail1, tail2):
91+
if not tail:
92+
continue
93+
i_mx = tail + (cycle_len >> 1)
94+
val_mx = 4 * min((cycle_len - 3) >> 1, tail)
95+
i_mx2 = i_mx - (1 - (cycle_len & 1))
96+
res3 = [val_mx] * i_mx
97+
res3[0] = 0
98+
res3[1] = 0
99+
if not cycle_len & 1:
100+
res3[-1] = 0
101+
for i, j in enumerate(range(4, val_mx, 4)):
102+
res3[i + 2] = j
103+
res3[i_mx2 - i - 1] = j
104+
for i in range(1, tail + 1):
105+
res3[i] += 2
106+
if not cycle_len & 1:
107+
mn = cycle_len >> 1
108+
for i in range(mn, mn + tail):
109+
res3[i] += 2
110+
for i in range(len(res3)):
111+
res[i] += res3[i]
112+
return res
72113
```
73114

74115
```java
75-
116+
class Solution {
117+
public long[] countOfPairs(int n, int x, int y) {
118+
--x;
119+
--y;
120+
if (x > y) {
121+
int temp = x;
122+
x = y;
123+
y = temp;
124+
}
125+
long[] diff = new long[n];
126+
for (int i = 0; i < n; ++i) {
127+
diff[0] += 1 + 1;
128+
++diff[Math.min(Math.abs(i - x), Math.abs(i - y) + 1)];
129+
++diff[Math.min(Math.abs(i - y), Math.abs(i - x) + 1)];
130+
--diff[Math.min(Math.abs(i - 0), Math.abs(i - y) + 1 + Math.abs(x - 0))];
131+
--diff[Math.min(Math.abs(i - (n - 1)),
132+
Math.abs(i - x) + 1 + Math.abs(y - (n - 1)))];
133+
--diff[Math.max(x - i, 0) + Math.max(i - y, 0) + ((y - x) + 0) / 2];
134+
--diff[Math.max(x - i, 0) + Math.max(i - y, 0) + ((y - x) + 1) / 2];
135+
}
136+
for (int i = 0; i + 1 < n; ++i) {
137+
diff[i + 1] += diff[i];
138+
}
139+
return diff;
140+
}
141+
}
76142
```
77143

78144
```cpp
79-
145+
class Solution {
146+
public:
147+
vector<long long> countOfPairs(int n, int x, int y) {
148+
--x, --y;
149+
if (x > y) {
150+
swap(x, y);
151+
}
152+
vector<long long> diff(n);
153+
for (int i = 0; i < n; ++i) {
154+
diff[0] += 1 + 1;
155+
++diff[min(abs(i - x), abs(i - y) + 1)];
156+
++diff[min(abs(i - y), abs(i - x) + 1)];
157+
--diff[min(abs(i - 0), abs(i - y) + 1 + abs(x - 0))];
158+
--diff[min(abs(i - (n - 1)), abs(i - x) + 1 + abs(y - (n - 1)))];
159+
--diff[max(x - i, 0) + max(i - y, 0) + ((y - x) + 0) / 2];
160+
--diff[max(x - i, 0) + max(i - y, 0) + ((y - x) + 1) / 2];
161+
}
162+
for (int i = 0; i + 1 < n; ++i) {
163+
diff[i + 1] += diff[i];
164+
}
165+
return diff;
166+
}
167+
};
80168
```
81169
82170
```go
83-
171+
func countOfPairs(n int, x int, y int) []int64 {
172+
if x > y {
173+
x, y = y, x
174+
}
175+
A := make([]int64, n)
176+
for i := 1; i <= n; i++ {
177+
A[0] += 2
178+
A[min(int64(i-1), int64(math.Abs(float64(i-y)))+int64(x))] -= 1
179+
A[min(int64(n-i), int64(math.Abs(float64(i-x)))+1+int64(n-y))] -= 1
180+
A[min(int64(math.Abs(float64(i-x))), int64(math.Abs(float64(y-i)))+1)] += 1
181+
A[min(int64(math.Abs(float64(i-x)))+1, int64(math.Abs(float64(y-i))))] += 1
182+
r := max(int64(x-i), 0) + max(int64(i-y), 0)
183+
A[r+int64((y-x+0)/2)] -= 1
184+
A[r+int64((y-x+1)/2)] -= 1
185+
}
186+
for i := 1; i < n; i++ {
187+
A[i] += A[i-1]
188+
}
189+
190+
return A
191+
}
84192
```
85193

86194
<!-- tabs:end -->
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public:
3+
vector<long long> countOfPairs(int n, int x, int y) {
4+
--x, --y;
5+
if (x > y) {
6+
swap(x, y);
7+
}
8+
vector<long long> diff(n);
9+
for (int i = 0; i < n; ++i) {
10+
diff[0] += 1 + 1;
11+
++diff[min(abs(i - x), abs(i - y) + 1)];
12+
++diff[min(abs(i - y), abs(i - x) + 1)];
13+
--diff[min(abs(i - 0), abs(i - y) + 1 + abs(x - 0))];
14+
--diff[min(abs(i - (n - 1)), abs(i - x) + 1 + abs(y - (n - 1)))];
15+
--diff[max(x - i, 0) + max(i - y, 0) + ((y - x) + 0) / 2];
16+
--diff[max(x - i, 0) + max(i - y, 0) + ((y - x) + 1) / 2];
17+
}
18+
for (int i = 0; i + 1 < n; ++i) {
19+
diff[i + 1] += diff[i];
20+
}
21+
return diff;
22+
}
23+
};
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
func countOfPairs(n int, x int, y int) []int64 {
2+
if x > y {
3+
x, y = y, x
4+
}
5+
A := make([]int64, n)
6+
for i := 1; i <= n; i++ {
7+
A[0] += 2
8+
A[min(int64(i-1), int64(math.Abs(float64(i-y)))+int64(x))] -= 1
9+
A[min(int64(n-i), int64(math.Abs(float64(i-x)))+1+int64(n-y))] -= 1
10+
A[min(int64(math.Abs(float64(i-x))), int64(math.Abs(float64(y-i)))+1)] += 1
11+
A[min(int64(math.Abs(float64(i-x)))+1, int64(math.Abs(float64(y-i))))] += 1
12+
r := max(int64(x-i), 0) + max(int64(i-y), 0)
13+
A[r+int64((y-x+0)/2)] -= 1
14+
A[r+int64((y-x+1)/2)] -= 1
15+
}
16+
for i := 1; i < n; i++ {
17+
A[i] += A[i-1]
18+
}
19+
20+
return A
21+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
public long[] countOfPairs(int n, int x, int y) {
3+
--x;
4+
--y;
5+
if (x > y) {
6+
int temp = x;
7+
x = y;
8+
y = temp;
9+
}
10+
long[] diff = new long[n];
11+
for (int i = 0; i < n; ++i) {
12+
diff[0] += 1 + 1;
13+
++diff[Math.min(Math.abs(i - x), Math.abs(i - y) + 1)];
14+
++diff[Math.min(Math.abs(i - y), Math.abs(i - x) + 1)];
15+
--diff[Math.min(Math.abs(i - 0), Math.abs(i - y) + 1 + Math.abs(x - 0))];
16+
--diff[Math.min(Math.abs(i - (n - 1)), Math.abs(i - x) + 1 + Math.abs(y - (n - 1)))];
17+
--diff[Math.max(x - i, 0) + Math.max(i - y, 0) + ((y - x) + 0) / 2];
18+
--diff[Math.max(x - i, 0) + Math.max(i - y, 0) + ((y - x) + 1) / 2];
19+
}
20+
for (int i = 0; i + 1 < n; ++i) {
21+
diff[i + 1] += diff[i];
22+
}
23+
return diff;
24+
}
25+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
class Solution:
2+
def countOfPairs(self, n: int, x: int, y: int) -> List[int]:
3+
if abs(x - y) <= 1:
4+
return [2 * x for x in reversed(range(n))]
5+
cycle_len = abs(x - y) + 1
6+
n2 = n - cycle_len + 2
7+
res = [2 * x for x in reversed(range(n2))]
8+
while len(res) < n:
9+
res.append(0)
10+
res2 = [cycle_len * 2] * (cycle_len >> 1)
11+
if not cycle_len & 1:
12+
res2[-1] = cycle_len
13+
res2[0] -= 2
14+
for i in range(len(res2)):
15+
res[i] += res2[i]
16+
if x > y:
17+
x, y = y, x
18+
tail1 = x - 1
19+
tail2 = n - y
20+
for tail in (tail1, tail2):
21+
if not tail:
22+
continue
23+
i_mx = tail + (cycle_len >> 1)
24+
val_mx = 4 * min((cycle_len - 3) >> 1, tail)
25+
i_mx2 = i_mx - (1 - (cycle_len & 1))
26+
res3 = [val_mx] * i_mx
27+
res3[0] = 0
28+
res3[1] = 0
29+
if not cycle_len & 1:
30+
res3[-1] = 0
31+
for i, j in enumerate(range(4, val_mx, 4)):
32+
res3[i + 2] = j
33+
res3[i_mx2 - i - 1] = j
34+
for i in range(1, tail + 1):
35+
res3[i] += 2
36+
if not cycle_len & 1:
37+
mn = cycle_len >> 1
38+
for i in range(mn, mn + tail):
39+
res3[i] += 2
40+
for i in range(len(res3)):
41+
res[i] += res3[i]
42+
return res

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /