|
| 1 | +package problem12979; |
| 2 | + |
| 3 | +public class BaseStationInstallation { |
| 4 | + public static void main(String[] args) { |
| 5 | + var sol = new BaseStationInstallation(); |
| 6 | + var n1 = 11; |
| 7 | + var stations1 = new int[]{4, 11}; |
| 8 | + var w1 = 1; |
| 9 | + var expected1 = 3; |
| 10 | + var result1 = sol.solution(n1, stations1, w1); |
| 11 | + System.out.println("test1: expected: " + expected1 + ", result: " + result1); |
| 12 | + var n2 = 16; |
| 13 | + var stations2 = new int[]{9}; |
| 14 | + var w2 = 2; |
| 15 | + var expected2 = 3; |
| 16 | + var result2 = sol.solution(n2, stations2, w2); |
| 17 | + System.out.println("test2: expected: " + expected2 + ", result: " + result2); |
| 18 | + } |
| 19 | + |
| 20 | + private static int needStationCount(int n, int w) { |
| 21 | + return (int) Math.ceil((double) n / (2 * w + 1)); |
| 22 | + } |
| 23 | + |
| 24 | + public int solution(int n, int[] stations, int w) { |
| 25 | + if (stations.length == 0) { |
| 26 | + return needStationCount(n, w); |
| 27 | + } |
| 28 | + |
| 29 | + var needCount = 0; |
| 30 | + var index = 0; |
| 31 | + var boundary = stations[index] - w; |
| 32 | + var shadow = 1; |
| 33 | + |
| 34 | + while (true) { |
| 35 | + if (boundary - shadow > 0) { |
| 36 | + needCount += needStationCount(boundary - shadow, w); |
| 37 | + } |
| 38 | + shadow = boundary + 2 * w + 1; |
| 39 | + if (shadow > n) { |
| 40 | + break; |
| 41 | + } |
| 42 | + |
| 43 | + if (index < stations.length - 1) { |
| 44 | + index++; |
| 45 | + boundary = stations[index] - w; |
| 46 | + } else { |
| 47 | + needCount += needStationCount(n - shadow + 1, w); |
| 48 | + break; |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + return needCount; |
| 53 | + } |
| 54 | +} |
0 commit comments