fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. static void selectionSort(int[] arr, int n) {
  11. for (int i = 0; i < n - 1; i++) {
  12. int min = i;
  13. for (int j = i + 1; j < n; j++) {
  14. if (arr[j] < arr[min]) {
  15. min = j;
  16. }
  17. }
  18. int tmp = arr[i];
  19. arr[i] = arr[min];
  20. arr[min] = tmp;
  21. }
  22. }
  23.  
  24. static int[] getRange(String input, boolean inclusiveEnd) {
  25. int[] temp = new int[100];
  26. int index = 0;
  27. String[] ranges = input.split(",\\s*");
  28. for (String range : ranges) {
  29. String[] parts = range.split("-");
  30. int start = Integer.parseInt(parts[0]);
  31. int end = Integer.parseInt(parts[1]);
  32.  
  33. for (int j = start; inclusiveEnd ? j <= end : j < end; j++) {
  34. temp[index++] = j;
  35. }
  36. }
  37. return Arrays.copyOf(temp, index);
  38. }
  39.  
  40.  
  41. static String arrToString(int[] boundArr, int boundLen, int[] busyArr, int busyLen) {
  42. String result = "";
  43. boolean inFreeSlot = false;
  44. int freeStart = 0;
  45.  
  46. for (int i = 0; i < boundLen; i++) {
  47. int hour = boundArr[i];
  48. boolean isBusyHour = false;
  49.  
  50. for (int j = 0; j < busyLen; j++) {
  51. if (hour == busyArr[j]) isBusyHour = true;
  52. }
  53.  
  54. if (!isBusyHour && !inFreeSlot) {
  55. freeStart = hour;
  56. inFreeSlot = true;
  57. }
  58.  
  59. if (isBusyHour && inFreeSlot) {
  60. result += freeStart + "-" + hour + ", ";
  61. inFreeSlot = false;
  62. }
  63. }
  64.  
  65. if (inFreeSlot) {
  66. result += freeStart + "-" + boundArr[boundLen - 1];
  67. }
  68. return result;
  69. }
  70.  
  71.  
  72. static String findFreeSlots(String busy, String boundary) {
  73. int[] boundArr = getRange("0-24", true);
  74. int[] busyArr = getRange("2-4, 8-10, 15-18", false);
  75.  
  76. int busyLen = busyArr.length;
  77. int boundLen = boundArr.length;
  78. return arrToString(boundArr, boundLen, busyArr, busyLen);
  79. }
  80.  
  81. public static void main(String[] args) {
  82. String busySlots = "8-10, 2-4, 15-18";
  83. String boundary = "0-24";
  84.  
  85. System.out.print(findFreeSlots(busySlots, boundary));
  86. }
  87. }
  88.  
Success #stdin #stdout 0.17s 60992KB
stdin
Standard input is empty
stdout
0-2, 4-8, 10-15, 18-24