1331. English Software
Xiao Lin is a representative of the English class in the class. He wants to develop a software to handle the grades of classmates.
Xiao LIn ’s software has a magical feature that can reflect the position of your grades in the class through a percentage. "Classmates with grades exceeding…%".
Suppose this percentage is p, and s score is tested, then p can be calculated by the following formula:
p = (number of people whose score does not exceed s-1) / total number of students in the class * 100%
Please design this software
Example
Example 1:
Input: score= [100,98,87], ask=[1,2,3]
Output: [66,33,0]
Explanation:
The first person scored 100 points, more than 66% of students
Notice
The score array is given to represent the i-th person to take score[i] points
Give the ask array to represent the score of the i-th individual
Each query will output the corresponding score percentage, no need to output a percent sign
The answer is rounded down(To avoid loss of precision, please calculate multiplication first)
Input test data (one parameter per line)How to understand a testcase?
解法1:CountingSort + Presum数组。
class Solution {
public:
/**
* @param score: Each student's grades
* @param ask: A series of inquiries
* @return: Find the percentage of each question asked
*/
vector<int> englishSoftware(vector<int> &score, vector<int> &ask) {
int score_len = score.size();
int ask_len = ask.size();
vector<int> counting(101, 0), prefix_sum(101, 0);
for (int i = 0; i < score_len; ++i) {
counting[score[i]]++;
}
prefix_sum[0] = counting[0];
for (int i = 1; i <= 100; ++i) {
prefix_sum[i] = prefix_sum[i - 1] + counting[i];
}
vector<int> result;
for (int i = 0; i < ask_len; ++i) {
result.push_back((prefix_sum[score[ask[i] - 1]] - 1) * 100 / score_len);
}
return result;
}
};