g0101_0200.s0164_maximum_gap.Solution Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-java Show documentation
Show all versions of leetcode-in-java Show documentation
Java-based LeetCode algorithm problem solutions, regularly updated
package g0101_0200.s0164_maximum_gap;
// #Hard #Array #Sorting #Bucket_Sort #Radix_Sort
// #2022_06_25_Time_48_ms_(53.59%)_Space_84.1_MB_(20.66%)
import java.util.Arrays;
public class Solution {
public int maximumGap(int[] nums) {
if (nums.length < 2) {
return 0;
}
int ret = Integer.MIN_VALUE;
Arrays.sort(nums);
for (int i = 0; i < nums.length - 1; i++) {
if ((nums[i + 1] - nums[i]) > ret) {
ret = (nums[i + 1] - nums[i]);
}
}
return ret;
}
}