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
'์๊ณ ๋ฆฌ์ฆ ๋ฌธ์ ํ์ด > ๊ตฌํ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ํ๋ก๊ทธ๋๋จธ์ค] ํ๋ก์ธ์ค (0) | 2023.06.06 |
---|---|
[ํ๋ก๊ทธ๋๋จธ์ค] ๊ธฐ๋ฅ๊ฐ๋ฐ (0) | 2023.06.04 |
[softeer] ๋น๋ฐ ๋ฉ๋ด (0) | 2023.05.25 |
[softeer] GBC (0) | 2023.05.18 |
[ํ๋ก๊ทธ๋๋จธ์ค]ํธ๋ ํ์ดํธ ๋ํ (0) | 2023.05.14 |