---
description: >-
  https://leetcode.com/discuss/interview-question/1692516/Amazon-OA1-full-time-question-Jan-2022
---

# NearestRetailer

![](https://1589944109-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M33ghpGB0tSXSGhMSB5%2Fuploads%2FXHvcvGAZ8WiIEZZ7Utbl%2Fimage.png?alt=media\&token=54ac0055-c38c-418c-a42b-735a89df2472)

![](https://1589944109-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M33ghpGB0tSXSGhMSB5%2Fuploads%2FGfirruWjQVN5agMaHs2F%2Fimage.png?alt=media\&token=b3d30e5b-c147-4125-bc65-5123070f25a9)

![](https://1589944109-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M33ghpGB0tSXSGhMSB5%2Fuploads%2FwAAvlGiZvANUzdWjQm2O%2Fimage.png?alt=media\&token=162aa7eb-e3fe-40de-9bb8-6f5e3d1915f6)

![](https://1589944109-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M33ghpGB0tSXSGhMSB5%2Fuploads%2FnORu6z3N3w77DlaibZ0P%2Fimage.png?alt=media\&token=05714a69-d937-44aa-b0ae-534a8ce96e75)



**Solution:**

[https://leetcode.com/discuss/interview-question/1692516/Amazon-OA1-full-time-question-Jan-2022](https://leetcode.com/discuss/interview-question/1692516/Amazon-OA1-full-time-question-Jan-2022)



Using a Treemap and taking advantage of the fact that y is between 1 and 100. I bucket sort it in a treemap, treemap functions are log(n) but turn into log(100). Possibly missed edge cases but the happy path seems to work and the logic makes sense. O(nlogn) I think, comments are in the code, the formatting is messed up but not sure how to change it. There may be an easier way too, but I think the key is abusing the fact that the y values are so low, since there's no other reason they would have made the condition 1<=request\[i]\[1]<=100.

```
// "static void main" must be defined in a public class.
public class Main {
public static void main(String[] args) {
int [][] retailers = new int[][]{{1,2},{2,3},{1,5}};
int [][] requests = new int[][]{{1,1},{1,4}};
System.out.println(Arrays.toString(countNumberOfRetailers(retailers,requests)));
}
public static int [] countNumberOfRetailers(int [][] retailers, int[][] requests){
//bucket sort since we know y is between 1 and 100
TreeMap<Integer, List<int[]>> map = new TreeMap<>();
Arrays.sort(retailers, (a,b)->{
return b[1]-a[1];
});
ArrayList<int[]> current = new ArrayList<>();
//keep a running total of valid retailers at each existing Y
//clone can only be called at most 100 times so amortized 100*N+O(nlogn) since there can be only at most 100 keys (different y coordinates)
for(int[] retailer: retailers){
int y = retailer[1];
List<int[]> temp = map.get(y);
if(!map.containsKey(y)){
temp = (ArrayList)current.clone();
}
temp.add(retailer);
current.add(retailer);
map.put(y, temp);
}

    //at most 100*nlogn
    for(Map.Entry<Integer,List<int[]>> entry: map.entrySet()){
        List<int[]> list = entry.getValue();
        //sort by x
        Collections.sort(list, (a,b)->{
            return a[0] - b[0]; 
        });
        map.put(entry.getKey(),list);
    }
    
    //now for each request we can grab a list of valid retailers eliminating y and binary search the sorted list O(logn)
    int [] res = new int[requests.length];
    for(int i=0; i<requests.length;++i){
        int [] request = requests[i];
        List<int[]> sortedList = map.ceilingEntry(request[1]).getValue();
        //System.out.println(sortedList.size()+" "+Arrays.toString(request));
        //binary search
        int start = 0;
        int end = sortedList.size()-1;
        int x = request[0];
        while(start<end){
            int mid = start+(end-start)/2;
            if( x < sortedList.get(mid)[0]){
                start=mid+1;
            }else{
                end = mid;
            }
        }
        res[i] = sortedList.size()-start;
    }
    return res;
    
}
}
```

\


