fork download
  1. def getMinMemory(memoryRequirement, memoryAvailable):
  2. """
  3. Determines the smallest memory capacity required for a new server.
  4.  
  5. Args:
  6. memoryRequirement: A list of integers for the memory requirements of the programs.
  7. memoryAvailable: A list of integers for the memory capacities of the existing servers.
  8.  
  9. Returns:
  10. The minimum memory size required for the new server, or -1 if impossible.
  11. """
  12. n = len(memoryRequirement)
  13.  
  14. # Step 1: Sort both lists
  15. prog_req_sorted = sorted(memoryRequirement)
  16. serv_avail_sorted = sorted(memoryAvailable)
  17.  
  18. # Step 2: Iterate through each program as a candidate for the new server
  19. for i in range(n):
  20. candidate_program = prog_req_sorted[i]
  21.  
  22. # Create a temporary list of remaining programs
  23. remaining_programs = prog_req_sorted[:i] + prog_req_sorted[i+1:]
  24.  
  25. # Step 3: Check if the remaining programs can be assigned
  26. is_possible = True
  27. for j in range(n - 1):
  28. if remaining_programs[j] > serv_avail_sorted[j]:
  29. is_possible = False
  30. break
  31.  
  32. # Step 4: If possible, we've found our minimum answer
  33. if is_possible:
  34. return candidate_program
  35.  
  36. # Step 5: If the loop completes, no solution was found
  37. return -1
  38.  
  39. # Example usage from the problem
  40. memoryRequirement = [2, 2, 7,3,5]
  41. memoryAvailable = [3, 2, 7,2]
  42. result = getMinMemory(memoryRequirement, memoryAvailable)
  43. print(f"The minimum memory required for the new server is: {result}")
  44. # Expected output: The minimum memory required for the new server is: 3
Success #stdin #stdout 0.09s 14060KB
stdin
Standard input is empty
stdout
The minimum memory required for the new server is: 5