1 parent 1221106 commit 95dba25Copy full SHA for 95dba25
1 file changed
coins_in_a_line_ii.py
@@ -0,0 +1,21 @@
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
0 commit comments