|
| 1 | +/** |
| 2 | + * [2190] Most Frequent Number Following Key In an Array |
| 3 | + * |
| 4 | + * You are given a 0-indexed integer array nums. You are also given an integer key, which is present in nums. |
| 5 | + * For every unique integer target in nums, count the number of times target immediately follows an occurrence of key in nums. In other words, count the number of indices i such that: |
| 6 | + * |
| 7 | + * 0 <= i <= nums.length - 2, |
| 8 | + * nums[i] == key and, |
| 9 | + * nums[i + 1] == target. |
| 10 | + * |
| 11 | + * Return the target with the maximum count. The test cases will be generated such that the target with maximum count is unique. |
| 12 | + * |
| 13 | + * Example 1: |
| 14 | + * |
| 15 | + * Input: nums = [1,100,200,1,100], key = 1 |
| 16 | + * Output: 100 |
| 17 | + * Explanation: For target = 100, there are 2 occurrences at indices 1 and 4 which follow an occurrence of key. |
| 18 | + * No other integers follow an occurrence of key, so we return 100. |
| 19 | + * |
| 20 | + * Example 2: |
| 21 | + * |
| 22 | + * Input: nums = [2,2,2,2,3], key = 2 |
| 23 | + * Output: 2 |
| 24 | + * Explanation: For target = 2, there are 3 occurrences at indices 1, 2, and 3 which follow an occurrence of key. |
| 25 | + * For target = 3, there is only one occurrence at index 4 which follows an occurrence of key. |
| 26 | + * target = 2 has the maximum number of occurrences following an occurrence of key, so we return 2. |
| 27 | + * |
| 28 | + * |
| 29 | + * Constraints: |
| 30 | + * |
| 31 | + * 2 <= nums.length <= 1000 |
| 32 | + * 1 <= nums[i] <= 1000 |
| 33 | + * The test cases will be generated such that the answer is unique. |
| 34 | + * |
| 35 | + */ |
| 36 | +pub struct Solution {} |
| 37 | + |
| 38 | +// problem: https://leetcode.com/problems/most-frequent-number-following-key-in-an-array/ |
| 39 | +// discuss: https://leetcode.com/problems/most-frequent-number-following-key-in-an-array/discuss/?currentPage=1&orderBy=most_votes&query= |
| 40 | + |
| 41 | +// submission codes start here |
| 42 | + |
| 43 | +impl Solution { |
| 44 | + pub fn most_frequent(nums: Vec<i32>, key: i32) -> i32 { |
| 45 | + nums.windows(2) |
| 46 | + .fold( |
| 47 | + std::collections::HashMap::<_, u32>::with_capacity(nums.len()), |
| 48 | + |mut map, win| { |
| 49 | + if win[0] == key { |
| 50 | + map.entry(win[1]).and_modify(|v| *v += 1).or_default(); |
| 51 | + } |
| 52 | + map |
| 53 | + }, |
| 54 | + ) |
| 55 | + .into_iter() |
| 56 | + .max_by_key(|(_, v)| *v) |
| 57 | + .unwrap() |
| 58 | + .0 |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +// submission codes end |
| 63 | + |
| 64 | +#[cfg(test)] |
| 65 | +mod tests { |
| 66 | + use super::*; |
| 67 | + |
| 68 | + #[test] |
| 69 | + fn test_2190_example_1() { |
| 70 | + let nums = vec![1, 100, 200, 1, 100]; |
| 71 | + let key = 1; |
| 72 | + |
| 73 | + let result = 100; |
| 74 | + |
| 75 | + assert_eq!(Solution::most_frequent(nums, key), result); |
| 76 | + } |
| 77 | + |
| 78 | + #[test] |
| 79 | + fn test_2190_example_2() { |
| 80 | + let nums = vec![2, 2, 2, 2, 3]; |
| 81 | + let key = 2; |
| 82 | + |
| 83 | + let result = 2; |
| 84 | + |
| 85 | + assert_eq!(Solution::most_frequent(nums, key), result); |
| 86 | + } |
| 87 | +} |
0 commit comments