|
1 | 1 | // https://leetcode.com/problems/minimum-index-sum-of-two-lists
|
2 |
| -// |
| 2 | +// |
3 | 3 | // Suppose Andy and Doris want to choose a restaurant for dinner, and they both have a list of favorite restaurants represented by strings.
|
4 |
| -// |
| 4 | +// |
5 | 5 | // You need to help them find out their **common interest** with the **least list index sum**. If there is a choice tie between answers, output all of them with no order requirement. You could assume there always exists an answer.
|
6 |
| -// |
| 6 | +// |
7 | 7 | // **Example 1:**
|
8 |
| -// |
| 8 | +// |
9 | 9 | // ```
|
10 | 10 | // **Input:** list1 = ["Shogun","Tapioca Express","Burger King","KFC"], list2 = ["Piatti","The Grill at Torrey Pines","Hungry Hunter Steakhouse","Shogun"]
|
11 | 11 | // **Output:** ["Shogun"]
|
12 | 12 | // **Explanation:** The only restaurant they both like is "Shogun".
|
13 | 13 | // ```
|
14 |
| -// |
| 14 | +// |
15 | 15 | // **Example 2:**
|
16 |
| -// |
| 16 | +// |
17 | 17 | // ```
|
18 | 18 | // **Input:** list1 = ["Shogun","Tapioca Express","Burger King","KFC"], list2 = ["KFC","Shogun","Burger King"]
|
19 | 19 | // **Output:** ["Shogun"]
|
20 | 20 | // **Explanation:** The restaurant they both like and have the least index sum is "Shogun" with index sum 1 (0+1).
|
21 | 21 | // ```
|
22 |
| -// |
| 22 | +// |
23 | 23 | // **Constraints:**
|
24 |
| -// |
| 24 | +// |
25 | 25 | // * `1 <= list1.length, list2.length <= 1000`
|
26 | 26 | // * `1 <= list1[i].length, list2[i].length <= 30`
|
27 | 27 | // * `list1[i]` and `list2[i]` consist of spaces `' '` and English letters.
|
28 | 28 | // * All the stings of `list1` are **unique**.
|
29 | 29 | // * All the stings of `list2` are **unique**.
|
30 | 30 |
|
31 | 31 | pub fn find_restaurant(list1: Vec<String>, list2: Vec<String>) -> Vec<String> {
|
32 |
| - |
| 32 | + let list1_map = list1 |
| 33 | + .iter() |
| 34 | + .enumerate() |
| 35 | + .map(|(i, s)| (s, i)) |
| 36 | + .collect::<std::collections::HashMap<_, _>>(); |
| 37 | + let mut res = vec![]; |
| 38 | + let mut min_sum = std::i32::MAX as usize; |
| 39 | + for i in 0..list2.len() { |
| 40 | + if let Some(j) = list1_map.get(&list2[i]) { |
| 41 | + if i + j == min_sum { |
| 42 | + res.push(list1[*j].clone()); |
| 43 | + } |
| 44 | + if i + j < min_sum { |
| 45 | + min_sum = i + j; |
| 46 | + res.clear(); |
| 47 | + res.push(list1[*j].clone()); |
| 48 | + } |
| 49 | + } |
33 | 50 | }
|
| 51 | + return res; |
| 52 | +} |
34 | 53 |
|
35 | 54 | #[test]
|
36 | 55 | pub fn t1() {
|
| 56 | + assert_eq!( |
| 57 | + find_restaurant( |
| 58 | + vec![ |
| 59 | + "Shogun".to_string(), |
| 60 | + "Tapioca Express".to_string(), |
| 61 | + "Burger King".to_string(), |
| 62 | + "KFC".to_string() |
| 63 | + ], |
| 64 | + vec![ |
| 65 | + "Piatti".to_string(), |
| 66 | + "The Grill at Torrey Pines".to_string(), |
| 67 | + "Hungry Hunter Steakhouse".to_string(), |
| 68 | + "Shogun".to_string() |
| 69 | + ] |
| 70 | + ), |
| 71 | + vec!["Shogun".to_string()] |
| 72 | + ); |
37 | 73 | }
|
0 commit comments