Skip to content

Commit

Permalink
similar questions - Allocate Minimum Number Of Pages
Browse files Browse the repository at this point in the history
  • Loading branch information
SamirPaulb committed Jan 18, 2022
1 parent 1324f8c commit c17d37b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
10 changes: 5 additions & 5 deletions 11_Binary-Search/01. Allocate Minimum Number Of Pages.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
34 changes: 34 additions & 0 deletions 11_Binary-Search/02. Split Array Largest Sum.py
Original file line number Diff line number Diff line change
@@ -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



0 comments on commit c17d37b

Please sign in to comment.