Notice
Recent Posts
Recent Comments
Today
Total
01-10 22:06
Link
๊ด€๋ฆฌ ๋ฉ”๋‰ด

Partially Committed

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ๊ฐ€์žฅ ํฐ ์ˆ˜ ๋ณธ๋ฌธ

๐Ÿ”ฅ Algorithm || ๋ฌธ์ œํ’€์ด/PS

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ๊ฐ€์žฅ ํฐ ์ˆ˜

WonderJay 2022. 7. 13. 09:22
728x90
๋ฐ˜์‘ํ˜•
SMALL

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

 

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

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

programmers.co.kr

์ˆซ์ž๋ฅผ ๋‹ด์€ ๋ฐฐ์—ด numbers ๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ํ•ด๋‹น ์ˆซ์ž๋“ค์„ ์ด์šฉํ•ด์„œ ๊ฐ€์žฅ ํฐ ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด์„œ ๋ฌธ์ž์—ด answer ๋กœ ๋ฐ˜ํ™˜ํ•ด์•ผ ํ•œ๋‹ค. numbers ์˜ ์›์†Œ a, b ๊ฐ€ ์žˆ๋‹ค๊ณ  ํ–ˆ์„ ๋•Œ ๊ฐ๊ฐ์„ string ์œผ๋กœ ๋ฐ”๋ผ๋ด์„œ a+b > b+a ์ด๋„๋ก ์ •๋ ฌํ•œ ๋’ค ๋ฌธ์ž์—ด answer ๋กœ ์ด์–ด๋ถ™์—ฌ์ฃผ๋ฉด ๋œ๋‹ค.

 

์ฆ‰, string ์„ ์›์†Œ๋กœ ๊ฐ€์ง€๋Š” string_nums ๋ฐฐ์—ด์„ ์„ ์–ธํ•˜์—ฌ numbers ์˜ ์›์†Œ๋ฅผ ๊ฐ๊ฐ string ์œผ๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ๋„ฃ์–ด์ค€๋‹ค. ๊ทธ๋ฆฌ๊ณ  string_nums ๋ฐฐ์—ด์„ a+b > b+a ๊ฐ€ ๋˜๋„๋ก ์ •๋ ฌํ•œ ๋’ค, ์ฐจ๋ก€๋Œ€๋กœ answer ๋ฌธ์ž์—ด์— ์ด์–ด๋ถ™์—ฌ์ฃผ๋ฉด ๋œ๋‹ค. ์ด๋•Œ ๋งŒ์•ฝ์— a = 0, b = 0 ์ธ ๊ฒฝ์šฐ answer ์ด 00 ๊ณผ ๊ฐ™์€ ๊ฒฝ์šฐ๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋‹ค. 0์ด ์—ฐ์†ํ•ด์„œ ๋‚˜์˜ค๋Š” ๊ฒฝ์šฐ์— ๋Œ€๋น„ํ•˜์—ฌ string_nums ์˜ 1, 2 ๋ฒˆ์งธ ์›์†Œ๊ฐ€ ๋ชจ๋‘ 0 ์ธ ๊ฒฝ์šฐ์—๋Š” answer ์„ 0 ์œผ๋กœ ๋ฐ˜ํ™˜ํ•˜๋„๋ก ํ•˜์˜€๋‹ค.

 

[C++]

#include <string>
#include <vector>
#include <algorithm>
#include <iostream>

using namespace std;

string solution(vector<int> numbers) {
    string answer = "";
    vector<string> string_nums;
    
    for(auto& ele : numbers)
        string_nums.push_back(to_string(ele));
    
    sort(string_nums.begin(), string_nums.end(), [](string a, string b)
         {
             return a+b > b+a;
         });
    
    for(auto& ele : string_nums)
        answer+=ele;
    
    if(string_nums.at(0) == "0" && string_nums.at(1) == "0")
        answer = "0";
    
    return answer;
}

728x90
๋ฐ˜์‘ํ˜•
LIST
Comments