imashutosh51

Allocate Minimum Pages

Feb 10th, 2026 (edited)
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.65 KB | None | 0 0
  1. """
  2. We want to minimize the maximum number of pages assigned to any student.
  3.  
  4. Observations:
  5. 1. Each student must receive at least one book and books must be assigned contiguously.
  6. 2. The minimum possible value of the answer is max(arr),
  7.   because the book with the maximum pages must be assigned to someone.
  8. 3. The maximum possible value of the answer is sum(arr),
  9.   when a single student is assigned all the books.
  10.  
  11. So the answer lies in the range [max(arr), sum(arr)].
  12.  
  13. Binary Search on Answer:
  14. We binary search on this range and assume a candidate value `x`
  15. as the maximum pages allowed per student.
  16.  
  17. For a given `x`, we greedily assign books in order:
  18. - Keep adding books to the current student until pages exceed `x`
  19. - When exceeded, assign books to a new student
  20.  
  21. This allows us to compute the minimum number of students required
  22. if no student is allowed more than `x` pages.
  23.  
  24. Decision:
  25. - If required students <= k, the allocation is possible.
  26.  Try to reduce `x` to find a better (smaller) maximum.
  27. - If required students > k, `x` is too small and must be increased.
  28.  
  29. Trick:
  30. Even if the allocation is possible, we continue shrinking the range
  31. because we want the minimum possible maximum pages.
  32.  
  33. T.C:O(n log(sum(arr)))
  34.  
  35. Here we making the groups intensionally less than equal to the mid pages, not greater than k pages & the reason is suppose you finished grouping for any other algo, then some group will be having 5,3,1,9 so the answer is 9 so we are targeting to
  36. force the mid on all the groups, make the groups less than or equal to 9.
  37. """
  38.  
  39. def minimum_count_of_students_with_n_pages(arr,max_pages): #it finds the total numbers of groups possible where the group combined pages is less than, equal to max_pages ie. mid
  40.     students = 1
  41.     cur = 0
  42.    
  43.     for pages in arr:
  44.         if cur + pages <= max_pages:
  45.             cur+=pages
  46.         else:
  47.             students += 1
  48.             cur = pages
  49.    
  50.     return students
  51.  
  52. class Solution:
  53.     def findPages(self, arr, k):
  54.         if k>len(arr):
  55.             return -1
  56.         low,high=max(arr),sum(arr)
  57.         while low<=high:
  58.             mid= (low+high)//2 #check how many minimum students would be enough to get all the books where maximum pages allocated to a student doesn't cross the mid
  59.             count=minimum_count_of_students_with_n_pages(arr,mid)
  60.  
  61.             if count<=k: #for < case, means the threshold is big so k groups couldn't form so we should reduce the threshold and for = case, we will check on the higher values, let's see we with more smaller values
  62.                 high=mid-1
  63.             else:
  64.                 low=mid+1
  65.         return low
  66.                
  67.  
Advertisement
Add Comment
Please, Sign In to add comment