Skip to content

feat: add solutions to lc problem: No.3423 #4483

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 11, 2025
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 @@ -76,7 +76,7 @@ tags:
```python
class Solution:
def maxAdjacentDistance(self, nums: List[int]) -> int:
return max(max(abs(a - b) for a, b in pairwise(nums)), abs(nums[0] - nums[-1]))
return max(abs(a - b) for a, b in pairwise(nums + [nums[0]]))
```

#### Java
Expand Down Expand Up @@ -141,6 +141,36 @@ function maxAdjacentDistance(nums: number[]): number {
}
```

#### Rust

```rust
impl Solution {
pub fn max_adjacent_distance(nums: Vec<i32>) -> i32 {
nums.iter()
.zip(nums.iter().cycle().skip(1))
.take(nums.len())
.map(|(a, b)| (*a - *b).abs())
.max()
.unwrap_or(0)
}
}
```

#### C#

```cs
public class Solution {
public int MaxAdjacentDistance(int[] nums) {
int n = nums.Length;
int ans = Math.Abs(nums[0] - nums[n - 1]);
for (int i = 1; i < n; ++i) {
ans = Math.Max(ans, Math.Abs(nums[i] - nums[i - 1]));
}
return ans;
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ The time complexity is $O(n)$, where $n$ is the length of the array $\textit{num
```python
class Solution:
def maxAdjacentDistance(self, nums: List[int]) -> int:
return max(max(abs(a - b) for a, b in pairwise(nums)), abs(nums[0] - nums[-1]))
return max(abs(a - b) for a, b in pairwise(nums + [nums[0]]))
```

#### Java
Expand Down Expand Up @@ -139,6 +139,36 @@ function maxAdjacentDistance(nums: number[]): number {
}
```

#### Rust

```rust
impl Solution {
pub fn max_adjacent_distance(nums: Vec<i32>) -> i32 {
nums.iter()
.zip(nums.iter().cycle().skip(1))
.take(nums.len())
.map(|(a, b)| (*a - *b).abs())
.max()
.unwrap_or(0)
}
}
```

#### C#

```cs
public class Solution {
public int MaxAdjacentDistance(int[] nums) {
int n = nums.Length;
int ans = Math.Abs(nums[0] - nums[n - 1]);
for (int i = 1; i < n; ++i) {
ans = Math.Max(ans, Math.Abs(nums[i] - nums[i - 1]));
}
return ans;
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
public class Solution {
public int MaxAdjacentDistance(int[] nums) {
int n = nums.Length;
int ans = Math.Abs(nums[0] - nums[n - 1]);
for (int i = 1; i < n; ++i) {
ans = Math.Max(ans, Math.Abs(nums[i] - nums[i - 1]));
}
return ans;
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
class Solution:
def maxAdjacentDistance(self, nums: List[int]) -> int:
return max(max(abs(a - b) for a, b in pairwise(nums)), abs(nums[0] - nums[-1]))
return max(abs(a - b) for a, b in pairwise(nums + [nums[0]]))
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
impl Solution {
pub fn max_adjacent_distance(nums: Vec<i32>) -> i32 {
nums.iter()
.zip(nums.iter().cycle().skip(1))
.take(nums.len())
.map(|(a, b)| (*a - *b).abs())
.max()
.unwrap_or(0)
}
}