|
| 1 | +package com.fishercoder.solutions; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.Collections; |
| 5 | +import java.util.Comparator; |
| 6 | +import java.util.HashMap; |
| 7 | +import java.util.List; |
| 8 | +import java.util.Map; |
| 9 | + |
| 10 | +/** |
| 11 | + * 1182. Shortest Distance to Target Color |
| 12 | + * |
| 13 | + * You are given an array colors, in which there are three colors: 1, 2 and 3. |
| 14 | + * You are also given some queries. Each query consists of two integers i and c, |
| 15 | + * return the shortest distance between the given index i and the target color c. If there is no solution return -1. |
| 16 | + * |
| 17 | + * Example 1: |
| 18 | + * Input: colors = [1,1,2,1,3,2,2,3,3], queries = [[1,3],[2,2],[6,1]] |
| 19 | + * Output: [3,0,3] |
| 20 | + * Explanation: |
| 21 | + * The nearest 3 from index 1 is at index 4 (3 steps away). |
| 22 | + * The nearest 2 from index 2 is at index 2 itself (0 steps away). |
| 23 | + * The nearest 1 from index 6 is at index 3 (3 steps away). |
| 24 | + * |
| 25 | + * Example 2: |
| 26 | + * Input: colors = [1,2], queries = [[0,3]] |
| 27 | + * Output: [-1] |
| 28 | + * Explanation: There is no 3 in the array. |
| 29 | + * |
| 30 | + * Constraints: |
| 31 | + * 1 <= colors.length <= 5*10^4 |
| 32 | + * 1 <= colors[i] <= 3 |
| 33 | + * 1 <= queries.length <= 5*10^4 |
| 34 | + * queries[i].length == 2 |
| 35 | + * 0 <= queries[i][0] < colors.length |
| 36 | + * 1 <= queries[i][1] <= 3 |
| 37 | + * */ |
| 38 | +public class _1182 { |
| 39 | + public static class Solution1 { |
| 40 | + public List<Integer> shortestDistanceColor(int[] colors, int[][] queries) { |
| 41 | + Map<Integer, List<Integer>> map = buildMap(colors); |
| 42 | + List<Integer> result = new ArrayList<>(); |
| 43 | + for (int[] query : queries) { |
| 44 | + result.add(binarySearch(query[0], map.get(query[1]))); |
| 45 | + } |
| 46 | + return result; |
| 47 | + } |
| 48 | + |
| 49 | + private Integer binarySearch(int index, List<Integer> indices) { |
| 50 | + if (indices == null) { |
| 51 | + return -1; |
| 52 | + } |
| 53 | + int left = 0; |
| 54 | + int right = indices.size() - 1; |
| 55 | + int minDistance = Integer.MAX_VALUE; |
| 56 | + while (left <= right) { |
| 57 | + int mid = left + (right - left) / 2; |
| 58 | + if (indices.get(mid) == index) { |
| 59 | + return 0; |
| 60 | + } else if (indices.get(mid) > index) { |
| 61 | + minDistance = Math.min(minDistance, indices.get(mid) - index); |
| 62 | + right = mid - 1; |
| 63 | + } else { |
| 64 | + minDistance = Math.min(minDistance, index - indices.get(mid)); |
| 65 | + left = mid + 1; |
| 66 | + } |
| 67 | + } |
| 68 | + return minDistance; |
| 69 | + } |
| 70 | + |
| 71 | + private Map<Integer, List<Integer>> buildMap(int[] colors) { |
| 72 | + Map<Integer, List<Integer>> map = new HashMap<>(); |
| 73 | + for (int i = 0; i < colors.length; i++) { |
| 74 | + if (!map.containsKey(colors[i])) { |
| 75 | + map.put(colors[i], new ArrayList<>()); |
| 76 | + } |
| 77 | + map.get(colors[i]).add(i); |
| 78 | + } |
| 79 | + for (Map.Entry<Integer, List<Integer>> entry : map.entrySet()) { |
| 80 | + Collections.sort(entry.getValue()); |
| 81 | + entry.setValue(entry.getValue()); |
| 82 | + } |
| 83 | + return map; |
| 84 | + } |
| 85 | + } |
| 86 | +} |
0 commit comments