File tree 1 file changed +22
-44
lines changed
scripts/algorithms/M/Minimum Index Sum of Two Lists
1 file changed +22
-44
lines changed Original file line number Diff line number Diff line change
1
+ // Runtime: 70 ms (Top 74.0%) | Memory: 37.30 MB (Top 38.8%)
2
+
1
3
class Solution {
2
4
public:
3
5
vector<string> findRestaurant (vector<string>& list1, vector<string>& list2) {
4
- vector<string>vc;
5
- unordered_map<string,int >umap,umap1;
6
-
7
- for (int i=0 ;i<list1.size ();i++)
8
- {
9
-
10
-
11
- umap[list1[i]]=i;
12
-
13
- }
14
-
15
-
16
- for (int i=0 ;i<list2.size ();i++)
17
- {
18
-
19
-
20
- umap1[list2[i]]=i;
6
+ int min=INT_MAX;
7
+ unordered_map<string,int >un;
8
+ vector<string>v;
9
+ for (int i=0 ;i<list1.size ();i++){
10
+ un[list1[i]]=i;
11
+ }
12
+ for (int i=0 ;i<list2.size ();i++){
13
+ if (un.count (list2[i])!=0 ){
14
+ int sum=i+un[list2[i]];
15
+ if (sum<min){
16
+ min=sum;
17
+ v.clear ();
18
+ v.push_back (list2[i]);
19
+ }else if (sum==min){
20
+ v.push_back (list2[i]);
21
+ }
22
+ }
23
+ }
24
+ return v;
21
25
22
- }
23
- int min=10000 ;
24
- for (auto i:umap)
25
- {
26
- int k=0 ;
27
- for (auto j :umap1)
28
- {
29
-
30
- if (i.first ==j.first )
31
- {
32
- k=i.second +j.second ;
33
- if (min>k)
34
- {vc.clear ();
35
- min=k;
36
- vc.push_back (j.first );
37
- }
38
- else if (k==min)
39
- {
40
- vc.push_back (j.first );
41
- }
42
26
43
27
}
44
- }
45
- }
46
-
47
-
48
- return vc;
49
- }
50
- };
28
+ };
You can’t perform that action at this time.
0 commit comments