Skip to content

Commit

Permalink
add 593 java
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Dec 20, 2024
1 parent 81bd50b commit 85def7f
Show file tree
Hide file tree
Showing 6 changed files with 174 additions and 12 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -713,7 +713,7 @@
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)
573 | [Squirrel Simulation](https://leetcode.com/problems/squirrel-simulation/) | [Python](./leetcode_python/Math/squirrel-simulation.py) | _O(n)_ | _O(1)_ | Medium |🔒| AGAIN (not start)
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
593 | [Valid Square](https://leetcode.com/problems/valid-square/) | [Python](./leetcode_python/Math/valid-square.py)| _O(1)_ | _O(1)_ | Medium || AGAIN
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
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
625 | [Minimum Factorization](https://leetcode.com/problems/minimum-factorization/) | [Python](./leetcode_python/Math/minimum-factorization.py) | _O(loga)_ | _O(1)_ | Medium |🔒| OK*
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)
Expand Down
1 change: 0 additions & 1 deletion data/progress.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
# 2024-12-20
- https://github.com/yennanliu/CS_basics/blob/master/doc/Leetcode_company_frequency-master/Google%206months-%20LeetCode.pdf


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

Expand Down
2 changes: 1 addition & 1 deletion data/progress.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
20241220: 815,871
20241220: 815,871,593
20241215: 1109
20241214: 560,523
20241208: 304,853,325
Expand Down
18 changes: 9 additions & 9 deletions data/to_review.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
2025-02-13 -> ['815,871']
2025-02-13 -> ['815,871,593']
2025-02-08 -> ['1109']
2025-02-07 -> ['560,523']
2025-02-01 -> ['304,853,325']
2025-01-26 -> ['370(todo)']
2025-01-24 -> ['34,767']
2025-01-23 -> ['815,871']
2025-01-23 -> ['815,871,593']
2025-01-20 -> ['722,380']
2025-01-19 -> ['33,81']
2025-01-18 -> ['1109']
Expand All @@ -13,21 +13,21 @@
2025-01-15 -> ['004(todo),34(todo),162(todo),275(todo)']
2025-01-14 -> ['986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
2025-01-11 -> ['304,853,325', '394']
2025-01-10 -> ['815,871', '833,950']
2025-01-10 -> ['815,871,593', '833,950']
2025-01-05 -> ['1109', '370(todo)']
2025-01-04 -> ['560,523', '53,210,207']
2025-01-03 -> ['34,767', '444']
2025-01-02 -> ['815,871', '1188,130,855(again)']
2025-01-02 -> ['815,871,593', '1188,130,855(again)']
2024-12-30 -> ['722,380']
2024-12-29 -> ['304,853,325', '33,81']
2024-12-28 -> ['815,871', '1109', '900']
2024-12-28 -> ['815,871,593', '1109', '900']
2024-12-27 -> ['560,523', '253', '26,27', '802,1197,26']
2024-12-26 -> ['776,31']
2024-12-25 -> ['815,871', '004(todo),34(todo),162(todo),275(todo)']
2024-12-25 -> ['815,871,593', '004(todo),34(todo),162(todo),275(todo)']
2024-12-24 -> ['986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
2024-12-23 -> ['815,871', '1109', '370(todo)']
2024-12-22 -> ['815,871', '560,523']
2024-12-21 -> ['815,871', '304,853,325', '34,767', '394', '855,846']
2024-12-23 -> ['815,871,593', '1109', '370(todo)']
2024-12-22 -> ['815,871,593', '560,523']
2024-12-21 -> ['815,871,593', '304,853,325', '34,767', '394', '855,846']
2024-12-20 -> ['1109', '833,950', '932']
2024-12-19 -> ['560,523']
2024-12-18 -> ['1109', '951,792']
Expand Down
111 changes: 111 additions & 0 deletions leetcode_java/src/main/java/LeetCodeJava/Math/ValidSquare.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package LeetCodeJava.Math;

// https://leetcode.com/problems/valid-square/description/

import java.util.Arrays;

/**
* 593. Valid Square Attempted Medium Topics Companies Given the coordinates of four points in 2D
* space p1, p2, p3 and p4, return true if the four points construct a square.
*
* <p>The coordinate of a point pi is represented as [xi, yi]. The input is not given in any order.
*
* <p>A valid square has four equal sides with positive length and four equal angles (90-degree
* angles).
*
* <p>Example 1:
*
* <p>Input: p1 = [0,0], p2 = [1,1], p3 = [1,0], p4 = [0,1] Output: true Example 2:
*
* <p>Input: p1 = [0,0], p2 = [1,1], p3 = [1,0], p4 = [0,12] Output: false Example 3:
*
* <p>Input: p1 = [1,0], p2 = [-1,0], p3 = [0,1], p4 = [0,-1] Output: true
*
* <p>Constraints:
*
* <p>p1.length == p2.length == p3.length == p4.length == 2 -104 <= xi, yi <= 104
*/
public class ValidSquare {

// V0
// TODO : implement
// public boolean validSquare(int[] p1, int[] p2, int[] p3, int[] p4) {
//
// }

// V1
// https://leetcode.com/problems/valid-square/editorial/
// IDEA : BRUTE FORCE
public double dist(int[] p1, int[] p2) {
return (p2[1] - p1[1]) * (p2[1] - p1[1]) + (p2[0] - p1[0]) * (p2[0] - p1[0]);
}

public boolean check(int[] p1, int[] p2, int[] p3, int[] p4) {
return dist(p1, p2) > 0
&& dist(p1, p2) == dist(p2, p3)
&& dist(p2, p3) == dist(p3, p4)
&& dist(p3, p4) == dist(p4, p1)
&& dist(p1, p3) == dist(p2, p4);
}

public boolean validSquare_1(int[] p1, int[] p2, int[] p3, int[] p4) {
int[][] p = {p1, p2, p3, p4};
return checkAllPermutations(p, 0);
}

boolean checkAllPermutations(int[][] p, int l) {
if (l == 4) {
return check(p[0], p[1], p[2], p[3]);
} else {
boolean res = false;
for (int i = l; i < 4; i++) {
swap(p, l, i);
res |= checkAllPermutations(p, l + 1);
swap(p, l, i);
}
return res;
}
}

public void swap(int[][] p, int x, int y) {
int[] temp = p[x];
p[x] = p[y];
p[y] = temp;
}

// V2
// https://leetcode.com/problems/valid-square/editorial/
// IDEA : Using Sorting
public double dist_2(int[] p1, int[] p2) {
return (p2[1] - p1[1]) * (p2[1] - p1[1]) + (p2[0] - p1[0]) * (p2[0] - p1[0]);
}

public boolean validSquare_2(int[] p1, int[] p2, int[] p3, int[] p4) {
int[][] p = {p1, p2, p3, p4};
Arrays.sort(p, (l1, l2) -> l2[0] == l1[0] ? l1[1] - l2[1] : l1[0] - l2[0]);
return dist_2(p[0], p[1]) != 0
&& dist_2(p[0], p[1]) == dist_2(p[1], p[3])
&& dist_2(p[1], p[3]) == dist_2(p[3], p[2])
&& dist_2(p[3], p[2]) == dist_2(p[2], p[0])
&& dist_2(p[0], p[3]) == dist_2(p[1], p[2]);
}

// V3
// https://leetcode.com/problems/valid-square/editorial/
// IDEA : Checking every case
public double dist_3(int[] p1, int[] p2) {
return (p2[1] - p1[1]) * (p2[1] - p1[1]) + (p2[0] - p1[0]) * (p2[0] - p1[0]);
}

public boolean check_3(int[] p1, int[] p2, int[] p3, int[] p4) {
return dist_3(p1, p2) > 0
&& dist_3(p1, p2) == dist_3(p2, p3)
&& dist_3(p2, p3) == dist_3(p3, p4)
&& dist_3(p3, p4) == dist_3(p4, p1)
&& dist_3(p1, p3) == dist_3(p2, p4);
}

public boolean validSquare_3(int[] p1, int[] p2, int[] p3, int[] p4) {
return check_3(p1, p2, p3, p4) || check_3(p1, p3, p2, p4) || check_3(p1, p2, p4, p3);
}
}
52 changes: 52 additions & 0 deletions leetcode_java/src/main/java/dev/workspace6.java
Original file line number Diff line number Diff line change
Expand Up @@ -171,4 +171,56 @@ public int numBusesToDestination(int[][] routes, int source, int target) {
return -1;
}

// LC 593
// https://leetcode.com/problems/valid-square/
// 2.05 pm - 2.20 pm
/**
* Idea 1) math
*
* A----B
* | |
* C----D
*
*
* -> so, dist(A,B) == dist(C,D)
*
* step 1) create a check func
* 1) check if same dist
* 2) check if "90 degree"
*
* step 2) go through each point, and check "if at least 2 combinations"
* fit the check logic, if any violation, return false directly
*
* step 3) return true
*
*/
public boolean validSquare(int[] p1, int[] p2, int[] p3, int[] p4) {

List<int[]> list = new ArrayList<>();
list.add(p1);
list.add(p2);
list.add(p3);
list.add(p4);

for (int i = 0; i < list.size(); i++){
int violationCnt = 0;
for (int j = 0; j < list.size(); j++){
if (i != j){
if (!canBuildSqaure(list.get(i), list.get(j))){
violationCnt += 1;
}
}
if (violationCnt > 1){
return false;
}
}
}
return true;
}

private boolean canBuildSqaure(int[] x1, int[] x2){

return true;
}

}

0 comments on commit 85def7f

Please sign in to comment.