1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| class Solution { public int minOperations(int[] target, int[] arr) { int n = target.length; Map<Integer, Integer> map = new HashMap<>(); for (int i = 0; i < n; ++i) map.put(target[i], i); List<Integer> list = new ArrayList<>(); for (int x : arr) { if (map.containsKey(x)) { list.add(map.get(x)); } } int length = list.size(), max = 0; int[] f = new int[length + 1], g = new int[length + 1]; Arrays.fill(g, Integer.MAX_VALUE); for (int i = 0; i < length; ++i) { int index = list.get(i); int left = 0, right = length; while (left < right) { int mid = left + right + 1 >> 1; if (g[mid] < index) left = mid; else right = mid - 1; } int t = right + 1; f[i] = t; g[t] = Math.min(g[t], index); max = Math.max(max, f[i]); } return n - max; } }
|