Question

{- Alexa loves movies and maintains a list of negative and/or positive integer ratings for the...

{-
Alexa loves movies and maintains a list of negative and/or positive integer ratings for the n movies in her collection. She's getting ready for a film festival and wants to choose some subsequence of movies from her collection to bring such that the following conditions are satisfied:
The collective sum of their ratings is maximal.
She must go through her list in order and cannot skip more than one movie in a row. In other words, she cannot skip over two or more consecutive movies. For example, if ratings = [-1, -3, -2], she must include either the second number or the first and third numbers to get a maximal rating sum of -3.
 
Complete the maximizeRatings function in the editor below. It has one parameter: an array of integers, ratings, denoting the respective ratings for each movie. The function must return an integer denoting the maximum possible rating sum for Alexa's chosen subsequence of movies without reordering them.
 
Input Format
Locked stub code in the editor reads the following input from stdin and passes it to the function:
The first line contains an integer, n, denoting the number of movie ratings in ratings.
Each line i of the n subsequent lines (where 0 ≤ i < n) contains an integer describing ratingsi.
 
Constraints
1 ≤ n ≤ 105
-1000 ≤ ratingsi ≤ 1000, where 0 ≤ i < n
 
Output Format
The function must return an integer denoting the maximum possible rating sum for Alexa's chosen subsequence of movies without reordering them. This is printed to stdout by locked stub code in the editor.
 
Sample Input 0
5
9
-1
-3
4
5
 
Sample Output 0
17
 
Explanation 0
ratings = [9, -1, -3, 4, 5]
Alexa picks the bolded items in ratings = [9, -1, -3, 4, 5] to get maximum rating = 9 + -1 + 4 + 5 = 17. Thus, we return 17 as our answer.
 
Sample Input 1
5
-1
-2
-3
-4
-5
 
Sample Output 1
-6
 
Explanation 1
Alexa picks the bolded items in ratings = [-1, -2, -3, -4, -5] to get maximum rating = -2 + -4 = -6. Thus, we return -6 as our answer.
-}

--problem broken on HR

Homework Answers

Answer #1
#include<bits/stdc++.h>
using namespace std;
int  maximizeRatings(int A[],int n){

    //I am Using two dimensional array for managing state
    int dp[2][n+1];
   //dp[0][i] means ith element is skipped
   //dp[1][i] means ith  element is included

   //Initially if n=1 then you could either take it or skip it

    dp[0][0]=0; //skipped
    dp[1][0]=A[0];//included
   
    
    for(int i=1;i<n;i++){
        //if ith element we are skipping means we have to take 
        //value such that i-1th element is not skipped
        dp[0][i]=dp[1][i-1];

        //if we include the ith element than we have option
        //like to conside value with i-1th element is include 
        //and excluded.we Will select the max of both ans add ith
        //value to it
        dp[1][i]=max(dp[1][i-1],dp[0][i-1])+A[i];

        

    }
    //Final answer is max of ith elemnt included and excluded
    return max(dp[0][n-1],dp[1][n-1]);

}
int main(){
    int n;
    cin>>n;
    int A[n];
    for(int i=0;i<n;i++)
    cin>>A[i];
    int ans=maximizeRatings(A,n);
    cout<<ans<<endl;



}
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Please code in Java and please implement constarints Digital Root and Iterations Given a non-negative integer,...
Please code in Java and please implement constarints Digital Root and Iterations Given a non-negative integer, print out its digital root and the number of iterations required to reach it. The digital root is the single digit number obtained by an iterative process of finding the sum of digits. In the next iteration, the sum of the digits in the previous iteration is computed, and the process repeated until a single digit value is obtained. Input Format The first line...
Hello, I'm having a hard time with the below array problem. I declare and initialized the...
Hello, I'm having a hard time with the below array problem. I declare and initialized the array, but when I create the command line argument I get syntax error. I can't get pass there. This is what I have so far: int [][] movieRatings; movieRatings = new int [args.length][args.length]; for (int i = 0; i < args.length; i++) movieRatings[i] = Integer.parseInt(args[i]); //getting a error here :( movieRatings[2] = new int [4]; Rotten Tomatoes (20 points). Write a program RURottenTomatoes.java that...
Divide-and-Conquer technique is famous for providing O(n log n) solutions for problems with O(n2) straight forward...
Divide-and-Conquer technique is famous for providing O(n log n) solutions for problems with O(n2) straight forward solutions. One of those problems is the “Maximum Subarray Sum” or “Maximum Value Contiguous Subsequence": Given a sequence of n numbers A[0 ... n-1], give an algorithm for finding a contiguous subsequence A[i ... j], for which the sum of elements in this subsequence is the maximum we can get by choosing different i or j.   For example: the maximum subarray sum for the...
Q2- Write a function solution that, given an integer N, returns the maximum possible value obtained...
Q2- Write a function solution that, given an integer N, returns the maximum possible value obtained by inserting one '5' digit inside the decimal representation of integer N. Examples: 1. Given N = 268, the function should return 5268. 2. Given N = 670, the function should return 6750. 3. Given N = 0, the function should return 50. 4. Given N = −999, the function should return −5999. Assume that: N is an integer within the range [−8,000..8,000].
APPLIED STATISTICS 2 USE R CODE ! SHOW R CODE! Write a function to calculate the...
APPLIED STATISTICS 2 USE R CODE ! SHOW R CODE! Write a function to calculate the sum of cubes from 1 to n, but skip the multiple of 5. Say, if n=10, the result is 1^3+2^3+3^3+4^3+6^3+7^3+8^3+9^3. The input is the value of n, the output is the summation. (you need if statement to check whether a number is a multiple of 5.In R, a%%b is the remainder for a divided by b. So, we have 10%%5=0) APPLIED STATISTICS 2 USE...
An integer 'n' greater than 1 is prime if its only positive divisor is 1 or...
An integer 'n' greater than 1 is prime if its only positive divisor is 1 or itself. For example, 2, 3, 5, and 7 are prime numbers, but 4, 6, 8, and 9 are not. Write a python program that defines a function isPrime (number) with the following header: def isPrime (number): that checks whether a number is prime or not. Use that function in your main program to count the number of prime numbers that are less than 5000....
There are N blocks, numbered from 0 to N-1, arranged in a row. A couple of...
There are N blocks, numbered from 0 to N-1, arranged in a row. A couple of frogs were sitting together on one block when they had a terrible quarrel. Now they want to jump away from one another so that the distance between them will be as large as possible. The distance between blocks numbered J and K, where JK, is computed as K-J+1. The frogs can only jump up, meaning that they can move from one block to another...
You will write a program that loops until the user selects 0 to exit. In the...
You will write a program that loops until the user selects 0 to exit. In the loop the user interactively selects a menu choice to compress or decompress a file. There are three menu options: Option 0: allows the user to exit the program. Option 1: allows the user to compress the specified input file and store the result in an output file. Option 2: allows the user to decompress the specified input file and store the result in an...
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g,...
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g, char wordlist[][MAX_WORD_LENGTH], int numwords)] for a C program hangman game. (The existing code for other functions and the program is below, along with what the function needs to do) What int setup_game needs to do setup_game() does exactly what the name suggests. It sets up a new game of hangman. This means that it picks a random word from the supplied wordlist array and...
Description: In this assignment, you need to implement a recursive descent parser in C++ for the...
Description: In this assignment, you need to implement a recursive descent parser in C++ for the following CFG: 1. exps --> exp | exp NEWLINE exps 2. exp --> term {addop term} 3. addop --> + | - 4. term --> factor {mulop factor} 5. mulop --> * | / 6. factor --> ( exp ) | INT The 1st production defines exps as an individual expression, or a sequence expressions separated by NEWLINE token. The 2nd production describes an...