Skip to content

Commit 2921c79

Browse files
committed
feat: add more
1 parent 4a39cbb commit 2921c79

File tree

2 files changed

+46
-9
lines changed

2 files changed

+46
-9
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,73 @@
11
// https://leetcode.com/problems/minimum-index-sum-of-two-lists
2-
//
2+
//
33
// 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+
//
55
// 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+
//
77
// **Example 1:**
8-
//
8+
//
99
// ```
1010
// **Input:** list1 = ["Shogun","Tapioca Express","Burger King","KFC"], list2 = ["Piatti","The Grill at Torrey Pines","Hungry Hunter Steakhouse","Shogun"]
1111
// **Output:** ["Shogun"]
1212
// **Explanation:** The only restaurant they both like is "Shogun".
1313
// ```
14-
//
14+
//
1515
// **Example 2:**
16-
//
16+
//
1717
// ```
1818
// **Input:** list1 = ["Shogun","Tapioca Express","Burger King","KFC"], list2 = ["KFC","Shogun","Burger King"]
1919
// **Output:** ["Shogun"]
2020
// **Explanation:** The restaurant they both like and have the least index sum is "Shogun" with index sum 1 (0+1).
2121
// ```
22-
//
22+
//
2323
// **Constraints:**
24-
//
24+
//
2525
// * `1 <= list1.length, list2.length <= 1000`
2626
// * `1 <= list1[i].length, list2[i].length <= 30`
2727
// * `list1[i]` and `list2[i]` consist of spaces `' '` and English letters.
2828
// * All the stings of `list1` are **unique**.
2929
// * All the stings of `list2` are **unique**.
3030

3131
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+
}
3350
}
51+
return res;
52+
}
3453

3554
#[test]
3655
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+
);
3773
}

Array/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ mod _0496_next_greater_element_i;
1818
mod _0500_keyboard_row;
1919
mod _0566_reshape_the_matrix;
2020
mod _0575_distribute_candies;
21+
mod _0599_minimum_index_sum_of_two_lists;
2122
mod _0661_image_smoother;
2223
mod _0682_baseball_game;
2324
mod _0697_degree_of_an_array;

0 commit comments

Comments
 (0)