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. for (int j = start; inclusiveEnd ? j <= end : j < end; j++) {
  33. temp[index++] = j;
  34. }
  35. }
  36. return Arrays.copyOf(temp, index);
  37. }
  38.  
  39.  
  40. static String arrToString(int[] boundArr, int boundLen, int[] busyArr, int busyLen) {
  41. String result = "";
  42. boolean inFreeSlot = false;
  43. int freeStart = 0;
  44. for (int i = 0; i < boundLen; i++) {
  45. int hour = boundArr[i];
  46. boolean isBusyHour = false;
  47. for (int j = 0; j < busyLen; j++) {
  48. if (hour == busyArr[j]) isBusyHour = true;
  49. }
  50. if (!isBusyHour && !inFreeSlot) {
  51. freeStart = hour;
  52. inFreeSlot = true;
  53. }
  54. if (isBusyHour && inFreeSlot) {
  55. result += freeStart + "-" + hour + ", ";
  56. inFreeSlot = false;
  57. }
  58. }
  59. if (inFreeSlot) {
  60. result += freeStart + "-" + boundArr[boundLen - 1];
  61. }
  62. return result;
  63. }
  64.  
  65.  
  66. static String findFreeSlots(String busy, String boundary) {
  67. int[] boundArr = getRange("0-24", true);
  68. int[] busyArr = getRange("2-4, 8-10, 15-18", false);
  69. int busyLen = busyArr.length;
  70. int boundLen = boundArr.length;
  71. return arrToString(boundArr, boundLen, busyArr, busyLen);
  72. }
  73.  
  74. public static void main(String[] args) {
  75. String busySlots = "3-10, 2-4, 15-18";
  76. String boundary = "0-24";
  77.  
  78. System.out.print(findFreeSlots(busySlots, boundary));
  79. }
  80. }
Success #stdin #stdout 0.2s 60964KB
stdin
Standard input is empty
stdout
0-2, 4-8, 10-15, 18-24