๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ํ’€์ด/๊ตฌํ˜„

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ํ‚คํŒจ๋“œ ๋ˆ„๋ฅด๊ธฐ

by syLim___ 2023. 5. 26.
728x90

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

 

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

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

programmers.co.kr


python

def solution(numbers, hand):
    answer = ''
    
    # ํŽธ์˜์ƒ * -> 10, 0 -> 11, # -> 12 ๋กœ ๋ฐ”๊พธ์–ด ์‚ฌ์šฉ
    
    # ์ขŒํ‘œ์ƒ ์œ„์น˜ ์ €์žฅ
    location = dict()
    for i in range(1,13):
        location[i] = [(i-1)//3, (i-1)%3]
    
    # ์ดˆ๊ธฐ ์œ„์น˜
    left = 10
    right = 12
    
    for i in numbers:
        if i == 0: # ํŽธ์˜์ƒ 0 -> 11 ๋กœ ๋ฐ”๊พธ์–ด ์‚ฌ์šฉ
            i = 11
        
        # ์™ผ์ชฝ์ค„์€ ์™ผ์†์œผ๋กœ ๋ˆ„๋ฅธ๋‹ค
        if i in [1,4,7]:
            answer += "L"
            left = i
        # ์˜ค๋ฅธ์ชฝ์ค„์€ ์˜ค๋ฅธ์†์œผ๋กœ ๋ˆ„๋ฅธ๋‹ค
        elif i in [3,6,9]:
            answer += "R"
            right = i
        else:
            # ๊ฑฐ๋ฆฌ ๊ณ„์‚ฐ
            ldist = abs(location[left][0] - location[i][0]) + abs(location[left][1] - location[i][1])
            rdist = abs(location[right][0] - location[i][0]) + abs(location[right][1] - location[i][1]) 
            
            # ํ‚คํŒจ๋“œ ๋ˆ„๋ฅผ ์†๊ฐ€๋ฝ ๊ฒฐ์ •
            if ldist > rdist:
                answer += "R"
                right = i
            elif ldist == rdist:
                if hand == "right":
                    answer += "R"
                    right = i
                else:
                    answer += "L"
                    left = i
            else:
                answer += "L"
                left = i

    return answer

 

 

c++

#include <string>
#include <vector>
#include <map>

using namespace std;

string solution(vector<int> numbers, string hand) {

	// ์ขŒํ‘œ ์œ„์น˜ ์ €์žฅ
	// ํŽธ์˜์ƒ * -> 10, 0 -> 11, # -> 12 ๋กœ ๋ฐ”๊ฟˆ
    map<int,pair<int,int>> location;
    for(int i=1; i<13; i++){
        location.insert({i, make_pair((i-1)/3, (i-1)%3)});
    }
    
    string answer = "";
    
    // ์™ผ์†, ์˜ค๋ฅธ์† ์œ„์น˜ ์ดˆ๊ธฐํ™”
    int left = 10;
    int right = 12;
    
    for(int i=0; i<numbers.size(); i++){
        int cur = numbers[i];
        
        if(cur==0) cur=11; // ํŽธ์˜์ƒ 0 -> 11๋กœ ๋ฐ”๊ฟˆ
        
        if(location[cur].second == 0){ // ์™ผ์ชฝ์ค„์ด๋ฉด ์™ผ์†
            answer += "L";
            left = cur;
        }
        else if (location[cur].second == 2){ // ์˜ค๋ฅธ์ชฝ์ค„์ด๋ฉด ์˜ค๋ฅธ์†
            answer += "R";
            right = cur;
        }
        else{
            // ๊ฑฐ๋ฆฌ ๊ณ„์‚ฐ
            int ldist = abs(location[left].first - location[cur].first) + abs(location[left].second - location[cur].second);
            int rdist = abs(location[right].first - location[cur].first) + abs(location[right].second - location[cur].second);
            
            // ์†๊ฐ€๋ฝ ๊ฒฐ์ •ํ•˜๊ธฐ
            if (ldist > rdist){
                answer += "R";
                right = cur;
            }else if (ldist == rdist){
                if (hand == "left"){
                    answer += "L";
                    left = cur;
                }else{
                    answer += "R";
                    right = cur;
                }
            }else{
                answer += "L";
                left = cur;
            }
        }
    }
    return answer;
}
728x90