Advertisement
HXXXXJ

322. Coin Change

Feb 15th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 0.49 KB | None | 0 0
  1.     func coinChange(_ coins: [Int], _ amount: Int) -> Int {
  2.         var arr = [Int](repeating: Int.max, count:amount + 1)
  3.         arr[0] = 0
  4.        
  5.         for i in stride(from: 1, through: amount, by : 1){
  6.             for coin in coins{
  7.                 if i - coin >= 0 && arr[i - coin] != Int.max {
  8.                     arr[i] = min(arr[i], arr[i - coin] + 1)
  9.                 }
  10.             }
  11.         }
  12.         if arr[amount] == Int.max { return -1}
  13.         return arr[amount]
  14.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement