Skip to content

Instantly share code, notes, and snippets.

@zedchance
Created December 13, 2021 23:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zedchance/839a6dddfe4bcfc0b3fc57fde0e4130e to your computer and use it in GitHub Desktop.
Save zedchance/839a6dddfe4bcfc0b3fc57fde0e4130e to your computer and use it in GitHub Desktop.
Divide books ordered on a bookshelf so each person reads roughly the same amount
# book partitioning so each person reads roughly the same amount
import math
def solve(pages, people):
n = len(pages)
m = [[0 for j in range(people)] for i in range(n)]
s = [[-1 for j in range(people)] for i in range(n)]
# work left to right
for j in range(people):
# top to bottom
for i in range(n):
if j == 0:
m[i][j] = sum(pages[:i + 1])
else:
potential = [max(m[k][j - 1], sum(pages[k + 1:i + 1])) for k in range(i + 1)]
new = math.inf
for k, p in enumerate(potential):
if p < new:
m[i][j] = p
s[i][j] = k
new = p
for r1, r2 in zip(m, s):
for col in r1:
print(f'{col:5}', end=" ")
print(end=" ")
for col in r2:
print(f'{col:5}', end=" ")
print()
print()
def _print_solution(i, j):
if j == 0:
for k in range(i + 1):
print(pages[k], end=" ")
else:
_print_solution(s[i][j], j - 1)
print("|", end=" ")
for k in range(s[i][j] + 1, i + 1):
print(pages[k], end=" ")
print("partitions:")
_print_solution(n - 1, people - 1)
print(f'\n{m[-1][-1]} max pages read by 1 person')
if __name__ == '__main__':
t1 = [100, 100, 100, 100, 100, 100, 100, 100, 100]
t2 = [100, 200, 300, 400, 500, 600, 700, 800, 900]
t3 = [694, 837, 880, 688, 331, 430, 111, 514, 605, 291, 278, 858, 554, 871, 898, 343, 628, 287, 782, 814, 273, 236, 625, 922, 735]
solve(t3, 6)
@zedchance
Copy link
Author

Output

  694   694   694   694   694   694         -1     0     0     0     0     0 
 1531   837   837   837   837   837         -1     0     0     0     0     0 
 2411  1531   880   880   880   880         -1     1     1     1     1     1 
 3099  1568  1531   880   880   880         -1     1     2     2     2     2 
 3430  1899  1531  1019   880   880         -1     1     2     2     3     3 
 3860  2329  1531  1449   880   880         -1     1     2     2     3     3 
 3971  2411  1560  1531   880   880         -1     2     2     3     3     3 
 4485  2411  1568  1531  1055   880         -1     2     3     3     4     5 
 5090  2679  1899  1531  1449  1055         -1     2     4     5     5     7 
 5381  2970  1951  1531  1521  1055         -1     2     4     5     5     7 
 5659  3099  2229  1568  1531  1174         -1     3     4     7     7     7 
 6517  3418  2411  1899  1531  1449         -1     3     7     8     8     8 
 7071  3641  2586  1951  1568  1531         -1     4     7     9    10    10 
 7942  3971  2852  2283  1899  1531         -1     6     8    10    11    11 
 8840  4485  3181  2411  1951  1769         -1     7    10    11    12    12 
 9183  4698  3418  2586  2112  1899         -1     7    11    12    12    13 
 9811  5090  3418  2740  2283  1899         -1     8    11    12    13    13 
10098  5090  3581  2852  2283  1951         -1     8    11    13    13    14 
10880  5499  3809  2938  2411  2040         -1     9    12    13    14    14 
11694  6035  3971  3181  2586  2283         -1    10    13    14    15    16 
11967  6308  4025  3181  2740  2283         -1    10    13    14    16    16 
12203  6517  4261  3363  2740  2283         -1    11    13    14    16    17 
12828  6517  4485  3418  2852  2411         -1    11    14    16    17    18 
13750  7071  4698  3652  2938  2586         -1    12    15    17    18    19 
14485  7414  5090  3809  3181  2740         -1    12    16    18    19    20 

partitions:
694 837 880 | 688 331 430 111 514 | 605 291 278 858 554 | 871 898 343 628 | 287 782 814 273 | 236 625 922 735 
2740 max pages read by 1 person

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment