Advertisement
Guest User

Min Steps in Infinite Grid

a guest
Mar 23rd, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.07 KB | None | 0 0
  1.     class Solution2
  2.     {
  3.         int distance(KeyValuePair<int,int> a, KeyValuePair<int, int> b)
  4.         {
  5.             if (a.Key == b.Key && a.Value == b.Value)
  6.                 return 0;
  7.  
  8.             var x1 = 0;
  9.             var y1 = 0;
  10.  
  11.             var x2 = Math.Abs(b.Key - a.Key);
  12.             var y2 = Math.Abs(b.Value - a.Value);
  13.  
  14.             if (x1 == x2)
  15.                 return y2;
  16.             if (y1 == y2)
  17.                 return x2;
  18.             return x2 + y2 - Math.Min(x2, y2);
  19.             //return 1 + distance(new KeyValuePair<int, int>(1, 1), new KeyValuePair<int, int>(x2, y2));
  20.         }
  21.  
  22.         public int coverPoints(List<int> A, List<int> B)
  23.         {
  24.             int idx = 0;
  25.  
  26.             int fullDistance = 0;
  27.  
  28.             while(idx < A.Count-1)
  29.             {
  30.                 var a = new KeyValuePair<int, int>(A[idx], B[idx]);
  31.                 var b = new KeyValuePair<int, int>(A[idx+1], B[idx+1]);
  32.  
  33.                 fullDistance += distance(a, b);
  34.  
  35.                 idx++;
  36.             }
  37.             return fullDistance;
  38.         }
  39.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement