मान लीजिए हमारे पास प्लेन में n बिंदु हैं जो सभी जोड़ीवार विशिष्ट हैं। अब एक "बूमरैंग" बिंदुओं का एक टपल है जैसे (i, j, k) जिससे i और j के बीच की दूरी i और k के बीच की दूरी के समान है। हमें बूमरैंग्स की संख्या ज्ञात करनी है।
उदाहरण के लिए, यदि इनपुट [[0,0],[1,0],[2,0]] है, तो आउटपुट 2 होगा, क्योंकि दो बूमरैंग्स हैं [[1,0],[0,0],[2,0]] और [[1,0],[2,0],[0,0]]।
हल करने के चरण
- counter_of_boomerangs := 0
- points सरणी में प्रत्येक point_1 के लिए:
- x1, y1 = point_1
- distance_count_dict नामक एक मैप परिभाषित करें
- points सरणी में प्रत्येक point_2 के लिए:
- x2, y2 = point_2
- diff_x := x2 - x1
- diff_y := y2 - y1
- dist := diff_x^2 + diff_y^2
- distance_count_dict[dist] := distance_count_dict[dist] + 1
- distance_count_dict में प्रत्येक d के लिए:
- n := distance_count_dict[d]
- counter_of_boomerangs := counter_of_boomerangs + n * (n - 1)
- counter_of_boomerangs वापस करें
उदाहरण
from collections import defaultdict
class Solution:
def numberOfBoomerangs(self, points):
counter_of_boomerangs = 0
for point_1 in points:
x1, y1 = point_1
distance_count_dict = defaultdict(int)
for point_2 in points:
x2, y2 = point_2
diff_x = x2 - x1
diff_y = y2 - y1
dist = diff_x ** 2 + diff_y ** 2
distance_count_dict[dist] += 1
for d in distance_count_dict:
n = distance_count_dict[d]
counter_of_boomerangs += n * (n - 1)
return counter_of_boomerangs
ob = Solution()
print(ob.numberOfBoomerangs([[0,0],[1,0],[2,0]]))
इनपुट
[[0,0],[1,0],[2,0]]
आउटपुट
0