https://school.programmers.co.kr/learn/courses/30/lessons/152995
๋ฌธ์ ์ค๋ช
์ํธ๋ค ํ์ฌ๋ ์ฐ๋ง๋ง๋ค 1๋ ๊ฐ์ ์ธ์ฌ๊ณ ๊ณผ์ ๋ฐ๋ผ ์ธ์ผํฐ๋ธ๋ฅผ ์ง๊ธํฉ๋๋ค. ๊ฐ ์ฌ์๋ง๋ค ๊ทผ๋ฌด ํ๋ ์ ์์ ๋๋ฃ ํ๊ฐ ์ ์๊ฐ ๊ธฐ๋ก๋์ด ์๋๋ฐ ๋ง์ฝ ์ด๋ค ์ฌ์์ด ๋ค๋ฅธ ์์์ ์ฌ์๋ณด๋ค ๋ ์ ์๊ฐ ๋ชจ๋ ๋ฎ์ ๊ฒฝ์ฐ๊ฐ ํ ๋ฒ์ด๋ผ๋ ์๋ค๋ฉด ๊ทธ ์ฌ์์ ์ธ์ผํฐ๋ธ๋ฅผ ๋ฐ์ง ๋ชปํฉ๋๋ค. ๊ทธ๋ ์ง ์์ ์ฌ์๋ค์ ๋ํด์๋ ๋ ์ ์์ ํฉ์ด ๋์ ์์ผ๋ก ์์ฐจ๋ฅผ ๋ด์ด ์์ฐจ์ ๋ฐ๋ผ ์ธ์ผํฐ๋ธ๊ฐ ์ฐจ๋ฑ ์ง๊ธ๋ฉ๋๋ค. ์ด๋, ๋ ์ ์์ ํฉ์ด ๋์ผํ ์ฌ์๋ค์ ๋์์ฐจ์ด๋ฉฐ, ๋์์ฐจ์ ์๋งํผ ๋ค์ ์์ฐจ๋ ๊ฑด๋ ๋๋๋ค. ์๋ฅผ ๋ค์ด ์ ์์ ํฉ์ด ๊ฐ์ฅ ํฐ ์ฌ์์ด 2๋ช ์ด๋ผ๋ฉด 1๋ฑ์ด 2๋ช ์ด๊ณ 2๋ฑ ์์ด ๋ค์ ์์ฐจ๋ 3๋ฑ๋ถํฐ์ ๋๋ค.
๊ฐ ์ฌ์์ ๊ทผ๋ฌด ํ๋ ์ ์์ ๋๋ฃ ํ๊ฐ ์ ์ ๋ชฉ๋ก scores์ด ์ฃผ์ด์ก์ ๋, ์ํธ์ ์์ฐจ๋ฅผ return ํ๋๋ก solution ํจ์๋ฅผ ์์ฑํด์ฃผ์ธ์.
๋ด ์ฝ๋
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int solution(vector<vector<int>> scores) {
int answer = 1;
int score = scores[0][0] + scores[0][1];
// ์ ์ ๋ด๋ฆผ์ฐจ์ ์ ๋ ฌ
auto comp = [](vector<int> v1, vector<int> v2)
{
return (v1[0]+v1[1]) > (v2[0]+v2[1]);
};
sort(scores.begin()+1, scores.end(), comp);
// ์ํธ ์์น ๊ตฌํ๊ธฐ
for(int i = 1; i < scores.size(); ++i)
{
// ์ํธ๊ฐ ์ธ์ผํฐ๋ธ๋ฅผ ๋ชป ๋ฐ๋ ๊ฒฝ์ฐ
if(scores[0][0] < scores[i][0]
&& scores[0][1] < scores[i][1])
return -1;
int other_score = scores[i][0] + scores[i][1];
if(score < other_score)
++answer;
else break;
}
// ์ํธ๋ณด๋ค ์ ์ ์ฌ๋๋ค ์ค ์ธ์ผํฐ๋ธ๋ฅผ ๋ฐ์ง ๋ชปํ๋ ์ฌ๋ ๋นผ๊ธฐ
int wanho = answer;
for(int c = 2; c < wanho; ++c)
{
for(int p = 1; p < c; ++p)
{
if(scores[c][0] < scores[p][0]
&& scores[c][1] < scores[p][1])
{
--answer;
break;
}
}
}
return answer;
}
'๐ฅ๏ธ Study Note > Coding Test' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ํ๋ก๊ทธ๋๋จธ์ค]level.2 - ๋ํ์ค ๊ฒ์(C++) (0) | 2023.08.07 |
---|---|
[ํ๋ก๊ทธ๋๋จธ์ค]level.2 - ํ ์ธ ํ์ฌ(C++) (0) | 2023.08.03 |
[ํ๋ก๊ทธ๋๋จธ์ค]level.2 - N-Queen(C++) (0) | 2023.07.31 |
[ํ๋ก๊ทธ๋๋จธ์ค]level.2 - n^2 ๋ฐฐ์ด ์๋ฅด๊ธฐ(C++) (0) | 2023.07.28 |
[ํ๋ก๊ทธ๋๋จธ์ค]level.2 - ํ ์ด๋ธ ํด์ ํจ์(C++) (0) | 2023.07.27 |