Skip to content

Commit 7415738

Browse files
committed
Add solution #164
1 parent 5db2af0 commit 7415738

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@
134134
151|[Reverse Words in a String](./0151-reverse-words-in-a-string.js)|Medium|
135135
152|[Maximum Product Subarray](./0152-maximum-product-subarray.js)|Medium|
136136
160|[Intersection of Two Linked Lists](./0160-intersection-of-two-linked-lists.js)|Medium|
137+
164|[Maximum Gap](./0164-maximum-gap.js)|Medium|
137138
167|[Two Sum II - Input Array Is Sorted](./0167-two-sum-ii-input-array-is-sorted.js)|Easy|
138139
168|[Excel Sheet Column Title](./0168-excel-sheet-column-title.js)|Easy|
139140
169|[Majority Element](./0169-majority-element.js)|Easy|

solutions/0164-maximum-gap.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* 164. Maximum Gap
3+
* https://leetcode.com/problems/maximum-gap/
4+
* Difficulty: Medium
5+
*
6+
* Given an integer array nums, return the maximum difference between two successive elements in
7+
* its sorted form. If the array contains less than two elements, return 0.
8+
*/
9+
10+
/**
11+
* @param {number[]} nums
12+
* @return {number}
13+
*/
14+
var maximumGap = function(nums) {
15+
nums.sort((a, b) => a - b);
16+
let result = 0;
17+
for (let i = 1; i < nums.length; i++) {
18+
result = Math.max(result, nums[i] - nums[i - 1]);
19+
}
20+
return result;
21+
};

0 commit comments

Comments
 (0)