forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
18ee23d
commit 9a06818
Showing
1 changed file
with
35 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
*/ |