From c17d37b40e3445dd0c356e4458f5ea3e1403bd42 Mon Sep 17 00:00:00 2001 From: Samir Paul Date: Tue, 18 Jan 2022 12:12:49 +0530 Subject: [PATCH] similar questions - Allocate Minimum Number Of Pages --- .../01. Allocate Minimum Number Of Pages.py | 10 +++--- .../02. Split Array Largest Sum.py | 34 +++++++++++++++++++ 2 files changed, 39 insertions(+), 5 deletions(-) diff --git a/11_Binary-Search/01. Allocate Minimum Number Of Pages.py b/11_Binary-Search/01. Allocate Minimum Number Of Pages.py index 1ae2dbe2..0bd4488f 100644 --- a/11_Binary-Search/01. Allocate Minimum Number Of Pages.py +++ b/11_Binary-Search/01. Allocate Minimum Number Of Pages.py @@ -10,11 +10,11 @@ r = sum(A) = 203 90----------------------------------------------------------------146--------------------------------------------203 -l mid r +l mid r mid = 146 isValid so ans = mid = 146; I will try to decrease mid => r = mid - 1 = 145 90-------------------------------------117------------------------145 -l mid r +l mid r mid = 117 isValid so ans = mid = 117; I will try to decrease mid => r = mid - 1 = 116 90--------------103--------------------116 @@ -44,7 +44,7 @@ class Solution: def findPages(self,A, N, M): l = max(A); r = sum(A); ans = -1 - if len(A) < M: return -1 # Number of books can not be lesser than number of of students as we have to give atleast 1 book to a student + if len(A) < M: return -1 # Number of books can not be lesser than number of students as we have to give atleast 1 book to a student def isValid(A, M, mid): pageSum = 0 # sum of pages of A that can be allocated to one student @@ -54,7 +54,7 @@ def isValid(A, M, mid): pageSum += pages if pageSum > mid: # sum of pages allocated to one student exceed max capacity of the student requiredStudents += 1 # We need one more student - pageSum = pages # start calculating sum of pages can be allocated to next student + pageSum = pages # start calculating sum of pages that can be allocated to next student if requiredStudents > M: return False else: return True @@ -64,7 +64,7 @@ def isValid(A, M, mid): mid = l + (r - l) // 2 if isValid(A, M, mid): - ans = mid # Updating answer the the current mid as it is the most optimized(least) ans till now + ans = mid # Updating answer to current mid as current mid is the most optimized(least) ans till now r = mid - 1 # I will try to decrease mid else: l = mid + 1 # current mid NOT isValid so I will try to increase mid diff --git a/11_Binary-Search/02. Split Array Largest Sum.py b/11_Binary-Search/02. Split Array Largest Sum.py index 4b70bfe3..c58fc74b 100644 --- a/11_Binary-Search/02. Split Array Largest Sum.py +++ b/11_Binary-Search/02. Split Array Largest Sum.py @@ -1,2 +1,36 @@ # https://leetcode.com/problems/split-array-largest-sum/ +class Solution: + def splitArray(self, nums: List[int], m: int) -> int: + # First Solve => Allocate Minimum Number Of Pages - Binary Search + + l = max(nums); r = sum(nums); ans = -1 + + if len(nums) < m: return -1 # Number of elements can not be lesser than number of subarrays as we have to give atleast 1 element to a subarray + + def isValid(nums, m, mid): + currSum = 0 # sum of elements of nums that can be allocated to one subarray + countOfSubarrays = 1 # Number of subarrays required if mid is the max capacity of a subarray + + for val in nums: + currSum += val + if currSum > mid: # sum of elements allocated to one subarray exceed max capacity of the subarray + countOfSubarrays += 1 # We need one more subarray + currSum = val # start calculating sum of elements that can be allocated to next subarray + + if countOfSubarrays > m: return False + else: return True + + + while l <= r: + mid = (r+l) // 2 + if isValid(nums, m, mid): + ans = mid # Updating answer to current mid as current mid is the most optimized(least) ans till now + r = mid - 1 # I will try to decrease mid + else: + l = mid + 1 # current mid NOT isValid so I will try to increase mid + + return ans # Most Optimized ans is stored here + + + \ No newline at end of file