Time：2020-8-17

# D117 599. Minimum Index Sum of Two Lists

599. Minimum Index Sum of Two Lists

## Topic analysis

Given two arrays, return their intersection and return the smallest sum of their subscripts in the intersection set.

## thinking

Because if an element is not in another array, it needs to be compared one by one. Therefore, we turn over the two arrays and use the isset function to determine whether there are elements.

The minimum sum was preserved in the extracorporeal circulation, and whether the sum of subscripts of two identical elements was less than the minimum value was judged. If yes, replace and empty the intersection array. Otherwise, judge whether it is equal to the current minimum value. If it is equal, it is appended to the intersection array. If the inequality is greater than the minimum value, discard it.

## Final code

``````<?php
class Solution {

/**
* @param String[] \$list1
* @param String[] \$list2
* @return String[]
*/
function findRestaurant(\$list1, \$list2) {
\$list1 = array_flip(\$list1);
\$list2 = array_flip(\$list2);
\$min = 99999999999;
\$intersect = [];
foreach(\$list1 as \$name => \$v){
if(!isset(\$list2[\$name])){
continue;
}
if(\$v+\$list2[\$name] < \$min){
\$min = \$v + \$list2[\$name];
\$intersect = [\$name];
}
else if(\$v+\$list2[\$name] == \$min){
\$intersect[] = \$name;
}
}
return \$intersect;
}
}``````