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 |
Tags
- critical rendering path
- SDK upgrade
- express
- Camera Zoom
- MySQL
- Spring Boot
- mongoDB
- Camera Movement
- screencapture
- springboot
- unity
- draganddrop
- Git
- docker
- OverTheWire
- Google Refund
- Google Developer API
- Unity Editor
- server
- nodejs
- Packet Network
- java
- rpg server
- Digital Ocean
- Unity IAP
- spread 연산자
- linux
- react
- css framework
- --watch
Archives
- Today
- Total
우당탕탕 개발일지
[프로그래머스] Level 3. 인사 고과 본문
https://school.programmers.co.kr/learn/courses/30/lessons/152995#
해결 방법
- 총합이 큰 순서대로 정렬한다. 두 점수 모두 나보다 높은 사원의 경우, 무조건 나보다 앞에 있게 된다.
- 총합이 큰 사원부터 랭크를 매긴다. 방법은 다음과 같다.
- 내 앞에있는 사원중에 두 점수가 모두 나보다 높을 경우, 나는 탈락이므로 랭크를 매기지 않고 끝낸다.
- 랭크를 부여받을 수 있을 경우, 내 바로 앞 사원(점수가 유효한 사원) 의 점수를 비교해서 점수가 같은지 아닌지 체크한다.
- 점수가 같다면 랭크는 똑같고, 다음 사원의 랭크만 늘어나므로 , next_rank를 올려준다.
- 점수가 다르다면, next_rank가 내 랭크가 되고, 내 다음 사원의 랭크는 next_rank+1이다.
- 루프를 돌다가 완호의 점수와 동일한 사원이 나오면 종료한다.
#include <string>
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
bool compare(vector<int>& a, vector<int>& b){
int suma = a[0] + a[1];
int sumb = b[0] + b[1];
if(suma != sumb) return suma < sumb;
return a[0]<b[0];
}
int solution(vector<vector<int>> scores) {
pair<int,int> mine = make_pair(scores[0][0],scores[0][1]); //완호 점수 저장
sort(scores.rbegin(), scores.rend(), compare); //내림차순으로 정렬
int rank = 0; //현재 사원의 등수.
int prev_idx =-1; //바로 이전에 랭크받은 사원의 idx
int next_rank = 1; //다음사원이 받는 등수.동점자가있을 경우를 위함.
for(int i=0; i<scores.size(); i++){
int a = scores[i][0]; int b = scores[i][1];
//탈락사원인지 체크.완호가 탈락사원이라면 -1, 아니라면 그냥 패스하기
bool is_out = false;
for(int j=0; j<i;j++){
if(a< scores[j][0] && b <scores[j][1]){
if(mine.first == a && mine.second == b) return -1; //내가 완호라니!!
is_out =true;
break;
}
}
if(is_out) continue;
if(prev_idx >=0 && scores[prev_idx][0] + scores[prev_idx][1] == a+b) //동점자일 경우, 다음랭크만 올림.
{
next_rank ++;
}
else{ //다음 랭크일 경우
rank = next_rank;
next_rank = rank+1;
}
prev_idx = i;
if(mine.first == a && mine.second == b) return rank; //내가 완호다..!
}
return -1;
}
마무리하며
Level 3도 점점 익숙해지는 것 같다. 제일 시간이 조금걸린 문제이다. 가즈앙~@
'Algorithm > Level 3' 카테고리의 다른 글
[프로그래머스] Level 3. 길 찾기 게임 (1) | 2025.01.28 |
---|---|
[프로그래머스] Level 3. 상담원 인원 (1) | 2025.01.26 |
[프로그래머스] Level 3. 미로 탈출 명령어 (0) | 2025.01.24 |
[프로그래머스] Level 3. n+1 카드게임 (0) | 2025.01.23 |
[프로그래머스] Level 3. 표 편집 (0) | 2025.01.19 |