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 5d33e36

Browse files
feat: add solutions to lc problem: No.3015 (doocs#2255)
No.3015.Count the Number of Houses at a Certain Distance I
1 parent 3acf5dd commit 5d33e36

File tree

7 files changed

+248
-8
lines changed

7 files changed

+248
-8
lines changed

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

Lines changed: 85 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,24 +67,105 @@
6767

6868
## 解法
6969

70-
### 方法一
70+
### 方法一:枚举
71+
72+
我们可以枚举每个点对 $(i, j),ドル那么从 $i$ 到 $j$ 的最短距离为 $min(|i - j|, |i - x| + 1 + |j - y|, |i - y| + 1 + |j - x|),ドル我们将该距离的出现次数加 2ドル,ドル因为 $(i, j)$ 和 $(j, i)$ 都是满足要求的点对。
73+
74+
时间复杂度 $O(n^2),ドル其中 $n$ 是题目给定的 $n$。忽略答案数组的空间消耗,空间复杂度 $O(1)$。
7175

7276
<!-- tabs:start -->
7377

7478
```python
75-
79+
class Solution:
80+
def countOfPairs(self, n: int, x: int, y: int) -> List[int]:
81+
x, y = x - 1, y - 1
82+
ans = [0] * n
83+
for i in range(n):
84+
for j in range(i + 1, n):
85+
a = j - i
86+
b = abs(i - x) + 1 + abs(j - y)
87+
c = abs(i - y) + 1 + abs(j - x)
88+
ans[min(a, b, c) - 1] += 2
89+
return ans
7690
```
7791

7892
```java
79-
93+
class Solution {
94+
public int[] countOfPairs(int n, int x, int y) {
95+
int[] ans = new int[n];
96+
x--;
97+
y--;
98+
for (int i = 0; i < n; ++i) {
99+
for (int j = i + 1; j < n; ++j) {
100+
int a = j - i;
101+
int b = Math.abs(i - x) + 1 + Math.abs(j - y);
102+
int c = Math.abs(i - y) + 1 + Math.abs(j - x);
103+
ans[Math.min(a, Math.min(b, c)) - 1] += 2;
104+
}
105+
}
106+
return ans;
107+
}
108+
}
80109
```
81110

82111
```cpp
83-
112+
class Solution {
113+
public:
114+
vector<int> countOfPairs(int n, int x, int y) {
115+
vector<int> ans(n);
116+
x--;
117+
y--;
118+
for (int i = 0; i < n; ++i) {
119+
for (int j = i + 1; j < n; ++j) {
120+
int a = j - i;
121+
int b = abs(x - i) + abs(y - j) + 1;
122+
int c = abs(y - i) + abs(x - j) + 1;
123+
ans[min({a, b, c}) - 1] += 2;
124+
}
125+
}
126+
return ans;
127+
}
128+
};
84129
```
85130
86131
```go
132+
func countOfPairs(n int, x int, y int) []int {
133+
ans := make([]int, n)
134+
x, y = x-1, y-1
135+
for i := 0; i < n; i++ {
136+
for j := i + 1; j < n; j++ {
137+
a := j - i
138+
b := abs(x-i) + abs(y-j) + 1
139+
c := abs(x-j) + abs(y-i) + 1
140+
ans[min(a, min(b, c))-1] += 2
141+
}
142+
}
143+
return ans
144+
}
145+
146+
func abs(x int) int {
147+
if x < 0 {
148+
return -x
149+
}
150+
return x
151+
}
152+
```
87153

154+
```ts
155+
function countOfPairs(n: number, x: number, y: number): number[] {
156+
const ans: number[] = Array(n).fill(0);
157+
x--;
158+
y--;
159+
for (let i = 0; i < n; ++i) {
160+
for (let j = i + 1; j < n; ++j) {
161+
const a = j - i;
162+
const b = Math.abs(x - i) + Math.abs(y - j) + 1;
163+
const c = Math.abs(y - i) + Math.abs(x - j) + 1;
164+
ans[Math.min(a, b, c) - 1] += 2;
165+
}
166+
}
167+
return ans;
168+
}
88169
```
89170

90171
<!-- tabs:end -->

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

Lines changed: 85 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,24 +63,105 @@
6363

6464
## Solutions
6565

66-
### Solution 1
66+
### Solution 1: Enumeration
67+
68+
We can enumerate each pair of points $(i, j)$. The shortest distance from $i$ to $j$ is $min(|i - j|, |i - x| + 1 + |j - y|, |i - y| + 1 + |j - x|)$. We add 2ドル$ to the count of this distance because both $(i, j)$ and $(j, i)$ are valid pairs of points.
69+
70+
The time complexity is $O(n^2),ドル where $n$ is the $n$ given in the problem. Ignoring the space consumption of the answer array, the space complexity is $O(1)$.
6771

6872
<!-- tabs:start -->
6973

7074
```python
71-
75+
class Solution:
76+
def countOfPairs(self, n: int, x: int, y: int) -> List[int]:
77+
x, y = x - 1, y - 1
78+
ans = [0] * n
79+
for i in range(n):
80+
for j in range(i + 1, n):
81+
a = j - i
82+
b = abs(i - x) + 1 + abs(j - y)
83+
c = abs(i - y) + 1 + abs(j - x)
84+
ans[min(a, b, c) - 1] += 2
85+
return ans
7286
```
7387

7488
```java
75-
89+
class Solution {
90+
public int[] countOfPairs(int n, int x, int y) {
91+
int[] ans = new int[n];
92+
x--;
93+
y--;
94+
for (int i = 0; i < n; ++i) {
95+
for (int j = i + 1; j < n; ++j) {
96+
int a = j - i;
97+
int b = Math.abs(i - x) + 1 + Math.abs(j - y);
98+
int c = Math.abs(i - y) + 1 + Math.abs(j - x);
99+
ans[Math.min(a, Math.min(b, c)) - 1] += 2;
100+
}
101+
}
102+
return ans;
103+
}
104+
}
76105
```
77106

78107
```cpp
79-
108+
class Solution {
109+
public:
110+
vector<int> countOfPairs(int n, int x, int y) {
111+
vector<int> ans(n);
112+
x--;
113+
y--;
114+
for (int i = 0; i < n; ++i) {
115+
for (int j = i + 1; j < n; ++j) {
116+
int a = j - i;
117+
int b = abs(x - i) + abs(y - j) + 1;
118+
int c = abs(y - i) + abs(x - j) + 1;
119+
ans[min({a, b, c}) - 1] += 2;
120+
}
121+
}
122+
return ans;
123+
}
124+
};
80125
```
81126
82127
```go
128+
func countOfPairs(n int, x int, y int) []int {
129+
ans := make([]int, n)
130+
x, y = x-1, y-1
131+
for i := 0; i < n; i++ {
132+
for j := i + 1; j < n; j++ {
133+
a := j - i
134+
b := abs(x-i) + abs(y-j) + 1
135+
c := abs(x-j) + abs(y-i) + 1
136+
ans[min(a, min(b, c))-1] += 2
137+
}
138+
}
139+
return ans
140+
}
141+
142+
func abs(x int) int {
143+
if x < 0 {
144+
return -x
145+
}
146+
return x
147+
}
148+
```
83149

150+
```ts
151+
function countOfPairs(n: number, x: number, y: number): number[] {
152+
const ans: number[] = Array(n).fill(0);
153+
x--;
154+
y--;
155+
for (let i = 0; i < n; ++i) {
156+
for (let j = i + 1; j < n; ++j) {
157+
const a = j - i;
158+
const b = Math.abs(x - i) + Math.abs(y - j) + 1;
159+
const c = Math.abs(y - i) + Math.abs(x - j) + 1;
160+
ans[Math.min(a, b, c) - 1] += 2;
161+
}
162+
}
163+
return ans;
164+
}
84165
```
85166

86167
<!-- tabs:end -->
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public:
3+
vector<int> countOfPairs(int n, int x, int y) {
4+
vector<int> ans(n);
5+
x--;
6+
y--;
7+
for (int i = 0; i < n; ++i) {
8+
for (int j = i + 1; j < n; ++j) {
9+
int a = j - i;
10+
int b = abs(x - i) + abs(y - j) + 1;
11+
int c = abs(y - i) + abs(x - j) + 1;
12+
ans[min({a, b, c}) - 1] += 2;
13+
}
14+
}
15+
return ans;
16+
}
17+
};
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
func countOfPairs(n int, x int, y int) []int {
2+
ans := make([]int, n)
3+
x, y = x-1, y-1
4+
for i := 0; i < n; i++ {
5+
for j := i + 1; j < n; j++ {
6+
a := j - i
7+
b := abs(x-i) + abs(y-j) + 1
8+
c := abs(x-j) + abs(y-i) + 1
9+
ans[min(a, min(b, c))-1] += 2
10+
}
11+
}
12+
return ans
13+
}
14+
15+
func abs(x int) int {
16+
if x < 0 {
17+
return -x
18+
}
19+
return x
20+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public int[] countOfPairs(int n, int x, int y) {
3+
int[] ans = new int[n];
4+
x--;
5+
y--;
6+
for (int i = 0; i < n; ++i) {
7+
for (int j = i + 1; j < n; ++j) {
8+
int a = j - i;
9+
int b = Math.abs(i - x) + 1 + Math.abs(j - y);
10+
int c = Math.abs(i - y) + 1 + Math.abs(j - x);
11+
ans[Math.min(a, Math.min(b, c)) - 1] += 2;
12+
}
13+
}
14+
return ans;
15+
}
16+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def countOfPairs(self, n: int, x: int, y: int) -> List[int]:
3+
x, y = x - 1, y - 1
4+
ans = [0] * n
5+
for i in range(n):
6+
for j in range(i + 1, n):
7+
a = j - i
8+
b = abs(i - x) + 1 + abs(j - y)
9+
c = abs(i - y) + 1 + abs(j - x)
10+
ans[min(a, b, c) - 1] += 2
11+
return ans
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function countOfPairs(n: number, x: number, y: number): number[] {
2+
const ans: number[] = Array(n).fill(0);
3+
x--;
4+
y--;
5+
for (let i = 0; i < n; ++i) {
6+
for (let j = i + 1; j < n; ++j) {
7+
const a = j - i;
8+
const b = Math.abs(x - i) + Math.abs(y - j) + 1;
9+
const c = Math.abs(y - i) + Math.abs(x - j) + 1;
10+
ans[Math.min(a, b, c) - 1] += 2;
11+
}
12+
}
13+
return ans;
14+
}

0 commit comments

Comments
(0)

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