Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 스코티쉬 스트레이트
- CDJ
- 소호정본점
- 먹기좋은곳
- 안동국시
- 카페
- 스파게티
- 파머스테이블
- A. Steed 2: Cruise Control
- 발산맛집
- 발산역 근처 카페
- codejam
- 치명적 귀여움
- 냥이
- 냥냥
- 부모님과
- 커플
- RED CAT COFFEE X LOUNGE
- 레스토랑
- 파버스
- coffee
- 냥스토리
- 스테이크
- 고양이
- 데이트
- 양재맛집
- 소호정
- 발산
- 고양이는 언제나 귀엽다
- CodeJam 2017 Round 1B
Archives
- Today
- Total
hubring
Climbing the Leaderboard 본문
문제
https://www.hackerrank.com/challenges/climbing-the-leaderboard/problem?h_r=profile
풀이
시간복잡도를 고려했을때 단순 정렬을 하면 시간제한이 걸리므로 이분탐색으로 찾아 해결하였다.
코드
// Complete the climbingLeaderboard function below.
vector<int> climbingLeaderboard(vector<int> scores, vector<int> alice) {
vector<int> rank;
vector<int> aliceRanks;
rank.push_back(1);
int beforeScore = scores[0];
for(int i=1; i<scores.size(); i++){
if(beforeScore == scores[i]){
rank.push_back(rank[i-1]);
}else{
rank.push_back(rank[i-1]+1);
}
beforeScore = scores[i];
}
for(int i=0; i<alice.size(); i++){
int aliceScore = alice[i];
int start = 0, end = scores.size()-1;
int aliceRank = -1;
int mid = -1;
while(end>=start){
mid = (start+end)/2;
if(scores[mid] == aliceScore){
break;
}
else if(scores[mid]<aliceScore){
end = mid-1;
}else{
start = mid+1;
}
}
aliceRank = (aliceScore>=scores[mid])? rank[mid] : rank[mid]+1;
aliceRanks.push_back(aliceRank);
}
return aliceRanks;
}
'Algorithm > hackerRank' 카테고리의 다른 글
Non-Divisible Subset (0) | 2020.08.04 |
---|