๋ฌธ์ :
https://programmers.co.kr/learn/courses/30/lessons/67256
๋์ด๋ level 1
๋ถ์ :
๊ฐ๋จํ ๊ตฌํ ๋ฌธ์ ์๋ค !
1, 4, 7๋ฒ์ ์ผ์์ผ๋ก 3, 6, 9๋ฒ์ ์ค๋ฅธ์์ผ๋ก ๋๋ฅด๊ณ 2, 5, 8, 0 ์ซ์๋ ํ์ฌ ์๋ ์ ์์น์์ ๊ฑฐ๋ฆฌ๊ฐ ๊ฐ๊น์ด ์์ด ๋๋ฅด๋ ๊ตฌํ ๋ฌธ์ ์๋ค.
์ซ์๊ฐ ํ๋ผ๋ฆฐ 3๋งํผ ์ฐจ์ด๊ฐ ๋๊ธฐ ๋๋ฌธ์ ์ด๋ผ๋ฆฐ 1๋งํผ ์ฐจ์ด๊ฐ ๋๊ธฐ ๋๋ฌธ์
์์น ๊ฐ์ /, %๋ก ํ์ ํ ์ ์๋ค.
์ด๋ ํ์ฌ ์ ์์น๋ left = 10 (*), right = 12(#)๋ก, 0๋ฒ = 11๋ก ์ด๊ธฐํ ํด์ค๋ค.
answer์ ๋ฌธ์์ด์ ๋ง๋ถ์ฌ์ค๋๋ง๋ค left, right ์ ์์น (now_leftPosition, now_rightPosition)์ ์ ๋ฐ์ดํธ ํด์ค๋ค.
swift ์ฝ๋ :
//
// main.swift
// SOMA๐ฉ๐ป๐ป
//
// Created by JoSoJeong on 2022/04/08.
//
import Foundation
var leftSet:Set = Set([1, 4, 7])
var rightSet: Set = Set([3, 6, 9])
var answer: String = ""
var now_leftPosition = 10 //*
var now_rightPosition = 12 //#
func solution(_ numbers:[Int], _ hand:String) -> String {
for i in numbers {
var target = i
if(i == 0) { target = 11 } // ๊ณ์ฐ์ ์ํด 12๋ก ์นํ
if leftSet.contains(target) {
answer += "L"
now_leftPosition = target
}else if rightSet.contains(target) {
answer += "R"
now_rightPosition = i
}else { // 2, 5, 8 ,0 ๊ฑฐ๋ฆฌ ๊ณ์ฐ
let tempLeft = abs(now_leftPosition - target)
let tempRight = abs(now_rightPosition - target)
let distLeft = tempLeft / 3 + tempLeft % 3
let distRight = tempRight / 3 + tempRight % 3
if(distLeft < distRight){
answer += "L"
now_leftPosition = target
}else if(distLeft > distRight) {
answer += "R"
now_rightPosition = target
}else { // ๊ฑฐ๋ฆฌ๊ฐ ๊ฐ์ ๋
if(hand == "right"){
answer += "R"
now_rightPosition = target
}else {
answer += "L"
now_leftPosition = target
}
}
}
}
return answer
}
'Algorithm๐ฐ > ํ๋ก๊ทธ๋๋จธ์ค' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ํ๋ก๊ทธ๋๋จธ์ค] Swift 2020 ์นด์นด์ค ๊ดํธ๋ณํ (0) | 2022.04.14 |
---|---|
[ํ๋ก๊ทธ๋๋จธ์ค] Swift 2018 ์นด์นด์ค ๋ด์ค ํด๋ฌ์คํฐ๋ง implementation (0) | 2022.04.13 |
[ํ๋ก๊ทธ๋๋จธ์ค] ํ๊ฒ ๋๋ฒ DFS C++ (0) | 2022.03.25 |
[ํ๋ก๊ทธ๋๋จธ์ค] ์ง์ง์ด ์ ๊ฑฐํ๊ธฐ stack (0) | 2022.03.25 |
[ํ๋ก๊ทธ๋๋จธ์ค] ๊ฐ์ฅ ํฐ ์ STL(sort) (0) | 2022.03.25 |
๋๊ธ