Skip to content

Commit

Permalink
tuples in swift
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonny Mac authored and Jonny Mac committed Aug 8, 2017
1 parent 271583a commit c7e942b
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions coinchange/prog1.swift
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import Foundation
let line = readLine()!.components(separatedBy: " ").map{ Int($0)! }
let n = line[0]
let m = line[1]
let (n, m): (Int, Int) = (line[0],line[1])
let c = readLine()!.components(separatedBy: " ").map{ Int($0)! }

func ways(_ n: Int, _ m: Int, _ c: [Int], _ w: inout [Int:[Int:Int]]) -> Int {
if n == 0 { return 1 }
if n < 1 || m == 0 { return 0 }
if w[n] != nil && w[n]![m] != nil { return w[n]![m]! }
if let res = w[n]?[m] { return res }
w[n] = w[n] ?? [Int:Int]()
w[n]![m] = ways(n, m-1, c, &w) + ways(n-c[m-1], m, c, &w)
w[n]?[m] = ways(n, m-1, c, &w) + ways(n-c[m-1], m, c, &w)
return w[n]![m]!
}

var w = [Int:[Int:Int]]()
print("\(String(describing: ways(n,m,c,&w)))")
print("\(String(describing: ways(n, m, c, &w)))")

0 comments on commit c7e942b

Please sign in to comment.