|
| 1 | +use std::collections::{HashMap, HashSet}; |
| 2 | + |
| 3 | +// 1604. Alert Using Same Key-Card Three or More Times in a One Hour Period, Medium |
| 4 | +// https://leetcode.com/problems/alert-using-same-key-card-three-or-more-times-in-a-one-hour-period/ |
| 5 | +impl Solution { |
| 6 | + pub fn alert_names(key_name: Vec<String>, key_time: Vec<String>) -> Vec<String> { |
| 7 | + let n = key_name.len(); |
| 8 | + let mut time_map = HashMap::<String, Vec<i32>>::new(); |
| 9 | + |
| 10 | + // HH:MM |
| 11 | + fn parse_time(time: &String) -> i32 { |
| 12 | + let chars = time.chars().collect::<Vec<char>>(); |
| 13 | + ((chars[0].to_digit(10).unwrap() * 10 + chars[1].to_digit(10).unwrap()) * 60 + (chars[3].to_digit(10).unwrap() * 10 + chars[4].to_digit(10).unwrap())) as i32 |
| 14 | + } |
| 15 | + |
| 16 | + let mut ans = HashSet::<String>::new(); |
| 17 | + for i in 0..n { |
| 18 | + let name = &key_name[i]; |
| 19 | + let time = parse_time(&key_time[i]); |
| 20 | + |
| 21 | + time_map.entry(name.clone()).or_insert(Vec::<i32>::new()); |
| 22 | + time_map.get_mut(name).unwrap().push(time); |
| 23 | + } |
| 24 | + |
| 25 | + time_map.iter_mut().for_each(|(_, times)| { |
| 26 | + times.sort_unstable(); |
| 27 | + }); |
| 28 | + |
| 29 | + time_map.iter().for_each(|(name, times)| { |
| 30 | + let mut i = 0; |
| 31 | + while times.len() - i > 2 { |
| 32 | + if i32::abs(times[i] - times[i + 2]) <= 60 { |
| 33 | + ans.insert(name.clone()); |
| 34 | + } |
| 35 | + i += 1; |
| 36 | + } |
| 37 | + }); |
| 38 | + |
| 39 | + let mut ans = ans.iter().map(|f| f.clone()).collect::<Vec<String>>(); |
| 40 | + ans.sort_unstable(); |
| 41 | + ans |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +struct Solution {} |
| 46 | + |
| 47 | +#[cfg(test)] |
| 48 | +mod tests { |
| 49 | + use super::*; |
| 50 | + use crate::vec_string; |
| 51 | + |
| 52 | + #[test] |
| 53 | + fn test_alert_names() { |
| 54 | + assert_eq!( |
| 55 | + Solution::alert_names( |
| 56 | + vec_string!["daniel", "daniel", "daniel", "luis", "luis", "luis", "luis"], |
| 57 | + vec_string!["10:00", "10:40", "11:00", "09:00", "11:00", "13:00", "15:00"] |
| 58 | + ), |
| 59 | + vec_string!["daniel"] |
| 60 | + ); |
| 61 | + } |
| 62 | + |
| 63 | + #[test] |
| 64 | + fn test_alert_names2() { |
| 65 | + assert_eq!( |
| 66 | + Solution::alert_names( |
| 67 | + vec_string!["alice", "alice", "alice", "bob", "bob", "bob", "bob"], |
| 68 | + vec_string!["12:01", "12:00", "18:00", "21:00", "21:20", "21:30", "23:00"] |
| 69 | + ), |
| 70 | + vec_string!["bob"] |
| 71 | + ); |
| 72 | + } |
| 73 | + |
| 74 | + #[test] |
| 75 | + fn test_alert_names3() { |
| 76 | + assert_eq!( |
| 77 | + Solution::alert_names(vec_string!["john", "john", "john"], vec_string!["23:58", "23:59", "00:01"]), |
| 78 | + vec_string![] |
| 79 | + ); |
| 80 | + } |
| 81 | + |
| 82 | + #[test] |
| 83 | + fn test_alert_names4() { |
| 84 | + assert_eq!( |
| 85 | + Solution::alert_names( |
| 86 | + vec_string!["leslie", "leslie", "leslie", "clare", "clare", "clare", "clare"], |
| 87 | + vec_string!["13:00", "13:20", "14:00", "18:00", "18:51", "19:30", "19:49"] |
| 88 | + ), |
| 89 | + vec_string!["clare", "leslie"] |
| 90 | + ); |
| 91 | + } |
| 92 | + |
| 93 | + #[test] |
| 94 | + fn test_alert_names5() { |
| 95 | + assert_eq!( |
| 96 | + Solution::alert_names( |
| 97 | + vec_string!["a", "a", "a", "a", "a", "a", "b", "b", "b", "b", "b"], |
| 98 | + vec_string!["23:27", "03:14", "12:57", "13:35", "13:18", "21:58", "22:39", "10:49", "19:37", "14:14", "10:41"] |
| 99 | + ), |
| 100 | + vec_string!["a"] |
| 101 | + ); |
| 102 | + } |
| 103 | + |
| 104 | + #[test] |
| 105 | + fn test_alert_names6() { |
| 106 | + assert_eq!( |
| 107 | + Solution::alert_names( |
| 108 | + vec_string!["a", "a", "a", "a", "a", "b", "b", "b", "b", "b", "b"], |
| 109 | + vec_string!["23:20", "11:09", "23:30", "23:02", "15:28", "22:57", "23:40", "03:43", "21:55", "20:38", "00:19"] |
| 110 | + ), |
| 111 | + vec_string!["a"] |
| 112 | + ); |
| 113 | + } |
| 114 | +} |
0 commit comments