447. Calculate the number of boomerang coordinates numberofboomerangs

Source: Internet
Author: User

given n  points in the plane that's all pairwise distinct, a" boomerang "is a tuple of POINTS  (I, J, K)  such that the distance between  i and  j  equals the distance between  i  and  k   ( the order of the tuple matters ).

Find the number of boomerangs. Assume that n would be is at most and coordinates of points is all in the range [-10000, 10 (inclusive).

Example:

Input: [[0,0],[1,0],[2,0]] Output:2explanation:[[1,0],[0,0],[2,0]][[1,0],[2,0],[0,0]]
Test instructions: Defines a ternary structure similar to "back-shaped", that is, the distance between I and J in the ternary group (I, J, K) is equal to the distance between I and K. Find a set of coordinate data that can form several sets of such "back-up" structures.
Idea: Traverse each point, use the dictionary to store the number of points in the same distance according to the quantity, using the permutation combination formula N (N-1), calculate the number of possible components of the boomerang

For those that don ' t understand how GroupCount * (GroupCount + 1) became N * (N-1):
Algorithm actually sets GroupCount to zero for the first point, 1 for the second point, etc. So, if N = = GroupCount + 1

N * (N - 1)== (groupCount + 1) * ((groupCount + 1) - 1)== (groupCount + 1) * (groupCount)== groupCount * (groupCount + 1)
  
 
  1. static public int NumberOfBoomerangs(int[,] points) {
  2. int number = 0;
  3. int disSqrt = 0;
  4. int count = points.GetLength(0);
  5. for (int i = 0; i < count; i++) {
  6. dictionary < int int > dic = new dictionary < int int > ();
  7. for (int j = 0; j < count; j++) {
  8. if (i == j) {
  9. continue;
  10. }
  11. dissqrt= (points[I, 0] -points[J, 0]) * (points[I, 0] -points[J, 0]) -
  12. (points[I, 1] -points[J, 1]) * (points[I, 1] -points[J, 1]);
  13. if (dic.ContainsKey(disSqrt)) {
  14. dic[disSqrt]++;
  15. } else {
  16. dic[disSqrt] = 0;
  17. }
  18. }
  19. foreach (int value in dic.Values) {
  20. number += value * (value + 1);
  21. }
  22. }
  23. return number;
  24. }


Https://discuss.leetcode.com/topic/67102/c-hashtable-solution-o-n-2-time-o-n-space-with-explaination/2

Https://discuss.leetcode.com/topic/68139/simple-java-solution-using-hashmap-beats-90/3



From for notes (Wiz)

447. Calculate the number of boomerang coordinates numberofboomerangs

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.