Minimum time to write characters using insert, delete and copy operation
Last Updated :
01 Nov, 2021
We need to write N same characters on a screen and each time we can insert a character, delete the last character and copy and paste all written characters i.e. after copy operation count of total written character will become twice. Now we are given time for insertion, deletion and copying. We need to output minimum time to write N characters on the screen using these operations.
Examples:
Input : N = 9
insert time = 1
removal time = 2
copy time = 1
Output : 5
N character can be written on screen in 5 time units as shown below,
insert a character
characters = 1 total time = 1
again insert character
characters = 2 total time = 2
copy characters
characters = 4 total time = 3
copy characters
characters = 8 total time = 4
insert character
characters = 9 total time = 5
We can solve this problem using dynamic programming. We can observe a pattern after solving some examples by hand that for writing each character we have two choices either get it by inserting or get it by copying, whichever takes less time. Now writing relation accordingly,
Let dp[i] be the optimal time to write i characters on screen then,
If i is even then,
dp[i] = min((dp[i-1] + insert_time),
(dp[i/2] + copy_time))
Else (If i is odd)
dp[i] = min(dp[i-1] + insert_time),
(dp[(i+1)/2] + copy_time + removal_time)
In the case of odd, removal time is added because when (i+1)/2 characters will be copied one extra character will be on the screen which needs to be removed.
C++
#include <bits/stdc++.h>
using namespace std;
int minTimeForWritingChars( int N, int insert,
int remove , int copy)
{
if (N == 0)
return 0;
if (N == 1)
return insert;
int dp[N + 1];
memset (dp, 0, sizeof (dp));
dp[1] = insert;
for ( int i = 2; i <= N; i++)
{
if (i % 2 == 0)
dp[i] = min(dp[i-1] + insert,
dp[i/2] + copy);
else
dp[i] = min(dp[i-1] + insert,
dp[(i+1)/2] + copy + remove );
}
return dp[N];
}
int main()
{
int N = 9;
int insert = 1, remove = 2, copy = 1;
cout << minTimeForWritingChars(N, insert,
remove , copy);
return 0;
}
|
Java
public class GFG{
static int minTimeForWritingChars( int N, int insert,
int remove, int copy)
{
if (N == 0 )
return 0 ;
if (N == 1 )
return insert;
int dp[] = new int [N + 1 ];
dp[ 1 ] = insert;
for ( int i = 2 ; i <= N; i++)
{
if (i % 2 == 0 )
dp[i] = Math.min(dp[i- 1 ] + insert, dp[i/ 2 ] + copy);
else
dp[i] = Math.min(dp[i- 1 ] + insert,
dp[(i+ 1 )/ 2 ] + copy + remove);
}
return dp[N];
}
public static void main(String []args)
{
int N = 9 ;
int insert = 1 , remove = 2 , copy = 1 ;
System.out.println(minTimeForWritingChars(N, insert,remove, copy));
}
}
|
Python3
def minTimeForWritingChars(N, insert,
remove, cpy):
if N = = 0 :
return 0
if N = = 1 :
return insert
dp = [ 0 ] * (N + 1 )
dp[ 1 ] = insert
for i in range ( 2 , N + 1 ):
if i % 2 = = 0 :
dp[i] = min (dp[i - 1 ] + insert,
dp[i / / 2 ] + cpy)
else :
dp[i] = min (dp[i - 1 ] + insert,
dp[(i + 1 ) / / 2 ] +
cpy + remove)
return dp[N]
if __name__ = = "__main__" :
N = 9
insert = 1
remove = 2
cpy = 1
print (minTimeForWritingChars(N, insert,
remove, cpy))
|
C#
using System;
class GFG
{
static int minTimeForWritingChars( int N, int insert,
int remove, int copy)
{
if (N == 0)
return 0;
if (N == 1)
return insert;
int [] dp = new int [N + 1];
for ( int i = 0; i < N + 1; i++)
dp[i] = 0;
dp[1] = insert;
for ( int i = 2; i <= N; i++)
{
if (i % 2 == 0)
dp[i] = Math.Min(dp[i - 1] + insert,
dp[i / 2] + copy);
else
dp[i] = Math.Min(dp[i - 1] + insert,
dp[(i + 1) / 2] + copy + remove);
}
return dp[N];
}
static void Main()
{
int N = 9;
int insert = 1, remove = 2, copy = 1;
Console.Write(minTimeForWritingChars(N, insert,
remove, copy));
}
}
|
Javascript
<script>
function minTimeForWritingChars(N, insert, remove, copy)
{
if (N == 0)
return 0;
if (N == 1)
return insert;
let dp = new Array(N + 1);
for (let i = 0; i < N + 1; i++)
dp[i] = 0;
dp[1] = insert;
for (let i = 2; i <= N; i++)
{
if (i % 2 == 0)
dp[i] = Math.min(dp[i - 1] + insert, dp[parseInt(i / 2, 10)] + copy);
else
dp[i] = Math.min(dp[i - 1] + insert,
dp[parseInt((i + 1) / 2, 10)] + copy + remove);
}
return dp[N];
}
let N = 9;
let insert = 1, remove = 2, copy = 1;
document.write(minTimeForWritingChars(N, insert,
remove, copy));
</script>
|
Time complexity: O(N)
Auxiliary space: O(N).
Share your thoughts in the comments
Please Login to comment...