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. Total time complexity of solution will be O(N) and auxiliary space needed will be O(N).

`// C++ program to write characters in ` `// minimum time by inserting, removing ` `// and copying operation ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// method returns minimum time to write ` `// 'N' characters ` `int` `minTimeForWritingChars(` `int` `N, ` `int` `insert, ` ` ` `int` `remove` `, ` `int` `copy) ` `{ ` ` ` `if` `(N == 0) ` ` ` `return` `0; ` ` ` `if` `(N == 1) ` ` ` `return` `insert; ` ` ` ` ` `// declare dp array and initialize with zero ` ` ` `int` `dp[N + 1]; ` ` ` `memset` `(dp, 0, ` `sizeof` `(dp)); ` ` ` ` ` `// loop for 'N' number of times ` ` ` `for` `(` `int` `i = 1; i <= N; i++) ` ` ` `{ ` ` ` `/* if current char count is even then ` ` ` `choose minimum from result for (i-1) ` ` ` `chars and time for insertion and ` ` ` `result for half of chars and time ` ` ` `for copy */` ` ` `if` `(i % 2 == 0) ` ` ` `dp[i] = min(dp[i-1] + insert, ` ` ` `dp[i/2] + copy); ` ` ` ` ` `/* if current char count is odd then ` ` ` `choose minimum from ` ` ` `result for (i-1) chars and time for ` ` ` `insertion and ` ` ` `result for half of chars and time for ` ` ` `copy and one extra character deletion*/` ` ` `else` ` ` `dp[i] = min(dp[i-1] + insert, ` ` ` `dp[(i+1)/2] + copy + ` `remove` `); ` ` ` `} ` ` ` `return` `dp[N]; ` `} ` ` ` `// Driver code to test above methods ` `int` `main() ` `{ ` ` ` `int` `N = 9; ` ` ` `int` `insert = 1, ` `remove` `= 2, copy = 1; ` ` ` `cout << minTimeForWritingChars(N, insert, ` ` ` `remove` `, copy); ` ` ` `return` `0; ` `} ` |

Output:

5

This article is contributed by **Utkarsh Trivedi**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

## Recommended Posts:

- Longest Common Substring (Space optimized DP solution)
- Value of continuous floor function : F(x) = F(floor(x/2)) + x
- Count Balanced Binary Trees of Height h
- Counts paths from a point to reach Origin
- Pyramid form (increasing then decreasing) consecutive array using reduce operations
- Sum of all substrings of a string representing a number | Set 1
- Minimum cost to fill given weight in a bag
- How to print maximum number of A's using given four keys
- Travelling Salesman Problem | Set 1 (Naive and Dynamic Programming)
- Longest Common Substring | DP-29
- Minimum insertions to form a palindrome | DP-28
- Word Wrap Problem | DP-19
- Minimum number of jumps to reach end
- Edit Distance | DP-5
- Largest Sum Contiguous Subarray