Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- static class Pin {
- String name;
- int x;
- int y;
- public Pin(String name_, int x_, int y_) {
- name = name_;
- x = x_;
- y = y_;
- }
- public int dist(Pin otherPin) {
- return Math.abs(x - otherPin.x) + Math.abs(y - otherPin.y);
- }
- }
- public static List<String> nearestPins(List<String> pins, List<Integer> xCoordinates, List<Integer> yCoordinates, List<String> queries) {
- // WRITE YOUR BRILLIANT CODE HERE
- return List.of();
- Map<Integer, ArrayList<Pin>> xTopins = new HashMap<>();
- Map<Integer, ArrayList<Pin>> yTopins = new HashMap<>();
- Map<String, Pin> pinByName = new HashMap<>();
- for (int i = 0; i < pins.size(); i++) {
- String pinName = pins.get(i);
- int x = xCoordinates.get(i);
- int y = yCoordinates.get(i);
- Pin pin = new Pin(pinName, x, y);
- xTopins.computeIfAbsent(x, v -> new ArrayList<>());
- yTopins.computeIfAbsent(y, v -> new ArrayList<>());
- xTopins.get(x).add(pin);
- yTopins.get(y).add(pin);
- pinByName.put(pinName, pin);
- }
- List<String> ans = new ArrayList<>();
- Map<String, String> cache = new HashMap<>();
- for (String name : queries) {
- if (cache.containsKey(name)) {
- ans.add(cache.get(name));
- continue;
- }
- Pin pin = pinByName.get(name);
- int minDist = Integer.MAX_VALUE;
- String closest = null;
- List<Pin> searchpins = xTopins.get(pin.x);
- searchpins.addAll(yTopins.get(pin.y));
- for (Pin otherPin : searchpins) {
- if (otherPin.equals(pin)) {
- continue;
- }
- int dist = pin.dist(otherPin);
- if (closest == null || dist < minDist || (dist == minDist && otherPin.name.compareTo(closest) < 0)) {
- minDist = pin.dist(otherPin);
- closest = otherPin.name;
- }
- }
- if (closest != null) {
- ans.add(closest);
- cache.put(name, closest);
- } else {
- ans.add("NONE");
- }
- }
- return ans;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement