| 11 | import static java.util.stream.Collectors.toList; |
| 12 | |
| 13 | class Result { |
| 14 | |
| 15 | // Create router class for easy reference to location and range |
| 16 | public static class Router { |
| 17 | int location; |
| 18 | int range; |
| 19 | |
| 20 | Router(int location, int range) { |
| 21 | this.location = location; |
| 22 | this.range = range; |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | public int getServedBuildings(List<Integer> buildingCount, List<Integer> routerLocation, List<Integer> routerRange) { |
| 27 | Integer[] buildingsServedArray = buildingCount.toArray(new Integer[0]); |
| 28 | List<Router> routers = new ArrayList<>(); |
| 29 | int buildingsServed = 0; |
| 30 | |
| 31 | // Loop and add location and range to routers list - (subtract 1 from location for 0 indexing) |
| 32 | for(int i = 0 ; i < routerLocation.size() ; i++) { |
| 33 | routers.add(new Router((routerLocation.get(i) - 1), routerRange.get(i))); |
| 34 | } |
| 35 | // Loop over routers |
| 36 | for(Router i : routers) { |
| 37 | // Check if router location minus range is in bounds (greater than or equal to 0) - set index |
| 38 | int index = Math.max(i.location - i.range, 0); |
| 39 | // While index is in bounds and less than router location plus router range |
| 40 | while(index < buildingsServedArray.length && index <= i.location + i.range) { |
| 41 | // Subtract 1 from each building in range |
| 42 | buildingsServedArray[index] -= 1; |
| 43 | // Increment index |
| 44 | index++; |
| 45 | } |
| 46 | } |
| 47 | // Loop buildings served array |
| 48 | for(int i : buildingsServedArray) { |
| 49 | // If the building has a value of 0 or less, it has been served |
| 50 | if(i <= 0) { |
| 51 | // Count |
| 52 | buildingsServed++; |
| 53 | } |
| 54 | } |
| 55 | // Return total buildings served |
| 56 | return buildingsServed; |
| 57 | } |
| 58 | |
| 59 | } |
| 60 | |
| 61 | |
| 62 | public class Solution { |
nothing calls this directly
no outgoing calls
no test coverage detected