Problem Statement
Create a class OnLineRank that contains a method calcRanks that is given k, the number of recent scores to use in each rank calculation, and int[] scores, the sequence of scores of the arriving proposals. The method should calculate the rank for each score and return the sum of all the ranks.
Each rank should be calculated based on the preceding k scores. If fewer than k scores have preceded this score, base the calculation on all the preceding scores. (The first proposal is thus guaranteed a rank of 1.)
Definition
- Class:
- OnLineRank
- Method:
- calcRanks
- Parameters:
- int, int[]
- Returns:
- int
- Method signature:
- int calcRanks(int k, int[] scores)
- (be sure your method is public)
Constraints
- k will be between 1 and 100 inclusive.
- scores will contain between 1 and 50 elements inclusive.
- Each element of scores will be between 0 and 1000 inclusive.
Examples
3
{6,9,8,15,7,12}Returns: 11
The 6 is the first score and earns a rank of 1. The 9 is compared with the 6, which does not exceed it, so it earns a rank of 1. The 8 is compared with the 6 and 9, and is beaten by the 9, so it earns a rank of 2. The 15 is compared with 6, 9, and 8, and earns a rank of 1. The 7 is compared with 9, 8, and 15, but not with 6, which is not recent. Its rank is 4. The 12 is beaten by the 15, earning a rank of 2. 1+1+2+1+4+2 = 1180
{3,3,3,3,3,3,3}Returns: 7
No score is exceeded by even one of its predecessors, so each of the seven scores earns a rank of 1.