Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Add solution 593 in Javascript #133

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
42 changes: 42 additions & 0 deletions javascript/_593.js
Original file line number Diff line number Diff line change
@@ -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
}