๐Ÿ“ Coding Test Study/Algorithm Problem

[C++][Programmers][์ •๋ ฌ] ๊ฐ€์žฅ ํฐ ์ˆ˜

ibelieveinme 2023. 4. 19. 01:33
728x90

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต ๊ณ ๋“์  Kit ์ •๋ ฌ ํŒŒํŠธ 2๋ฒˆ - ๊ฐ€์žฅ ํฐ ์ˆ˜

 

[๋ฌธ์ œ]

https://school.programmers.co.kr/learn/courses/30/lessons/42746

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”.

programmers.co.kr

 

[ํ•ด๊ฒฐ๋ฒ•]

๋ฌธ์ œ๋ฅผ ๋ณด๊ณ  ๋“  ์ƒ๊ฐ...

"์กฐํ•ฉํ•จ์ˆ˜๋กœ ์ˆซ์ž์กฐํ•ฉ์€ ๋ฝ‘์„ ์ˆ˜ ์žˆ์„๊ฑฐ ๊ฐ™์€๋ฐ ์ˆซ์ž๋ฅผ ์–ด๋–ป๊ฒŒ ํ•ฉ์น˜์ง€?" ์˜€๋Š”๋ฐ

return ์ด string  ์ธ๊ฑฐ ๋ณด๊ณ  ๊ฑ ๋ฌธ์ž์—ด๋กœ ํ•ฉ์น˜๋ฉด ๋˜๋Š”๊ตฌ๋‚˜~๋ฅผ ์•Œ์•˜๋‹ค.

 

(์˜ค๋‹ต)

1) ์กฐํ•ฉ ๋Œ๋ฆฌ๊ฒŒ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌํ•ด๋†“๊ธฐ

2) ์กฐํ•ฉ์œผ๋กœ ์ˆซ์ž์กฐํ•ฉ ๋ฝ‘๊ธฐ -----------------------> ์‹œ๊ฐ„์ดˆ๊ณผ

3) ๋ฐ˜๋ณต๋ฌธ ๋Œ๋ฆฌ๋ฉด์„œ ํ•˜๋‚˜๋กœ ํ•ฉ์ณ์„œ int๋กœ ๊ฐ•์ œ ํ˜•๋ฒˆํ™˜

4) answer ๋ฒกํ„ฐ์— ์ถ”๊ฐ€

5) ์ •๋ ฌํ•ด์„œ ๋งˆ์ง€๋ง‰ ์›์†Œ ๋ฝ‘๊ธฐ

 

(์ •๋‹ต)

๋ฌธ์ž์—ด ๋น„๊ตํ•ด์„œ ์ฒ˜์Œ๋ถ€ํ„ฐ ํฐ ์กฐํ•ฉ์œผ๋กœ ์ •๋ ฌํ•˜๊ธฐ.

 

์ฐธ๊ณ ๋กœ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค 11๋ฒˆ์ด [0,0,0,0] ์ด๋ผ์„œ ๊ทธ๋ƒฅ 0์„ ๋ฆฌํ„ดํ•˜๋ฉด ์•ˆ๋œ๋‹ค!

 

[์•Œ์•„๋‘˜ ๊ฒƒ]

1. ์กฐํ•ฉ์ฝ”๋“œ

do{
   //์กฐํ•ฉ์œผ๋กœ ํ•  ์ผ
}while(next_permutation(v.begin(), v.end()));

next_permutation ์˜ ์‹œ๊ฐ„๋ณต์žก๋„๋Š” O(N) ์ด์–ด์„œ ์ˆซ์ž๊ฐ€ ํฌ๋ฉด ๋ถ€์ ํ•ฉํ•˜๋‹ค.

 

2. int to string ์ฝ”๋“œ

to_string(100);

 

3. ๋ฌธ์ž์—ด๋„ ๋น„๊ต๊ฐ€ ๋œ๋‹ค๋Š” ๊ฒƒ. 3๋ฒˆ์งธ ์ธ์ž์— bool ํ•จ์ˆ˜๋ช… ๋„ฃ๊ธฐ.

bool compare(string &a, string &b){
    return a + b > b + a;
}

sort(stringNumbers.begin(), stringNumbers.end(), compare);

 

[์ฝ”๋“œ]

#include <string>
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;

bool compare(string &a, string &b){
    return a + b > b + a;
}

string solution(vector<int> numbers) {
    string answer;
    vector<string> stringNumbers;
    
    //๋ฌธ์ž์—ด ๋น„๊ต๋ฅผ ์œ„ํ•ด ์ˆซ์ž ๋ฒกํ„ฐ -> ๋ฌธ์ž์—ด ๋ฒกํ„ฐ์— ๋„ฃ์–ด๋‘๊ธฐ
    for(int i = 0; i<numbers.size(); i++){
        stringNumbers.push_back(to_string(numbers[i]));
    }
    
    //๋น„๊ตํ•˜๋ฉฐ ํฐ ์ˆœ์„œ๋Œ€๋กœ ์ •๋ ฌ
    sort(stringNumbers.begin(), stringNumbers.end(), compare);
    
    //์ˆซ์ž ํ•ฉ์น˜๊ธฐ
    for(int i = 0; i < stringNumbers.size(); i++){
        answer += stringNumbers[i];
    }
    
    //๊ฐ’์ด 0์œผ๋กœ ์ด๋ฃจ์–ด์ง„ ๋ฐฐ์ผ์ด๋ฉด 0์„ ๋ฆฌํ„ดํ•˜๊ธฐ
    if(answer[0] == '0') return "0";      
    return answer;
}

 

2๋…„์ „ ์—ด์‹ฌํžˆ ํ•  ๋• ์ด๋ ‡๊ฒŒ ํ’€์—ˆ์—ˆ๋„ค ~

#include <string>
#include <vector>
#include <algorithm>
using namespace std;

bool compare(int a, int b) {
	string string_ab = to_string(a) + to_string(b);
	string string_ba = to_string(b) + to_string(a);
	return string_ab > string_ba;
}

string solution(vector<int> numbers) {
	string answer = "";
	sort(numbers.begin(), numbers.end(), compare);
	for (auto& i : numbers) {
		answer += to_string(i);
	}
	return (answer[0] == '0') ? "0" : answer;
}

 

728x90