Skip to content

Commit de9cf82

Browse files
committed
Add problem 2948: Make Lexicographically Smallest Array by Swapping Elements
1 parent bf09433 commit de9cf82

File tree

3 files changed

+82
-0
lines changed

3 files changed

+82
-0
lines changed

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2022,6 +2022,7 @@ pub mod problem_2938_separate_black_and_white_balls;
20222022
pub mod problem_2942_find_words_containing_character;
20232023
pub mod problem_2943_maximize_area_of_square_hole_in_grid;
20242024
pub mod problem_2946_matrix_similarity_after_cyclic_shifts;
2025+
pub mod problem_2948_make_lexicographically_smallest_array_by_swapping_elements;
20252026

20262027
#[cfg(test)]
20272028
mod test_utilities;
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
pub struct Solution;
2+
3+
// ------------------------------------------------------ snip ------------------------------------------------------ //
4+
5+
impl Solution {
6+
fn sort_group(targets: &mut [&mut i32], buffer: &mut Vec<i32>) {
7+
buffer.extend(targets.iter().map(|&&mut num| num));
8+
targets.sort_unstable_by_key(|target| &raw const **target);
9+
10+
targets
11+
.iter_mut()
12+
.zip(&*buffer)
13+
.for_each(|(target, &value)| **target = value);
14+
}
15+
16+
pub fn lexicographically_smallest_array(nums: Vec<i32>, limit: i32) -> Vec<i32> {
17+
let mut nums = nums;
18+
let limit = limit as u32;
19+
let mut nums_sorted = nums.iter_mut().collect::<Box<_>>();
20+
21+
nums_sorted.sort_unstable_by_key(|&&mut x| x as u32);
22+
23+
let mut prev = limit.wrapping_neg();
24+
let mut buffer = Vec::new();
25+
let mut group_start = 0;
26+
27+
for i in 0..nums_sorted.len() {
28+
let value = *nums_sorted[i] as u32;
29+
30+
if value.wrapping_sub(prev) > limit {
31+
Self::sort_group(&mut nums_sorted[group_start..i], &mut buffer);
32+
buffer.clear();
33+
group_start = i;
34+
}
35+
36+
prev = value;
37+
}
38+
39+
Self::sort_group(&mut nums_sorted[group_start..], &mut buffer);
40+
41+
nums
42+
}
43+
}
44+
45+
// ------------------------------------------------------ snip ------------------------------------------------------ //
46+
47+
impl super::Solution for Solution {
48+
fn lexicographically_smallest_array(nums: Vec<i32>, limit: i32) -> Vec<i32> {
49+
Self::lexicographically_smallest_array(nums, limit)
50+
}
51+
}
52+
53+
#[cfg(test)]
54+
mod tests {
55+
#[test]
56+
fn test_solution() {
57+
super::super::tests::run::<super::Solution>();
58+
}
59+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
pub mod iterative;
2+
3+
pub trait Solution {
4+
fn lexicographically_smallest_array(nums: Vec<i32>, limit: i32) -> Vec<i32>;
5+
}
6+
7+
#[cfg(test)]
8+
mod tests {
9+
use super::Solution;
10+
11+
pub fn run<S: Solution>() {
12+
let test_cases = [
13+
((&[1, 5, 3, 9, 8] as &[_], 2), &[1, 3, 5, 8, 9] as &[_]),
14+
((&[1, 7, 6, 18, 2, 1], 3), &[1, 6, 7, 18, 1, 2]),
15+
((&[1, 7, 28, 19, 10], 3), &[1, 7, 28, 19, 10]),
16+
];
17+
18+
for ((nums, limit), expected) in test_cases {
19+
assert_eq!(S::lexicographically_smallest_array(nums.to_vec(), limit), expected);
20+
}
21+
}
22+
}

0 commit comments

Comments
 (0)