Problem Statement
You took a test consisting of N questions, each of which has a distinct point value between 1 and N, inclusive, and you have finally received the results. Along with your final score, you are told which questions you answered correctly. However, you are not given the point values that were assigned to the questions. For each correct answer, you received the full point value of the question, and for each wrong answer, you received 0 points. You must determine the minimum possible error of a valid point assignment that would result in the final score that you received. The error of a valid assignment of points is defined as follows: For each question i (where i is a 1-based index), let e(i) = the absolute value of (i minus the point value of the question). The error of the point assignment is the maximum value of e(i).
Given a
Definition
- Class:
- ScoreRecomposition
- Method:
- minError
- Parameters:
- String, int
- Returns:
- int
- Method signature:
- int minError(String questions, int score)
- (be sure your method is public)
Constraints
- questions will contain exactly N elements, where N is between 1 and 10, inclusive.
- Each character of questions will be either 'C' or 'W'.
- score will be between 0 and N*(N+1)/2, inclusive.
Examples
"CCC"
5
Returns: -1
Contact the contest director immediately! Since you answered every question correctly, your score should be 6.
"WCWW"
4
Returns: 2
Obviously, you answered only the 4-point question correctly.
"CWW"
1
Returns: 0
The minimum error occurs when each question i is assigned a point value of i.
"CWCC"
6
Returns: 2
One valid point assignment with the lowest possible error is 1, 4, 2, 3.
"WWCC"
3
Returns: 2
"WWWWW"
0
Returns: 0
"C"
1
Returns: 0
"WWCCWWW"
19
Returns: -1
"WWWCCC"
14
Returns: 1
"WCWCCCCCW"
35
Returns: 2
"CCWCWCWWCW"
50
Returns: -1
"WWCCCC"
18
Returns: 0
"CWW"
1
Returns: 0
"CW"
2
Returns: 1
"WWCWCWWCC"
25
Returns: 0
"W"
0
Returns: 0
"WWCCWWWCWW"
17
Returns: 1
"CCCWWCWCC"
34
Returns: 2
"WWCW"
0
Returns: -1
"CWCCWWCWCC"
55
Returns: -1
"CWWCCCCW"
24
Returns: 1
"CCCCCWWWW"
26
Returns: 4
"CCCCWCCCWC"
49
Returns: 4
"CW"
2
Returns: 1
"WCWWCWWWC"
22
Returns: 3
"WWW"
0
Returns: 0
"CWWCWCCWWC"
37
Returns: 3
"CWCWC"
10
Returns: 1
"CWWC"
6
Returns: 1
"CCCCCWCC"
17
Returns: -1
"CCWWWCWCWW"
50
Returns: -1
"CWC"
5
Returns: 1
"CCCCCC"
21
Returns: 0
"WCCWWCWCCC"
40
Returns: 1
"CCW"
3
Returns: 0
"WCCWC"
14
Returns: -1
"WC"
3
Returns: -1
"WWCWCWWW"
25
Returns: -1
"WCW"
2
Returns: 0
"WWCCC"
4
Returns: -1
"WC"
3
Returns: -1
"CWCCWCWCW"
21
Returns: 1
"WCWCWC"
15
Returns: 2
"WCCCWCCCC"
38
Returns: 1
"CCCCWWCC"
28
Returns: 2
"CW"
2
Returns: 1
"WCWWWWC"
23
Returns: -1
"CWC"
2
Returns: -1
"CCWWCC"
1
Returns: -1
"WWWCC"
14
Returns: -1
"CCWWC"
8
Returns: 0
"CWCC"
2
Returns: -1
"WWCWCC"
3
Returns: -1
"CCCWCCCWW"
6
Returns: -1
"WCWWWC"
3
Returns: 4
"WCWCWWWC"
4
Returns: -1
"WCCW"
2
Returns: -1
"WWWCCWCWCC"
52
Returns: -1
"CW"
1
Returns: 0
"WWWCCWWCCW"
22
Returns: 2
"CWWWCWCCWW"
52
Returns: -1
"CWWC"
1
Returns: -1
"CWWWWWWWWW"
10
Returns: 9
"CCWCWCCWCW"
43
Returns: 5
"WCCWW"
5
Returns: 0
"CWWCWCCWWC"
37
Returns: 3
"WCWW"
4
Returns: 2
"CCCCCC"
5
Returns: -1
"C"
1
Returns: 0
"WWWWWCCCCC"
27
Returns: 4
"WCWCWCWCWC"
26
Returns: 1
"WWCCCWWWCC"
21
Returns: 3
"CCCCCCCCCC"
55
Returns: 0
"WCCW"
5
Returns: 0
"WWCCWWCWCC"
29
Returns: 2
"CCCCCCCCWW"
52
Returns: 8
"CCW"
5
Returns: 2
"CWWCWWWCC"
13
Returns: 4
"CCWCCWWCCW"
44
Returns: 5
"WCCCCCC"
21
Returns: 6
"WWWWWWWWCW"
2
Returns: 7
"CWCC"
6
Returns: 2
"WCWC"
4
Returns: 1
"WWWWWC"
1
Returns: 5