Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- https://practice.geeksforgeeks.org/problems/allocate-minimum-number-of-pages0937/1#
- NOTE:
- we use "return True if cnt<=M becasue it gives the directions to reduce search space.
- The func allocate is giving directions for search space.(VVIMP)
- class Solution:
- #Function to find minimum number of pages.
- def findPages(self,A, N, M):
- #code here
- def allocate(barrier):
- cur=0
- students=1
- for i in range(N):
- if A[i]>barrier:
- return False
- if cur+A[i]<=barrier:
- cur+=A[i]
- else:
- students+=1
- cur=A[i]
- return True if students<=M else False
- ans=float('inf')
- l,h=min(A),sum(A)
- while l<=h:
- m=(l+h)//2
- if allocate(m):
- ans=min(ans,m)
- h=m-1
- else:
- l=m+1
- return ans
- #{
- # Driver Code Starts
- #Initial Template for Python 3
- #contributed by RavinderSinghPB
- if __name__=='__main__':
- t=int(input())
- for _ in range(t):
- n=int(input())
- arr=[int(x) for x in input().strip().split()]
- m=int(input())
- ob=Solution()
- print(ob.findPages(arr,n,m))
- # } Driver Code Ends
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement