Can you suggest the most efficient algorithm to solve the below problem:
If there are multiple IP addresses, how would you classify them into different cities that they belong to. If range for New York is: 134.123.65.5 - 135.123.124.21. and then the IP address is 134.126.232.12 does not belong to NY and IP address 134.123.89.45 does belong to NY.
My approach :
- Convert each range in the following format : 134*(256^3) + 123*(256^2) + 65*(256^1) + 5*(256^0)
- Store above value in a TreeMap with mapping - (ConvertedIpAddress, CityName)
- When given a query, convert address to a similar format and find map.floorKey(query)
Please tell me if approach is alright or if better approach exists.