Maximizing Readability-Coefficient for Book Printing
Last Updated :
07 Dec, 2023
A book publisher collected data on the readability level of his n books. Book publisher can print any book in 1 unit of time. Readability-Coefficient of a book is defined as the time taken to print that book including previous books multiplied by its readability level, i.e. total_time * readability[i], the task is to return the maximum sum of Readability-Coefficient that the publisher can obtain after books are printed, you can keep the books even if its readability level is negative, so as to get the maximum value of total Readability-Coefficient. Books can be printed in any order and the publisher can discard some books to get this maximum value.
Examples:
Input: readability: [-1, -7, 3]
Output: 5
Explanation: Here we could have taken -1 and -7 but taking both of them reduces the total Readability-Coefficient , as -7*1 + -1*2 + 3*3 = 0 , we can neglect -7 readability and -1 readability but now the total Readability-Coefficient becomes 3*1 = 3, hence we keep -1*1 + 3*2 = 5 as the total Readability-Coefficient, which is the maximum value.
Input: readability: [-1, -3, -9]
Output: 0
Explanation: People do not like the books. No book is published.
Approach: To solve the problem follow the below idea:
The idea behind this is to Take or Not Take the current Book with certain readability, and simultaneously keep incrementing the counter to keep track of the time.
Steps to solve the problem:
- The solver function is designed to recursively explore the possibilities and calculate the maximum readability.
- It takes three parameters: readability (vector of integers), i (current index), and counter (a counter to keep track of the time).
- Base Cases:
- If i exceeds the size of the readability vector, it returns 0, as there are no more elements to consider.
- If the value for the current i and counter combination has already been calculated (dp[i][counter] is not -1), it returns the memoized result.
- Recursive Cases:
- It considers two possibilities: either taking the current readability or not taking it.
- take is calculated by multiplying the readability at index i with the counter and recursively calling solver for the next index (i+1) and an incremented counter (counter+1).
- nottake is calculated by directly calling solver for the next index (i+1) with the same counter.
- Sorting:
- The readability vector is sorted before the process starts.
- Modulo Operation:The final result is taken modulo 1000000007 to ensure that the result remains within the desired range.
Below is the implementation of the above approach:
C++14
#include <bits/stdc++.h>
using namespace std;
int dp[1001][1001];
int solver(vector< int >& readability, int i, int counter)
{
if (i >= readability.size()) {
return 0;
}
if (dp[i][counter] != -1)
return dp[i][counter];
int take = readability[i] * counter
+ solver(readability, i + 1, counter + 1);
int nottake = solver(readability, i + 1, counter);
return dp[i][counter] = max(take, nottake) % 1000000007;
}
int maxreadability(vector< int >& readability)
{
sort(readability.begin(), readability.end());
memset (dp, -1, sizeof (dp));
return solver(readability, 0, 1);
}
int main()
{
vector< int > readability = { -1, -7, -3, 5, -9 };
cout << maxreadability(readability);
return 0;
}
|
Java
import java.util.Arrays;
import java.util.Vector;
public class Main {
static int [][] dp = new int [ 1001 ][ 1001 ];
public static void main(String[] args) {
Vector<Integer> readability = new Vector<>(Arrays.asList(- 1 , - 7 , - 3 , 5 , - 9 ));
readability.sort( null );
for ( int [] row : dp) {
Arrays.fill(row, - 1 );
}
System.out.println(maxreadability(readability));
}
public static int solver(Vector<Integer> readability, int i, int counter) {
if (i >= readability.size()) {
return 0 ;
}
if (dp[i][counter] != - 1 ) {
return dp[i][counter];
}
int take = readability.get(i) * counter + solver(readability, i + 1 , counter + 1 );
int nottake = solver(readability, i + 1 , counter);
return dp[i][counter] = Math.max(take, nottake) % 1000000007 ;
}
public static int maxreadability(Vector<Integer> readability) {
return solver(readability, 0 , 1 );
}
}
|
Python3
dp = [[ - 1 ] * 1001 for _ in range ( 1001 )]
def solver(readability, i, counter):
if i > = len (readability):
return 0
if dp[i][counter] ! = - 1 :
return dp[i][counter]
take = readability[i] * counter + solver(readability, i + 1 , counter + 1 )
nottake = solver(readability, i + 1 , counter)
dp[i][counter] = max (take, nottake) % 1000000007
return dp[i][counter]
def maxreadability(readability):
return solver(readability, 0 , 1 )
if __name__ = = "__main__" :
readability = [ - 1 , - 7 , - 3 , 5 , - 9 ]
readability.sort()
print (maxreadability(readability))
|
C#
using System;
using System.Collections.Generic;
class GFG {
static int [, ] dp = new int [1001, 1001];
public static int solver(List< int > readability, int i,
int counter)
{
if (i >= readability.Count) {
return 0;
}
if (dp[i, counter] != -1) {
return dp[i, counter];
}
int take
= readability[i] * counter
+ solver(readability, i + 1, counter + 1);
int nottake = solver(readability, i + 1, counter);
return dp[i, counter]
= Math.Max(take, nottake) % 1000000007;
}
public static int maxreadability(List< int > readability)
{
return solver(readability, 0, 1);
}
public static void Main()
{
List< int > readability
= new List< int >{ -1, -7, -3, 5, -9 };
readability.Sort();
for ( int i = 0; i < 1001; i++) {
for ( int j = 0; j < 1001; j++) {
dp[i, j] = -1;
}
}
Console.WriteLine(maxreadability(readability));
}
}
|
Javascript
const dp = new Array(1001);
for (let i = 0; i < 1001; i++) {
dp[i] = new Array(1001).fill(-1);
}
function solver(readability, i, counter) {
if (i >= readability.length) {
return 0;
}
if (dp[i][counter] != -1)
return dp[i][counter];
let take = readability[i] * counter +
solver(readability, i + 1, counter + 1);
let nottake = solver(readability, i + 1, counter);
dp[i][counter] = Math.max(take, nottake) % 1000000007;
return dp[i][counter];
}
function maxreadability(readability) {
readability.sort((a, b) => a - b);
return solver(readability, 0, 1);
}
const readability = [-1, -7, -3, 5, -9]
console.log(maxreadability(readability))
|
Time Complexity: O(n^2). where n is the size of the ‘readability‘ vector
Auxiliary Space: O(n^2) .
Share your thoughts in the comments
Please Login to comment...