
Problem
์ฝ๋ฉํ ์คํธ ์ฐ์ต - ํ ํธ์ง
8 2 ["D 2","C","U 3","C","D 4","C","U 2","Z","Z"] "OOOOXOOO" 8 2 ["D 2","C","U 3","C","D 4","C","U 2","Z","Z","U 1","C"] "OOXOXOOO"
programmers.co.kr
Solution
ํด๋น ๋ฌธ์ ๋ Linked List ์๋ฃ๊ตฌ์กฐ๋ฅผ ์ด์ฉํด์ ํ์ด์ผ ํ๋ ๋ฌธ์ ์ ๋๋ค.
1. 0๋ถํฐ n๊น์ง ์๊ธฐ ์ด์ ์ ์ซ์์ ๋ค์ ์ซ์๋ฅผ ๋ฐฐ์ด์ ๋ด์๋์ต๋๋ค.
func setLinkedList(n:Int) {
for i in 0..<n {
linkedList.append([i-1,i,i+1])
}
}
(์ ์์ธ Linked List๋ ์๋์ง๋ง ์ ๋ฐฉ์๋๋ก ๋ง๋ค์ด๋ดค์ต๋๋ค.)
2. ํ์ ์ or ์๋๋ก ์ด๋ํ ๋ ์ ํ๋ index๋ฅผ ๋ณ๊ฒฝํด์ค๋๋ค.(D X or U X)
์๋์ ๊ฐ์ด ํ์ฌ ์ซ์์ ์ ์ past๋ก ๋ค์์ next๋ก ํ์ํ๊ณ
x๋งํผ ํ์ ์ด๋ํด์ค๋๋ค.
func past(_ index:Int) -> Int {
return linkedList[index][0]
}
func next(_ index:Int) -> Int{
return linkedList[index][2]
}
func moveColumn(x:Int,isUp:Bool){
for _ in 0..<x {
if isUp {
selectedIndex = past(selectedIndex)
}else {
selectedIndex = next(selectedIndex)
}
}
}
3. ํ์ด ์ญ์ ๋์์๋ linkedList๋ฅผ ๋ฐ๊ฟ์ฃผ๊ณ ์ญ์ ๋ index๋ฅผ ์ ์ฅํฉ๋๋ค.
๋ง์ฝ 1 - 3 - 5๊ฐ ์ฐ๊ฒฐ๋์๊ณ 3์ด ์ญ์ ๋์๋ค๋ฉด 1 - 5 ๊ฐ ์ฐ๊ฒฐ๋์ด์ผ๊ฒ ์ฃ ?
(0 1 3 - 1 3 5 - 3 5 6 ๊ณผ ๊ฐ์ด ์ ์ฅ๋์ด ์์๊ฑฐ์์)
๊ทธ๋ฌ๋ฏ๋ก ์ญ์ ๋ ์ ์ซ์์ ๋ค์ ์ซ์๋ฅผ ์ญ์ ๋ ๋ค์ ์ซ์๋ก ์ฐ๊ฒฐํด์ฃผ๊ณ (0 1 3 -> 0 1 5)
์ญ์ ๋ ๋ค์ ์ซ์์ ์ ์ซ์๋ฅผ ์ญ์ ๋ ์ ์ซ์๋ก ์ฐ๊ฒฐํด์ค๋๋ค. ( 3 5 6 -> 1 5 6)
๊ทธ๋ฆฌ๊ณค ๋์ค์ ๋ณต๊ตฌ๋ฅผ ์ํด์ ์ญ์ ๋ Index๋ฅผ removeHistory์ ์ ์ฅํด์ค๋๋ค.
func removeColumn() {
changeLinkedList(selected: selectedIndex)
removeHistory.append(selectedIndex)
if next(selectedIndex) == linkedList.count {
selectedIndex = past(selectedIndex)
}else {
selectedIndex = next(selectedIndex)
}
}
4. ๊ฐ์ฅ ๋ง์ง๋ง์ ์ญ์ ๋ index๋ฅผ ๋ณต๊ตฌ์์ผ์ฃผ๊ณ ๊ทธ์ ๋ง๊ฒ linkedList๋ฅผ ๋ณ๊ฒฝํด์ค๋๋ค.
๋ณต๊ตฌ๋๋ ์ซ์์ ์ ์ซ์์ ๋ค์ ์ซ์๋ฅผ ํ์ฌ ๋ณต๊ตฌ๋๋ ์ซ์๋ก ๋ฐ๊ฟ์ฃผ๊ณ
๋ณต๊ตฌ๋๋ ์ซ์์ ๋ค์ ์ซ์์ ์ ์ซ์๋ฅผ ํ์ฌ ๋ณต๊ตฌ๋๋ ์ซ์๋ก ๋ฐ๊ฟ์ค๋๋ค.
func restoreColumn() {
let restore = removeHistory.removeLast()
if past(restore) >= 0 {
linkedList[past(restore)][2] = restore
}
if next(restore) <= linkedList.count - 1 {
linkedList[next(restore)][0] = restore
}
}
5. n๊ฐฏ์๋งํผ O๊ฐ ๋ด๊ธด ๋ฐฐ์ด์ ๋ง๋ค์ด์ฃผ๊ณ removeHistory์ index๋ฅผ X๋ก ๋ฐ๊ฟ์ค๋๋ค.
๋ฐ๋ ๋ฐฐ์ด์ joined ๋ฉ์๋๋ฅผ ์ด์ฉํด ๋ฌธ์์ด๋ก ๋ฐ๊ฟ์ฃผ๊ณ ๋ฐํํด์ค๋๋ค.
var result = Array(repeating: "O", count: n)
for r in removeHistory {
result[r] = "X"
}
return result.joined()
Source Code
import Foundation | |
var selectedIndex:Int = 0 | |
var linkedList = [[Int]]() | |
var removeHistory:[Int] = [] | |
func solution(_ n:Int, _ k:Int, _ cmd:[String]) -> String { | |
return makeResultByCommand(cmd: cmd, n: n,k: k) | |
} | |
func makeResultByCommand(cmd:[String],n:Int,k:Int) -> String { | |
initialSetting(n: n,k:k) | |
for command in cmd { | |
changeByCommands(command: command) | |
} | |
var result = Array(repeating: "O", count: n) | |
for r in removeHistory { | |
result[r] = "X" | |
} | |
return result.joined() | |
} | |
func initialSetting(n:Int,k:Int) { | |
selectedIndex = k | |
for i in 0..<n { | |
linkedList.append([i-1,i,i+1]) | |
} | |
} | |
func changeByCommands(command:String) { | |
let split = command.components(separatedBy: " ") | |
if command.first == "D" { | |
moveColumn(x: Int(split.last!)!, isUp: false) | |
}else if command.first == "U" { | |
moveColumn(x: Int(split.last!)!, isUp: true) | |
}else if command.first == "C" { | |
removeColumn() | |
}else { | |
restoreColumn() | |
} | |
} | |
func past(_ index:Int) -> Int { | |
return linkedList[index][0] | |
} | |
func next(_ index:Int) -> Int{ | |
return linkedList[index][2] | |
} | |
func changeLinkedList(selected:Int) { | |
linkedList[selected][1] = -1 | |
if past(selected) >= 0 { | |
linkedList[past(selected)][2] = next(selected) | |
} | |
if next(selected) <= linkedList.count - 1 { | |
linkedList[next(selected)][0] = past(selected) | |
} | |
} | |
func moveColumn(x:Int,isUp:Bool){ | |
for _ in 0..<x { | |
if isUp { | |
selectedIndex = past(selectedIndex) | |
}else { | |
selectedIndex = next(selectedIndex) | |
} | |
} | |
} | |
func removeColumn() { | |
changeLinkedList(selected: selectedIndex) | |
removeHistory.append(selectedIndex) | |
if next(selectedIndex) == linkedList.count { | |
selectedIndex = past(selectedIndex) | |
}else { | |
selectedIndex = next(selectedIndex) | |
} | |
} | |
func restoreColumn() { | |
let restore = removeHistory.removeLast() | |
if past(restore) >= 0 { | |
linkedList[past(restore)][2] = restore | |
} | |
if next(restore) <= linkedList.count - 1 { | |
linkedList[next(restore)][0] = restore | |
} | |
} |
P.S
์ฒ์์ ๋ฌธ์ ๋ฅผ ํ ๋ ๋ญ ์ด๋ ๊ฒ ์ฌ์? ํ๋ฉด์ ํ์๋๋ฐ... ์ญ์๋ ํจ์จ์ฑ์์ ์ ๋ถ ๋ค ์๊ฐ์ด๊ณผ๊ฐ ๋ฌ๋ค...
๊ณ์ ์ฝ๋๋ฅผ ๊ฐ์ ์ํค๋ฉด์ ํด๋ ์๊ฐ์ด๊ณผ๊ฐ ํด๊ฒฐ๋์ง ์์๋ค.
๊ฒฐ๊ตญ ์นด์นด์ค ํํ์ด์ง๋ก ๊ฐ์ ํ์ด๋ฅผ ๋ณด๋ Linked List๋ก ํ์ด์ผํ๋ ๋ฌธ์ ์๋ค.
๊ทธ๋์ Linked List๋ฅผ ํ๋ฒ๋ ์์จ๋ด์ ์ด๋ป๊ฒ ์ ๊ทผํด์ผํ ์ง ๋ง๋งํ๋๋ฐ ๊ทธ๋ฅ ๋ฐฐ์ด์ ์ ์ซ์์ ๋ค์ ์ซ์๋ฅผ
๋ด์๋์ผ๋๊น ์ด๋์ ๋ ๊ตฌํ์ ๋๋ค.
๋์ค์ Linked List๋ฅผ Swift๋ก ์ด๋ป๊ฒ ๋ง๋๋์ง ๊ณต๋ถํด๋ด์ผ๊ฒ ๋ค.
'๐ Problem Solution > Programmers' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Swift] 2019 KAKAO WINTER INTERNSHIP ์ง๊ฒ๋ค๋ฆฌ ๊ฑด๋๊ธฐ (0) | 2021.07.23 |
---|---|
[Swift] 2021 KAKAO INTERNSHIP ์ซ์ ๋ฌธ์์ด๊ณผ ์๋จ์ด (0) | 2021.07.18 |
[Swift] ํ๋ก๊ทธ๋๋จธ์ค ์๊ฐ ์ฝ๋ ์ฑ๋ฆฐ์ง 2 2๊ฐ ์ดํ๋ก ๋ค๋ฅธ ๋นํธ (0) | 2021.07.03 |
[Swift] 2019 ์นด์นด์ค ๊ฐ๋ฐ์ ๊ฒจ์ธ ์ธํด์ฝ ๋ถ๋ ์ฌ์ฉ์ (0) | 2021.06.26 |
[Swift] ํ๋ก๊ทธ๋๋จธ์ค ๊ฐ์ฅ ๊ธด ํฐ๋ฆฐ๋๋กฌ (0) | 2021.06.22 |
๋๊ธ