Skip to content

Commit 9479f0d

Browse files
committed
Added solution of problem 164
1 parent 9477190 commit 9479f0d

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

src/solutions/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ pub mod s0152_maximum_product_subarray;
9191
pub mod s0153_find_minimum_in_rotated_sorted_array;
9292
pub mod s0155_min_stack;
9393
pub mod s0162_find_peak_element;
94+
pub mod s0164_maximum_gap;
9495
pub mod s0231_power_of_two;
9596
pub mod s0232_implement_queue_using_stacks;
9697
pub mod s0234_palindrome_linked_list;

src/solutions/s0164_maximum_gap.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
pub fn maximum_gap(nums: Vec<i32>) -> i32 {
2+
if nums.len() < 2 {
3+
return 0;
4+
}
5+
6+
let mut nums = nums;
7+
nums.sort();
8+
9+
let mut result: i32 = 0;
10+
for i in 1..nums.len() {
11+
result = std::cmp::max(result, (nums[i - 1] - nums[i]).abs());
12+
}
13+
14+
result
15+
}
16+
17+
#[cfg(test)]
18+
mod test {
19+
use super::*;
20+
21+
#[test]
22+
fn test_case_1() {
23+
assert_eq!(maximum_gap(vec![3, 6, 9, 1]), 3);
24+
}
25+
26+
#[test]
27+
fn test_case_2() {
28+
assert_eq!(maximum_gap(vec![10]), 0);
29+
}
30+
}

0 commit comments

Comments
 (0)