ๆๆๆ้ป้ฝๅป่ฉฆ่ฉฆ็ ๅ
ถไปไธๅ้ปๆฏๅฆๅญๅจ, ่ถ
็ด็ชฎ่, 3000*3000 => ็็ธ
using x, y range, ๅฐ่ง็ท็ๆณๆณ
(i, j) ๅฐ่ง็ท็้ป, ๅ
ถไปๅ
ฉ้ปๆๆฏ...
for i to 1000
x = point[0], y =point[1]
d = abs(i - x)
so ไธๅฐ่ง็ท็้ป: (i, j)
j = y+d
i,j [ x, j]
[i, y] x,y
ไธๅฐ่ง็ท็้ป: (i, j)
j = y-d
[i, y] x,y
i, j [x, j]
T: count: O(1000) , add: O(1)
S: O(1000*1000)
class DetectSquares {
int[][] pCounts;
public DetectSquares() {
pCounts = new int[1001][1001];
}
public void add(int[] point) {
pCounts[point[0]][point[1]]++;
}
public int count(int[] point) {
int x = point[0];
int y = point[1];
int res = 0;
for (int i = 0; i <= 1000; i++) {
int dist = Math.abs(i - x);
if (dist == 0) { // if ้ป้ๅ pass
continue;
}
int j = y + dist;
if (j >= 0 && j <= 1000) {
res += pCounts[i][j]*pCounts[x][j]*pCounts[i][y];
}
j = y - dist;
if (j >= 0 && j <= 1000) {
res += pCounts[i][j]*pCounts[x][j]*pCounts[i][y];
}
}
return res;
}
}
/**
* Your DetectSquares object will be instantiated and called as such:
* DetectSquares obj = new DetectSquares();
* obj.add(point);
* int param_2 = obj.count(point);
for i to 1000
x = point[0], y =point[1]
d = abs(i - x)
so
j = y+d
i,j [ x, j]
[i, y] x,y
j = y-d
[i, y] x,y
i, j [x, j]
T: count: O(1000) , add: O(1)
S: O(1000*1000)
*/