fork download
  1.  
  2. /*
  3. PROBLEM:
  4. Minimum Rental Cars - Brute Force
  5.  
  6. Given an unsorted array of rental records:
  7.  
  8.   [recordId, pickupTime, returnTime]
  9.  
  10. Assign every rental to a car while using the
  11. minimum possible number of cars.
  12.  
  13. Car IDs start from 0.
  14.  
  15. Return a String[] in increasing car-ID order.
  16.  
  17. Example:
  18.  
  19. requests = [
  20.   [101, 1, 3],
  21.   [102, 2, 4],
  22.   [103, 4, 6]
  23. ]
  24.  
  25. Output:
  26.   ["0: 101 103", "1: 102"]
  27.  
  28.  
  29. RULES:
  30.  
  31. 1. Sort rentals by:
  32.   a. pickupTime
  33.   b. returnTime
  34.   c. recordId
  35.  
  36. 2. A car returned at time t can be reused at t.
  37.  
  38. 3. Always assign the smallest available car ID.
  39.  
  40. 4. If no car is available, create a new car.
  41.  
  42. 5. Zero-duration rentals [t, t):
  43.   - Always assign to car 0.
  44.   - Create car 0 if necessary.
  45.   - Do not update car availability.
  46.  
  47.  
  48. APPROACH: Sorting + Linear Scan (Brute Force)
  49.  
  50. We maintain two arrays:
  51.  
  52. 1. availableAt[carId]
  53.  
  54.   Stores the earliest time when the car
  55.   can be reused.
  56.  
  57.   Example:
  58.  
  59.   availableAt = [10, 5, 8]
  60.  
  61.   Car 0 becomes available at time 10.
  62.   Car 1 becomes available at time 5.
  63.   Car 2 becomes available at time 8.
  64.  
  65.  
  66. 2. histories[carId]
  67.  
  68.   Stores rental record IDs assigned to each car.
  69.  
  70.   Example:
  71.  
  72.   histories = [
  73.   [101, 103],
  74.   [102]
  75.   ]
  76.  
  77.  
  78. ALGORITHM:
  79.  
  80. 1. Sort requests by pickupTime, returnTime, recordId.
  81.  
  82. 2. Initialize:
  83.   availableAt = []
  84.   histories = []
  85.  
  86. 3. For each rental:
  87.  
  88.   a. If pickupTime == returnTime:
  89.   - Create car 0 if necessary.
  90.   - Append recordId to histories[0].
  91.   - Do not update availableAt.
  92.   - Continue.
  93.  
  94.   b. Otherwise, scan availableAt from index 0.
  95.  
  96.   c. Find the first car satisfying:
  97.  
  98.   availableAt[carId] <= pickupTime
  99.  
  100.   d. If found, reuse that car.
  101.  
  102.   e. Otherwise, create a new car.
  103.  
  104.   f. Append recordId to that car's history.
  105.  
  106.   g. Update its availability:
  107.  
  108.   availableAt[carId] = returnTime
  109.  
  110. 4. Format and return histories.
  111.  
  112.  
  113. WHY DOES LINEAR SCANNING WORK?
  114.  
  115. Car IDs are stored in increasing order:
  116.  
  117.   0, 1, 2, 3, ...
  118.  
  119. We scan from index 0.
  120.  
  121. The first car satisfying:
  122.  
  123.   availableAt[carId] <= pickupTime
  124.  
  125. is automatically the smallest available car ID.
  126.  
  127. Therefore, we do not need a min-heap for the
  128. brute-force solution.
  129.  
  130.  
  131. WHY IS THIS OPTIMAL IN NUMBER OF CARS?
  132.  
  133. For every positive-duration rental:
  134.  
  135. - If a car is free, reuse it.
  136. - Otherwise, every existing car is occupied
  137.   by an overlapping rental.
  138.  
  139. In that case, creating another car is necessary.
  140.  
  141. Therefore, this greedy assignment minimizes
  142. the total number of cars.
  143.  
  144.  
  145. TIME COMPLEXITY: O(N^2)
  146.  
  147. N = number of rental records
  148. C = number of cars created, C <= N
  149.  
  150. 1. Sorting:
  151.   O(N log N)
  152.  
  153. 2. Processing N rentals:
  154.   For each rental, scan up to C cars.
  155.  
  156.   O(N * C)
  157.  
  158. 3. Building output:
  159.   O(N + C) in record count.
  160.  
  161. Total:
  162.  
  163.   O(N log N + N*C)
  164.  
  165. Worst case C = N:
  166.  
  167.   O(N^2)
  168.  
  169.  
  170. SPACE COMPLEXITY: O(N)
  171.  
  172. - Sorted requests: O(N)
  173. - availableAt: O(C)
  174. - histories: O(N + C)
  175. - Output: O(N + C) in record count
  176.  
  177. Since C <= N:
  178.  
  179.   O(N)
  180.  
  181. Sorting implementation workspace is excluded.
  182. */
  183.  
  184.  
  185. // ==================== SOLUTION ====================
  186.  
  187. function solve(requests) {
  188.  
  189. if (requests.length === 0) return [];
  190.  
  191.  
  192. // STEP 1: Sort rentals by:
  193. // pickupTime, returnTime, recordId.
  194. //
  195. // Copy to avoid modifying the input.
  196. const records = [...requests].sort((a, b) =>
  197. a[1] - b[1] ||
  198. a[2] - b[2] ||
  199. a[0] - b[0]
  200. );
  201.  
  202.  
  203. // STEP 2: Initialize arrays.
  204. //
  205. // availableAt[carId] = time when car becomes free.
  206. const availableAt = [];
  207.  
  208. // histories[carId] = rental IDs assigned to car.
  209. const histories = [];
  210.  
  211.  
  212. // STEP 3: Process every rental.
  213. for (const [recordId, pickup, returned] of records) {
  214.  
  215.  
  216. // CONDITION 1: Zero-duration rental.
  217. //
  218. // Always assign to car 0.
  219. // Do not modify car availability.
  220. if (pickup === returned) {
  221.  
  222. if (histories.length === 0) {
  223.  
  224. // Create car 0 if it does not exist.
  225. histories.push([]);
  226.  
  227. // Initial availability of car 0.
  228. availableAt.push(0);
  229. }
  230.  
  231. histories[0].push(recordId);
  232.  
  233. continue;
  234. }
  235.  
  236.  
  237. // CONDITION 2: Find the smallest available
  238. // car ID using a linear scan.
  239. let car = -1;
  240.  
  241. for (let carId = 0; carId < availableAt.length; carId++) {
  242.  
  243. // Endpoint reuse is allowed.
  244. //
  245. // <= is important, not <.
  246. if (availableAt[carId] <= pickup) {
  247.  
  248. car = carId;
  249.  
  250. // First available car is the smallest ID.
  251. break;
  252. }
  253. }
  254.  
  255.  
  256. // CONDITION 3: No available car found.
  257. if (car === -1) {
  258.  
  259. // Create next consecutive car ID.
  260. car = histories.length;
  261.  
  262. histories.push([]);
  263.  
  264. // Initialize new car availability.
  265. availableAt.push(0);
  266. }
  267.  
  268.  
  269. // STEP 4: Record the assignment.
  270. histories[car].push(recordId);
  271.  
  272.  
  273. // STEP 5: Update this car's availability.
  274. availableAt[car] = returned;
  275. }
  276.  
  277.  
  278. // STEP 6: Format output.
  279. //
  280. // Car IDs are already in increasing order.
  281. return histories.map((ids, carId) =>
  282. `${carId}: ${ids.join(" ")}`
  283. );
  284. }
  285.  
  286.  
  287. // ==================== TEST CASES ====================
  288.  
  289. /*
  290. TEST COVERAGE CHECKLIST:
  291.  
  292. 1. Normal overlapping rentals
  293. 2. Unsorted input - imp.
  294. 3. Zero-duration rentals - imp.
  295. 4. Exact endpoint reuse - imp.
  296. 5. All rentals overlap - imp.
  297. 6. All rentals sequential
  298. 7. Multiple cars available - imp.
  299. 8. Smallest available car ID - imp.
  300. 9. Tie-breaking by returnTime - imp.
  301. 10. Tie-breaking by recordId - imp.
  302. 11. Zero-duration while car 0 is busy - imp.
  303. 12. Only zero-duration rentals - imp.
  304. 13. Zero-duration before positive rental
  305. 14. Single positive rental
  306. 15. Single zero-duration rental
  307. 16. Large timestamps and record IDs
  308. 17. Empty input
  309. 18. Reuse after long idle period
  310. 19. Zero-duration does not create extra cars - imp.
  311. 20. Mixed unsorted records
  312. */
  313.  
  314.  
  315. const tests = [
  316.  
  317. {
  318. name: "Normal overlapping rentals",
  319. requests: [
  320. [101, 1, 3],
  321. [102, 2, 4],
  322. [103, 4, 6]
  323. ],
  324. expected: ["0: 101 103", "1: 102"]
  325. },
  326.  
  327. {
  328. name: "Unsorted input - imp.",
  329. requests: [
  330. [203, 5, 7],
  331. [201, 1, 5],
  332. [202, 2, 3]
  333. ],
  334. expected: ["0: 201 203", "1: 202"]
  335. },
  336.  
  337. {
  338. name: "Zero-duration rentals - imp.",
  339. requests: [
  340. [11, 3, 3],
  341. [10, 3, 3],
  342. [12, 3, 4]
  343. ],
  344. expected: ["0: 10 11 12"]
  345. },
  346.  
  347. {
  348. name: "Exact endpoint reuse - imp.",
  349. requests: [
  350. [1, 0, 2],
  351. [2, 2, 4],
  352. [3, 4, 6]
  353. ],
  354. expected: ["0: 1 2 3"]
  355. },
  356.  
  357. {
  358. name: "All rentals overlap - imp.",
  359. requests: [
  360. [1, 0, 10],
  361. [2, 1, 9],
  362. [3, 2, 8],
  363. [4, 3, 7]
  364. ],
  365. expected: [
  366. "0: 1",
  367. "1: 2",
  368. "2: 3",
  369. "3: 4"
  370. ]
  371. },
  372.  
  373. {
  374. name: "All rentals sequential",
  375. requests: [
  376. [1, 1, 3],
  377. [2, 3, 5],
  378. [3, 5, 7],
  379. [4, 7, 9]
  380. ],
  381. expected: ["0: 1 2 3 4"]
  382. },
  383.  
  384. {
  385. name: "Multiple cars available - imp.",
  386. requests: [
  387. [1, 0, 5],
  388. [2, 1, 3],
  389. [3, 2, 4],
  390. [4, 6, 8]
  391. ],
  392. expected: [
  393. "0: 1 4",
  394. "1: 2",
  395. "2: 3"
  396. ]
  397. },
  398.  
  399. {
  400. name: "Smallest available car ID - imp.",
  401. requests: [
  402. [1, 0, 10],
  403. [2, 1, 5],
  404. [3, 20, 25]
  405. ],
  406. expected: [
  407. "0: 1 3",
  408. "1: 2"
  409. ]
  410. },
  411.  
  412. {
  413. name: "Tie-breaking by returnTime - imp.",
  414. requests: [
  415. [3, 1, 10],
  416. [2, 1, 5],
  417. [1, 1, 7]
  418. ],
  419. expected: [
  420. "0: 2",
  421. "1: 1",
  422. "2: 3"
  423. ]
  424. },
  425.  
  426. {
  427. name: "Tie-breaking by recordId - imp.",
  428. requests: [
  429. [30, 1, 5],
  430. [10, 1, 5],
  431. [20, 1, 5]
  432. ],
  433. expected: [
  434. "0: 10",
  435. "1: 20",
  436. "2: 30"
  437. ]
  438. },
  439.  
  440. {
  441. name: "Zero-duration while car 0 is busy - imp.",
  442. requests: [
  443. [1, 0, 10],
  444. [2, 5, 5],
  445. [3, 6, 8]
  446. ],
  447. expected: [
  448. "0: 1 2",
  449. "1: 3"
  450. ]
  451. },
  452.  
  453. {
  454. name: "Only zero-duration rentals - imp.",
  455. requests: [
  456. [3, 5, 5],
  457. [1, 2, 2],
  458. [2, 2, 2]
  459. ],
  460. expected: ["0: 1 2 3"]
  461. },
  462.  
  463. {
  464. name: "Zero-duration before positive rental",
  465. requests: [
  466. [1, 0, 0],
  467. [2, 1, 5],
  468. [3, 6, 8]
  469. ],
  470. expected: ["0: 1 2 3"]
  471. },
  472.  
  473. {
  474. name: "Single positive rental",
  475. requests: [
  476. [100, 10, 20]
  477. ],
  478. expected: ["0: 100"]
  479. },
  480.  
  481. {
  482. name: "Single zero-duration rental",
  483. requests: [
  484. [100, 10, 10]
  485. ],
  486. expected: ["0: 100"]
  487. },
  488.  
  489. {
  490. name: "Large timestamps and record IDs",
  491. requests: [
  492. [1000000000, 0, 1000000000],
  493. [999999999, 1, 1000000000],
  494. [999999998, 1000000000, 1000000000]
  495. ],
  496. expected: [
  497. "0: 1000000000 999999998",
  498. "1: 999999999"
  499. ]
  500. },
  501.  
  502. {
  503. name: "Empty input (defensive test)",
  504. requests: [],
  505. expected: []
  506. },
  507.  
  508. {
  509. name: "Reuse after long idle period",
  510. requests: [
  511. [1, 0, 2],
  512. [2, 100, 102],
  513. [3, 200, 202]
  514. ],
  515. expected: ["0: 1 2 3"]
  516. },
  517.  
  518. {
  519. name: "Zero-duration does not create extra cars - imp.",
  520. requests: [
  521. [1, 0, 10],
  522. [2, 1, 1],
  523. [3, 2, 2],
  524. [4, 10, 12]
  525. ],
  526. expected: ["0: 1 2 3 4"]
  527. },
  528.  
  529. {
  530. name: "Mixed unsorted records",
  531. requests: [
  532. [5, 8, 10],
  533. [3, 2, 6],
  534. [1, 0, 4],
  535. [4, 6, 8],
  536. [2, 1, 3]
  537. ],
  538. expected: [
  539. "0: 1 4 5",
  540. "1: 2 3"
  541. ]
  542. }
  543. ];
  544.  
  545.  
  546. // ==================== TEST RUNNER ====================
  547.  
  548. let passed = 0;
  549.  
  550. tests.forEach((test, index) => {
  551.  
  552. const actual = solve(test.requests);
  553.  
  554. const isCorrect =
  555. JSON.stringify(actual) ===
  556. JSON.stringify(test.expected);
  557.  
  558. if (isCorrect) passed++;
  559.  
  560. console.log(`Test ${index + 1}: ${test.name}`);
  561. console.log("Expected:", JSON.stringify(test.expected));
  562. console.log("Actual: ", JSON.stringify(actual));
  563. console.log("Result: ", isCorrect ? "PASS" : "FAIL");
  564. console.log("----------------------------------------");
  565. });
  566.  
  567. console.log(`Total passed: ${passed}/${tests.length}`);
  568.  
Success #stdin #stdout 0.03s 5612KB
stdin
Standard input is empty
stdout
Test 1: Normal overlapping rentals
Expected: ["0: 101 103","1: 102"]
Actual:   ["0: 101 103","1: 102"]
Result:   PASS
----------------------------------------
Test 2: Unsorted input - imp.
Expected: ["0: 201 203","1: 202"]
Actual:   ["0: 201 203","1: 202"]
Result:   PASS
----------------------------------------
Test 3: Zero-duration rentals - imp.
Expected: ["0: 10 11 12"]
Actual:   ["0: 10 11 12"]
Result:   PASS
----------------------------------------
Test 4: Exact endpoint reuse - imp.
Expected: ["0: 1 2 3"]
Actual:   ["0: 1 2 3"]
Result:   PASS
----------------------------------------
Test 5: All rentals overlap - imp.
Expected: ["0: 1","1: 2","2: 3","3: 4"]
Actual:   ["0: 1","1: 2","2: 3","3: 4"]
Result:   PASS
----------------------------------------
Test 6: All rentals sequential
Expected: ["0: 1 2 3 4"]
Actual:   ["0: 1 2 3 4"]
Result:   PASS
----------------------------------------
Test 7: Multiple cars available - imp.
Expected: ["0: 1 4","1: 2","2: 3"]
Actual:   ["0: 1 4","1: 2","2: 3"]
Result:   PASS
----------------------------------------
Test 8: Smallest available car ID - imp.
Expected: ["0: 1 3","1: 2"]
Actual:   ["0: 1 3","1: 2"]
Result:   PASS
----------------------------------------
Test 9: Tie-breaking by returnTime - imp.
Expected: ["0: 2","1: 1","2: 3"]
Actual:   ["0: 2","1: 1","2: 3"]
Result:   PASS
----------------------------------------
Test 10: Tie-breaking by recordId - imp.
Expected: ["0: 10","1: 20","2: 30"]
Actual:   ["0: 10","1: 20","2: 30"]
Result:   PASS
----------------------------------------
Test 11: Zero-duration while car 0 is busy - imp.
Expected: ["0: 1 2","1: 3"]
Actual:   ["0: 1 2","1: 3"]
Result:   PASS
----------------------------------------
Test 12: Only zero-duration rentals - imp.
Expected: ["0: 1 2 3"]
Actual:   ["0: 1 2 3"]
Result:   PASS
----------------------------------------
Test 13: Zero-duration before positive rental
Expected: ["0: 1 2 3"]
Actual:   ["0: 1 2 3"]
Result:   PASS
----------------------------------------
Test 14: Single positive rental
Expected: ["0: 100"]
Actual:   ["0: 100"]
Result:   PASS
----------------------------------------
Test 15: Single zero-duration rental
Expected: ["0: 100"]
Actual:   ["0: 100"]
Result:   PASS
----------------------------------------
Test 16: Large timestamps and record IDs
Expected: ["0: 1000000000 999999998","1: 999999999"]
Actual:   ["0: 1000000000 999999998","1: 999999999"]
Result:   PASS
----------------------------------------
Test 17: Empty input (defensive test)
Expected: []
Actual:   []
Result:   PASS
----------------------------------------
Test 18: Reuse after long idle period
Expected: ["0: 1 2 3"]
Actual:   ["0: 1 2 3"]
Result:   PASS
----------------------------------------
Test 19: Zero-duration does not create extra cars - imp.
Expected: ["0: 1 2 3 4"]
Actual:   ["0: 1 2 3 4"]
Result:   PASS
----------------------------------------
Test 20: Mixed unsorted records
Expected: ["0: 1 4 5","1: 2 3"]
Actual:   ["0: 1 4 5","1: 2","2: 3"]
Result:   FAIL
----------------------------------------
Total passed: 19/20