HXXXXJ

airbnb - find max money

Mar 18th, 2019
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 0.68 KB | None | 0 0
  1.  
  2. func findMaxMoney(_ arr: [(Int, Int, Int)]) -> Int{
  3.     var sorted = arr.sorted{
  4.         $0.0 < $1.0
  5.     }
  6.     var dp = [Int](repeating: 0 ,count : arr.count)
  7.     for (index, cur) in sorted.enumerated(){
  8.         dp[index] = cur.2
  9.         for i in 0 ..< index{
  10.             let pre = sorted[i]
  11.             if pre.1 <= cur.0 {
  12.                 dp[index] = max(dp[index], dp[i] + cur.2)
  13.             }
  14.         }
  15.     }
  16.     var res = 0
  17.     for p in dp {
  18.         res = max(res, p)
  19.     }
  20.     return res
  21. }
  22.  
  23. var input=[(1,6,400),(2,4,200),(4,8,200),(6,8,250),(5,7,300),(6,10,300)];
  24.  
  25. //var input1=[['2-4','100'],['4-8','200'],['5-7','300']];
  26.  
  27. let res  = findMaxMoney(input)
  28. print(res)
Add Comment
Please, Sign In to add comment