Skip to content

Commit 1634479

Browse files
committed
Sync LeetCode submission Runtime - 4 ms (23.02%), Memory - 17.8 MB (48.85%)
1 parent 3dc8e7a commit 1634479

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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>&nbsp;</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>&nbsp;</p>
31+
<p><strong>Constraints:</strong></p>
32+
33+
<ul>
34+
<li><code>2 &lt;= nums.length &lt;= 100</code></li>
35+
<li><code>-100 &lt;= nums[i] &lt;= 100</code></li>
36+
</ul>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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

0 commit comments

Comments
 (0)