From c13189a27725c3c187d6c039f4a015dfc4f0794e Mon Sep 17 00:00:00 2001 From: Phuong Lam Date: Wed, 11 Nov 2020 17:14:36 -0500 Subject: [PATCH] Add solution 593 in Javascript --- README.md | 2 +- javascript/_593.js | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 javascript/_593.js diff --git a/README.md b/README.md index 5c624c4386..bb7924c9ba 100644 --- a/README.md +++ b/README.md @@ -491,7 +491,7 @@ _If you like this project, please leave me a star._ ★ |599|[Minimum Index Sum of Two Lists](https://leetcode.com/problems/minimum-index-sum-of-two-lists/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_599.java) | |Easy | HashMap |598|[Range Addition II](https://leetcode.com/problems/range-addition-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_598.java) | |Easy | |594|[Longest Harmonious Subsequence](https://leetcode.com/problems/longest-harmonious-subsequence/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_594.java) | |Easy | Array, HashMap -|593|[Valid Square](https://leetcode.com/problems/valid-square/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_593.java) | |Medium | Math +|593|[Valid Square](https://leetcode.com/problems/valid-square/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_593.java), [Javascript](./javascript/_593.js) | |Medium | Math |592|[Fraction Addition and Subtraction](https://leetcode.com/problems/fraction-addition-and-subtraction/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_592.java) | |Medium | Math |591|[Tag Validator](https://leetcode.com/problems/tag-validator/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_591.java) | |Hard | Stack, String |590|[N-ary Tree Postorder Traversal](https://leetcode.com/problems/n-ary-tree-postorder-traversal/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_590.java) | |Easy| DFS, recursion diff --git a/javascript/_593.js b/javascript/_593.js new file mode 100644 index 0000000000..b3dd4db272 --- /dev/null +++ b/javascript/_593.js @@ -0,0 +1,42 @@ +/** + * @param {number[]} p1 + * @param {number[]} p2 + * @param {number[]} p3 + * @param {number[]} p4 + * @return {boolean} + */ + +// const d12 = distanceSquare(p1, p2) +// const d13 = distanceSquare(p1, p3) +// const d14 = distanceSquare(p1, p4) +// const d23 = distanceSquare(p2, p3) +// const d24 = distanceSquare(p2, p4) +// const d34 = distanceSquare(p3, p4) + +// if (d12 === 0 || d13 === 0 || d14 === 0) return false + +// // 1 2 +// // 3 4 +// if (d12 === d13 && d12 + d13 === d14 && d23 === d24 + d34) return true + +// // 1 2 +// // 4 3 +// if (d12 === d14 && d12 + d14 === d13 && d24 === d34 + d23) return true + +// // 1 3 +// // 4 2 +// if (d13 === d14 && d13 + d14 === d12 && d34 === d23 + d24) return true + +var validSquare = function (p1, p2, p3, p4) { + function distanceSquare(a, b) { + return (a[0] - b[0]) * (a[0] - b[0]) + (a[1] - b[1]) * (a[1] - b[1]) + } + + const ds = [distanceSquare(p1, p2), distanceSquare(p1, p3), distanceSquare(p1, p4), distanceSquare(p2, p3), distanceSquare(p2, p4), distanceSquare(p3, p4)].sort((a, b) => a - b) + + for (var i = 0; i < 3; i++) if (ds[i] !== ds[i + 1] || ds[i] === 0) return false + + if (ds[4] === ds[5] && ds[4] === ds[0] * 2) return true + + return false +}