Skip to content

Commit b82cd35

Browse files
authored
Create game_optimal_strategy_recursive.py
1 parent cc50e03 commit b82cd35

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Copyright (c) June 02, 2017 CareerMonk Publications and others.
2+
3+
# Creation Date : 2017-06-02 06:15:46
4+
# Last modification : 2017-06-02
5+
# Modified by : Narasimha Karumanchi
6+
# Book Title : Algorithm Design Techniques
7+
# Warranty : This software is provided "as is" without any
8+
# warranty; without even the implied warranty of
9+
# merchantability or fitness for a particular purpose.
10+
11+
def game(coins, i, j):
12+
#exit condition, i > j (not i == j)
13+
if i > j:
14+
return 0
15+
else:
16+
#Each player leaves the minimum value
17+
path1 = coins[i] + min(game(coins, i+2, j), game(coins, i+1, j-1))
18+
path2 = coins[j] + min(game(coins, i+1, j-1), game(coins, i, j-2))
19+
return max(path1,path2)
20+
21+
# row of n coins
22+
coins = [4, 3, 3, 4, 2, 3]
23+
print game(coins, 0, len(coins)-1)

0 commit comments

Comments
 (0)