Skip to content

Commit 79c38cb

Browse files
committed
feat: add solutions to lc problem: No.3706
1 parent 6f855e0 commit 79c38cb

File tree

7 files changed

+223
-8
lines changed

7 files changed

+223
-8
lines changed

solution/3700-3799/3706.Maximum Distance Between Unequal Words in Array II/README.md

Lines changed: 78 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,32 +77,106 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3700-3799/3706.Ma
7777

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

80-
### 方法一
80+
### 方法一:一次遍历
81+
82+
我们可以发现,最大距离的两个单词中至少有一个单词在数组的两端(即下标为 $0$ 或 $n - 1$)。否则,假设最大距离的两个单词分别在下标 $i$ 和 $j$ 处,即 $0 < i < j < n - 1$,那么单词 $\textit{words}[0]$ 和 $\textit{words}[j]$ 相同,而单词 $\textit{words}[n - 1]$ 和 $\textit{words}[i]$ 也相同(否则距离会更大),因此单词 $\textit{words}[0]$ 和 $\textit{words}[n - 1]$ 不同,且它们的距离 $n - 1 - 0 + 1 = n$ 一定大于 $j - i + 1$,与假设矛盾。因此,最大距离的两个单词中至少有一个单词在数组的两端。
83+
84+
所以,我们只需要遍历数组,计算每个单词与数组两端单词的距离,并更新最大距离。
85+
86+
时间复杂度 $O(n)$,其中 $n$ 是数组 $\textit{words}$ 的长度。空间复杂度 $O(1)$。
8187

8288
<!-- tabs:start -->
8389

8490
#### Python3
8591

8692
```python
87-
93+
class Solution:
94+
def maxDistance(self, words: List[str]) -> int:
95+
n = len(words)
96+
ans = 0
97+
for i in range(n):
98+
if words[i] != words[0]:
99+
ans = max(ans, i + 1)
100+
if words[i] != words[-1]:
101+
ans = max(ans, n - i)
102+
return ans
88103
```
89104

90105
#### Java
91106

92107
```java
93-
108+
class Solution {
109+
public int maxDistance(String[] words) {
110+
int n = words.length;
111+
int ans = 0;
112+
for (int i = 0; i < n; ++i) {
113+
if (!words[i].equals(words[0])) {
114+
ans = Math.max(ans, i + 1);
115+
}
116+
if (!words[i].equals(words[n - 1])) {
117+
ans = Math.max(ans, n - i);
118+
}
119+
}
120+
return ans;
121+
}
122+
}
94123
```
95124

96125
#### C++
97126

98127
```cpp
99-
128+
class Solution {
129+
public:
130+
int maxDistance(vector<string>& words) {
131+
int n = words.size();
132+
int ans = 0;
133+
for (int i = 0; i < n; ++i) {
134+
if (words[i] != words[0]) {
135+
ans = max(ans, i + 1);
136+
}
137+
if (words[i] != words[n - 1]) {
138+
ans = max(ans, n - i);
139+
}
140+
}
141+
return ans;
142+
}
143+
};
100144
```
101145
102146
#### Go
103147
104148
```go
149+
func maxDistance(words []string) int {
150+
n := len(words)
151+
ans := 0
152+
for i := 0; i < n; i++ {
153+
if words[i] != words[0] {
154+
ans = max(ans, i+1)
155+
}
156+
if words[i] != words[n-1] {
157+
ans = max(ans, n-i)
158+
}
159+
}
160+
return ans
161+
}
162+
```
105163

164+
#### TypeScript
165+
166+
```ts
167+
function maxDistance(words: string[]): number {
168+
const n = words.length;
169+
let ans = 0;
170+
for (let i = 0; i < n; i++) {
171+
if (words[i] !== words[0]) {
172+
ans = Math.max(ans, i + 1);
173+
}
174+
if (words[i] !== words[n - 1]) {
175+
ans = Math.max(ans, n - i);
176+
}
177+
}
178+
return ans;
179+
}
106180
```
107181

108182
<!-- tabs:end -->

solution/3700-3799/3706.Maximum Distance Between Unequal Words in Array II/README_EN.md

Lines changed: 78 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,32 +77,106 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3700-3799/3706.Ma
7777

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

80-
### Solution 1
80+
### Solution: Single Pass
81+
82+
We can observe that at least one of the two words with maximum distance must be at either end of the array (i.e., at index $0$ or $n - 1$). Otherwise, suppose the two words with maximum distance are at indices $i$ and $j$ where $0 < i < j < n - 1$. Then $\textit{words}[0]$ must be the same as $\textit{words}[j]$, and $\textit{words}[n - 1]$ must be the same as $\textit{words}[i]$ (otherwise the distance would be greater). This means $\textit{words}[0]$ and $\textit{words}[n - 1]$ are different, and their distance $n - 1 - 0 + 1 = n$ is definitely greater than $j - i + 1$, which contradicts our assumption. Therefore, at least one of the two words with maximum distance must be at either end of the array.
83+
84+
So, we only need to traverse the array, calculate the distance between each word and the words at both ends of the array, and update the maximum distance.
85+
86+
The time complexity is $O(n)$, where $n$ is the length of array $\textit{words}$. The space complexity is $O(1)$.
8187

8288
<!-- tabs:start -->
8389

8490
#### Python3
8591

8692
```python
87-
93+
class Solution:
94+
def maxDistance(self, words: List[str]) -> int:
95+
n = len(words)
96+
ans = 0
97+
for i in range(n):
98+
if words[i] != words[0]:
99+
ans = max(ans, i + 1)
100+
if words[i] != words[-1]:
101+
ans = max(ans, n - i)
102+
return ans
88103
```
89104

90105
#### Java
91106

92107
```java
93-
108+
class Solution {
109+
public int maxDistance(String[] words) {
110+
int n = words.length;
111+
int ans = 0;
112+
for (int i = 0; i < n; ++i) {
113+
if (!words[i].equals(words[0])) {
114+
ans = Math.max(ans, i + 1);
115+
}
116+
if (!words[i].equals(words[n - 1])) {
117+
ans = Math.max(ans, n - i);
118+
}
119+
}
120+
return ans;
121+
}
122+
}
94123
```
95124

96125
#### C++
97126

98127
```cpp
99-
128+
class Solution {
129+
public:
130+
int maxDistance(vector<string>& words) {
131+
int n = words.size();
132+
int ans = 0;
133+
for (int i = 0; i < n; ++i) {
134+
if (words[i] != words[0]) {
135+
ans = max(ans, i + 1);
136+
}
137+
if (words[i] != words[n - 1]) {
138+
ans = max(ans, n - i);
139+
}
140+
}
141+
return ans;
142+
}
143+
};
100144
```
101145
102146
#### Go
103147
104148
```go
149+
func maxDistance(words []string) int {
150+
n := len(words)
151+
ans := 0
152+
for i := 0; i < n; i++ {
153+
if words[i] != words[0] {
154+
ans = max(ans, i+1)
155+
}
156+
if words[i] != words[n-1] {
157+
ans = max(ans, n-i)
158+
}
159+
}
160+
return ans
161+
}
162+
```
105163

164+
#### TypeScript
165+
166+
```ts
167+
function maxDistance(words: string[]): number {
168+
const n = words.length;
169+
let ans = 0;
170+
for (let i = 0; i < n; i++) {
171+
if (words[i] !== words[0]) {
172+
ans = Math.max(ans, i + 1);
173+
}
174+
if (words[i] !== words[n - 1]) {
175+
ans = Math.max(ans, n - i);
176+
}
177+
}
178+
return ans;
179+
}
106180
```
107181

108182
<!-- tabs:end -->
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public:
3+
int maxDistance(vector<string>& words) {
4+
int n = words.size();
5+
int ans = 0;
6+
for (int i = 0; i < n; ++i) {
7+
if (words[i] != words[0]) {
8+
ans = max(ans, i + 1);
9+
}
10+
if (words[i] != words[n - 1]) {
11+
ans = max(ans, n - i);
12+
}
13+
}
14+
return ans;
15+
}
16+
};
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
func maxDistance(words []string) int {
2+
n := len(words)
3+
ans := 0
4+
for i := 0; i < n; i++ {
5+
if words[i] != words[0] {
6+
ans = max(ans, i+1)
7+
}
8+
if words[i] != words[n-1] {
9+
ans = max(ans, n-i)
10+
}
11+
}
12+
return ans
13+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public int maxDistance(String[] words) {
3+
int n = words.length;
4+
int ans = 0;
5+
for (int i = 0; i < n; ++i) {
6+
if (!words[i].equals(words[0])) {
7+
ans = Math.max(ans, i + 1);
8+
}
9+
if (!words[i].equals(words[n - 1])) {
10+
ans = Math.max(ans, n - i);
11+
}
12+
}
13+
return ans;
14+
}
15+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution:
2+
def maxDistance(self, words: List[str]) -> int:
3+
n = len(words)
4+
ans = 0
5+
for i in range(n):
6+
if words[i] != words[0]:
7+
ans = max(ans, i + 1)
8+
if words[i] != words[-1]:
9+
ans = max(ans, n - i)
10+
return ans
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
function maxDistance(words: string[]): number {
2+
const n = words.length;
3+
let ans = 0;
4+
for (let i = 0; i < n; i++) {
5+
if (words[i] !== words[0]) {
6+
ans = Math.max(ans, i + 1);
7+
}
8+
if (words[i] !== words[n - 1]) {
9+
ans = Math.max(ans, n - i);
10+
}
11+
}
12+
return ans;
13+
}

0 commit comments

Comments
 (0)