程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Leetcode solution (0573): Squirrel simulation (Python)

編輯:Python


subject :​ ​ The original title is link ​​( secondary )

label : mathematics

solution

Time complexity

Spatial complexity

Execution time

Ans 1 (Python)

O ( N )

O ( N )

216ms (8.33%)

Ans 2 (Python)

Ans 3 (Python)

Solution 1 :

class Solution:

def minDistance(self, height: int, width: int, tree: List[int], squirrel: List[int], nuts: List[List[int]]) -> int:
d1 = [] # The distance from the pine cone to the tree
d2 = [] # Pinecones to squirrels + The distance from the pineal cone to the tree
for i1, i2 in nuts:
if 0 <= i1 < height and 0 <= i2 < width:
d1.append((abs(i1 - tree[0]) + abs(i2 - tree[1])) * 2)
d2.append(abs(i1 - tree[0]) + abs(i2 - tree[1]) + abs(i1 - squirrel[0]) + abs(i2 - squirrel[1]))

ans = 0
more = float("inf") # Distance change of the first pineal cone
for i in range(len(d1)):
ans += d1[i]
more = min(more, d2[i] - d1[i])

return ans + more
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.




  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved