攀登排行榜 - 黑客排名
Climbing the leaderboard- hacker rank
我在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]=...
是越界的矢量访问。
相关文章:
- 黑客级别的Mini-Max Sum
- 如何改进我的代码,使其不会因超时而终止?(黑客排名挑战)
- 我遇到了黑客排名中的问题"TWO STRINGS"的三个测试用例的分段错误。原因是什么?
- 攀登排行榜 - 黑客排名
- 需要帮助调试"attribute parser"!C++ 来自黑客排名的问题
- 使用堆栈从黑客等级中解决平衡括号检查的错误
- 这是一个二叉搜索树吗?黑客排名问题
- 在黑客地球上没有获得以下C ++代码问题的输出
- C++ 黑客等级 - 数组操作
- 黑客兰克中的错误比较三元组代码
- 黑客等级C++的最小最大总和
- 分配给阵列时出现分段错误?黑客排名 2D 阵列 - DS.
- 缓冲区溢出怎么会成为黑客的攻击
- 在C++中解决最小掉期黑客排名问题
- 优化:黑客地球邮递员软件工程师实习生问题
- 我在运行这个黑客排名 Gridland 地铁的解决方案时遇到分段错误
- 使用位黑客反转二进制数
- 提升 asio 回声服务器示例中的黑客或错误?
- 得到~对标准输出没有回应~在黑客排名问题上
- 是否有一种非间接、非黑客的方式来保证 constexpr 函数仅在编译时可调用?