Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public String[] findRestaurant(String[] list1, String[] list2) {
- HashMap<String, Integer> map = new HashMap<>();
- for(int i = 0; i < list1.length; i++) {
- map.put(list1[i], i);
- }
- ArrayList<String> ans = new ArrayList<>();
- int minSum = Integer.MAX_VALUE;
- int tmpSum;
- for(int i = 0; i < list2.length && i <= minSum; i++) {
- if(map.containsKey(list2[i])) {
- tmpSum = i + map.get(list2[i]);
- if(tmpSum < minSum) {
- minSum = tmpSum;
- ans.clear();
- ans.add(list2[i]);
- }
- else if(tmpSum == minSum) {
- ans.add(list2[i]);
- }
- }
- }
- return ans.toArray(new String[0]);
- }
- }
Add Comment
Please, Sign In to add comment