The Gateway to Computer Science Excellence
+1 vote

A college professor gives several quizzes during the semester, with negative marking. He has become bored of the usual "Best $M$ out of $N$ quizzes" formula to award marks for internal assessment. Instead, each student will be evaluated based on the sum of the best contiguous segment (i.e., no gaps) of marks in the overall sequence of quizzes. However, the student is allowed to drop upto $K$ quizzes before calculating this sum.

Suppose a student has scored the following marks in $10$ quizzes during the semester.

$$\begin{array}{|c|c|c|c|c|}\hline \text{Quiz} & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10  \\\hline \text{Marks } & 6 & -5 & 3 & -7 & 6 & -1 & 10 & -8 & -8 & 8 \\ \hline \end{array}$$

Without dropping any quizzes, the best segment is quiz $5-7$, which yields a total of $15$ marks. If the student is allowed to drop upto $2$ quizzes in a segment, the best segment is quiz $1-7$, which yields a total of $24$ marks after dropping quizzes $2$ and $4$. If the student is allowed to drop upto $6$ quizzes in a segment, the best total is obtained by taking the entire list and dropping all $5$ negative entries, yielding $33$ marks.

For $1\leq i<N, 0\leq j\leq K,$ let $B[i][j]$ denote the maximum sum segment ending at position $i$ with at most $j$ marks dropped.

  1. Write a recursive formula for $B[i][j].$
  2. Explain how to calculate, using dynamic programming, the score the professor needs to award each student.
  3. Describe the space and time complexity of your dynamic programming algorithm.
in Algorithms by Boss (17.5k points)
edited by | 56 views
$A.\,\,\, B[i][j] = max(B[i-1][j-1],\,\,A[i]+B[i-1][j])$ // if we pick this value at index $i$ or not

$B.\,\,\, for(i=0\,\,to\,\,n)score=max(score, B[i][k])$

$C.\,\,\,Time\,Complexity : O(nk), \,\,Space\,Complexity:O(n)$

Space complexity is $O(n)$ as for calculating $B[\,][j]$ we only need values in $B[\,][j-1]$. So, no need to store all the values, and also at last we only need values in $B[\,][k]$ for calculating the score.

Please log in or register to answer this question.

Related questions

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
50,737 questions
57,376 answers
105,311 users