★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
在由 2D 网格表示的校园里有 n
位工人(worker
)和 m
辆自行车(bike
),n <= m
。所有工人和自行车的位置都用网格上的 2D 坐标表示。
我们需要为每位工人分配一辆自行车。在所有可用的自行车和工人中,我们选取彼此之间曼哈顿距离最短的工人自行车对 (worker, bike) ,并将其中的自行车分配給工人。如果有多个 (worker, bike) 对之间的曼哈顿距离相同,那么我们选择工人索引最小的那对。类似地,如果有多种不同的分配方法,则选择自行车索引最小的一对。不断重复这一过程,直到所有工人都分配到自行车为止。
给定两点 p1
和 p2
之间的曼哈顿距离为 Manhattan(p1, p2) = |p1.x - p2.x| + |p1.y - p2.y|
。
返回长度为 n
的向量 ans
,其中 a[i]
是第 i
位工人分配到的自行车的索引(从 0 开始)。
示例 1:
输入:workers = [[0,0],[2,1]], bikes = [[1,2],[3,3]] 输出:[1,0] 解释: 工人 1 分配到自行车 0,因为他们最接近且不存在冲突,工人 0 分配到自行车 1 。所以输出是 [1,0]。
示例 2:
输入:workers = [[0,0],[1,1],[2,0]], bikes = [[1,0],[2,2],[2,1]] 输出:[0,2,1] 解释: 工人 0 首先分配到自行车 0 。工人 1 和工人 2 与自行车 2 距离相同,因此工人 1 分配到自行车 2,工人 2 将分配到自行车 1 。因此输出为 [0,2,1]。
提示:
0 <= workers[i][j], bikes[i][j] < 1000
- 所有工人和自行车的位置都不相同。
1 <= workers.length <= bikes.length <= 1000
3800 ms
1 class Solution { 2 func assignBikes(_ workers: [[Int]], _ bikes: [[Int]]) -> [Int] { 3 var r:Int = workers.count 4 var c:Int = bikes.count 5 var distance:[[Int]] = [[Int]]() 6 var visited:[Bool] = [Bool](repeating:false,count:c) 7 var ans:[Int] = [Int](repeating:-1,count:r) 8 9 for i in 0..<r 10 { 11 for j in 0..<c 12 { 13 let num:Int = getManhattan(workers[i],bikes[j]) 14 distance.append([num,i,j]) 15 } 16 } 17 18 distance.sort(){ 19 if $0[0] == $1[0] 20 { 21 if $0[1] == $1[1] 22 { 23 return $0[2] < $1[2] 24 } 25 return $0[1] < $1[1] 26 } 27 else 28 { 29 return $0[0] < $1[0] 30 } 31 } 32 var count:Int = 0 33 for i in 0..<distance.count 34 { 35 var worker:Int = distance[i][1] 36 var bike:Int = distance[i][2] 37 if visited[bike] == false && ans[worker] == -1 38 { 39 visited[bike] = true 40 ans[worker] = bike 41 count += 1 42 if count == r {break} 43 } 44 } 45 return ans 46 } 47 48 func getManhattan(_ p1:[Int],_ p2:[Int]) -> Int 49 { 50 return abs(p1[0] - p2[0]) + abs(p1[1] - p2[1]) 51 } 52 }
超出时间限制
1 class Solution { 2 func assignBikes(_ workers: [[Int]], _ bikes: [[Int]]) -> [Int] { 3 var workers = workers 4 var bikes = bikes 5 var res:[Int] = [Int](repeating:0,count:min(workers.count,bikes.count)) 6 while(!bikes.isEmpty) 7 { 8 var map:[Int:[Int]] = [Int:[Int]]() 9 for i in 0..<workers.count 10 { 11 if workers[i][0] == -1 || workers[i][1] == -1 12 { 13 continue 14 } 15 for j in 0..<bikes.count 16 { 17 if bikes[j][0] == -1 || bikes[j][1] == -1 18 { 19 continue 20 } 21 let len:Int = getManhattan(workers[i],bikes[j]) 22 if map[len] == nil 23 { 24 map[len] = [i,j] 25 } 26 } 27 } 28 if map.count == 0 29 { 30 break 31 } 32 else 33 { 34 let numMin:Int = [Int](map.keys).min()! 35 let arrMin:[Int] = map[numMin]! 36 res[arrMin[0]] = arrMin[1] 37 workers[arrMin[0]] = [-1,-1] 38 bikes[arrMin[1]] = [-1,-1] 39 } 40 } 41 return res 42 } 43 44 func getManhattan(_ p1:[Int],_ p2:[Int]) -> Int 45 { 46 return Int(abs(Double(p1[0] - p2[0])) + abs(Double(p1[1] - p2[1]))) 47 } 48 }
超出时间限制
1 class Solution { 2 func assignBikes(_ workers: [[Int]], _ bikes: [[Int]]) -> [Int] { 3 var workers = workers 4 var bikes = bikes 5 var res:[Int] = [Int](repeating:0,count:min(workers.count,bikes.count)) 6 while(!bikes.isEmpty) 7 { 8 var arrRecord:[Int] = [Int](repeating:Int.max,count:3) 9 for i in 0..<workers.count 10 { 11 if workers[i][0] == -1 || workers[i][1] == -1 12 { 13 continue 14 } 15 for j in 0..<bikes.count 16 { 17 if bikes[j][0] == -1 || bikes[j][1] == -1 18 { 19 continue 20 } 21 let len:Int = getManhattan(workers[i],bikes[j]) 22 if len < arrRecord[0] 23 { 24 arrRecord[0] = len 25 arrRecord[1] = i 26 arrRecord[2] = j 27 } 28 } 29 } 30 if arrRecord[0] == Int.max 31 { 32 break 33 } 34 else 35 { 36 res[arrRecord[1]] = arrRecord[2] 37 workers[arrRecord[1]] = [-1,-1] 38 bikes[arrRecord[2]] = [-1,-1] 39 } 40 } 41 return res 42 } 43 44 func getManhattan(_ p1:[Int],_ p2:[Int]) -> Int 45 { 46 return Int(abs(Double(p1[0] - p2[0])) + abs(Double(p1[1] - p2[1]))) 47 } 48 }