Guest User

Untitled

a guest
Oct 18th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.68 KB | None | 0 0
  1. class Racecar {
  2. func racecar(_ target: Int) -> Int {
  3. var dp:[Int:Int] = [0:0]
  4. for i in 1..<target + 1{
  5. let num = log2(Double(i+1))
  6. if num == Double(Int(num)){
  7. dp[i] = Int(num)
  8. }else{
  9. for j in 0..<Int(num){
  10. let a = i - (1<<Int(num)-1) + (1<<j) - 1
  11. if a >= 0{
  12. print(dp)
  13. dp[i] = dp[i] == nil ? Int(num)+j+2+dp[a]! : min(dp[i]!, Int(num)+j+2+dp[a]!)
  14. }
  15. }
  16. dp[i] = min(dp[i]!, Int(num) + 2 + dp[(1<<Int(num+1))-1-i]!)
  17. }
  18. }
  19. return dp[target]!
  20. }
  21. }
Add Comment
Please, Sign In to add comment