File tree Expand file tree Collapse file tree 2 files changed +50
-0
lines changed
3747-maximum-difference-between-adjacent-elements-in-a-circular-array Expand file tree Collapse file tree 2 files changed +50
-0
lines changed Original file line number Diff line number Diff line change 1+ <p >Given a <strong >circular</strong > array <code >nums</code >, find the <b >maximum</b > absolute difference between adjacent elements.</p >
2+
3+ <p ><strong >Note</strong >: In a circular array, the first and last elements are adjacent.</p >
4+
5+ <p >  ; </p >
6+ <p ><strong class =" example " >Example 1:</strong ></p >
7+
8+ <div class =" example-block " >
9+ <p ><strong >Input:</strong > <span class =" example-io " >nums = [1,2,4]</span ></p >
10+
11+ <p ><strong >Output:</strong > <span class =" example-io " >3</span ></p >
12+
13+ <p ><strong >Explanation:</strong ></p >
14+
15+ <p >Because <code >nums</code > is circular, <code >nums[0]</code > and <code >nums[2]</code > are adjacent. They have the maximum absolute difference of <code >|4 - 1| = 3</code >.</p >
16+ </div >
17+
18+ <p ><strong class =" example " >Example 2:</strong ></p >
19+
20+ <div class =" example-block " >
21+ <p ><strong >Input:</strong > <span class =" example-io " >nums = [-5,-10,-5]</span ></p >
22+
23+ <p ><strong >Output:</strong > <span class =" example-io " >5</span ></p >
24+
25+ <p ><strong >Explanation:</strong ></p >
26+
27+ <p >The adjacent elements <code >nums[0]</code > and <code >nums[1]</code > have the maximum absolute difference of <code >|-5 - (-10)| = 5</code >.</p >
28+ </div >
29+
30+ <p >  ; </p >
31+ <p ><strong >Constraints:</strong ></p >
32+
33+ <ul >
34+ <li><code>2 <= nums.length <= 100</code></li>
35+ <li><code>-100 <= nums[i] <= 100</code></li>
36+ </ul >
Original file line number Diff line number Diff line change 1+ # Approach 1: Traversal
2+
3+ # Time: O(n)
4+ # Space: O(1)
5+
6+ class Solution :
7+ def maxAdjacentDistance (self , nums : List [int ]) -> int :
8+ n = len (nums )
9+ res = abs (nums [0 ] - nums [n - 1 ])
10+
11+ for i in range (n - 1 ):
12+ res = max (res , abs (nums [i ] - nums [i + 1 ]))
13+
14+ return res
You can’t perform that action at this time.
0 commit comments