-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOptimalStrategyforaGame.java
More file actions
171 lines (142 loc) · 4.03 KB
/
OptimalStrategyforaGame.java
File metadata and controls
171 lines (142 loc) · 4.03 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
Consider a row of N coins of values V1 . . . Vn, where N is even. We play a game against an opponent by alternating turns. In each turn, a player selects either the first or last coin from the row, removes it from the row permanently, and receives the value of the coin. Determine the maximum possible amount of money we can definitely win if we move first.
Note: The opponent is as clever as the user.
Examples:
Input: {5, 3, 7, 10}
Output: 15 -> (10 + 5)
// Java code to implement the approach
import java.util.*;
class GFG {
static ArrayList<Integer> arr = new ArrayList<>();
static HashMap<ArrayList<Integer>, Integer> memo
= new HashMap<>();
static int n = 0;
// recursive top down memoized solution
static int solve(int i, int j)
{
if ((i > j) || (i >= n) || (j < 0))
return 0;
ArrayList<Integer> k = new ArrayList<Integer>();
k.add(i);
k.add(j);
if (memo.containsKey(k))
return memo.get(k);
// if the user chooses ith coin, the opponent can
// choose from i+1th or jth coin. if he chooses
// i+1th coin, user is left with [i+2,j] range. if
// opp chooses jth coin, then user is left with
// [i+1,j-1] range to choose from. Also opponent
// tries to choose in such a way that the user has
// minimum value left.
int option1 = arr.get(i)
+ Math.min(solve(i + 2, j),
solve(i + 1, j - 1));
// if user chooses jth coin, opponent can choose ith
// coin or j-1th coin. if opp chooses ith coin,user
// can choose in range [i+1,j-1]. if opp chooses
// j-1th coin, user can choose in range [i,j-2].
int option2 = arr.get(j)
+ Math.min(solve(i + 1, j - 1),
solve(i, j - 2));
// since the user wants to get maximum money
memo.put(k, Math.max(option1, option2));
return memo.get(k);
}
static int optimalStrategyOfGame()
{
memo.clear();
return solve(0, n - 1);
}
// Driver code
public static void main(String[] args)
{
arr.add(8);
arr.add(15);
arr.add(3);
arr.add(7);
n = arr.size();
System.out.println(optimalStrategyOfGame());
arr.clear();
arr.add(2);
arr.add(2);
arr.add(2);
arr.add(2);
n = arr.size();
System.out.println(optimalStrategyOfGame());
arr.clear();
arr.add(20);
arr.add(30);
arr.add(2);
arr.add(2);
arr.add(2);
arr.add(10);
n = arr.size();
System.out.println(optimalStrategyOfGame());
}
}
// This code is contributed by phasing17
// C++ code to implement the approach
#include <bits/stdc++.h>
using namespace std;
vector<int> arr;
map<vector<int>, int> memo;
int n = arr.size();
// recursive top down memoized solution
int solve(int i, int j)
{
if ((i > j) || (i >= n) || (j < 0))
return 0;
vector<int> k{ i, j };
if (memo[k] != 0)
return memo[k];
// if the user chooses ith coin, the opponent can choose
// from i+1th or jth coin. if he chooses i+1th coin,
// user is left with [i+2,j] range. if opp chooses jth
// coin, then user is left with [i+1,j-1] range to
// choose from. Also opponent tries to choose in such a
// way that the user has minimum value left.
int option1
= arr[i]
+ min(solve(i + 2, j), solve(i + 1, j - 1));
// if user chooses jth coin, opponent can choose ith
// coin or j-1th coin. if opp chooses ith coin,user can
// choose in range [i+1,j-1]. if opp chooses j-1th coin,
// user can choose in range [i,j-2].
int option2
= arr[j]
+ min(solve(i + 1, j - 1), solve(i, j - 2));
// since the user wants to get maximum money
memo[k] = max(option1, option2);
return memo[k];
}
int optimalStrategyOfGame()
{
memo.clear();
return solve(0, n - 1);
}
// Driver code
int main()
{
arr.push_back(8);
arr.push_back(15);
arr.push_back(3);
arr.push_back(7);
n = arr.size();
cout << optimalStrategyOfGame() << endl;
arr.clear();
arr.push_back(2);
arr.push_back(2);
arr.push_back(2);
arr.push_back(2);
n = arr.size();
cout << optimalStrategyOfGame() << endl;
arr.clear();
arr.push_back(20);
arr.push_back(30);
arr.push_back(2);
arr.push_back(2);
arr.push_back(2);
arr.push_back(10);
n = arr.size();
cout << optimalStrategyOfGame() << endl;
}
// This code is contributed by phasing17