Advertisement
imashutosh51

Painter's partition problem

Mar 13th, 2025
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.42 KB | None | 0 0
  1. /*If all the buildings were painted by only one painter then it will take maximum time to paint all the house ie. sum(arr)
  2. If each buildings were painted by one painter parallely then it will take minimum time to paint all the house ie. max(arr)
  3. So our answer ranges between min to max so we will use binary search and we will verify mid time is possible to paint with given number of painters or not,if possible then we will try to reduce time else we will increase time.
  4.  
  5. In ispossible function:
  6. we will allocated building to one labour while interating the houses,once it's time exceeds our given time,we will allocate another painter and we will in final,how many painter we used to paint the houses in given time ie.mid if exceeds the given painter in problem,return false else true.
  7. */
  8. MOD = 10000003
  9. def ispossible(arr,limit,maxm_allowed_painters):  
  10.    cur_time=0
  11.    cur_painters=1
  12.    for i in arr:
  13.        cur_time+=i
  14.        if cur_time>limit:
  15.            cur_painters+=1
  16.            cur_time=i
  17.    if cur_painters>maxm_allowed_painters:
  18.        return False
  19.    return True
  20. class Solution:            
  21.     def paint(self, A, B, C):
  22.        ans=math.inf
  23.        l=max(C)
  24.        r=sum(C)
  25.        while l<=r:
  26.            mid=(l+r)//2
  27.            temp=ispossible(C,mid,A)
  28.            if temp:
  29.                ans=min(ans,mid)
  30.                r=mid-1
  31.            else:
  32.                l=mid+1
  33.        return (ans*B)%MOD
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement