-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add problem 2311: Longest Binary Subsequence Less Than or Equal to K
- Loading branch information
Showing
3 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
src/problem_2311_longest_binary_subsequence_less_than_or_equal_to_k/greedy.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
pub struct Solution; | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl Solution { | ||
pub fn longest_subsequence(s: String, k: i32) -> i32 { | ||
let s = s.as_bytes(); | ||
let n = s.len(); | ||
let k = k as u32; | ||
let bits = 32 - k.leading_zeros() as usize; | ||
|
||
if n < bits { | ||
return n as _; | ||
} | ||
|
||
let (left, right) = s.split_at(n - bits); | ||
let right = right.iter().fold(0, |right, &c| (right << 1) + u32::from(c & 1)); | ||
let length = bits as u16 - u16::from(right > k); | ||
|
||
i32::from(left.iter().fold(length, |length, &c| length + u16::from(b'1' - c))) | ||
} | ||
} | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl super::Solution for Solution { | ||
fn longest_subsequence(s: String, k: i32) -> i32 { | ||
Self::longest_subsequence(s, k) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn test_solution() { | ||
super::super::tests::run::<super::Solution>(); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/problem_2311_longest_binary_subsequence_less_than_or_equal_to_k/mod.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
pub mod greedy; | ||
|
||
pub trait Solution { | ||
fn longest_subsequence(s: String, k: i32) -> i32; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::Solution; | ||
|
||
pub fn run<S: Solution>() { | ||
let test_cases = [ | ||
(("1001010", 5), 5), | ||
(("00101001", 1), 6), | ||
( | ||
( | ||
"101100011000010101011100011111111001011101000101000010001100101110010010011000100010001110011111000100100101110000100010010010100010", | ||
978_095_074, | ||
), | ||
84, | ||
), | ||
(("0", 583_196_182), 1), | ||
]; | ||
|
||
for ((s, k), expected) in test_cases { | ||
assert_eq!(S::longest_subsequence(s.to_string(), k), expected); | ||
} | ||
} | ||
} |