Skip to content

Instantly share code, notes, and snippets.

@akolybelnikov
Forked from rust-play/playground.rs
Last active November 9, 2020 13:13
Show Gist options
  • Save akolybelnikov/3d193dea4a050fd8db3b0a8875f0ced1 to your computer and use it in GitHub Desktop.
Save akolybelnikov/3d193dea4a050fd8db3b0a8875f0ced1 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
fn find_subseq(s: &[u8]) -> u8 {
let mut res: u8 = 1;
let mut i = 0;
while (i < s.len() - 1) && s[i] < 255 && (s[i] + 1 == s[i + 1]) {
res += 1;
i += 1;
}
return res;
}
pub fn longest_incrementing_subslice(s: &[u8]) -> &[u8] {
let mut res = (0, 1);
let mut el = 0;
while el < s.len() - 1 {
let subl = find_subseq(&s[el..s.len()]);
if subl > res.1 {
res.0 = el;
res.1 = subl;
}
el = el + subl as usize;
}
return &s[res.0..res.0 + res.1 as usize];
}
fn main() {
let data = &[2, 5, 2, 253, 254, 255, 1];
let result = longest_incrementing_subslice(data);
println!("{:?}", result);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment