You are viewing a single comment's thread. Return to all comments →
My solution was to use stack and to store only distinct numbers.
#include <stack> #include <iostream> using namespace std; int main(){ unsigned long n, m, i, tmp; cin >> n; stack<unsigned long> scores; for (i = 0; i < n; ++i) { cin >> tmp; if (scores.empty() || scores.top() != tmp) scores.push(tmp); } cin >> m; for (i = 0; i < m; ++i) { cin >> tmp; while (!scores.empty() && tmp >= scores.top()) scores.pop(); cout << (scores.size() + 1) << endl; } return 0; }
One more good solution without using stack is to store only distinct numbers like above, then we can use binary search to find out the rank, as nelson_matos brought in at https://www.hackerrank.com/challenges/climbing-the-leaderboard/forum/comments/271253.
Seems like cookies are disabled on this browser, please enable them to open this website
Climbing the Leaderboard
You are viewing a single comment's thread. Return to all comments →
My solution was to use stack and to store only distinct numbers.
One more good solution without using stack is to store only distinct numbers like above, then we can use binary search to find out the rank, as nelson_matos brought in at https://www.hackerrank.com/challenges/climbing-the-leaderboard/forum/comments/271253.