|
| 1 | +--- |
| 2 | +id: valid-square |
| 3 | +title: Valid Square |
| 4 | +sidebar_label: 0593 - Valid Square |
| 5 | +tags: |
| 6 | + - Math |
| 7 | + - Geometry |
| 8 | + - Sorting |
| 9 | +description: "This is a solution to the Valid Square problem on LeetCode." |
| 10 | +--- |
| 11 | + |
| 12 | +## Problem Description |
| 13 | + |
| 14 | +Given the coordinates of four points in 2D space `p1`, `p2`, `p3` and `p4`, return `true` if the four points construct a square. |
| 15 | + |
| 16 | +The coordinate of a point pi is represented as `[xi, yi]`. The input is **not** given in any order. |
| 17 | + |
| 18 | +A **valid square** has four equal sides with positive length and four equal angles (90-degree angles). |
| 19 | + |
| 20 | +### Examples |
| 21 | +**Example 1:** |
| 22 | + |
| 23 | +``` |
| 24 | +Input: p1 = [0,0], p2 = [1,1], p3 = [1,0], p4 = [0,1] |
| 25 | +Output: true |
| 26 | +``` |
| 27 | + |
| 28 | +**Example 2:** |
| 29 | + |
| 30 | +``` |
| 31 | +Input: p1 = [0,0], p2 = [1,1], p3 = [1,0], p4 = [0,12] |
| 32 | +Output: false |
| 33 | +``` |
| 34 | + |
| 35 | +### Constraints |
| 36 | + |
| 37 | +- `p1.length == p2.length == p3.length == p4.length == 2` |
| 38 | +- $-10^4 <= xi, yi <= 10^4$ |
| 39 | + |
| 40 | +## Solution for Valid Square |
| 41 | +### Approach: Using Sorting |
| 42 | + |
| 43 | +We can make use of maths to simplify this problem a bit. If we sort the given set of points based on their x-coordinate values, and in the case of a tie, based on their y-coordinate value, we can obtain an arrangement, which directly reflects the arrangement of points on a valid square boundary possible. |
| 44 | + |
| 45 | +Consider the only possible cases as shown in the figure below: |
| 46 | + |
| 47 | +In each case, after sorting, we obtain the following conclusion regarding the connections of the points: |
| 48 | + |
| 49 | +1. p0p1, p1p3, p3p2 and p2p0 form the four sides of any valid square. |
| 50 | + |
| 51 | +2. p0p3 and p1p2 form the diagonals of the square. |
| 52 | + |
| 53 | +Thus, once the sorting of the points is done, based on the above knowledge, we can directly compare p0p1, p1p3, p3p2 and p2p0 or equality of lengths(corresponding to the sides); and p0p3 and p1p2 for equality of lengths(corresponding to the diagonals). |
| 54 | + |
| 55 | +## Code in Different Languages |
| 56 | + |
| 57 | +<Tabs> |
| 58 | +<TabItem value="cpp" label="C++"> |
| 59 | + <SolutionAuthor name="@Shreyash3087"/> |
| 60 | + |
| 61 | +```cpp |
| 62 | +#include <cmath> |
| 63 | +#include <algorithm> |
| 64 | +#include <vector> |
| 65 | + |
| 66 | +class Solution { |
| 67 | +public: |
| 68 | + double dist(std::vector<int>& p1, std::vector<int>& p2) { |
| 69 | + return (p2[1] - p1[1]) * (p2[1] - p1[1]) + (p2[0] - p1[0]) * (p2[0] - p1[0]); |
| 70 | + } |
| 71 | + bool validSquare(std::vector<int>& p1, std::vector<int>& p2, std::vector<int>& p3, std::vector<int>& p4) { |
| 72 | + std::vector<std::vector<int>> p = {p1, p2, p3, p4}; |
| 73 | + std::sort(p.begin(), p.end(), [](const std::vector<int>& l1, const std::vector<int>& l2) { |
| 74 | + return l1[0] != l2[0] ? l1[0] < l2[0] : l1[1] < l2[1]; |
| 75 | + }); |
| 76 | + return dist(p[0], p[1]) != 0 && dist(p[0], p[1]) == dist(p[1], p[3]) && dist(p[1], p[3]) == dist(p[3], p[2]) && dist(p[3], p[2]) == dist(p[2], p[0]) && dist(p[0], p[3]) == dist(p[1], p[2]); |
| 77 | + } |
| 78 | +}; |
| 79 | + |
| 80 | + |
| 81 | +``` |
| 82 | +</TabItem> |
| 83 | +<TabItem value="java" label="Java"> |
| 84 | + <SolutionAuthor name="@Shreyash3087"/> |
| 85 | +
|
| 86 | +```java |
| 87 | +public class Solution { |
| 88 | + public double dist(int[] p1, int[] p2) { |
| 89 | + return (p2[1] - p1[1]) * (p2[1] - p1[1]) + (p2[0] - p1[0]) * (p2[0] - p1[0]); |
| 90 | + } |
| 91 | + public boolean validSquare(int[] p1, int[] p2, int[] p3, int[] p4) { |
| 92 | + int[][] p={p1,p2,p3,p4}; |
| 93 | + Arrays.sort(p, (l1, l2) -> l2[0] == l1[0] ? l1[1] - l2[1] : l1[0] - l2[0]); |
| 94 | + return dist(p[0], p[1]) != 0 && dist(p[0], p[1]) == dist(p[1], p[3]) && dist(p[1], p[3]) == dist(p[3], p[2]) && dist(p[3], p[2]) == dist(p[2], p[0]) && dist(p[0],p[3])==dist(p[1],p[2]); |
| 95 | + } |
| 96 | +} |
| 97 | +
|
| 98 | +``` |
| 99 | + |
| 100 | +</TabItem> |
| 101 | +<TabItem value="python" label="Python"> |
| 102 | + <SolutionAuthor name="@Shreyash3087"/> |
| 103 | + |
| 104 | +```python |
| 105 | +import math |
| 106 | + |
| 107 | +class Solution: |
| 108 | + def dist(self, p1, p2): |
| 109 | + return (p2[1] - p1[1]) ** 2 + (p2[0] - p1[0]) ** 2 |
| 110 | + |
| 111 | + def validSquare(self, p1, p2, p3, p4): |
| 112 | + points = [p1, p2, p3, p4] |
| 113 | + points.sort(key=lambda x: (x[0], x[1])) |
| 114 | + |
| 115 | + return self.dist(points[0], points[1]) != 0 and \ |
| 116 | + self.dist(points[0], points[1]) == self.dist(points[1], points[3]) and \ |
| 117 | + self.dist(points[1], points[3]) == self.dist(points[3], points[2]) and \ |
| 118 | + self.dist(points[3], points[2]) == self.dist(points[2], points[0]) and \ |
| 119 | + self.dist(points[0], points[3]) == self.dist(points[1], points[2]) |
| 120 | + |
| 121 | + |
| 122 | +``` |
| 123 | +</TabItem> |
| 124 | +</Tabs> |
| 125 | + |
| 126 | +## Complexity Analysis |
| 127 | + |
| 128 | +### Time Complexity: $O(1)$ |
| 129 | + |
| 130 | +> **Reason**: Sorting 4 points takes constant time. |
| 131 | + |
| 132 | +### Space Complexity: $O(1)$ |
| 133 | + |
| 134 | +> **Reason**: Constant space is required. |
| 135 | + |
| 136 | +## References |
| 137 | + |
| 138 | +- **LeetCode Problem**: [Valid Square](https://leetcode.com/problems/valid-square/description/) |
| 139 | + |
| 140 | +- **Solution Link**: [Valid Square](https://leetcode.com/problems/valid-square/solutions/) |
0 commit comments