daily pastebin goal
76%
SHARE
TWEET

Untitled

a guest May 26th, 2018 56 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Solution {
  2.     public String[] findRestaurant(String[] list1, String[] list2) {
  3.         HashMap<String, Integer> map = new HashMap<>();
  4.        
  5.         for(int i = 0; i < list1.length; i++) {
  6.             map.put(list1[i], i);
  7.         }
  8.        
  9.         ArrayList<String> ans = new ArrayList<>();
  10.         int minSum = Integer.MAX_VALUE;
  11.         int tmpSum;
  12.         for(int i = 0; i < list2.length && i <= minSum; i++) {
  13.             if(map.containsKey(list2[i])) {
  14.                 tmpSum = i + map.get(list2[i]);
  15.                
  16.                 if(tmpSum < minSum) {
  17.                     minSum = tmpSum;
  18.                     ans.clear();
  19.                     ans.add(list2[i]);
  20.                 }
  21.                 else if(tmpSum == minSum) {
  22.                     ans.add(list2[i]);
  23.                 }
  24.             }
  25.         }
  26.        
  27.         return ans.toArray(new String[0]);
  28.     }
  29. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top