Skip to content

Commit

Permalink
Create 2013-Detect-Squares.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
ShrujanKotturi committed Sep 30, 2022
1 parent 18ee23d commit 9a06818
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions csharp/2013-Detect-Squares.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
public class DetectSquares {

private Dictionary<(int x, int y), int> _pointsCounter = new();
private List<(int x, int y)> _points = new();

public DetectSquares() { }

public void Add(int[] point) {
var p = (point[0], point[1]);
_points.Add(p);
_pointsCounter[p] = 1 + _pointsCounter.GetValueOrDefault(p, 0);
}

public int Count(int[] point) {
int px = point[0], py = point[1];
int result = 0;

foreach (var (x, y) in _points) {

if (Math.Abs(px - x) != Math.Abs(py - y)
|| x == px || y == py) {
continue;
}
result += _pointsCounter.GetValueOrDefault((px, y), 0) * _pointsCounter.GetValueOrDefault((x, py), 0);
}
return result;
}
}

/**
* Your DetectSquares object will be instantiated and called as such:
* DetectSquares obj = new DetectSquares();
* obj.Add(point);
* int param_2 = obj.Count(point);
*/

0 comments on commit 9a06818

Please sign in to comment.