๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿš“ Self Study/๐Ÿ”“ Programmers

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค (ํ‚คํŒจ๋“œ ๋ˆ„๋ฅด๊ธฐ, 2020 ์นด์นด์˜ค ์ธํ„ด์‹ญ) C++

by UKHYUN22 2021. 12. 29.
728x90

 

๊ฐ€๋กœ๋กœ ์ธ์ ‘ํ•œ ํ‚คํŒจ๋“œ๋Š” 1๋งŒํผ ์ฐจ์ด๊ฐ€ ๋‚˜๊ณ  ์„ธ๋กœ๋กœ ์ธ์ ‘ํ•œ ํ‚คํŒจ๋“œ๋Š” 3๋งŒํผ ์ฐจ์ด๊ฐ€ ๋‚˜๋Š” ์ด ๊ฒฝ์šฐ๋ฅผ ์–ด๋–ป๊ฒŒ ํ•ด๊ฒฐํ•ด์•ผ ํ• ์ง€ ๋ชฐ๋ผ์„œ ๊ณ ๋ฏผ์„ ๋„ˆ๋ฌด ๋งŽ์ด ํ–ˆ๋‹ค. ์‚ฌ์‹ค ๊ตฌ๊ธ€์—์„œ ์–ด๋–ค ์‚ฌ๋žŒ๋„ ๊ณ ๋ฏผํ•ด์„œ ํ•ด๊ฒฐํ•œ ๊ฒฐ๊ณผ๋ฅผ ๋ณด๊ณ  ์ถฉ๊ฒฉ์— ๋น ์ง„.... 3์œผ๋กœ ๋‚˜๋ˆˆ ๋ชซ๊ณผ 3์œผ๋กœ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€๋ฅผ ๋”ํ•˜๋ฉด ๊ทธ ์ฐจ์ด๊ฐ€ ์—†์–ด์ง„๋‹ค๋Š” ๊ฒƒ์ด ์ƒˆ๋กญ๊ฒŒ ๋ฐ”๋ผ๋ณผ ์ˆ˜ ์žˆ๋Š” ์‹œ์•ผ๋ฅผ ๋„“ํ˜”๋‹ค.

 

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

using namespace std;

string solution(vector<int> numbers, string hand) {
    string answer = "";
    int left_count = 10;
    int right_count = 12;
    
    for(int i = 0 ; i < numbers.size() ; i++) {
        if(numbers[i] == 1 || numbers[i] == 4 || numbers[i] == 7) {
            // 1,4,7 ์— ํ•ด๋‹นํ•˜๋Š” ๋ฒˆํ˜ธ๋Š” L๋ฅผ push_back ํ•œ๋‹ค.
            answer.push_back('L');
            
            // left_count์—๋Š” ๊ฐ€์žฅ ์ตœ๊ทผ์— ์‚ฌ์šฉ๋œ ์ˆซ์ž๋ฅผ ์ €์žฅํ•œ๋‹ค.
            left_count = numbers[i];
        }
        
        else if(numbers[i] == 3 || numbers[i] == 6 || numbers[i] == 9) {
            // 3,6,9 ์— ํ•ด๋‹นํ•˜๋Š” ๋ฒˆํ˜ธ๋Š” R๋ฅผ push_back ํ•œ๋‹ค.
            answer.push_back('R');
            
            // right_count์—๋Š” ๊ฐ€์žฅ ์ตœ๊ทผ์— ์‚ฌ์šฉํ•œ ์ˆซ์ž๋ฅผ ์ €์žฅํ•œ๋‹ค.
            right_count = numbers[i];
        }
        
        else {
            // ์ˆซ์ž 0์„ ๋ˆ„๋ฅธ ๊ฒฝ์šฐ 11๋กœ ๋™์ผํ•œ ๋…ผ๋ฆฌ๋ฅผ ์ ์šฉํ•˜๊ธฐ ์œ„ํ•ด 11๋กœ ๋ฐ”๊พผ๋‹ค.
            if(numbers[i] == 0) {
                numbers[i] = 11;
            }
            
            // left_dist1์€ ์ง€๊ธˆ ๋ˆ„๋ฅธ ๋ฒˆํ˜ธ์™€ ๊ฐ€์žฅ ์ตœ๊ทผ์— ์™ผ์†์ด ๋ˆ„๋ฅธ ๋ฒˆํ˜ธ์˜ ์ฐจ์˜ ์ ˆ๋Œ“๊ฐ’
            // right_dist1์€ ์ง€๊ธˆ ๋ˆ„๋ฅธ ๋ฒˆํ˜ธ์™€ ๊ฐ€์žฅ ์ตœ๊ทผ์— ์˜ค๋ฅธ์†์ด ๋ˆ„๋ฅธ ๋ฒˆํ˜ธ์˜ ์ฐจ์˜ ์ ˆ๋Œ“๊ฐ’
            int left_dist1 = abs(numbers[i] - left_count);
            int right_dist1 = abs(numbers[i] - right_count);
            
            // ๊ฐ€๋กœ๋กœ ์ธ์ ‘ํ•œ ๊ฒฝ์šฐ๋Š” ์ฐจ์ด๊ฐ€ 1์ด๊ณ  ์„ธ๋กœ๋กœ ์ธ์ ‘ํ•˜๋ฉด ์ฐจ์ด๊ฐ€ 3์ด๋ฏ€๋กœ
            // 3์œผ๋กœ ๋‚˜๋ˆˆ ๋ชซ๊ณผ 3์œผ๋กœ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€๋ฅผ ๋”ํ•˜๋ฉด ๊ทธ ์ฐจ์ด๊ฐ€ ํ•ด๊ฒฐ๋œ๋‹ค.(***)
            int left_dist = (left_dist1 / 3) + (left_dist1 % 3);
            int right_dist = (right_dist1 / 3) + (right_dist1 % 3);
            
            // ๋งŒ์•ฝ ์™ผ์†๊ณผ ์˜ค๋ฅธ์†์˜ ๊ฑฐ๋ฆฌ๊ฐ€ ๊ฐ™๋‹ค๋ฉด
            if(left_dist == right_dist) {
                // ์˜ค๋ฅธ์†์ธ ๊ฒฝ์šฐ
                if(hand == "right") {
                    // R์„ push_backํ•˜๊ณ 
                    answer.push_back('R');
                    
                    // right_count์— ๊ฐ€์žฅ ์ตœ๊ทผ์— ์‚ฌ์šฉํ•œ ๋ฒˆํ˜ธ๋ฅผ ์ตœ์‹ ํ™”ํ•œ๋‹ค.
                    right_count = numbers[i];
                  
                // ์™ผ์†์ธ ๊ฒฝ์šฐ
                } else if(hand == "left") {
                    // L์„ push_backํ•˜๊ณ 
                    answer.push_back('L');
                    
                    // left_count์— ๊ฐ€์žฅ ์ตœ๊ทผ์— ์‚ฌ์šฉํ•œ ๋ฒˆํ˜ธ๋ฅผ ์ตœ์‹ ํ™”ํ•œ๋‹ค.
                    left_count = numbers[i];
                }
            }
            // ์™ผ์†์ด ๋” ๊ฐ€๊นŒ์šด ๊ฒฝ์šฐ
            else if(left_dist < right_dist) {
                // L์„ push_back ํ•˜๊ณ 
                answer.push_back('L');
                
                // left_count์— ๊ฐ€์žฅ ์ตœ๊ทผ์— ์‚ฌ์šฉํ•œ ๋ฒˆํ˜ธ๋ฅผ ์ตœ์‹ ํ™”ํ•œ๋‹ค.
                left_count = numbers[i];
            }
            // ์˜ค๋ฅธ์†์ด ๋” ๊ฐ€๊นŒ์šด ๊ฒฝ์šฐ
            else if(left_dist > right_dist) {
                // R์„ push_back ํ•˜๊ณ 
                answer.push_back('R');
                
                // right_count์— ๊ฐ€์žฅ ์ตœ๊ทผ์— ์‚ฌ์šฉํ•œ ๋ฒˆํ˜ธ๋ฅผ ์ตœ์‹ ํ™”ํ•œ๋‹ค.
                right_count = numbers[i];
            }
        }
    }
    
    cout << answer << endl;
    return answer;
}