
The above recursion has overlapping subproblem property. In the following formula, C is the optimized total cost for arranging words from 1 to j. Once we have the lc table constructed, we can calculate total cost using following recursive formula. If a sequence of words from i to j cannot fit in a single line, then lc is considered infinite (to avoid it from being a part of the solution). The value lc indicates the cost to put words from i to j in a single line where i and j are indexes of words in the input sequences. First we compute costs of all possible lines in a 2D table lc. The following Dynamic approach strictly follows the algorithm given in solution of Cormen book. The cost function with cubic sum serves the purpose because the value of total cost in second scenario is less. Total extra spaces are 3 in both scenarios, but second arrangement should be preferred because extra spaces are balanced in all three lines. One line has 3 extra spaces and all other lines have 0 extra spaces. For example, consider the following two arrangement of same set of words:ġ) There are 3 lines. The idea behind this cost function is to balance the spaces among lines. Please note that the total cost function is not sum of extra spaces, but sum of cubes (or square is also used) of extra spaces. So optimal value of total cost is 0 + 2*2*2 + 3*3*3 = 35 The total extra spaces in line 1, line 2 and line 3 are 0, 2 and 3 respectively. "Geeks for Geeks presents word wrap problem"įollowing is the optimized arrangement of words in 3 lines The problem is to minimize the following total cost.Ĭost of a line = (Number of extra spaces in the line)^3įor example, consider the following string and line width M = 15 The extra spaces includes spaces put at the end of every line except the last one. In other words, not have few lines with lots of extra spaces and some lines with small amount of extra spaces. The word processors like MS Word do task of placing line breaks. Assume that the length of each word is smaller than the line width. Put line breaks in the given sequence such that the lines are printed neatly. Given a sequence of words, and a limit on the number of characters that can be put in one line (line width). Rearrange array in alternating positive & negative items with O(1) extra space | Set 1.Rearrange positive and negative numbers in O(n) time and O(1) extra space.Rearrange array such that arr >= arr if i is even and arrWrite a program to reverse an array or string.Write a program to reverse digits of a number.Reverse digits of an integer with overflow handled.Maximum and minimum of an array using minimum number of comparisons.Find the smallest and second smallest elements in an array.Find Second largest element in an array.k-th smallest absolute difference of two elements in an array.Find k pairs with smallest sums in two arrays.k smallest elements in same order using O(1) extra space.K maximum sums of non-overlapping contiguous sub-arrays.K maximum sums of overlapping contiguous sub-arrays.K maximum sum combinations from two arrays.Minimum product of k integers in an array of positive Integers.Median of Stream of Running Integers using STL.Median in a stream of integers (running integers).Longest Increasing Subsequence Size (N log N).Maximum size square sub-matrix with all 1s.Maximum size rectangle binary sub-matrix with all 1s.ISRO CS Syllabus for Scientist/Engineer Exam.ISRO CS Original Papers and Official Keys.GATE CS Original Papers and Official Keys.
