Dashboard Temp Share Shortlinks Frames API

HTMLify

The Painter's Partition Problem-II
Views: 12 | Author: prakhardoneria
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class Solution:
    def minTime(self, arr, k):
        # Helper function to check if a max_time limit is feasible
        def is_possible(max_time):
            painters_used = 1
            current_sum = 0
            for board in arr:
                if current_sum + board > max_time:
                    painters_used += 1
                    current_sum = board
                    if painters_used > k:
                        return False
                else:
                    current_sum += board
            return True

        # Binary search range
        low = max(arr)
        high = sum(arr)
        ans = high
        
        while low <= high:
            mid = (low + high) // 2
            if is_possible(mid):
                ans = mid
                high = mid - 1
            else:
                low = mid + 1
        return ans