-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
54 lines (48 loc) · 1.4 KB
/
main.go
File metadata and controls
54 lines (48 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Source: https://leetcode.com/problems/maximum-difference-between-adjacent-elements-in-a-circular-array
// Title: Maximum Difference Between Adjacent Elements in a Circular Array
// Difficulty: Easy
// Author: Mu Yang <http://muyang.pro>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Given a **circular** array `nums`, find the **maximum** absolute difference between adjacent elements.
//
// **Note**: In a circular array, the first and last elements are adjacent.
//
// **Example 1:**
//
// ```
// Input: nums = [1,2,4]
// Output: 3
// Explanation:
// Because `nums` is circular, `nums[0]` and `nums[2]` are adjacent. They have the maximum absolute difference of `|4 - 1| = 3`.
// ```
//
// **Example 2:**
//
// ```
// Input: nums = [-5,-10,-5]
// Output: 5
// Explanation:
// The adjacent elements `nums[0]` and `nums[1]` have the maximum absolute difference of `|-5 - (-10)| = 5`.
// ```
//
// **Constraints:**
//
// - `2 <= nums.length <= 100`
// - `-100 <= nums[i] <= 100`
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
package main
func maxAdjacentDistance(nums []int) int {
n := len(nums)
ans := 0
for i := range n {
ans = max(ans, _abs(nums[i]-nums[(i+1)%n]))
}
return ans
}
func _abs(v int) int {
if v >= 0 {
return v
}
return -v
}