Skip to content

Commit 426a27d

Browse files
Allocate Minimum Pages
1 parent dce2d6b commit 426a27d

1 file changed

Lines changed: 180 additions & 0 deletions

File tree

Leetcode/Allocate Minimum Pages.py

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
"""
2+
Problem: Allocate Minimum Pages
3+
4+
Given an array arr[] where each element represents the number of pages in a book
5+
and an integer k representing the number of students, allocate books such that:
6+
7+
1. Each student gets at least one book.
8+
2. Each book is assigned to exactly one student.
9+
3. Books must be allocated in contiguous order.
10+
4. Minimize the maximum number of pages assigned to any student.
11+
12+
Approach:
13+
- Use Binary Search on the answer.
14+
- The minimum possible answer is max(arr) because a student must read
15+
at least the largest book.
16+
- The maximum possible answer is sum(arr) because one student can read all books.
17+
- For a given maximum page limit (mid), check if allocation is possible
18+
using at most k students.
19+
20+
Time Complexity:
21+
- Feasibility Check: O(n)
22+
- Binary Search Range: O(log(sum(arr) - max(arr)))
23+
- Overall: O(n * log(sum(arr)))
24+
25+
Space Complexity:
26+
- O(1)
27+
28+
Example:
29+
Input:
30+
arr = [12, 34, 67, 90]
31+
k = 2
32+
33+
Output:
34+
113
35+
36+
Explanation:
37+
Student 1 -> [12, 34, 67] = 113 pages
38+
Student 2 -> [90] = 90 pages
39+
40+
Maximum pages assigned = 113
41+
This is the minimum possible maximum allocation.
42+
"""
43+
44+
45+
class Solution:
46+
def findPages(self, arr, k):
47+
"""
48+
Finds the minimum possible maximum pages assigned to any student.
49+
50+
Args:
51+
arr (List[int]): Pages in each book
52+
k (int): Number of students
53+
54+
Returns:
55+
int: Minimum possible maximum pages allocation
56+
"""
57+
58+
# More students than books is invalid
59+
if k > len(arr):
60+
return -1
61+
62+
# Search space
63+
left = max(arr)
64+
right = sum(arr)
65+
66+
while left <= right:
67+
mid = left + (right - left) // 2
68+
69+
if self.can_allocate(mid, arr, k):
70+
right = mid - 1
71+
else:
72+
left = mid + 1
73+
74+
return left
75+
76+
def can_allocate(self, max_pages, arr, k):
77+
"""
78+
Checks whether books can be allocated to at most k students
79+
such that no student gets more than max_pages.
80+
81+
Args:
82+
max_pages (int): Candidate answer
83+
arr (List[int]): Pages in each book
84+
k (int): Number of students
85+
86+
Returns:
87+
bool: True if allocation is possible, otherwise False
88+
"""
89+
90+
students = 1
91+
current_pages = 0
92+
93+
for pages in arr:
94+
95+
# Continue assigning books to current student
96+
if current_pages + pages <= max_pages:
97+
current_pages += pages
98+
99+
# Assign to next student
100+
else:
101+
students += 1
102+
current_pages = pages
103+
104+
return students <= k
105+
106+
107+
# --------------------------
108+
# Driver Code
109+
# --------------------------
110+
if __name__ == "__main__":
111+
112+
arr = [12, 34, 67, 90]
113+
k = 2
114+
115+
solution = Solution()
116+
answer = solution.findPages(arr, k)
117+
118+
print("Books:", arr)
119+
print("Students:", k)
120+
print("Minimum Maximum Pages:", answer)
121+
122+
"""
123+
Dry Run:
124+
125+
arr = [12, 34, 67, 90]
126+
k = 2
127+
128+
Search Space:
129+
left = 90
130+
right = 203
131+
132+
mid = 146
133+
Allocation possible with 2 students
134+
Move left -> search smaller answer
135+
136+
mid = 117
137+
Allocation possible
138+
Move left
139+
140+
mid = 103
141+
Requires 3 students
142+
Move right
143+
144+
mid = 110
145+
Requires 3 students
146+
Move right
147+
148+
mid = 113
149+
Possible with 2 students
150+
151+
Final Answer = 113
152+
153+
--------------------------------
154+
155+
Binary Search Pattern:
156+
157+
Minimum Valid Answer:
158+
159+
while left <= right:
160+
mid = left + (right - left) // 2
161+
162+
if valid(mid):
163+
right = mid - 1
164+
else:
165+
left = mid + 1
166+
167+
return left
168+
169+
--------------------------------
170+
171+
Key Insight:
172+
173+
We are NOT searching inside the array.
174+
175+
We are searching on the answer space:
176+
177+
[max(arr), sum(arr)]
178+
179+
This is a classic "Binary Search on Answer" problem.
180+
"""

0 commit comments

Comments
 (0)