攀登排行榜 - 黑客排名

Climbing the leaderboard- hacker rank

本文关键字:黑客 排行榜 攀登      更新时间:2023-10-16

我在Hackerrank中解决了这个问题。

这个问题将为我们提供一系列分数。我们必须根据密集的排行榜排名对这些疮进行排名。这意味着最高分获得第一名,如果下一个分数等于最高排名,我们给他们相同的排名。如果它小于这个值,我们给它下一个直接排名。

例如,分数 100、80、80、60 将获得排名 1、2、2 3。

所以我们会得到一个爱丽丝的分数数组,我们必须找出爱丽丝的每个分数会得到什么等级。

请注意,分数按降序给出。爱丽丝的分数是按升序给出的。

我做的是首先创建一个数组 whoose 第 i 个元素将表示分数向量中第 i 个分数的排名。

然后我取ALice的最小分数,并搜索最小的分数>=Alice的分数。然后我相应地给ALice的分数排名。我选择第二小的爱丽丝分数,这次开始搜索最小的分数>=爱丽丝的分数从我离开的地方。

这是我写的代码——

#include <vector>
#include <iostream>
using namespace std;
//function for the question.
vector<int> climbingLeaderboard(vector<int> scores, vector<int> alice)
{
vector<int> rank; //another array that will store the rank of all people already on the leaderboard
vector<int> result; //vector that will store the return value of alice's rank.
int val = scores[0]; //stores the current score that we are pointing on.
int len = 1, k, n = 0; //len is the current rank we are on. it changes if value(score) changes.
for (int i = 0; i < scores.size(); i++) {
if (scores[i] == val) { //if the scores are equal...
rank.push_back(len); //it stores the value of len that is the rank. This command ensures that the same scores have the same rank.
}
else if (scores[i] < val) { //if the score is less than val....
len++; //increments len by 1.
rank.push_back(len); //gives the current len as rank to the next person.
val = scores[i]; //sets the new value to the current score
}
}
//now I have stored ranks. Now for giving alice ranks...
k = scores.size() - 1; //points to the current score that we are comparing for Alice.
for (int j = 0; j < alice.size(); j++) { //does the loop for every score of Alice given.
k = n; //sets k=n so that we begin our search with the same index we left on for the next time.
while (k >= 0) {
if (scores[k] < alice[j]) { //if score is less, sub 1 from k
k--;
}
else if (scores[k] == alice[j]) {
result[j] = rank[k]; //if scores are equal, we set the rank of alice same as that of the rank that this score got.
n = k; //we store the value of k for which the scores are equal.
k = -1; //to end the while loop
}
else if (scores[k] > alice[j]) {
result[j] = rank[k] - 1;
n = k; //we do the same if the scores are one less, but give one less rank
k = -1;
}
}
}
return result; //return the vector.
}
// main function just takes in all the values and prints the vector that we get
int main()
{
int n, value, m;
cin >> n;
vector<int> scores;
vector<int> alice;
for (int i = 0; i < n; i++) {
cin >> value;
scores.push_back(value);
}
cin >> m;
for (int i = 0; i < m; i++) {
cin >> value;
alice.push_back(value);
}
vector<int> result;
result = climbingLeaderboard(scores, alice);
for (int k = 0; k < m; k++) {
cout << result[k];
cout << endl;
}
return 0;
}

它显示运行时错误。请查看代码并告诉我出了什么问题。此外,我建议查看一次链接,因为它将比我 evr 更好地解释这个问题。

它还包含示例输入和示例输出。输入格式有点奇怪。

查看调试输出,很明显您对其中一个向量具有越界访问权限。

查看代码,我可以看到至少一个这样的问题(可能还有更多(

vector<int> result;
...
result[j]=rank[k];
...
result[j]=rank[k]-1;

result是大小为 0 的向量,则绝不会调整其大小。所以result[j]=...是越界的矢量访问。