import Foundation
func solution(_ cards:[Int]) -> Int {
var index: Int = 0
var tmp: [Int] = [index]
var result: [Int] = []
var visited: [Bool] = Array(repeating: false, count: cards.count)
visited[index] = true
while visited.contains(false) {
index = cards[index] - 1
if visited[index] {
index = visited.firstIndex(of: false)!
result.append(tmp.count)
tmp = [index]
visited[index] = true
} else {
visited[index] = true
tmp.append(index)
}
}
result.append(tmp.count)
result = result.sorted(by: >)
return result.count < 2 ? 0 : result[0] * result[1]
}
func solution2(_ cards:[Int]) -> Int {
var answerArr = [Int]()
var visited = [Bool](repeating: false, count: cards.count)
for i in 0..<cards.count {
if visited[i] { continue }
var a = i
var cnt = 0
while true {
if visited[a] { break }
visited[a] = true
a = cards[a]-1
cnt += 1
}
answerArr.append(cnt)
}
answerArr.sort(by: <)
return answerArr.count < 2 ? 0 : answerArr[answerArr.count-1]*answerArr[answerArr.count-2]
}
'프로그래머스 > Lv.2' 카테고리의 다른 글
[Swift] 과제 진행하기 (0) | 2024.01.07 |
---|---|
[Swift] 테이블 해시 함수 (0) | 2023.06.24 |
[Swift] 미로 탈출 (0) | 2023.06.24 |
[Swift] 문자열 압축 (0) | 2023.06.23 |
[Swift] 시소 짝궁 (0) | 2023.06.23 |