Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- func minAreaRect(_ points: [[Int]]) -> Int {
- var map = [Int: Set<Int>]()
- //build row - point list
- for p in points {
- map[p[0], default : Set<Int>()].insert(p[1])
- }
- let rows = map.keys.sorted()
- var res = Int.max
- for i in 0 ..< rows.count - 1{
- let row = rows[i]
- let list = Array(map[row]!).sorted() // get the array sorted
- for j in 0 ..< list.count - 1 {
- for k in j + 1 ..< list.count {
- for nexti in i + 1 ..< rows.count{
- let nextRow = rows[nexti]
- let nextSet = map[nextRow]!
- if nextSet.contains(list[j]) && nextSet.contains(list[k]){
- res = min(res, (nextRow - row) * (list[k] - list[j]) )
- }
- }
- }
- }
- }
- if res == Int.max {return 0}
- return res
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement