diff --git a/solution/3600-3699/3696.Maximum Distance Between Unequal Words in Array I/README.md b/solution/3600-3699/3696.Maximum Distance Between Unequal Words in Array I/README.md index a799795032efd..dd0b53bb87a8a 100644 --- a/solution/3600-3699/3696.Maximum Distance Between Unequal Words in Array I/README.md +++ b/solution/3600-3699/3696.Maximum Distance Between Unequal Words in Array I/README.md @@ -76,32 +76,106 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3696.Ma -### 方法一 +### 方法一:一次遍历 + +我们可以发现,最大距离的两个单词中至少有一个单词在数组的两端(即下标为 $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$,与假设矛盾。因此,最大距离的两个单词中至少有一个单词在数组的两端。 + +所以,我们只需要遍历数组,计算每个单词与数组两端单词的距离,并更新最大距离。 + +时间复杂度 $O(n)$,其中 $n$ 是数组 $\textit{words}$ 的长度。空间复杂度 $O(1)$。 #### Python3 ```python - +class Solution: + def maxDistance(self, words: List[str]) -> int: + n = len(words) + ans = 0 + for i in range(n): + if words[i] != words[0]: + ans = max(ans, i + 1) + if words[i] != words[-1]: + ans = max(ans, n - i) + return ans ``` #### Java ```java - +class Solution { + public int maxDistance(String[] words) { + int n = words.length; + int ans = 0; + for (int i = 0; i < n; ++i) { + if (!words[i].equals(words[0])) { + ans = Math.max(ans, i + 1); + } + if (!words[i].equals(words[n - 1])) { + ans = Math.max(ans, n - i); + } + } + return ans; + } +} ``` #### C++ ```cpp - +class Solution { +public: + int maxDistance(vector& words) { + int n = words.size(); + int ans = 0; + for (int i = 0; i < n; ++i) { + if (words[i] != words[0]) { + ans = max(ans, i + 1); + } + if (words[i] != words[n - 1]) { + ans = max(ans, n - i); + } + } + return ans; + } +}; ``` #### Go ```go +func maxDistance(words []string) int { + n := len(words) + ans := 0 + for i := 0; i < n; i++ { + if words[i] != words[0] { + ans = max(ans, i+1) + } + if words[i] != words[n-1] { + ans = max(ans, n-i) + } + } + return ans +} +``` +#### TypeScript + +```ts +function maxDistance(words: string[]): number { + const n = words.length; + let ans = 0; + for (let i = 0; i < n; i++) { + if (words[i] !== words[0]) { + ans = Math.max(ans, i + 1); + } + if (words[i] !== words[n - 1]) { + ans = Math.max(ans, n - i); + } + } + return ans; +} ``` diff --git a/solution/3600-3699/3696.Maximum Distance Between Unequal Words in Array I/README_EN.md b/solution/3600-3699/3696.Maximum Distance Between Unequal Words in Array I/README_EN.md index 2a962365cc1d1..f4be16cb0d3ea 100644 --- a/solution/3600-3699/3696.Maximum Distance Between Unequal Words in Array I/README_EN.md +++ b/solution/3600-3699/3696.Maximum Distance Between Unequal Words in Array I/README_EN.md @@ -76,32 +76,106 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3696.Ma -### Solution 1 +### Solution: Single Pass + +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. + +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. + +The time complexity is $O(n)$, where $n$ is the length of array $\textit{words}$. The space complexity is $O(1)$. #### Python3 ```python - +class Solution: + def maxDistance(self, words: List[str]) -> int: + n = len(words) + ans = 0 + for i in range(n): + if words[i] != words[0]: + ans = max(ans, i + 1) + if words[i] != words[-1]: + ans = max(ans, n - i) + return ans ``` #### Java ```java - +class Solution { + public int maxDistance(String[] words) { + int n = words.length; + int ans = 0; + for (int i = 0; i < n; ++i) { + if (!words[i].equals(words[0])) { + ans = Math.max(ans, i + 1); + } + if (!words[i].equals(words[n - 1])) { + ans = Math.max(ans, n - i); + } + } + return ans; + } +} ``` #### C++ ```cpp - +class Solution { +public: + int maxDistance(vector& words) { + int n = words.size(); + int ans = 0; + for (int i = 0; i < n; ++i) { + if (words[i] != words[0]) { + ans = max(ans, i + 1); + } + if (words[i] != words[n - 1]) { + ans = max(ans, n - i); + } + } + return ans; + } +}; ``` #### Go ```go +func maxDistance(words []string) int { + n := len(words) + ans := 0 + for i := 0; i < n; i++ { + if words[i] != words[0] { + ans = max(ans, i+1) + } + if words[i] != words[n-1] { + ans = max(ans, n-i) + } + } + return ans +} +``` +#### TypeScript + +```ts +function maxDistance(words: string[]): number { + const n = words.length; + let ans = 0; + for (let i = 0; i < n; i++) { + if (words[i] !== words[0]) { + ans = Math.max(ans, i + 1); + } + if (words[i] !== words[n - 1]) { + ans = Math.max(ans, n - i); + } + } + return ans; +} ``` diff --git a/solution/3600-3699/3696.Maximum Distance Between Unequal Words in Array I/Solution.cpp b/solution/3600-3699/3696.Maximum Distance Between Unequal Words in Array I/Solution.cpp new file mode 100644 index 0000000000000..d0bbc39ff99f5 --- /dev/null +++ b/solution/3600-3699/3696.Maximum Distance Between Unequal Words in Array I/Solution.cpp @@ -0,0 +1,16 @@ +class Solution { +public: + int maxDistance(vector& words) { + int n = words.size(); + int ans = 0; + for (int i = 0; i < n; ++i) { + if (words[i] != words[0]) { + ans = max(ans, i + 1); + } + if (words[i] != words[n - 1]) { + ans = max(ans, n - i); + } + } + return ans; + } +}; diff --git a/solution/3600-3699/3696.Maximum Distance Between Unequal Words in Array I/Solution.go b/solution/3600-3699/3696.Maximum Distance Between Unequal Words in Array I/Solution.go new file mode 100644 index 0000000000000..f928dfaaba94a --- /dev/null +++ b/solution/3600-3699/3696.Maximum Distance Between Unequal Words in Array I/Solution.go @@ -0,0 +1,13 @@ +func maxDistance(words []string) int { + n := len(words) + ans := 0 + for i := 0; i < n; i++ { + if words[i] != words[0] { + ans = max(ans, i+1) + } + if words[i] != words[n-1] { + ans = max(ans, n-i) + } + } + return ans +} diff --git a/solution/3600-3699/3696.Maximum Distance Between Unequal Words in Array I/Solution.java b/solution/3600-3699/3696.Maximum Distance Between Unequal Words in Array I/Solution.java new file mode 100644 index 0000000000000..56614a9567265 --- /dev/null +++ b/solution/3600-3699/3696.Maximum Distance Between Unequal Words in Array I/Solution.java @@ -0,0 +1,15 @@ +class Solution { + public int maxDistance(String[] words) { + int n = words.length; + int ans = 0; + for (int i = 0; i < n; ++i) { + if (!words[i].equals(words[0])) { + ans = Math.max(ans, i + 1); + } + if (!words[i].equals(words[n - 1])) { + ans = Math.max(ans, n - i); + } + } + return ans; + } +} diff --git a/solution/3600-3699/3696.Maximum Distance Between Unequal Words in Array I/Solution.py b/solution/3600-3699/3696.Maximum Distance Between Unequal Words in Array I/Solution.py new file mode 100644 index 0000000000000..39552c7a6d52a --- /dev/null +++ b/solution/3600-3699/3696.Maximum Distance Between Unequal Words in Array I/Solution.py @@ -0,0 +1,10 @@ +class Solution: + def maxDistance(self, words: List[str]) -> int: + n = len(words) + ans = 0 + for i in range(n): + if words[i] != words[0]: + ans = max(ans, i + 1) + if words[i] != words[-1]: + ans = max(ans, n - i) + return ans diff --git a/solution/3600-3699/3696.Maximum Distance Between Unequal Words in Array I/Solution.ts b/solution/3600-3699/3696.Maximum Distance Between Unequal Words in Array I/Solution.ts new file mode 100644 index 0000000000000..9e6eb46fe6491 --- /dev/null +++ b/solution/3600-3699/3696.Maximum Distance Between Unequal Words in Array I/Solution.ts @@ -0,0 +1,13 @@ +function maxDistance(words: string[]): number { + const n = words.length; + let ans = 0; + for (let i = 0; i < n; i++) { + if (words[i] !== words[0]) { + ans = Math.max(ans, i + 1); + } + if (words[i] !== words[n - 1]) { + ans = Math.max(ans, n - i); + } + } + return ans; +}