Skip to content

Commit 7af3058

Browse files
committed
Add solution #593
1 parent 675de8a commit 7af3058

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,7 @@
460460
590|[N-ary Tree Postorder Traversal](./0590-n-ary-tree-postorder-traversal.js)|Easy|
461461
591|[Tag Validator](./0591-tag-validator.js)|Hard|
462462
592|[Fraction Addition and Subtraction](./0592-fraction-addition-and-subtraction.js)|Medium|
463+
593|[Valid Square](./0593-valid-square.js)|Medium|
463464
594|[Longest Harmonious Subsequence](./0594-longest-harmonious-subsequence.js)|Easy|
464465
599|[Minimum Index Sum of Two Lists](./0599-minimum-index-sum-of-two-lists.js)|Easy|
465466
605|[Can Place Flowers](./0605-can-place-flowers.js)|Easy|

solutions/0593-valid-square.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* 593. Valid Square
3+
* https://leetcode.com/problems/valid-square/
4+
* Difficulty: Medium
5+
*
6+
* Given the coordinates of four points in 2D space p1, p2, p3 and p4, return true if the
7+
* four points construct a square.
8+
*
9+
* The coordinate of a point pi is represented as [xi, yi]. The input is not given in any order.
10+
*
11+
* A valid square has four equal sides with positive length and four equal angles (90-degree
12+
* angles).
13+
*/
14+
15+
/**
16+
* @param {number[]} p1
17+
* @param {number[]} p2
18+
* @param {number[]} p3
19+
* @param {number[]} p4
20+
* @return {boolean}
21+
*/
22+
var validSquare = function(p1, p2, p3, p4) {
23+
const helper = (a, b) => (a[0] - b[0]) ** 2 + (a[1] - b[1]) ** 2;
24+
const points = [p1, p2, p3, p4];
25+
const set = new Set();
26+
27+
for (let i = 0; i < 4; i++) {
28+
for (let j = i + 1; j < 4; j++) {
29+
const d = helper(points[i], points[j]);
30+
if (!d) {
31+
return false;
32+
}
33+
set.add(d);
34+
}
35+
}
36+
37+
return set.size === 2;
38+
};

0 commit comments

Comments
 (0)