Skip to content

Commit b91312c

Browse files
authored
feat: add solutions to lc problem: No.3696 (doocs#4759)
1 parent 2d95ff9 commit b91312c

File tree

7 files changed

+223
-8
lines changed

7 files changed

+223
-8
lines changed

solution/3600-3699/3696.Maximum Distance Between Unequal Words in Array I/README.md

Lines changed: 78 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,32 +76,106 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3696.Ma
7676

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

79-
### 方法一
79+
### 方法一:一次遍历
80+
81+
我们可以发现,最大距离的两个单词中至少有一个单词在数组的两端(即下标为 $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$,与假设矛盾。因此,最大距离的两个单词中至少有一个单词在数组的两端。
82+
83+
所以,我们只需要遍历数组,计算每个单词与数组两端单词的距离,并更新最大距离。
84+
85+
时间复杂度 $O(n)$,其中 $n$ 是数组 $\textit{words}$ 的长度。空间复杂度 $O(1)$。
8086

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

8389
#### Python3
8490

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

89104
#### Java
90105

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

95124
#### C++
96125

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

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

107181
<!-- tabs:end -->

solution/3600-3699/3696.Maximum Distance Between Unequal Words in Array I/README_EN.md

Lines changed: 78 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,32 +76,106 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3696.Ma
7676

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

79-
### Solution 1
79+
### Solution: Single Pass
80+
81+
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.
82+
83+
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.
84+
85+
The time complexity is $O(n)$, where $n$ is the length of array $\textit{words}$. The space complexity is $O(1)$.
8086

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

8389
#### Python3
8490

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

89104
#### Java
90105

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

95124
#### C++
96125

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

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

107181
<!-- 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)