-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path085_counting_rectangles.py
70 lines (53 loc) · 1.66 KB
/
085_counting_rectangles.py
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
"""
idea:
"""
import time
import math
def main():
best_width = 0
best_height = 0
best_diff = 2000000
width = 1
height = 1
arrangements = 0
while arrangements < 2000000:
width += 1
arrangements = count_all_arrangements(width, height)
diff = abs(2000000-arrangements)
if diff < best_diff:
best_diff = diff
best_width = width
best_height = height
running = True
while running:
if arrangements < 2000000:
height += 1
width += 1
elif arrangements > 2000000:
width -= 1
if height > width:
break
arrangements = count_all_arrangements(width, height)
diff = abs(2000000 - arrangements)
if diff < best_diff:
best_diff = diff
best_width = width
best_height = height
print('closest diff: {}, {} x {}'.format(best_diff, best_width, best_height))
print('closest diff: {}, {} x {}'.format(best_diff, best_width, best_height))
def count_all_arrangements(width, height):
possibilities = 0
for i in range(1, width + 1):
for j in range(1, height + 1):
possibilities += count_arrangements(width, height, i, j)
return possibilities
def count_arrangements(width, height, sub_w, sub_h):
""" on how many ways can a sub_w times sub_h rectangle be
placed in an rectangle of width times height"""
width_times = width - sub_w + 1
height_times = height - sub_h + 1
return width_times * height_times
if __name__ == '__main__':
start_time = time.time()
main()
print("time:", time.time() - start_time)