Skip to content

Commit 3527a24

Browse files
add 4084
1 parent 6f0f4c8 commit 3527a24

2 files changed

Lines changed: 37 additions & 0 deletions

File tree

  • paginated_contents/algorithms/5th_thousand
  • src/main/java/com/fishercoder/solutions/fifththousand

‎paginated_contents/algorithms/5th_thousand/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
| # | Title | Solutions | Video | Difficulty | Tag
22
|------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------|--------------------------------------|------------|----------------------------------------------------------------------
3+
| 4084 | [Last Visited Cell in a Directional Grid](https://leetcode.com/problems/last-visited-cell-in-a-directional-grid/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fifththousand/_4084.java) | | Medium |
34
| 4083 | [Minimum Deletions to Make Array Sharp](https://leetcode.com/problems/minimum-deletions-to-make-array-sharp/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fifththousand/_4083.java) | | Easy |
45
| 4080 | [Design Bus Seat Booking System](https://leetcode.com/problems/design-bus-seat-booking-system/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fifththousand/_4080.java) | | Medium |
56
| 4079 | [Prime Subset Selection I](https://leetcode.com/problems/prime-subset-selection-i/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fifththousand/_4079.java) | | Medium |
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.fishercoder.solutions.fifththousand;
2+
3+
import java.util.HashSet;
4+
import java.util.Set;
5+
6+
public class _4084 {
7+
public static class Solution1 {
8+
public int[] lastVisitedCell(char[][] grid) {
9+
Set<Integer> visited = new HashSet<>();
10+
int n = grid.length > 0 ? grid[0].length : 0;
11+
int row = 0;
12+
int col = 0;
13+
int lastRow = 0;
14+
int lastCol = 0;
15+
while (!visited.contains(row * n + col)) {
16+
char nextMove = grid[row][col];
17+
visited.add(row * n + col);
18+
lastRow = row;
19+
lastCol = col;
20+
if (nextMove == 'R') {
21+
col++;
22+
} else if (nextMove == 'L') {
23+
col--;
24+
} else if (nextMove == 'U') {
25+
row--;
26+
} else {
27+
row++;
28+
}
29+
if (row < 0 || col < 0 || row >= grid.length || col >= grid[0].length) {
30+
break;
31+
}
32+
}
33+
return new int[]{lastRow, lastCol};
34+
}
35+
}
36+
}

0 commit comments

Comments
 (0)