climbing leaderboard java code example

Example: climbing the leaderboard

int n, m, score;
cin >> n;
vector<int> scores(n);
copy_n(istream_iterator<int>(cin), n, begin(scores));
scores.erase(unique(begin(scores), end(scores)), end(scores));
cin >> m;
int curr = scores.size()-1;
while (m--)
{
	cin >> score;
	auto rank = 0;
	
	while (curr >= 0 && score >= scores[curr]) 
		curr--;
	
	if (curr == -1) 
		rank = 1;
	else if (score == scores[curr]) 
		rank = curr+1;
	else if (score < scores[curr]) 
		rank = curr+2;
	
	cout << rank << "\n";
}

Tags:

Php Example