-
Notifications
You must be signed in to change notification settings - Fork 891
/
problem_281.py
43 lines (32 loc) · 1.08 KB
/
problem_281.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
from collections import Counter
def get_min_cuts(brick_wall):
for i in range(len(brick_wall)):
# Make wall of cumulative brick lengths
prev_bricks_len = 0
for k in range(len(brick_wall[i])):
brick_wall[i][k] += prev_bricks_len
prev_bricks_len = brick_wall[i][k]
brick_wall[i] = brick_wall[i][:-1]
# Find the most common edge to cut down through
brick_counter = Counter()
for row in brick_wall:
brick_counter.update(row)
most_common_bricks = brick_counter.most_common()
bricks_avoided = most_common_bricks[0][1] if most_common_bricks else 0
return len(brick_wall) - bricks_avoided
# Tests
brick_wall = [[3, 5, 1, 1],
[2, 3, 3, 2],
[5, 5],
[4, 4, 2],
[1, 3, 3, 3],
[1, 1, 6, 1, 1]]
assert get_min_cuts(brick_wall) == 2
brick_wall = [[1]]
assert get_min_cuts(brick_wall) == 1
brick_wall = [[1],
[1, 2]]
assert get_min_cuts(brick_wall) == 1
brick_wall = [[1, 2],
[1, 2]]
assert get_min_cuts(brick_wall) == 0