Skip to content

Commit 607b975

Browse files
committed
feat: add solutions for lc No.2078
1 parent 616280b commit 607b975

File tree

12 files changed

+200
-269
lines changed

12 files changed

+200
-269
lines changed

solution/2000-2099/2078.Two Furthest Houses With Different Colors/README.md

Lines changed: 62 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -78,89 +78,13 @@ tags:
7878

7979
<!-- solution:start -->
8080

81-
### 方法一:暴力枚举
81+
### 方法一:贪心
8282

83-
时间复杂度 $O(n^2)$。
83+
我们可以发现,如果第一栋房子和最后一栋房子颜色不同,那么它们之间的距离就是最大的距离,即 $n - 1$。
8484

85-
<!-- tabs:start -->
86-
87-
#### Python3
88-
89-
```python
90-
class Solution:
91-
def maxDistance(self, colors: List[int]) -> int:
92-
ans, n = 0, len(colors)
93-
for i in range(n):
94-
for j in range(i + 1, n):
95-
if colors[i] != colors[j]:
96-
ans = max(ans, abs(i - j))
97-
return ans
98-
```
99-
100-
#### Java
101-
102-
```java
103-
class Solution {
104-
public int maxDistance(int[] colors) {
105-
int ans = 0, n = colors.length;
106-
for (int i = 0; i < n; ++i) {
107-
for (int j = i + 1; j < n; ++j) {
108-
if (colors[i] != colors[j]) {
109-
ans = Math.max(ans, Math.abs(i - j));
110-
}
111-
}
112-
}
113-
return ans;
114-
}
115-
}
116-
```
117-
118-
#### C++
85+
如果第一栋房子和最后一栋房子颜色相同,那么我们可以从左向右找到第一栋颜色不同的房子,记为 $i$,从右向左找到第一栋颜色不同的房子,记为 $j$,那么最大的距离就是 $\max(n - i - 1, j)$。
11986

120-
```cpp
121-
class Solution {
122-
public:
123-
int maxDistance(vector<int>& colors) {
124-
int ans = 0, n = colors.size();
125-
for (int i = 0; i < n; ++i)
126-
for (int j = i + 1; j < n; ++j)
127-
if (colors[i] != colors[j])
128-
ans = max(ans, abs(i - j));
129-
return ans;
130-
}
131-
};
132-
```
133-
134-
#### Go
135-
136-
```go
137-
func maxDistance(colors []int) int {
138-
ans, n := 0, len(colors)
139-
for i := 0; i < n; i++ {
140-
for j := i + 1; j < n; j++ {
141-
if colors[i] != colors[j] {
142-
ans = max(ans, abs(i-j))
143-
}
144-
}
145-
}
146-
return ans
147-
}
148-
149-
func abs(x int) int {
150-
if x >= 0 {
151-
return x
152-
}
153-
return -x
154-
}
155-
```
156-
157-
<!-- tabs:end -->
158-
159-
<!-- solution:end -->
160-
161-
<!-- solution:start -->
162-
163-
### 方法二:贪心
87+
时间复杂度 $O(n)$,其中 $n$ 是房子的数量。空间复杂度 $O(1)$。
16488

16589
<!-- tabs:start -->
16690

@@ -189,11 +113,13 @@ class Solution {
189113
if (colors[0] != colors[n - 1]) {
190114
return n - 1;
191115
}
192-
int i = 0, j = n - 1;
193-
while (colors[++i] == colors[0])
194-
;
195-
while (colors[--j] == colors[0])
196-
;
116+
int i = 1, j = n - 2;
117+
while (colors[i] == colors[0]) {
118+
++i;
119+
}
120+
while (colors[j] == colors[0]) {
121+
--j;
122+
}
197123
return Math.max(n - i - 1, j);
198124
}
199125
}
@@ -206,12 +132,16 @@ class Solution {
206132
public:
207133
int maxDistance(vector<int>& colors) {
208134
int n = colors.size();
209-
if (colors[0] != colors[n - 1]) return n - 1;
210-
int i = 0, j = n;
211-
while (colors[++i] == colors[0])
212-
;
213-
while (colors[--j] == colors[0])
214-
;
135+
if (colors[0] != colors[n - 1]) {
136+
return n - 1;
137+
}
138+
int i = 1, j = n - 2;
139+
while (colors[i] == colors[0]) {
140+
++i;
141+
}
142+
while (colors[j] == colors[0]) {
143+
--j;
144+
}
215145
return max(n - i - 1, j);
216146
}
217147
};
@@ -236,6 +166,47 @@ func maxDistance(colors []int) int {
236166
}
237167
```
238168

169+
#### TypeScript
170+
171+
```ts
172+
function maxDistance(colors: number[]): number {
173+
const n = colors.length;
174+
if (colors[0] !== colors[n - 1]) {
175+
return n - 1;
176+
}
177+
let [i, j] = [1, n - 2];
178+
while (colors[i] === colors[0]) {
179+
i++;
180+
}
181+
while (colors[j] === colors[0]) {
182+
j--;
183+
}
184+
return Math.max(n - i - 1, j);
185+
};
186+
```
187+
188+
#### Rust
189+
190+
```rust
191+
impl Solution {
192+
pub fn max_distance(colors: Vec<i32>) -> i32 {
193+
let n = colors.len();
194+
if colors[0] != colors[n - 1] {
195+
return (n - 1) as i32;
196+
}
197+
let mut i = 1;
198+
while colors[i] == colors[0] {
199+
i += 1;
200+
}
201+
let mut j = n - 2;
202+
while colors[j] == colors[0] {
203+
j -= 1;
204+
}
205+
std::cmp::max((n - i - 1) as i32, j as i32)
206+
}
207+
}
208+
```
209+
239210
<!-- tabs:end -->
240211

241212
<!-- solution:end -->

solution/2000-2099/2078.Two Furthest Houses With Different Colors/README_EN.md

Lines changed: 62 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -72,87 +72,13 @@ House 0 has color 0, and house 1 has color 1. The distance between them is abs(0
7272

7373
<!-- solution:start -->
7474

75-
### Solution 1
75+
### Solution 1: Greedy
7676

77-
<!-- tabs:start -->
78-
79-
#### Python3
80-
81-
```python
82-
class Solution:
83-
def maxDistance(self, colors: List[int]) -> int:
84-
ans, n = 0, len(colors)
85-
for i in range(n):
86-
for j in range(i + 1, n):
87-
if colors[i] != colors[j]:
88-
ans = max(ans, abs(i - j))
89-
return ans
90-
```
91-
92-
#### Java
93-
94-
```java
95-
class Solution {
96-
public int maxDistance(int[] colors) {
97-
int ans = 0, n = colors.length;
98-
for (int i = 0; i < n; ++i) {
99-
for (int j = i + 1; j < n; ++j) {
100-
if (colors[i] != colors[j]) {
101-
ans = Math.max(ans, Math.abs(i - j));
102-
}
103-
}
104-
}
105-
return ans;
106-
}
107-
}
108-
```
109-
110-
#### C++
111-
112-
```cpp
113-
class Solution {
114-
public:
115-
int maxDistance(vector<int>& colors) {
116-
int ans = 0, n = colors.size();
117-
for (int i = 0; i < n; ++i)
118-
for (int j = i + 1; j < n; ++j)
119-
if (colors[i] != colors[j])
120-
ans = max(ans, abs(i - j));
121-
return ans;
122-
}
123-
};
124-
```
125-
126-
#### Go
127-
128-
```go
129-
func maxDistance(colors []int) int {
130-
ans, n := 0, len(colors)
131-
for i := 0; i < n; i++ {
132-
for j := i + 1; j < n; j++ {
133-
if colors[i] != colors[j] {
134-
ans = max(ans, abs(i-j))
135-
}
136-
}
137-
}
138-
return ans
139-
}
77+
We can observe that if the first and last houses have different colors, the maximum distance is $n - 1$.
14078

141-
func abs(x int) int {
142-
if x >= 0 {
143-
return x
144-
}
145-
return -x
146-
}
147-
```
79+
If the first and last houses have the same color, we can scan from the left to find the first house with a different color (let its index be $i$), and scan from the right to find the first house with a different color (let its index be $j$). The maximum distance is then $\max(n - i - 1, j)$.
14880

149-
<!-- tabs:end -->
150-
151-
<!-- solution:end -->
152-
153-
<!-- solution:start -->
154-
155-
### Solution 2
81+
The time complexity is $O(n)$, where $n$ is the number of houses. The space complexity is $O(1)$.
15682

15783
<!-- tabs:start -->
15884

@@ -181,11 +107,13 @@ class Solution {
181107
if (colors[0] != colors[n - 1]) {
182108
return n - 1;
183109
}
184-
int i = 0, j = n - 1;
185-
while (colors[++i] == colors[0])
186-
;
187-
while (colors[--j] == colors[0])
188-
;
110+
int i = 1, j = n - 2;
111+
while (colors[i] == colors[0]) {
112+
++i;
113+
}
114+
while (colors[j] == colors[0]) {
115+
--j;
116+
}
189117
return Math.max(n - i - 1, j);
190118
}
191119
}
@@ -198,12 +126,16 @@ class Solution {
198126
public:
199127
int maxDistance(vector<int>& colors) {
200128
int n = colors.size();
201-
if (colors[0] != colors[n - 1]) return n - 1;
202-
int i = 0, j = n;
203-
while (colors[++i] == colors[0])
204-
;
205-
while (colors[--j] == colors[0])
206-
;
129+
if (colors[0] != colors[n - 1]) {
130+
return n - 1;
131+
}
132+
int i = 1, j = n - 2;
133+
while (colors[i] == colors[0]) {
134+
++i;
135+
}
136+
while (colors[j] == colors[0]) {
137+
--j;
138+
}
207139
return max(n - i - 1, j);
208140
}
209141
};
@@ -228,6 +160,47 @@ func maxDistance(colors []int) int {
228160
}
229161
```
230162

163+
#### TypeScript
164+
165+
```ts
166+
function maxDistance(colors: number[]): number {
167+
const n = colors.length;
168+
if (colors[0] !== colors[n - 1]) {
169+
return n - 1;
170+
}
171+
let [i, j] = [1, n - 2];
172+
while (colors[i] === colors[0]) {
173+
i++;
174+
}
175+
while (colors[j] === colors[0]) {
176+
j--;
177+
}
178+
return Math.max(n - i - 1, j);
179+
};
180+
```
181+
182+
#### Rust
183+
184+
```rust
185+
impl Solution {
186+
pub fn max_distance(colors: Vec<i32>) -> i32 {
187+
let n = colors.len();
188+
if colors[0] != colors[n - 1] {
189+
return (n - 1) as i32;
190+
}
191+
let mut i = 1;
192+
while colors[i] == colors[0] {
193+
i += 1;
194+
}
195+
let mut j = n - 2;
196+
while colors[j] == colors[0] {
197+
j -= 1;
198+
}
199+
std::cmp::max((n - i - 1) as i32, j as i32)
200+
}
201+
}
202+
```
203+
231204
<!-- tabs:end -->
232205

233206
<!-- solution:end -->
Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
class Solution {
22
public:
33
int maxDistance(vector<int>& colors) {
4-
int ans = 0, n = colors.size();
5-
for (int i = 0; i < n; ++i)
6-
for (int j = i + 1; j < n; ++j)
7-
if (colors[i] != colors[j])
8-
ans = max(ans, abs(i - j));
9-
return ans;
4+
int n = colors.size();
5+
if (colors[0] != colors[n - 1]) {
6+
return n - 1;
7+
}
8+
int i = 1, j = n - 2;
9+
while (colors[i] == colors[0]) {
10+
++i;
11+
}
12+
while (colors[j] == colors[0]) {
13+
--j;
14+
}
15+
return max(n - i - 1, j);
1016
}
11-
};
17+
};

0 commit comments

Comments
 (0)