-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path375.py
33 lines (24 loc) · 887 Bytes
/
375.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = ['"wuyadong" <[email protected]>']
class Solution(object):
def getMoneyAmount(self, n):
"""
:type n: int
:rtype: int
"""
matrix = [[0]*(n+1)]
for j in xrange(1, n+1):
for i in xrange(j, 0, -1):
if i == j:
matrix.append([0]*(n+1))
matrix[i][j] = 0
else:
min_matrix_i_j = max(i, i+matrix[i+1][j])
for k in xrange(i+1, j+1):
matrix_i_j = max(matrix[i][k-1]+k, (matrix[k+1][j] if k+1 <= j else 0)+k)
min_matrix_i_j = min(min_matrix_i_j, matrix_i_j)
matrix[i][j] = min_matrix_i_j
return matrix[1][n]
if __name__ == "__main__":
print Solution().getMoneyAmount(3)