Skip to content

Commit b39ee74

Browse files
committed
Add problem 2864: Maximum Odd Binary Number
1 parent a1243fd commit b39ee74

File tree

4 files changed

+54
-1
lines changed

4 files changed

+54
-1
lines changed

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
clippy::cast_sign_loss,
1515
clippy::missing_panics_doc,
1616
clippy::must_use_candidate,
17+
clippy::naive_bytecount,
1718
reason = "unnecessary"
1819
)]
1920

@@ -1994,6 +1995,7 @@ pub mod problem_2849_determine_if_a_cell_is_reachable_at_a_given_time;
19941995
pub mod problem_2855_minimum_right_shifts_to_sort_the_array;
19951996
pub mod problem_2856_minimum_array_length_after_pair_removals;
19961997
pub mod problem_2859_sum_of_values_at_indices_with_k_set_bits;
1998+
pub mod problem_2864_maximum_odd_binary_number;
19971999

19982000
#[cfg(test)]
19992001
mod test_utilities;

src/problem_2103_rings_and_rods/iterative.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ pub struct Solution;
33
// ------------------------------------------------------ snip ------------------------------------------------------ //
44

55
impl Solution {
6-
#[expect(clippy::naive_bytecount, reason = "optimal")]
76
pub fn count_points(rings: String) -> i32 {
87
let mut iter_2 = rings.bytes();
98

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
pub struct Solution;
2+
3+
// ------------------------------------------------------ snip ------------------------------------------------------ //
4+
5+
impl Solution {
6+
pub fn maximum_odd_binary_number(s: String) -> String {
7+
let mut s = s.into_bytes();
8+
let ones = s.iter().filter(|&&c| c == b'1').count();
9+
let (left, right) = s.split_at_mut(ones - 1);
10+
let (last, rest) = right.split_last_mut().unwrap();
11+
12+
left.fill(b'1');
13+
rest.fill(b'0');
14+
*last = b'1';
15+
16+
String::from_utf8(s).unwrap()
17+
}
18+
}
19+
20+
// ------------------------------------------------------ snip ------------------------------------------------------ //
21+
22+
impl super::Solution for Solution {
23+
fn maximum_odd_binary_number(s: String) -> String {
24+
Self::maximum_odd_binary_number(s)
25+
}
26+
}
27+
28+
#[cfg(test)]
29+
mod tests {
30+
#[test]
31+
fn test_solution() {
32+
super::super::tests::run::<super::Solution>();
33+
}
34+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
pub mod greedy;
2+
3+
pub trait Solution {
4+
fn maximum_odd_binary_number(s: String) -> String;
5+
}
6+
7+
#[cfg(test)]
8+
mod tests {
9+
use super::Solution;
10+
11+
pub fn run<S: Solution>() {
12+
let test_cases = [("010", "001"), ("0101", "1001")];
13+
14+
for (s, expected) in test_cases {
15+
assert_eq!(S::maximum_odd_binary_number(s.to_string()), expected);
16+
}
17+
}
18+
}

0 commit comments

Comments
 (0)