We are given a matrix with R
rows and C
columns has cells with integer coordinates (r, c)
, where 0 <= r < R
and 0 <= c < C
.
Additionally, we are given a cell in that matrix with coordinates (r0, c0)
.
Return the coordinates of all cells in the matrix, sorted by their distance from (r0, c0)
from smallest distance to largest distance. Here, the distance between two cells (r1, c1)
and (r2, c2)
is the Manhattan distance, |r1 - r2| + |c1 - c2|
. (You may return the answer in any order that satisfies this condition.)
Example 1:
Input: R = 1, C = 2, r0 = 0, c0 = 0 Output: [[0,0],[0,1]] Explanation: The distances from (r0, c0) to other cells are: [0,1]
Example 2:
Input: R = 2, C = 2, r0 = 0, c0 = 1 Output: [[0,1],[0,0],[1,1],[1,0]] Explanation: The distances from (r0, c0) to other cells are: [0,1,1,2] The answer [[0,1],[1,1],[0,0],[1,0]] would also be accepted as correct.
Example 3:
Input: R = 2, C = 3, r0 = 1, c0 = 2 Output: [[1,2],[0,2],[1,1],[0,1],[1,0],[0,0]] Explanation: The distances from (r0, c0) to other cells are: [0,1,1,2,2,3] There are other answers that would also be accepted as correct, such as [[1,2],[1,1],[0,2],[1,0],[0,1],[0,0]].
Note:
1 <= R <= 100
1 <= C <= 100
0 <= r0 < R
0 <= c0 < C
给出
R
行 C
列的矩阵,其中的单元格的整数坐标为 (r, c)
,满足 0 <= r < R
且 0 <= c < C
。
另外,我们在该矩阵中给出了一个坐标为 (r0, c0)
的单元格。
返回矩阵中的所有单元格的坐标,并按到 (r0, c0)
的距离从最小到最大的顺序排,其中,两单元格(r1, c1)
和 (r2, c2)
之间的距离是曼哈顿距离,|r1 - r2| + |c1 - c2|
。(你可以按任何满足此条件的顺序返回答案。)
示例 1:
输入:R = 1, C = 2, r0 = 0, c0 = 0 输出:[[0,0],[0,1]] 解释:从 (r0, c0) 到其他单元格的距离为:[0,1]
示例 2:
输入:R = 2, C = 2, r0 = 0, c0 = 1 输出:[[0,1],[0,0],[1,1],[1,0]] 解释:从 (r0, c0) 到其他单元格的距离为:[0,1,1,2] [[0,1],[1,1],[0,0],[1,0]] 也会被视作正确答案。
示例 3:
输入:R = 2, C = 3, r0 = 1, c0 = 2 输出:[[1,2],[0,2],[1,1],[0,1],[1,0],[0,0]] 解释:从 (r0, c0) 到其他单元格的距离为:[0,1,1,2,2,3] 其他满足题目要求的答案也会被视为正确,例如 [[1,2],[1,1],[0,2],[1,0],[0,1],[0,0]]。
提示:
1 <= R <= 100
1 <= C <= 100
0 <= r0 < R
0 <= c0 < C
Runtime: 480 ms
Memory Usage: 20.5 MB
1 class Solution { 2 func allCellsDistOrder(_ R: Int, _ C: Int, _ r0: Int, _ c0: Int) -> [[Int]] { 3 //var arr:[[Int]] = [[Int]](repeating:[Int](repeating:0,count:C),count:R) 4 //优先队列 5 var q = Heap<(Int,[Int])>.init { (f, s) -> Bool in 6 return f.0 < s.0 7 } 8 9 for i in 0..<R 10 { 11 for j in 0..<C 12 { 13 q.insert((getManhattan([r0,c0],[i,j]),[i,j])) 14 } 15 } 16 var res:[[Int]] = [[Int]]() 17 while(!q.isEmpty) 18 { 19 res.append(q.remove()!.1) 20 } 21 return res 22 } 23 24 func getManhattan(_ p1:[Int],_ p2:[Int]) -> Int 25 { 26 return Int(abs(Double(p1[0] - p2[0])) + abs(Double(p1[1] - p2[1]))) 27 } 28 } 29 30 public struct Heap<T> { 31 public var nodes = [T]() 32 private var orderCriteria: (T, T) -> Bool 33 34 public init(sort: @escaping (T, T) -> Bool) { 35 orderCriteria = sort 36 } 37 38 public init(array: [T], sort: @escaping (T, T) -> Bool) { 39 self.orderCriteria = sort 40 configureHeap(from: array) 41 } 42 43 public var isEmpty: Bool { 44 return nodes.isEmpty 45 } 46 47 public var count: Int { 48 return nodes.count 49 } 50 51 public mutating func configureHeap(from array: [T]) { 52 nodes = array 53 for i in stride(from: nodes.count / 2 - 1, through: 0, by: -1) { 54 shiftDown(i) 55 } 56 } 57 58 public mutating func reset() { 59 for i in stride(from: nodes.count / 2 - 1, through: 0, by: -1) { 60 shiftDown(i) 61 } 62 } 63 64 @inline(__always) internal func parentIndex(ofIndex index: Int) -> Int { 65 return (index - 1) / 2 66 } 67 68 @inline(__always) internal func leftChildIndex(ofIndex index: Int) -> Int { 69 return index * 2 + 1 70 } 71 72 @inline(__always) internal func rightChildIndex(ofIndex index: Int) -> Int { 73 return index * 2 + 2 74 } 75 76 public func peek() -> T? { 77 return nodes.first 78 } 79 80 internal mutating func shiftUp(_ index: Int) { 81 var childIndex = index 82 let child = nodes[childIndex] 83 var parentIndex = self.parentIndex(ofIndex: index) 84 while childIndex > 0 && orderCriteria(child, nodes[parentIndex]) { 85 nodes[childIndex] = nodes[parentIndex] 86 childIndex = parentIndex 87 parentIndex = self.parentIndex(ofIndex: childIndex) 88 } 89 nodes[childIndex] = child 90 } 91 92 internal mutating func shiftDown(from index: Int, until endIndex: Int) { 93 let leftChildIndex = self.leftChildIndex(ofIndex: index) 94 let rightChildIndex = self.rightChildIndex(ofIndex: index) 95 96 var first = index 97 if leftChildIndex < endIndex && orderCriteria(nodes[leftChildIndex], nodes[first]) { 98 first = leftChildIndex 99 } 100 if rightChildIndex < endIndex && orderCriteria(nodes[rightChildIndex], nodes[first]) { 101 first = rightChildIndex 102 } 103 if first == index { 104 return 105 } 106 nodes.swapAt(index, first) 107 shiftDown(from: first, until: endIndex) 108 } 109 110 internal mutating func shiftDown(_ index: Int) { 111 shiftDown(from: index, until: nodes.count) 112 } 113 114 public mutating func insert(_ value: T) { 115 nodes.append(value) 116 shiftUp(nodes.count - 1) 117 } 118 119 public mutating func insert<S: Sequence>(_ sequence:S) where S.Iterator.Element == T { 120 for value in sequence { 121 insert(value) 122 } 123 } 124 125 public mutating func replace(index i: Int, value: T) { 126 guard i < nodes.count else { 127 return 128 } 129 remove(at: i) 130 insert(value) 131 } 132 133 @discardableResult 134 public mutating func remove() -> T? { 135 guard !nodes.isEmpty else { 136 return nil 137 } 138 if nodes.count == 1 { 139 return nodes.removeLast() 140 } else { 141 let value = nodes[0] 142 nodes[0] = nodes.removeLast() 143 shiftDown(0) 144 return value 145 } 146 } 147 148 @discardableResult 149 public mutating func remove(at index: Int) -> T? { 150 guard index < nodes.count else { return nil} 151 let size = nodes.count - 1 152 if index != size { 153 nodes.swapAt(index, size) 154 shiftDown(from: index, until: size) 155 shiftUp(index) 156 } 157 return nodes.removeLast() 158 } 159 160 public mutating func sort() -> [T] { 161 for i in stride(from: self.nodes.count - 1, to: 0, by: -1) { 162 nodes.swapAt(0, i) 163 shiftDown(from: 0, until: i) 164 } 165 return nodes 166 } 167 }
原文地址:https://www.cnblogs.com/strengthen/p/10744606.html