317. Shortest Distance from All Buildings
You want to build a house on an empty land that reaches all buildings in the shortest total travel distance.
ๆไปฅ่ฆๅพๆฏๅ 1 ๅบ็ผๅปๅ bfs, ๅปบๆงๅบๆๅฐ็่ท้ข๏ผreaches all buildings ็็ธฝ่ท้ข๏ผ
ๆไปฅ้่ฆๅ
ฉๅ่กจ:
dist: ็ธฝ่ท้ข่กจ
nums: ๅฏไปฅๅฐๅนพๅๅปบ็ฏ็ฉ, ๅ ็บๆๅพ่จ็ฎๆๅๅช่ฆ่ฝๅฐ reaches all buildings ็้ป,
nums[i][j] ๅฆๆๆธ้ = all buildings ๆธ้, ๆๆฏๅฏไปฅ่ๆฟๅญ็็ฉบๅฐ, ๅพ้่ฃก้ขๆพ่ท้ขๆๅฐ็
bfs part:
่ทๅพๅธธๅฏซๆณไธๆจฃ, ่ฃก้ขๅ งๆถตไธๅ visited[][], ๆฏๆฌก่ทbfs้ฝๆ้็ฝฎ, ๅคๆทborder
ๅ จ้จ่ทๅฎๅพ, ๆๅพไพๆพๆๅฐ่ท้ข (reaches all buildings), ๆพไธๅฐๅ -1
time: O(m*n*BFS) = O(m^2*n^2)
space: O(m*n)
Last updated