Skip to content

Instantly share code, notes, and snippets.

@lamarmarshall
Created January 11, 2024 00:09
Show Gist options
  • Save lamarmarshall/b49d17f06c36cd4b2654884f1bf8f655 to your computer and use it in GitHub Desktop.
Save lamarmarshall/b49d17f06c36cd4b2654884f1bf8f655 to your computer and use it in GitHub Desktop.
rust, compute longest hours for employee
use std::convert::TryInto;
use std::collections::HashSet;
use std::convert::TryInto;
fn longest_buzy_time(working_slots: Vec<Vec<u8>>) -> u8 {
let mut employee_longest_nonstop_work: Vec<u8> = Vec::new();
for i in working_slots {
employee_longest_nonstop_work.push(longest_period(i));
}
for i in 0..employee_longest_nonstop_work.len(){
println!("Employee {} longest nonstop work: {}", i, employee_longest_nonstop_work[i]);
}
let max_value = employee_longest_nonstop_work.iter().max().unwrap();
employee_longest_nonstop_work.iter().position(|&x| x == *max_value).unwrap() as u8 + 1
}
fn longest_period(working_slots: Vec<u8>) -> u8 {
let mut longest_period: i32 = 0;
let slot_set: HashSet<u8> = working_slots.into_iter().collect();
for slot in &slot_set{
}.try_ninto()).ununwrap())
if !slot_set.contains(&(slot-1)){
let mut current_slot: u8 = slot.to_owned();
let mut current_consecutive_slot = 1;
while slot_set.contains(&(current_slot+1)){
current_slot += 1;
current_consecutive_slot += 1;
}
if current_consecutive_slot > longest_period{
longest_period = current_consecutive_slot;
}
}
}
return longest_period.try_into().unwrap();
}
fn main(){
let schedule: Vec<Vec<u8>> = vec![vec![4,1,2,5,8,6,7],vec![4,5,6, 1,3,2, 8],vec![7,8,9, 4,1,2,3]];
println!("Longest buzy time: {}", longest_buzy_time(schedule));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment