๋ฐ์ํ
import Foundation
func solution(_ n:Int, _ info:[Int]) -> [Int] {
var max_diff:Int = 0
var max_lst:[Int] = []
var dfs = [(0,0,0,0,Array(repeating: 0, count:11))]
while dfs.count > 0 {
let now = dfs.removeLast()
let ascore = now.0
let lscore = now.1
let idx = now.2
let cnt = now.3
var lst = now.4
if cnt > n {
continue
}
if idx > 10 {
let diff = lscore - ascore
if diff > max_diff {
lst[10] = n - cnt
max_lst = lst
max_diff = diff
}
continue
}
if info[10-idx] > 0 {
dfs.append((ascore+idx, lscore, idx+1, cnt, lst))
} else {
dfs.append((ascore, lscore, idx+1, cnt, lst))
}
if cnt < n {
lst[10-idx] = info[10-idx] + 1
dfs.append((ascore, lscore+idx, idx+1, cnt+info[10-idx]+1, lst))
}
}
return max_diff == 0 ? [-1] : max_lst
}
๋๋ฌด ์ด๋ ค์์ ๋ชปํ์๋ค.
continue๋ฅผ ํ์ฉํด ํ์์๋ ๋ก์ง์ ๋๊ธฐ๋ ๋ถ๋ถ๊ณผ dfs์์ด๋์ด ๋๋ฌด ์ข์์ ๊ฐ์ ธ์๋ค.
๋ฐ์ํ
'๐ ์ฝํ > ํ๋ก๊ทธ๋๋จธ์ค' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Swift] ํ๋ก๊ทธ๋๋จธ์ค154539 ๋ค์ ์๋ ํฐ ์ ์ฐพ๊ธฐ (0) | 2023.05.13 |
---|---|
[Swift] ํ๋ก๊ทธ๋๋จธ์ค181188 ์๊ฒฉ์์คํ (0) | 2023.05.12 |
[Swift] ํ๋ก๊ทธ๋๋จธ์ค118667 ๋ ํ ํฉ ๊ฐ๊ฒ ๋ง๋ค๊ธฐ (0) | 2023.05.11 |
[Swift] ํ๋ก๊ทธ๋๋จธ์ค150369 ํ๋ฐฐ๋ฐฐ๋ฌ๊ณผ ์๊ฑฐํ๊ธฐ (0) | 2023.05.10 |
[Swift] ํ๋ก๊ทธ๋๋จธ์ค150368 ์ด๋ชจํฐ์ฝ ํ ์ธํ์ฌ (0) | 2023.05.09 |