LintCode 1331: English Software (Counting Sort 经典题)

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;
    }
};

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值