Skip to content

Commit 85def7f

Browse files
committed
add 593 java
1 parent 81bd50b commit 85def7f

File tree

6 files changed

+174
-12
lines changed

6 files changed

+174
-12
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -713,7 +713,7 @@
713713
553 | [Optimal Division](https://leetcode.com/problems/optimal-division/) |[Python](./leetcode_python/Math/optimal-division.py)| _O(n)_ | _O(1)_ | Medium |string, math, `amazon`| OK* (4)
714714
573 | [Squirrel Simulation](https://leetcode.com/problems/squirrel-simulation/) | [Python](./leetcode_python/Math/squirrel-simulation.py) | _O(n)_ | _O(1)_ | Medium |🔒| AGAIN (not start)
715715
592 | [Fraction Addition and Subtraction](https://leetcode.com/problems/fraction-addition-and-subtraction/) | [Python](./leetcode_python/Math/fraction-addition-and-subtraction.py)| _O(nlogx)_ | _O(n)_ | Medium || AGAIN
716-
593 | [Valid Square](https://leetcode.com/problems/valid-square/) | [Python](./leetcode_python/Math/valid-square.py)| _O(1)_ | _O(1)_ | Medium || AGAIN
716+
593 | [Valid Square](https://leetcode.com/problems/valid-square/) | [Python](./leetcode_python/Math/valid-square.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/Math/ValidSquare.java)| _O(1)_ | _O(1)_ | Medium |google, math| AGAIN
717717
598 | [Range Addition II](https://leetcode.com/problems/range-addition-ii/) | [Python](./leetcode_python/Math/range-addition-ii.py) | _O(p)_ | _O(1)_ | Easy || AGAIN
718718
625 | [Minimum Factorization](https://leetcode.com/problems/minimum-factorization/) | [Python](./leetcode_python/Math/minimum-factorization.py) | _O(loga)_ | _O(1)_ | Medium |🔒| OK*
719719
628| [Maximum Product of Three Numbers](https://leetcode.com/problems/maximum-product-of-three-numbers/) | [Python](./leetcode_python/Math/maximum-product-of-three-numbers.py) | _O(n)_ | _O(1)_ | Easy |`amazon`| OK (2)

data/progress.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
# 2024-12-20
44
- https://github.com/yennanliu/CS_basics/blob/master/doc/Leetcode_company_frequency-master/Google%206months-%20LeetCode.pdf
55

6-
76
# 2024-12-15
87
- https://github.com/yennanliu/CS_basics/blob/master/doc/Leetcode_company_frequency-master/Google%206months-%20LeetCode.pdf
98

data/progress.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
20241220: 815,871
1+
20241220: 815,871,593
22
20241215: 1109
33
20241214: 560,523
44
20241208: 304,853,325

data/to_review.txt

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
2025-02-13 -> ['815,871']
1+
2025-02-13 -> ['815,871,593']
22
2025-02-08 -> ['1109']
33
2025-02-07 -> ['560,523']
44
2025-02-01 -> ['304,853,325']
55
2025-01-26 -> ['370(todo)']
66
2025-01-24 -> ['34,767']
7-
2025-01-23 -> ['815,871']
7+
2025-01-23 -> ['815,871,593']
88
2025-01-20 -> ['722,380']
99
2025-01-19 -> ['33,81']
1010
2025-01-18 -> ['1109']
@@ -13,21 +13,21 @@
1313
2025-01-15 -> ['004(todo),34(todo),162(todo),275(todo)']
1414
2025-01-14 -> ['986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
1515
2025-01-11 -> ['304,853,325', '394']
16-
2025-01-10 -> ['815,871', '833,950']
16+
2025-01-10 -> ['815,871,593', '833,950']
1717
2025-01-05 -> ['1109', '370(todo)']
1818
2025-01-04 -> ['560,523', '53,210,207']
1919
2025-01-03 -> ['34,767', '444']
20-
2025-01-02 -> ['815,871', '1188,130,855(again)']
20+
2025-01-02 -> ['815,871,593', '1188,130,855(again)']
2121
2024-12-30 -> ['722,380']
2222
2024-12-29 -> ['304,853,325', '33,81']
23-
2024-12-28 -> ['815,871', '1109', '900']
23+
2024-12-28 -> ['815,871,593', '1109', '900']
2424
2024-12-27 -> ['560,523', '253', '26,27', '802,1197,26']
2525
2024-12-26 -> ['776,31']
26-
2024-12-25 -> ['815,871', '004(todo),34(todo),162(todo),275(todo)']
26+
2024-12-25 -> ['815,871,593', '004(todo),34(todo),162(todo),275(todo)']
2727
2024-12-24 -> ['986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
28-
2024-12-23 -> ['815,871', '1109', '370(todo)']
29-
2024-12-22 -> ['815,871', '560,523']
30-
2024-12-21 -> ['815,871', '304,853,325', '34,767', '394', '855,846']
28+
2024-12-23 -> ['815,871,593', '1109', '370(todo)']
29+
2024-12-22 -> ['815,871,593', '560,523']
30+
2024-12-21 -> ['815,871,593', '304,853,325', '34,767', '394', '855,846']
3131
2024-12-20 -> ['1109', '833,950', '932']
3232
2024-12-19 -> ['560,523']
3333
2024-12-18 -> ['1109', '951,792']
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
package LeetCodeJava.Math;
2+
3+
// https://leetcode.com/problems/valid-square/description/
4+
5+
import java.util.Arrays;
6+
7+
/**
8+
* 593. Valid Square Attempted Medium Topics Companies Given the coordinates of four points in 2D
9+
* space p1, p2, p3 and p4, return true if the four points construct a square.
10+
*
11+
* <p>The coordinate of a point pi is represented as [xi, yi]. The input is not given in any order.
12+
*
13+
* <p>A valid square has four equal sides with positive length and four equal angles (90-degree
14+
* angles).
15+
*
16+
* <p>Example 1:
17+
*
18+
* <p>Input: p1 = [0,0], p2 = [1,1], p3 = [1,0], p4 = [0,1] Output: true Example 2:
19+
*
20+
* <p>Input: p1 = [0,0], p2 = [1,1], p3 = [1,0], p4 = [0,12] Output: false Example 3:
21+
*
22+
* <p>Input: p1 = [1,0], p2 = [-1,0], p3 = [0,1], p4 = [0,-1] Output: true
23+
*
24+
* <p>Constraints:
25+
*
26+
* <p>p1.length == p2.length == p3.length == p4.length == 2 -104 <= xi, yi <= 104
27+
*/
28+
public class ValidSquare {
29+
30+
// V0
31+
// TODO : implement
32+
// public boolean validSquare(int[] p1, int[] p2, int[] p3, int[] p4) {
33+
//
34+
// }
35+
36+
// V1
37+
// https://leetcode.com/problems/valid-square/editorial/
38+
// IDEA : BRUTE FORCE
39+
public double dist(int[] p1, int[] p2) {
40+
return (p2[1] - p1[1]) * (p2[1] - p1[1]) + (p2[0] - p1[0]) * (p2[0] - p1[0]);
41+
}
42+
43+
public boolean check(int[] p1, int[] p2, int[] p3, int[] p4) {
44+
return dist(p1, p2) > 0
45+
&& dist(p1, p2) == dist(p2, p3)
46+
&& dist(p2, p3) == dist(p3, p4)
47+
&& dist(p3, p4) == dist(p4, p1)
48+
&& dist(p1, p3) == dist(p2, p4);
49+
}
50+
51+
public boolean validSquare_1(int[] p1, int[] p2, int[] p3, int[] p4) {
52+
int[][] p = {p1, p2, p3, p4};
53+
return checkAllPermutations(p, 0);
54+
}
55+
56+
boolean checkAllPermutations(int[][] p, int l) {
57+
if (l == 4) {
58+
return check(p[0], p[1], p[2], p[3]);
59+
} else {
60+
boolean res = false;
61+
for (int i = l; i < 4; i++) {
62+
swap(p, l, i);
63+
res |= checkAllPermutations(p, l + 1);
64+
swap(p, l, i);
65+
}
66+
return res;
67+
}
68+
}
69+
70+
public void swap(int[][] p, int x, int y) {
71+
int[] temp = p[x];
72+
p[x] = p[y];
73+
p[y] = temp;
74+
}
75+
76+
// V2
77+
// https://leetcode.com/problems/valid-square/editorial/
78+
// IDEA : Using Sorting
79+
public double dist_2(int[] p1, int[] p2) {
80+
return (p2[1] - p1[1]) * (p2[1] - p1[1]) + (p2[0] - p1[0]) * (p2[0] - p1[0]);
81+
}
82+
83+
public boolean validSquare_2(int[] p1, int[] p2, int[] p3, int[] p4) {
84+
int[][] p = {p1, p2, p3, p4};
85+
Arrays.sort(p, (l1, l2) -> l2[0] == l1[0] ? l1[1] - l2[1] : l1[0] - l2[0]);
86+
return dist_2(p[0], p[1]) != 0
87+
&& dist_2(p[0], p[1]) == dist_2(p[1], p[3])
88+
&& dist_2(p[1], p[3]) == dist_2(p[3], p[2])
89+
&& dist_2(p[3], p[2]) == dist_2(p[2], p[0])
90+
&& dist_2(p[0], p[3]) == dist_2(p[1], p[2]);
91+
}
92+
93+
// V3
94+
// https://leetcode.com/problems/valid-square/editorial/
95+
// IDEA : Checking every case
96+
public double dist_3(int[] p1, int[] p2) {
97+
return (p2[1] - p1[1]) * (p2[1] - p1[1]) + (p2[0] - p1[0]) * (p2[0] - p1[0]);
98+
}
99+
100+
public boolean check_3(int[] p1, int[] p2, int[] p3, int[] p4) {
101+
return dist_3(p1, p2) > 0
102+
&& dist_3(p1, p2) == dist_3(p2, p3)
103+
&& dist_3(p2, p3) == dist_3(p3, p4)
104+
&& dist_3(p3, p4) == dist_3(p4, p1)
105+
&& dist_3(p1, p3) == dist_3(p2, p4);
106+
}
107+
108+
public boolean validSquare_3(int[] p1, int[] p2, int[] p3, int[] p4) {
109+
return check_3(p1, p2, p3, p4) || check_3(p1, p3, p2, p4) || check_3(p1, p2, p4, p3);
110+
}
111+
}

leetcode_java/src/main/java/dev/workspace6.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,4 +171,56 @@ public int numBusesToDestination(int[][] routes, int source, int target) {
171171
return -1;
172172
}
173173

174+
// LC 593
175+
// https://leetcode.com/problems/valid-square/
176+
// 2.05 pm - 2.20 pm
177+
/**
178+
* Idea 1) math
179+
*
180+
* A----B
181+
* | |
182+
* C----D
183+
*
184+
*
185+
* -> so, dist(A,B) == dist(C,D)
186+
*
187+
* step 1) create a check func
188+
* 1) check if same dist
189+
* 2) check if "90 degree"
190+
*
191+
* step 2) go through each point, and check "if at least 2 combinations"
192+
* fit the check logic, if any violation, return false directly
193+
*
194+
* step 3) return true
195+
*
196+
*/
197+
public boolean validSquare(int[] p1, int[] p2, int[] p3, int[] p4) {
198+
199+
List<int[]> list = new ArrayList<>();
200+
list.add(p1);
201+
list.add(p2);
202+
list.add(p3);
203+
list.add(p4);
204+
205+
for (int i = 0; i < list.size(); i++){
206+
int violationCnt = 0;
207+
for (int j = 0; j < list.size(); j++){
208+
if (i != j){
209+
if (!canBuildSqaure(list.get(i), list.get(j))){
210+
violationCnt += 1;
211+
}
212+
}
213+
if (violationCnt > 1){
214+
return false;
215+
}
216+
}
217+
}
218+
return true;
219+
}
220+
221+
private boolean canBuildSqaure(int[] x1, int[] x2){
222+
223+
return true;
224+
}
225+
174226
}

0 commit comments

Comments
 (0)