Amazon | OA 2019 | Optimal Utilization
Given 2 lists a and b. Each element is a pair of integers where the first integer represents the unique id and the second integer represents a value. Your task is to find an element from a and an element form b such that the sum of their values is less or equal to target and as close to target as possible. Return a list of ids of selected elements. If no pair is possible, return an empty list. Example 1: Input: a = [[1, 2], [2, 4], [3, 6]] b = [[1, 2]] target = 7 Output: [[2, 1]] Explanation: There are only three combinations [1, 1], [2, 1], and [3, 1], which have a total sum of 4, 6 and 8, respectively. Since 6 is the largest sum that does not exceed 7, [2, 1] is the optimal pair. Example 2: Input: a = [[1, 3], [2, 5], [3, 7], [4, 10]] b = [[1, 2], [2, 3], [3, 4], [4, 5]] target = 10 Output: [[2, 4], [3, 2]] Explanation: There are two pairs possible. Element with id = 2 from the list `a` has a value 5, and element with id = 4 from the list `b` also has a value 5. Combined, they add up to 10. Similarily, element with id = 3 from `a` has a value 7, and element with id = 2 from `b` has a value 3. These also add up to 10. Therefore, the optimal pairs are [2, 4] and [3, 2]. Example 3: Input: a = [[1, 8], [2, 7], [3, 14]] b = [[1, 5], [2, 10], [3, 14]] target = 20 Output: [[3, 1]] Example 4: Input: a = [[1, 8], [2, 15], [3, 9]] b = [[1, 8], [2, 11], [3, 12]] target = 20 Output: [[1, 3], [3, 2]]
2-Pointers
Syntax side: Pay attention to how to print an array: System.out.println(Arrays.toString(int[] item));
1 import java.util.*; 2 /** 3 * https://leetcode.com/discuss/interview-question/373202 4 */ 5 public class OptimalUtilization { 6 public List<int[]> optimal(List<int[]> a, List<int[]> b, int target) { 7 if (a == null || a.isEmpty() || b == null || b.isEmpty()) { 8 return new ArrayList<int[]>(); 9 } 10 11 Collections.sort(a, (a1, a2) -> Integer.compare(a1[1], a2[1])); 12 Collections.sort(b, (b1, b2) -> Integer.compare(b1[1], b2[1])); 13 int m = a.size(); 14 int n = b.size(); 15 int i = 0; 16 int j = n - 1; 17 List<int[]> result = new ArrayList<>(); 18 int max = Integer.MIN_VALUE; 19 while (i < m && j >= 0) { 20 int sum = a.get(i)[1] + b.get(j)[1]; 21 if (sum <= target) { 22 // maybe duplicate ele 23 if (sum > max) { 24 result.clear(); 25 max = sum; 26 result.add(new int[]{a.get(i)[0], b.get(j)[0]}); 27 } else if (sum == max) { 28 result.add(new int[]{a.get(i)[0], b.get(j)[0]}); 29 } 30 i++; 31 } else { 32 j--; 33 } 34 } 35 return result; 36 } 37 38 public static void main(String[] args) { 39 OptimalUtilization sol = new OptimalUtilization(); 40 List<int[]> aa = new ArrayList<>(); 41 aa.add(new int[]{1, 8}); 42 aa.add(new int[]{2, 15}); 43 aa.add(new int[]{3, 9}); 44 List<int[]> bb = new ArrayList<>(); 45 bb.add(new int[]{1, 8}); 46 bb.add(new int[]{2, 11}); 47 bb.add(new int[]{3, 12}); 48 List<int[]> res = sol.optimal(aa, bb, 20); 49 for (int[] item : res) { 50 System.out.println(Arrays.toString(item)); 51 } 52 } 53 }