Minimum score possible for a player by selecting one or two consecutive array elements from given binary array
Last Updated :
08 Oct, 2021
Given a binary array arr[] of size N and two players, A and B. The task is to minimize the score for player A by selecting the scores for the players as per the given constraints:
- Each player can remove one or two consecutive numbers in their turn from the array and elements are removed in the order from left to right.
- Players will have alternate turns, starting from player A.
- Initially, the penalty is 0 and is increased by numbers, which player A remove.
Examples:
Input: arr[] = {1, 1, 1, 1, 0, 0, 1}
Output: 2
Explanation: Elements can be removed as follows:
Turn 1: Player A remove the element at index 0. Therefore, penalty is 0 + 1 = 1.
Turn 2: Player B remove the elements at indices 1 and 2. Still, penalty is 1.
Turn 3: Player A remove the element at index 3. Therefore, penalty is 1 + 1 = 2.
Turn 4: Player B remove the element at index 4. Still, penalty is 2.
Turn 5: Player A remove the element at index 5. Still, penalty is 2 + 0 = 2.
Turn 6: Player B remove the element at index 6. Still, penalty is 2.
Hence, the minimum score for player A = 2.
Input: arr[] = {1, 0, 1, 1, 0, 1, 1, 1}
Output: 2
Explanation: Elements can be removed as follows:
Turn 1: Player A remove the element at indices 0 and 1. Therefore, penalty is 0 + 1 + 0 = 1.
Turn 2: Player B remove the elements at indices 2 and 3. Still, penalty is 1.
Turn 3: Player A remove the element at index 4. Therefore, penalty is 1 + 0 = 1.
Turn 4: Player B remove the elements at indices 5 and 6. Still, penalty is 1.
Turn 5: Player A remove the element at index 7. Therefore, penalty is 2 + 1 = 2.
Hence, the minimum score for player A = 2.
Naive Approach: The simplest approach is to try all possible combinations to remove elements from the given array. Each time, there are two possible options i.e., one or two consecutive elements can be removed. At each position, from 1 to N – 1, there are 2 choices. Hence, 2N possible combinations can be made. Penalties for each combination can be found and amongst them, print the minimum penalty.
Time Complexity: O(2N)
Auxiliary Space: O(1)
Efficient Approach: To optimize the above approach, the idea is to use Dynamic Programming. It can be solved using the following dp transitions, where dp[i][0] stores the minimum penalty from i to N – 1. If player A start choosing from index i. Similarly, dp[i][1] can be defined for player B.
On player A’s turn:
dp[i][0] = min(dp(i+1, 1)+arr[i], dp(i+2, 1)+arr[i+1]+arr[i+2])
where,
i denotes the current position.
1 denotes that it is B’s turn on next state.
On player B’s turn:
dp[i][1] = min(dp(i+1, 0), dp(i+2, 0))
where,
i denotes the current position.
0 denotes that it is A’s turn on next state.
Follow the steps below to solve the problem:
- Recursion with memorization can be used. For base condition, check if the current position exceeds or becomes N, return 0
- Apply the transitions defined above according to the player’s turn and return the minimum answer.
- Initialize the recursive function with player A’s turn and penalty as 0.
- For each recursive call, store the minimum of penalties calculated in a Map M to avoid calculated for Overlapping Subproblems.
- Print the minimum score for the player A after the above recursive call ends
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
map<pair< int , int >, int > m;
int findMinimum( int a[], int n, int pos,
int myturn)
{
if (m.find({ pos, myturn }) != m.end()) {
return m[{ pos, myturn }];
}
if (pos >= n) {
return 0;
}
if (!myturn) {
int ans = min(
findMinimum(a, n, pos + 1, !myturn)
+ a[pos],
findMinimum(a, n, pos + 2, !myturn)
+ a[pos] + a[pos + 1]);
m[{ pos, myturn }] = ans;
return ans;
}
if (myturn) {
int ans = min(
findMinimum(a, n, pos + 1, !myturn),
findMinimum(a, n, pos + 2, !myturn));
m[{ pos, myturn }] = ans;
return ans;
}
return 0;
}
int countPenality( int arr[], int N)
{
int pos = 0;
int turn = 0;
return findMinimum(arr, N, pos, turn);
}
void printAnswer( int * arr, int N)
{
int a = countPenality(arr, N);
int sum = 0;
for ( int i = 0; i < N; i++) {
sum += arr[i];
}
cout << a;
}
int main()
{
int arr[] = { 1, 0, 1, 1, 0, 1, 1, 1 };
int N = sizeof (arr) / sizeof (arr[0]);
printAnswer(arr, N);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG{
static class R
{
int x, y;
public R( int x, int y)
{
this .x = x;
this .y = y;
}
}
static HashMap<R, Integer> m = new HashMap<>();
public static int findMinimum( int [] arr, int N,
int pos, int turn)
{
R x = new R(pos, turn);
if (m.containsKey(x))
{
return m.get(x);
}
if (pos >= N - 1 )
{
return 0 ;
}
if (turn == 0 )
{
int ans = Math.min(
findMinimum(arr, N, pos + 1 , 1 ) + arr[pos],
findMinimum(arr, N, pos + 2 , 1 ) + arr[pos] +
arr[pos + 1 ]);
R v = new R(pos, turn);
m.put(v, ans);
return ans;
}
if (turn != 0 )
{
int ans = Math.min(
findMinimum(arr, N, pos + 1 , 0 ),
findMinimum(arr, N, pos + 2 , 0 ));
R v = new R(pos, turn);
m.put(v, ans);
return ans;
}
return 0 ;
}
public static int countPenality( int [] arr, int N)
{
int pos = 0 ;
int turn = 0 ;
return findMinimum(arr, N, pos, turn) + 1 ;
}
public static void printAnswer( int [] arr, int N)
{
int a = countPenality(arr, N);
int sum = 0 ;
for ( int i = 0 ; i < N; i++)
{
sum += arr[i];
}
System.out.println(a);
}
public static void main(String[] args)
{
int arr[] = { 1 , 0 , 1 , 1 , 0 , 1 , 1 , 1 };
int N = 8 ;
printAnswer(arr, N);
}
}
|
Python3
m = dict ()
def findMinimum(a, n, pos, myturn):
if (pos, myturn) in m:
return m[( pos, myturn )];
if (pos > = n - 1 ):
return 0 ;
if ( not myturn):
ans = min ( findMinimum(a, n, pos + 1 , not myturn) + a[pos],
findMinimum(a, n, pos + 2 , not myturn) + a[pos] + a[pos + 1 ]);
m[( pos, myturn )] = ans;
return ans;
if (myturn):
ans = min ( findMinimum(a, n, pos + 1 , not myturn),
findMinimum(a, n, pos + 2 , not myturn));
m[( pos, myturn )] = ans;
return ans;
return 0 ;
def countPenality(arr, N):
pos = 0 ;
turn = False ;
return findMinimum(arr, N, pos, turn) + 1 ;
def printAnswer(arr, N):
a = countPenality(arr, N);
sum = 0 ;
for i in range (N):
sum + = arr[i];
print (a)
if __name__ = = '__main__' :
arr = [ 1 , 0 , 1 , 1 , 0 , 1 , 1 , 1 ]
N = len (arr)
printAnswer(arr, N);
|
C#
using System;
using System.Collections.Generic;
class GFG {
static Dictionary<Tuple< int , int >, int > m = new Dictionary<Tuple< int , int >, int >();
static int findMinimum( int [] arr, int N, int pos, int turn)
{
Tuple< int , int > x = new Tuple< int , int >(pos, turn);
if (m.ContainsKey(x))
{
return m[x];
}
if (pos >= N - 1)
{
return 0;
}
if (turn == 0)
{
int ans = Math.Min(
findMinimum(arr, N, pos + 1, 1) + arr[pos],
findMinimum(arr, N, pos + 2, 1) + arr[pos] +
arr[pos + 1]);
Tuple< int , int > v = new Tuple< int , int >(pos, turn);
m[v] = ans;
return ans;
}
if (turn != 0)
{
int ans = Math.Min(
findMinimum(arr, N, pos + 1, 0),
findMinimum(arr, N, pos + 2, 0));
Tuple< int , int > v = new Tuple< int , int >(pos, turn);
m[v] = ans;
return ans;
}
return 0;
}
static int countPenality( int [] arr, int N)
{
int pos = 0;
int turn = 0;
return findMinimum(arr, N, pos, turn) + 1;
}
static void printAnswer( int [] arr, int N)
{
int a = countPenality(arr, N);
int sum = 0;
for ( int i = 0; i < N; i++)
{
sum += arr[i];
}
Console.WriteLine(a);
}
static void Main() {
int [] arr = { 1, 0, 1, 1, 0, 1, 1, 1 };
int N = 8;
printAnswer(arr, N);
}
}
|
Javascript
<script>
let m = new Map();
function findMinimum(arr, N, pos, turn)
{
let x = [pos, turn];
if (m.has(x))
{
return m[x];
}
if (pos >= N - 1)
{
return 0;
}
if (turn == 0)
{
let ans = Math.min(
findMinimum(arr, N, pos + 1, 1) + arr[pos],
findMinimum(arr, N, pos + 2, 1) + arr[pos] +
arr[pos + 1]);
let v = [pos, turn];
m[v] = ans;
return ans;
}
if (turn != 0)
{
let ans = Math.min(
findMinimum(arr, N, pos + 1, 0),
findMinimum(arr, N, pos + 2, 0));
let v = [pos, turn];
m[v] = ans;
return ans;
}
return 0;
}
function countPenality(arr, N)
{
let pos = 0;
let turn = 0;
return findMinimum(arr, N, pos, turn) + 1;
}
function printAnswer(arr, N)
{
let a = countPenality(arr, N);
let sum = 0;
for (let i = 0; i < N; i++)
{
sum += arr[i];
}
document.write(a);
}
let arr = [ 1, 0, 1, 1, 0, 1, 1, 1 ];
let N = 8;
printAnswer(arr, N);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...