File tree Expand file tree Collapse file tree 1 file changed +21
-0
lines changed
Expand file tree Collapse file tree 1 file changed +21
-0
lines changed Original file line number Diff line number Diff line change 1+ # -*- coding: utf-8 -*-
2+
3+ class Solution :
4+ # @param values: a list of integers
5+ # @return: a boolean which equals to True if the first player will win
6+ def firstWillWin (self , values ):
7+ # write your code here
8+ # score[i]表示从i开始到end能获取的最大值。
9+ if len (values ) <= 2 :
10+ return True
11+ score = [0 ] * (len (values ) + 1 )
12+ score [- 1 ] = 0
13+ score [- 2 ] = values [- 1 ]
14+ score [- 3 ] = values [- 2 ] + values [- 1 ]
15+ score [- 4 ] = values [- 3 ] + values [- 2 ]
16+ for i in xrange (len (values ) - 4 , - 1 , - 1 ):
17+ # 拿1个后,对方取1个或2个的情况。
18+ score [i ] = values [i ] + min (score [i + 2 ], score [i + 3 ])
19+ # 拿2个后,对方取1个或2个的情况。
20+ score [i ] = max (score [i ], values [i ] + values [i + 1 ] + min (score [i + 3 ], score [i + 4 ]))
21+ return True if score [0 ] > (sum (values ) - score [0 ]) else False
You can’t perform that action at this time.
0 commit comments