Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 128977c

Browse files
feat: add solutions to lc problem: No.3423 (doocs#4483)
No.3423.Maximum Difference Between Adjacent Elements in a Circular Array
1 parent e51cd08 commit 128977c

File tree

5 files changed

+83
-3
lines changed

5 files changed

+83
-3
lines changed

‎solution/3400-3499/3423.Maximum Difference Between Adjacent Elements in a Circular Array/README.md‎

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ tags:
7676
```python
7777
class Solution:
7878
def maxAdjacentDistance(self, nums: List[int]) -> int:
79-
return max(max(abs(a - b) for a, b in pairwise(nums)), abs(nums[0]- nums[-1]))
79+
return max(abs(a - b) for a, b in pairwise(nums+ [nums[0]]))
8080
```
8181

8282
#### Java
@@ -141,6 +141,36 @@ function maxAdjacentDistance(nums: number[]): number {
141141
}
142142
```
143143

144+
#### Rust
145+
146+
```rust
147+
impl Solution {
148+
pub fn max_adjacent_distance(nums: Vec<i32>) -> i32 {
149+
nums.iter()
150+
.zip(nums.iter().cycle().skip(1))
151+
.take(nums.len())
152+
.map(|(a, b)| (*a - *b).abs())
153+
.max()
154+
.unwrap_or(0)
155+
}
156+
}
157+
```
158+
159+
#### C#
160+
161+
```cs
162+
public class Solution {
163+
public int MaxAdjacentDistance(int[] nums) {
164+
int n = nums.Length;
165+
int ans = Math.Abs(nums[0] - nums[n - 1]);
166+
for (int i = 1; i < n; ++i) {
167+
ans = Math.Max(ans, Math.Abs(nums[i] - nums[i - 1]));
168+
}
169+
return ans;
170+
}
171+
}
172+
```
173+
144174
<!-- tabs:end -->
145175

146176
<!-- solution:end -->

‎solution/3400-3499/3423.Maximum Difference Between Adjacent Elements in a Circular Array/README_EN.md‎

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ The time complexity is $O(n),ドル where $n$ is the length of the array $\textit{num
7474
```python
7575
class Solution:
7676
def maxAdjacentDistance(self, nums: List[int]) -> int:
77-
return max(max(abs(a - b) for a, b in pairwise(nums)), abs(nums[0]- nums[-1]))
77+
return max(abs(a - b) for a, b in pairwise(nums+ [nums[0]]))
7878
```
7979

8080
#### Java
@@ -139,6 +139,36 @@ function maxAdjacentDistance(nums: number[]): number {
139139
}
140140
```
141141

142+
#### Rust
143+
144+
```rust
145+
impl Solution {
146+
pub fn max_adjacent_distance(nums: Vec<i32>) -> i32 {
147+
nums.iter()
148+
.zip(nums.iter().cycle().skip(1))
149+
.take(nums.len())
150+
.map(|(a, b)| (*a - *b).abs())
151+
.max()
152+
.unwrap_or(0)
153+
}
154+
}
155+
```
156+
157+
#### C#
158+
159+
```cs
160+
public class Solution {
161+
public int MaxAdjacentDistance(int[] nums) {
162+
int n = nums.Length;
163+
int ans = Math.Abs(nums[0] - nums[n - 1]);
164+
for (int i = 1; i < n; ++i) {
165+
ans = Math.Max(ans, Math.Abs(nums[i] - nums[i - 1]));
166+
}
167+
return ans;
168+
}
169+
}
170+
```
171+
142172
<!-- tabs:end -->
143173

144174
<!-- solution:end -->
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
public class Solution {
2+
public int MaxAdjacentDistance(int[] nums) {
3+
int n = nums.Length;
4+
int ans = Math.Abs(nums[0] - nums[n - 1]);
5+
for (int i = 1; i < n; ++i) {
6+
ans = Math.Max(ans, Math.Abs(nums[i] - nums[i - 1]));
7+
}
8+
return ans;
9+
}
10+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
class Solution:
22
def maxAdjacentDistance(self, nums: List[int]) -> int:
3-
return max(max(abs(a - b) for a, b in pairwise(nums)), abs(nums[0]-nums[-1]))
3+
return max(abs(a - b) for a, b in pairwise(nums+ [nums[0]]))
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
impl Solution {
2+
pub fn max_adjacent_distance(nums: Vec<i32>) -> i32 {
3+
nums.iter()
4+
.zip(nums.iter().cycle().skip(1))
5+
.take(nums.len())
6+
.map(|(a, b)| (*a - *b).abs())
7+
.max()
8+
.unwrap_or(0)
9+
}
10+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /