forked from yingl/LintCodeInPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoins_in_a_line_ii.py
More file actions
23 lines (21 loc) · 984 Bytes
/
coins_in_a_line_ii.py
File metadata and controls
23 lines (21 loc) · 984 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# coding: utf-8
class Solution:
# @param values: a list of integers
# @return: a boolean which equals to True if the first player will win
def firstWillWin(self, values):
# write your code here
# score[i]表示从i开始到end能获取的最大值。
if len(values) <= 2:
return True
score = [0] * (len(values) + 1)
score[-1] = 0
score[-2] = values[-1]
score[-3] = values[-2] + values[-1]
score[-4] = values[-3] + values[-2]
for i in xrange(len(values) - 4, -1, -1):
# 拿1个后,对方取1个或2个的情况。
score[i] = values[i] + min(score[i + 2], score[i + 3])
# 拿2个后,对方取1个或2个的情况。
score[i] = max(score[i], values[i] + values[i + 1] + min(score[i + 3], score[i + 4]))
return True if score[0] > (sum(values) - score[0]) else False
# medium: http://lintcode.com/zh-cn/problem/coins-in-a-line-ii/