Form N-copy string with add, remove and append operations
Last Updated :
13 Sep, 2022
A string is called a N-copy string when it is formed by N copies of letter ‘G’. ie “GGGG” is a 4-copy string since it contains 4 copies of the letter ‘G’. Initially we have a empty string on which we can perform the following three operations:
- Add a single letter ‘G’ with a cost of X.
- Remove a single letter ‘G’ with a cost of X.
- Append the string formed till now to itself with a cost of Y, i.e we have the string ‘GG’ we can append it to itself to make ‘GGGG’
Given N, X and Y find the minimum cost to generate the N-copy string using the above operations
Examples:
Input : N = 4, X = 2, Y = 1
Output : 4
Explanation:
- Initially, the string it empty, we perform one Add operation to form string ‘G’.
- Next, we perform a operation of type 3, and form the string ‘GG’.
- Again we perform the operation 3 and form ‘GGGG’, which is the required N-copy string.
Cost = X + Y + Y = 2 + 1 + 1 = 4
Input : N = 4, X = 1, Y = 3
Output : 4
We can perform 4 consecutive Add operations to form ‘GGGG’.
Cost = X + X + X + X = 1 + 1 + 1 + 1 = 4
The Problem can be solved using a Dynamic Programming Approach.
If we carefully analyse the operations, it is easy to see that we perform the remove operation only when there is an extra copy than what is required, this can only be the case when before this remove operation there was a operation of type 3 involved, because if there was an operation of type 1 before it, then it is meaningless as consecutive add and remove operations only increase the cost, without introducing any new copies into our string.
Thus we can say we have the following operations,
- Add a single character ‘G’, with cost X
- Append the string to itself and then remove a single character ‘G’, with cost Y + X
- Append the string to itself, with cost Y
Note: Now on we refer to these modified operations
Now, it can be solved with a O(n) DP approach.
Let dp[i] represent the minimum cost to form a i-copy string
then the state transitions can be:
- If i is odd,
- Case 1: Add a character to the (i-1)-copy string,
- Case 2: Perform an operation of type 2 on the ((i+1)/2)-copy string
- i.e dp[i] = min(dp[i – 1] + X, dp[(i + 1) / 2)] + Y + X)
- If i is even
- Case 1: Add a character to the (i-1)-copy string,
- Case 2: Perform an operation of type 3 on the (i/2)-copy string
- i.e dp[i] = min(dp[i – 1] + X, dp[i / 2] + Y)
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
int findMinimumCost( int n, int x, int y)
{
int * dp = new int [n + 1];
dp[1] = x;
for ( int i = 2; i <= n; i++) {
if (i & 1) {
dp[i] = min(dp[i - 1] + x, dp[(i + 1) / 2] + y + x);
}
else {
dp[i] = min(dp[i - 1] + x, dp[i / 2] + y);
}
}
return dp[n];
}
int main()
{
int n = 4, x = 2, y = 1;
cout << findMinimumCost(n, x, y);
return 0;
}
|
Java
class Solution
{
static int findMinimumCost( int n, int x, int y)
{
int dp[] = new int [n + 1 ];
dp[ 1 ] = x;
for ( int i = 2 ; i <= n; i++) {
if ((i & 1 )!= 0 ) {
dp[i] = Math.min(dp[i - 1 ] + x, dp[(i + 1 ) / 2 ] + y + x);
}
else {
dp[i] = Math.min(dp[i - 1 ] + x, dp[i / 2 ] + y);
}
}
return dp[n];
}
public static void main(String args[])
{
int n = 4 , x = 2 , y = 1 ;
System.out.println( findMinimumCost(n, x, y));
}
}
|
Python3
def findMinimumCost(n, x, y):
dp = [ 0 for i in range (n + 1 )]
dp[ 1 ] = x
for i in range ( 2 , n + 1 ):
if i & 1 :
dp[i] = min (dp[i - 1 ] + x,
dp[(i + 1 ) / / 2 ] + y + x)
else :
dp[i] = min (dp[i - 1 ] + x,
dp[i / / 2 ] + y)
return dp[n]
n, x, y = 4 , 2 , 1
print (findMinimumCost(n, x, y))
|
C#
using System;
class GFG
{
static int findMinimumCost( int n, int x, int y)
{
int [] dp = new int [n + 1];
dp[1] = x;
for ( int i = 2; i <= n; i++)
{
if ((i & 1)!=0)
{
dp[i] = Math.Min(dp[i - 1] + x,
dp[(i + 1) / 2] + y + x);
}
else
{
dp[i] = Math.Min(dp[i - 1] + x,
dp[i / 2] + y);
}
}
return dp[n];
}
public static void Main()
{
int n = 4, x = 2, y = 1;
Console.WriteLine(findMinimumCost(n, x, y));
}
}
|
PHP
<?php
function findMinimumCost( $n , $x , $y )
{
$dp [ $n + 1] = array ();
$dp [1] = $x ;
for ( $i = 2; $i <= $n ; $i ++)
{
if ( $i & 1)
{
$dp [ $i ] = min( $dp [ $i - 1] + $x ,
$dp [( $i + 1) / 2] + $y + $x );
}
else
{
$dp [ $i ] = min( $dp [ $i - 1] + $x ,
$dp [ $i / 2] + $y );
}
}
return $dp [ $n ];
}
$n = 4;
$x = 2;
$y = 1;
echo findMinimumCost( $n , $x , $y );
?>
|
Javascript
<script>
function findMinimumCost(n, x, y)
{
let dp = new Array(n + 1);
dp[1] = x;
for (let i = 2; i <= n; i++) {
if ((i & 1)!=0) {
dp[i] = Math.min(dp[i - 1] + x, dp[parseInt((i + 1) / 2, 10)] + y + x);
}
else {
dp[i] = Math.min(dp[i - 1] + x, dp[parseInt(i / 2, 10)] + y);
}
}
return dp[n];
}
let n = 4, x = 2, y = 1;
document.write(findMinimumCost(n, x, y));
</script>
|
Complexity Analysis:
- Time Complexity O(n)
- Auxiliary Space O(n)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...