Skip to content
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 @@ -78,89 +78,13 @@ tags:

<!-- solution:start -->

### 方法一:暴力枚举
### 方法一:贪心

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

<!-- tabs:start -->

#### Python3

```python
class Solution:
def maxDistance(self, colors: List[int]) -> int:
ans, n = 0, len(colors)
for i in range(n):
for j in range(i + 1, n):
if colors[i] != colors[j]:
ans = max(ans, abs(i - j))
return ans
```

#### Java

```java
class Solution {
public int maxDistance(int[] colors) {
int ans = 0, n = colors.length;
for (int i = 0; i < n; ++i) {
for (int j = i + 1; j < n; ++j) {
if (colors[i] != colors[j]) {
ans = Math.max(ans, Math.abs(i - j));
}
}
}
return ans;
}
}
```

#### C++
如果第一栋房子和最后一栋房子颜色相同,那么我们可以从左向右找到第一栋颜色不同的房子,记为 $i$,从右向左找到第一栋颜色不同的房子,记为 $j$,那么最大的距离就是 $\max(n - i - 1, j)$。

```cpp
class Solution {
public:
int maxDistance(vector<int>& colors) {
int ans = 0, n = colors.size();
for (int i = 0; i < n; ++i)
for (int j = i + 1; j < n; ++j)
if (colors[i] != colors[j])
ans = max(ans, abs(i - j));
return ans;
}
};
```

#### Go

```go
func maxDistance(colors []int) int {
ans, n := 0, len(colors)
for i := 0; i < n; i++ {
for j := i + 1; j < n; j++ {
if colors[i] != colors[j] {
ans = max(ans, abs(i-j))
}
}
}
return ans
}

func abs(x int) int {
if x >= 0 {
return x
}
return -x
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- solution:start -->

### 方法二:贪心
时间复杂度 $O(n)$,其中 $n$ 是房子的数量。空间复杂度 $O(1)$。

<!-- tabs:start -->

Expand Down Expand Up @@ -189,11 +113,13 @@ class Solution {
if (colors[0] != colors[n - 1]) {
return n - 1;
}
int i = 0, j = n - 1;
while (colors[++i] == colors[0])
;
while (colors[--j] == colors[0])
;
int i = 1, j = n - 2;
while (colors[i] == colors[0]) {
++i;
}
while (colors[j] == colors[0]) {
--j;
}
return Math.max(n - i - 1, j);
}
}
Expand All @@ -206,12 +132,16 @@ class Solution {
public:
int maxDistance(vector<int>& colors) {
int n = colors.size();
if (colors[0] != colors[n - 1]) return n - 1;
int i = 0, j = n;
while (colors[++i] == colors[0])
;
while (colors[--j] == colors[0])
;
if (colors[0] != colors[n - 1]) {
return n - 1;
}
int i = 1, j = n - 2;
while (colors[i] == colors[0]) {
++i;
}
while (colors[j] == colors[0]) {
--j;
}
return max(n - i - 1, j);
}
};
Expand All @@ -236,6 +166,47 @@ func maxDistance(colors []int) int {
}
```

#### TypeScript

```ts
function maxDistance(colors: number[]): number {
const n = colors.length;
if (colors[0] !== colors[n - 1]) {
return n - 1;
}
let [i, j] = [1, n - 2];
while (colors[i] === colors[0]) {
i++;
}
while (colors[j] === colors[0]) {
j--;
}
return Math.max(n - i - 1, j);
};
```

#### Rust

```rust
impl Solution {
pub fn max_distance(colors: Vec<i32>) -> i32 {
let n = colors.len();
if colors[0] != colors[n - 1] {
return (n - 1) as i32;
}
let mut i = 1;
while colors[i] == colors[0] {
i += 1;
}
let mut j = n - 2;
while colors[j] == colors[0] {
j -= 1;
}
std::cmp::max((n - i - 1) as i32, j as i32)
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,87 +72,13 @@ House 0 has color 0, and house 1 has color 1. The distance between them is abs(0

<!-- solution:start -->

### Solution 1
### Solution 1: Greedy

<!-- tabs:start -->

#### Python3

```python
class Solution:
def maxDistance(self, colors: List[int]) -> int:
ans, n = 0, len(colors)
for i in range(n):
for j in range(i + 1, n):
if colors[i] != colors[j]:
ans = max(ans, abs(i - j))
return ans
```

#### Java

```java
class Solution {
public int maxDistance(int[] colors) {
int ans = 0, n = colors.length;
for (int i = 0; i < n; ++i) {
for (int j = i + 1; j < n; ++j) {
if (colors[i] != colors[j]) {
ans = Math.max(ans, Math.abs(i - j));
}
}
}
return ans;
}
}
```

#### C++

```cpp
class Solution {
public:
int maxDistance(vector<int>& colors) {
int ans = 0, n = colors.size();
for (int i = 0; i < n; ++i)
for (int j = i + 1; j < n; ++j)
if (colors[i] != colors[j])
ans = max(ans, abs(i - j));
return ans;
}
};
```

#### Go

```go
func maxDistance(colors []int) int {
ans, n := 0, len(colors)
for i := 0; i < n; i++ {
for j := i + 1; j < n; j++ {
if colors[i] != colors[j] {
ans = max(ans, abs(i-j))
}
}
}
return ans
}
We can observe that if the first and last houses have different colors, the maximum distance is $n - 1$.

func abs(x int) int {
if x >= 0 {
return x
}
return -x
}
```
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)$.

<!-- tabs:end -->

<!-- solution:end -->

<!-- solution:start -->

### Solution 2
The time complexity is $O(n)$, where $n$ is the number of houses. The space complexity is $O(1)$.

<!-- tabs:start -->

Expand Down Expand Up @@ -181,11 +107,13 @@ class Solution {
if (colors[0] != colors[n - 1]) {
return n - 1;
}
int i = 0, j = n - 1;
while (colors[++i] == colors[0])
;
while (colors[--j] == colors[0])
;
int i = 1, j = n - 2;
while (colors[i] == colors[0]) {
++i;
}
while (colors[j] == colors[0]) {
--j;
}
return Math.max(n - i - 1, j);
}
}
Expand All @@ -198,12 +126,16 @@ class Solution {
public:
int maxDistance(vector<int>& colors) {
int n = colors.size();
if (colors[0] != colors[n - 1]) return n - 1;
int i = 0, j = n;
while (colors[++i] == colors[0])
;
while (colors[--j] == colors[0])
;
if (colors[0] != colors[n - 1]) {
return n - 1;
}
int i = 1, j = n - 2;
while (colors[i] == colors[0]) {
++i;
}
while (colors[j] == colors[0]) {
--j;
}
return max(n - i - 1, j);
}
};
Expand All @@ -228,6 +160,47 @@ func maxDistance(colors []int) int {
}
```

#### TypeScript

```ts
function maxDistance(colors: number[]): number {
const n = colors.length;
if (colors[0] !== colors[n - 1]) {
return n - 1;
}
let [i, j] = [1, n - 2];
while (colors[i] === colors[0]) {
i++;
}
while (colors[j] === colors[0]) {
j--;
}
return Math.max(n - i - 1, j);
};
```

#### Rust

```rust
impl Solution {
pub fn max_distance(colors: Vec<i32>) -> i32 {
let n = colors.len();
if colors[0] != colors[n - 1] {
return (n - 1) as i32;
}
let mut i = 1;
while colors[i] == colors[0] {
i += 1;
}
let mut j = n - 2;
while colors[j] == colors[0] {
j -= 1;
}
std::cmp::max((n - i - 1) as i32, j as i32)
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
class Solution {
public:
int maxDistance(vector<int>& colors) {
int ans = 0, n = colors.size();
for (int i = 0; i < n; ++i)
for (int j = i + 1; j < n; ++j)
if (colors[i] != colors[j])
ans = max(ans, abs(i - j));
return ans;
int n = colors.size();
if (colors[0] != colors[n - 1]) {
return n - 1;
}
int i = 1, j = n - 2;
while (colors[i] == colors[0]) {
++i;
}
while (colors[j] == colors[0]) {
--j;
}
return max(n - i - 1, j);
}
};
};
Loading
Loading