fork download
  1. #include <iostream>
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5. class Interval
  6. {
  7. public :
  8. int startTime;
  9. int endTime;
  10. Interval(int startTimeObj,int endTimeObj)
  11. {
  12. startTime = startTimeObj;
  13. endTime = endTimeObj;
  14. }
  15. };
  16. class RoomService
  17. {
  18. public:
  19. int roomNum;
  20. unordered_map <int ,vector<Interval>> roomMap;
  21. RoomService(int n)
  22. {
  23.  
  24. roomNum =n;
  25. }
  26.  
  27. int createMeeting(int startTime,int endTime)
  28. {
  29. int roomAssigned =-1;
  30. Interval givenInterval(startTime,endTime);
  31. for(int i=0;i<roomNum;i++)
  32. {
  33. if(!roomMap.count(i))
  34. {
  35. roomAssigned = i;
  36. break;
  37. }
  38. else
  39. {
  40. int isAvbl =0;
  41. for(auto it : roomMap[i])
  42. {
  43. if(it.endTime < startTime || endTime < it.startTime)
  44. {
  45. isAvbl = 1;
  46. }
  47. else
  48. {
  49. isAvbl=0;
  50. break;
  51. }
  52.  
  53. }
  54.  
  55. if(isAvbl)
  56. {
  57. roomAssigned = i;
  58. break;
  59. }
  60. }
  61. }
  62. if(roomAssigned!=-1)
  63. {
  64. roomMap[roomAssigned].push_back(givenInterval);
  65. return roomAssigned;
  66. }
  67. else
  68. {
  69. // throw expception
  70. return roomAssigned;
  71. }
  72. }
  73. };
  74.  
  75. int main() {
  76. RoomService rs (2);
  77. cout<<"Started assigning";
  78. cout<<"\n";
  79. cout<<rs.createMeeting(1000,2000);
  80. cout<<"\n";
  81. cout<<rs.createMeeting(500,700);
  82. cout<<"\n";
  83. cout<<rs.createMeeting(1100,1700);
  84. cout<<"\n";
  85. return 0;
  86. }
Success #stdin #stdout 0s 5304KB
stdin
Standard input is empty
stdout
Started assigning
0
0
1