Guest User

Untitled

a guest
Feb 23rd, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. using System;
  2.  
  3. class Solution
  4. {
  5. public static int[] MeetingPlanner(int[,] slotsA, int[,] slotsB, int dur)
  6. {
  7. if(slotsA == null || slotsB == null || dur <= 0)
  8. {
  9. return new int[0];
  10. }
  11.  
  12. var rowsA = slotsA.GetLength(0);
  13. var rowsB = slotsB.GetLength(0);
  14.  
  15. int indexA = 0;
  16. int indexB = 0;
  17.  
  18. while(indexA < rowsA && indexB < rowsB)
  19. {
  20. var startA = slotsA[indexA, 0];
  21. var endA = slotsA[indexA, 1];
  22.  
  23. var startB = slotsB[indexB, 0];
  24. var endB = slotsB[indexB, 1];
  25.  
  26. var overlapStart = Math.Max(startA, startB);
  27. var overlapEnd = Math.Min(endA, endB);
  28.  
  29. var interval = overlapEnd - overlapStart;
  30. if(interval >= dur)
  31. {
  32. return new int[]{overlapStart, overlapStart + dur};
  33. }
  34. else if(endA < endB)
  35. {
  36. indexA++;
  37. }
  38. else
  39. {
  40. indexB++;
  41. }
  42. }
  43.  
  44. return new int[0];
  45. }
  46.  
  47. static void Main(string[] args)
  48. {
  49.  
  50. }
  51. }
  52.  
  53. /*
  54. keyword:
  55. Each solts, nonoverlap , sorted by start time, ascending order
  56. two intervals overlap -> given dur -> return
  57. advance one of pointers
  58.  
  59. 10 50 60 120
  60. ----------- ___________
  61.  
  62. 0 15
  63. _____
  64.  
  65.  
  66. ___________________
  67. |______|
  68.  
  69.  
  70. max - min overlap - nonexist
  71.  
  72.  
  73. time complexity :
  74.  
  75. O(n + m), n time solts, m length of time slots second one
  76.  
  77. */
Add Comment
Please, Sign In to add comment