Skip to content

Commit 21adf34

Browse files
solves path crossing
1 parent 150edd2 commit 21adf34

File tree

2 files changed

+49
-3
lines changed

2 files changed

+49
-3
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# LeetCode Algorithms
22

3-
![problems-solved](https://img.shields.io/badge/Problems%20Solved-282/2081-1f425f.svg)
4-
![problems-solved-java](https://img.shields.io/badge/Java-282/2081-1abc9c.svg)
3+
![problems-solved](https://img.shields.io/badge/Problems%20Solved-304/2081-1f425f.svg)
4+
![problems-solved-java](https://img.shields.io/badge/Java-304/2081-1abc9c.svg)
55
![problems-solved-python](https://img.shields.io/badge/Python-186/2081-1abc9c.svg)
66
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](CONTRIBUTING.md)
77
[![cp](https://img.shields.io/badge/also%20see-Competitve%20Programming-1f72ff.svg)](https://github.com/anishLearnsToCode/competitive-programming)
@@ -379,7 +379,7 @@
379379
| 1480 | [Running Sum of 1D Array](https://leetcode.com/problems/running-sum-of-1d-array) | [![Java](assets/java.png)](src/RunningSumOf1DArray.java) | |
380380
| 1486 | [XOR Operations in An Array](https://leetcode.com/problems/xor-operation-in-an-array) | [![Java](assets/java.png)](src/XOROperationInAnArray.java) | |
381381
| 1491 | [Average Salary Excluding the Minimum and Maximum Salary](https://leetcode.com/problems/average-salary-excluding-the-minimum-and-maximum-salary) | [![Java](assets/java.png)](src/AverageSalaryExcludingTheMinimumAndMaximumSalary.java) | |
382-
| 1496 | [Path Crossing](https://leetcode.com/problems/path-crossing) | | |
382+
| 1496 | [Path Crossing](https://leetcode.com/problems/path-crossing) | [![Java](assets/java.png)](src/PathCrossing.java) | |
383383
| 1502 | [Can Make Arithmetic Progression From Sequence](https://leetcode.com/problems/can-make-arithmetic-progression-from-sequence) | | |
384384
| 1507 | [Reformat Date](https://leetcode.com/problems/reformat-date) | | |
385385
| 1512 | [Number of Good Pairs](https://leetcode.com/problems/number-of-good-pairs) | | |

src/PathCrossing.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import java.util.HashSet;
2+
import java.util.Objects;
3+
import java.util.Set;
4+
5+
public class PathCrossing {
6+
public boolean isPathCrossing(String path) {
7+
Set<Point> visitedPoints = new HashSet<>();
8+
visitedPoints.add(new Point(0, 0));
9+
for (int index = 0, x = 0, y = 0 ; index < path.length() ; index++) {
10+
switch (path.charAt(index)) {
11+
case 'N' -> y++;
12+
case 'E' -> x++;
13+
case 'S' -> y--;
14+
case 'W' -> x--;
15+
}
16+
Point newPosition = new Point(x, y);
17+
if (visitedPoints.contains(newPosition)) return true;
18+
visitedPoints.add(newPosition);
19+
}
20+
return false;
21+
}
22+
23+
private static final class Point {
24+
private final int x;
25+
private final int y;
26+
27+
private Point(int x, int y) {
28+
this.x = x;
29+
this.y = y;
30+
}
31+
32+
@Override
33+
public boolean equals(Object obj) {
34+
if (obj == this) return true;
35+
if (obj == null || obj.getClass() != this.getClass()) return false;
36+
var that = (Point) obj;
37+
return this.x == that.x &&
38+
this.y == that.y;
39+
}
40+
41+
@Override
42+
public int hashCode() {
43+
return Objects.hash(x, y);
44+
}
45+
}
46+
}

0 commit comments

Comments
 (0)