728x90
์ผ๋จ ์์ด๋ก ์ ๊ทผ์ ํ๋๋ฐ ๋ช ๊ฐ์ Test case๊ฐ ๋์๊ฐ์ง ์์์ ์ข ๋ต๋ตํ๋ค. Sort๋ฅผ ํ ๋ ๋น๊ต ์ฐ์ ์์๋ฅผ ๋ถ์ฌํ๋๊น ๋ฐ๋ก ํ๋ ธ๋ ๋ฌธ์
#include <string>
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
bool cmp(const string& a, const string& b) {
if(a + b > b + a) return true;
else return false;
}
string solution(vector<int> numbers) {
string answer = "";
vector<string> real;
// ๋ฒกํฐ์ string์ผ๋ก ํ ๋น
for(int i = 0 ; i < numbers.size() ; i++) {
real.push_back(to_string(numbers[i]));
}
// ์ค๋ฆ์ฐจ์์ผ๋ก ์ซ์๋ฅผ ์ ๋ ฌ
sort(real.begin(), real.end(), cmp);
// answer ๋ฌธ์์ด์ ๋ง๋ค์ด์ค
for(int i = 0 ; i < real.size() ; i++) {
answer += real[i];
}
if(answer[0] == '0')
return "0";
else
return answer;
}
next_permutation์ ์ฌ์ฉํด์ ํ์ด๋ดค๋ ๋ฌธ์
#include <string>
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
string solution(vector<int> numbers) {
string answer = "";
string real = "";
int max = 0;
// ์ค๋ฆ์ฐจ์์ผ๋ก ์ซ์๋ฅผ ์ ๋ ฌ
sort(numbers.begin(), numbers.end());
// ์์ด์กฐํฉ์ ์ด์ฉ
do {
// ๊ฐ ์ซ์๋ฅผ ๋ฌธ์์ด๋ก ๋ณํํด์ answer์ ๋ด์์ฃผ๊ธฐ
for(int i = 0 ; i < numbers.size() ; i++)
answer += to_string(numbers[i]);
// answer๊ฐ ์ต๋๊ฐ์ธ ๊ฒฝ์ฐ ๊ณ์ ์ต์ ํ์์ผ์ค๋ค.
if(max < stoi(answer))
max = stoi(answer);
// answer ์ด๊ธฐํ
answer = "";
} while(next_permutation(numbers.begin(), numbers.end()));
real = to_string(max);
if(real[0] == '0') return "0";
else
return real;
}
'๐ Self Study > ๐ Programmers' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
ํ๋ก๊ทธ๋๋จธ์ค (์ด์ค์ฐ์ ์์ํ, ํ(Heap)) C++ (0) | 2022.01.05 |
---|---|
ํ๋ก๊ทธ๋๋จธ์ค (๋์คํฌ ์ปจํธ๋กค๋ฌ, ํ(Heap)) C++ (0) | 2022.01.05 |
ํ๋ก๊ทธ๋๋จธ์ค (ํ๋ฆฐํฐ, ์คํ/ํ) C++ (0) | 2022.01.04 |
ํ๋ก๊ทธ๋๋จธ์ค (์ง์ง์ด ์ ๊ฑฐํ๊ธฐ, 2017 ํ์คํ์ด) C++ (0) | 2022.01.04 |
ํ๋ก๊ทธ๋๋จธ์ค (๊ธฐ๋ฅ ๊ฐ๋ฐ, ์คํ/ํ) C++ (0) | 2022.01.03 |