Skip to content

Commit 62818b9

Browse files
solves valid boomerang
1 parent 84097c0 commit 62818b9

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@
280280
| 1025 | [Divisor Game](https://leetcode.com/problems/divisor-game) | [![Java](assets/java.png)](src/DivisorGame.java) | |
281281
| 1029 | [Two City Scheduling](https://leetcode.com/problems/two-city-scheduling) | | |
282282
| 1030 | [Matrix Cells in Distance Order](https://leetcode.com/problems/matrix-cells-in-distance-order) | [![Java](assets/java.png)](src/MatrixCellsInDistanceOrder.java) | |
283-
| 1033 | [Moving Stones Until Consecutive](https://leetcode.com/problems/moving-stones-until-consecutive) | | |
283+
| 1033 | [Moving Stones Until Consecutive](https://leetcode.com/problems/moving-stones-until-consecutive) | [![Java](assets/java.png)](src/ValidBoomerang.java) | |
284284
| 1037 | [Valid Boomerang](https://leetcode.com/problems/valid-boomerang) | | |
285285
| 1042 | [Flower Planting with no Adjacent](https://leetcode.com/problems/flower-planting-with-no-adjacent) | | |
286286
| 1046 | [Last Stone Weight](https://leetcode.com/problems/last-stone-weight) | | |

src/ValidBoomerang.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import java.util.Objects;
2+
3+
public class ValidBoomerang {
4+
public boolean isBoomerang(int[][] points) {
5+
Position p1 = Position.from(points[0]);
6+
Position p2 = Position.from(points[1]);
7+
Position p3 = Position.from(points[2]);
8+
if (p1.equals(p2) || p2.equals(p3) || p3.equals(p1)) return false;
9+
if (p1.x == p2.x) return p3.x != p2.x;
10+
if (p2.x == p3.x) return true;
11+
return (p2.y - p1.y) * (p3.x - p2.x) != (p3.y - p2.y) * (p2.x - p1.x);
12+
}
13+
14+
private final static class Position {
15+
private final int x;
16+
private final int y;
17+
18+
private static Position from(int[] array){
19+
return new Position(array[0], array[1]);
20+
}
21+
22+
Position(int x, int y) {
23+
this.x = x;
24+
this.y = y;
25+
}
26+
27+
@Override
28+
public boolean equals(Object o) {
29+
if (this == o) return true;
30+
if (o == null || getClass() != o.getClass()) return false;
31+
Position position = (Position) o;
32+
return x == position.x && y == position.y;
33+
}
34+
35+
@Override
36+
public int hashCode() {
37+
return Objects.hash(x, y);
38+
}
39+
}
40+
}

0 commit comments

Comments
 (0)