Advertisement
HXXXXJ

939. Minimum Area Rectangle

Apr 13th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 0.98 KB | None | 0 0
  1.     func minAreaRect(_ points: [[Int]]) -> Int {
  2.         var map = [Int: Set<Int>]()
  3.         //build row - point list
  4.         for p in points {
  5.             map[p[0], default : Set<Int>()].insert(p[1])
  6.         }
  7.         let rows = map.keys.sorted()
  8.         var res = Int.max
  9.         for i in 0 ..< rows.count - 1{
  10.             let row = rows[i]
  11.             let list = Array(map[row]!).sorted()   // get the array sorted
  12.             for j in 0 ..< list.count - 1 {
  13.                 for k in j + 1 ..< list.count {
  14.                    for nexti in i + 1 ..< rows.count{
  15.                        let nextRow = rows[nexti]
  16.                        let nextSet = map[nextRow]!
  17.                        if nextSet.contains(list[j]) && nextSet.contains(list[k]){
  18.                            res = min(res, (nextRow - row) * (list[k] - list[j]) )
  19.                        }
  20.                    }
  21.                 }
  22.             }
  23.         }
  24.         if res == Int.max {return 0}
  25.         return res
  26.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement