Skip to content

Commit 4a6da3b

Browse files
authored
Improved tasks 874, 885, 999, 1001, 1931
1 parent 06a8162 commit 4a6da3b

File tree

5 files changed

+281
-272
lines changed

5 files changed

+281
-272
lines changed

src/main/java/g0801_0900/s0874_walking_robot_simulation/Solution.java

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -30,44 +30,16 @@ public int robotSim(int[] commands, int[][] obstacles) {
3030
Integer next = null;
3131
switch (direction) {
3232
case 0:
33-
if (yMap.containsKey(y)) {
34-
next = yMap.get(y).ceiling(x + 1);
35-
}
36-
if (next != null) {
37-
x = Math.min(x + cmd, next - 1);
38-
} else {
39-
x += cmd;
40-
}
33+
x = getXPlusOne(x, y, yMap, cmd, next);
4134
break;
4235
case 1:
43-
if (xMap.containsKey(x)) {
44-
next = xMap.get(x).ceiling(y + 1);
45-
}
46-
if (next != null) {
47-
y = Math.min(y + cmd, next - 1);
48-
} else {
49-
y += cmd;
50-
}
36+
y = getXPlusOne(y, x, xMap, cmd, next);
5137
break;
5238
case 2:
53-
if (yMap.containsKey(y)) {
54-
next = yMap.get(y).floor(x - 1);
55-
}
56-
if (next != null) {
57-
x = Math.max(x - cmd, next + 1);
58-
} else {
59-
x -= cmd;
60-
}
39+
x = getXMinusOne(x, y, yMap, cmd, next);
6140
break;
6241
case 3:
63-
if (xMap.containsKey(x)) {
64-
next = xMap.get(x).floor(y - 1);
65-
}
66-
if (next != null) {
67-
y = Math.max(y - cmd, next + 1);
68-
} else {
69-
y -= cmd;
70-
}
42+
y = getXMinusOne(y, x, xMap, cmd, next);
7143
break;
7244
default:
7345
// error
@@ -78,4 +50,30 @@ public int robotSim(int[] commands, int[][] obstacles) {
7850
}
7951
return maxDis;
8052
}
53+
54+
private int getXMinusOne(
55+
int x, int y, Map<Integer, TreeSet<Integer>> yMap, int cmd, Integer next) {
56+
if (yMap.containsKey(y)) {
57+
next = yMap.get(y).floor(x - 1);
58+
}
59+
if (next != null) {
60+
x = Math.max(x - cmd, next + 1);
61+
} else {
62+
x -= cmd;
63+
}
64+
return x;
65+
}
66+
67+
private int getXPlusOne(
68+
int x, int y, Map<Integer, TreeSet<Integer>> yMap, int cmd, Integer next) {
69+
if (yMap.containsKey(y)) {
70+
next = yMap.get(y).ceiling(x + 1);
71+
}
72+
if (next != null) {
73+
x = Math.min(x + cmd, next - 1);
74+
} else {
75+
x += cmd;
76+
}
77+
return x;
78+
}
8179
}
Lines changed: 83 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,107 @@
11
package g0801_0900.s0885_spiral_matrix_iii;
22

3-
// #Medium #Array #Matrix #Simulation #2022_03_28_Time_3_ms_(95.45%)_Space_64_MB_(42.42%)
3+
// #Medium #Array #Matrix #Simulation #2023_04_26_Time_2_ms_(100.00%)_Space_43.8_MB_(33.14%)
44

55
@SuppressWarnings("java:S135")
66
public class Solution {
77
public int[][] spiralMatrixIII(int rows, int cols, int y, int x) {
8-
int j;
9-
int i = 0;
10-
int moves = 0;
8+
int localX = x;
9+
int localY = y;
10+
int[] i = {0};
11+
int[] moves = {0};
1112
int[][] result = new int[rows * cols][2];
1213
result[0][0] = y;
1314
result[0][1] = x;
14-
i++;
15-
while (i < result.length) {
16-
moves++;
15+
i[0]++;
16+
while (i[0] < result.length) {
17+
moves[0]++;
1718
// Move right
18-
if (y < 0 || y >= rows) {
19-
x += moves;
20-
} else {
21-
for (j = 0; j < moves; j++) {
22-
x++;
23-
if (x >= 0 && x < cols && y >= 0 && y < rows) {
24-
result[i][0] = y;
25-
result[i][1] = x;
26-
i++;
27-
}
28-
}
29-
}
30-
if (i >= result.length) {
19+
localX = getXRight(rows, cols, localX, localY, i, moves, result);
20+
if (i[0] >= result.length) {
3121
break;
3222
}
3323
// Move down
34-
if (x < 0 || x >= cols) {
35-
y += moves;
36-
} else {
37-
for (j = 0; j < moves; j++) {
38-
y++;
39-
if (x >= 0 && x < cols && y >= 0 && y < rows) {
40-
result[i][0] = y;
41-
result[i][1] = x;
42-
i++;
43-
}
44-
}
45-
}
46-
if (i >= result.length) {
24+
localY = getYDown(rows, cols, localX, localY, i, moves, result);
25+
if (i[0] >= result.length) {
4726
break;
4827
}
49-
moves++;
28+
moves[0]++;
5029
// Move left
51-
if (y < 0 || y >= rows) {
52-
x -= moves;
53-
} else {
54-
for (j = 0; j < moves; j++) {
55-
x--;
56-
if (x >= 0 && x < cols && y >= 0 && y < rows) {
57-
result[i][0] = y;
58-
result[i][1] = x;
59-
i++;
60-
}
61-
}
62-
}
63-
if (i >= result.length) {
30+
localX = getXLeft(rows, cols, localX, localY, i, moves, result);
31+
if (i[0] >= result.length) {
6432
break;
6533
}
6634
// Move up
67-
if (x < 0 || x >= cols) {
68-
y -= moves;
69-
} else {
70-
for (j = 0; j < moves; j++) {
71-
y--;
72-
if (x >= 0 && x < cols && y >= 0 && y < rows) {
73-
result[i][0] = y;
74-
result[i][1] = x;
75-
i++;
76-
}
35+
localY = getYUp(rows, cols, localX, localY, i, moves, result);
36+
}
37+
return result;
38+
}
39+
40+
private int getYUp(
41+
int rows, int cols, int localX, int localY, int[] i, int[] moves, int[][] result) {
42+
if (localX < 0 || localX >= cols) {
43+
localY -= moves[0];
44+
} else {
45+
for (int j = 0; j < moves[0]; j++) {
46+
localY--;
47+
if (localY >= 0 && localY < rows) {
48+
result[i[0]][0] = localY;
49+
result[i[0]][1] = localX;
50+
i[0]++;
7751
}
7852
}
7953
}
80-
return result;
54+
return localY;
55+
}
56+
57+
private int getXLeft(
58+
int rows, int cols, int localX, int localY, int[] i, int[] moves, int[][] result) {
59+
if (localY < 0 || localY >= rows) {
60+
localX -= moves[0];
61+
} else {
62+
for (int j = 0; j < moves[0]; j++) {
63+
localX--;
64+
if (localX >= 0 && localX < cols) {
65+
result[i[0]][0] = localY;
66+
result[i[0]][1] = localX;
67+
i[0]++;
68+
}
69+
}
70+
}
71+
return localX;
72+
}
73+
74+
private int getYDown(
75+
int rows, int cols, int localX, int localY, int[] i, int[] moves, int[][] result) {
76+
if (localX < 0 || localX >= cols) {
77+
localY += moves[0];
78+
} else {
79+
for (int j = 0; j < moves[0]; j++) {
80+
localY++;
81+
if (localY >= 0 && localY < rows) {
82+
result[i[0]][0] = localY;
83+
result[i[0]][1] = localX;
84+
i[0]++;
85+
}
86+
}
87+
}
88+
return localY;
89+
}
90+
91+
private int getXRight(
92+
int rows, int cols, int localX, int localY, int[] i, int[] moves, int[][] result) {
93+
if (localY < 0 || localY >= rows) {
94+
localX += moves[0];
95+
} else {
96+
for (int j = 0; j < moves[0]; j++) {
97+
localX++;
98+
if (localX >= 0 && localX < cols) {
99+
result[i[0]][0] = localY;
100+
result[i[0]][1] = localX;
101+
i[0]++;
102+
}
103+
}
104+
}
105+
return localX;
81106
}
82107
}

src/main/java/g0901_1000/s0999_available_captures_for_rook/Solution.java

Lines changed: 58 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public int numRookCaptures(char[][] board) {
2020
}
2121
}
2222
}
23-
int count = 0;
23+
int[] count = {0};
2424
for (int i = 0; i < 4; i++) {
2525
int neighborRow = rowR + directions[i];
2626
int neighborCol = colR + directions[i + 1];
@@ -30,52 +30,68 @@ public int numRookCaptures(char[][] board) {
3030
&& neighborCol < n
3131
&& board[neighborRow][neighborCol] != 'B') {
3232
if (directions[i] == 0 && directions[i + 1] == 1) {
33-
while (neighborCol < n) {
34-
if (board[neighborRow][neighborCol] == 'p') {
35-
count++;
36-
break;
37-
} else if (board[neighborRow][neighborCol] == 'B') {
38-
break;
39-
} else {
40-
neighborCol++;
41-
}
42-
}
33+
extracted(board, n, count, neighborRow, neighborCol);
4334
} else if (directions[i] == 1 && directions[i + 1] == 0) {
44-
while (neighborRow < m) {
45-
if (board[neighborRow][neighborCol] == 'p') {
46-
count++;
47-
break;
48-
} else if (board[neighborRow][neighborCol] == 'B') {
49-
break;
50-
} else {
51-
neighborRow++;
52-
}
53-
}
35+
extracted1(board, m, count, neighborRow, neighborCol);
5436
} else if (directions[i] == 0 && directions[i + 1] == -1) {
55-
while (neighborCol >= 0) {
56-
if (board[neighborRow][neighborCol] == 'p') {
57-
count++;
58-
break;
59-
} else if (board[neighborRow][neighborCol] == 'B') {
60-
break;
61-
} else {
62-
neighborCol--;
63-
}
64-
}
37+
extracted(board, count, neighborRow, neighborCol);
6538
} else {
66-
while (neighborRow >= 0) {
67-
if (board[neighborRow][neighborCol] == 'p') {
68-
count++;
69-
break;
70-
} else if (board[neighborRow][neighborCol] == 'B') {
71-
break;
72-
} else {
73-
neighborRow--;
74-
}
75-
}
39+
extracted1(board, count, neighborRow, neighborCol);
7640
}
7741
}
7842
}
79-
return count;
43+
return count[0];
44+
}
45+
46+
private void extracted(char[][] board, int[] count, int neighborRow, int neighborCol) {
47+
while (neighborCol >= 0) {
48+
if (board[neighborRow][neighborCol] == 'p') {
49+
count[0]++;
50+
break;
51+
} else if (board[neighborRow][neighborCol] == 'B') {
52+
break;
53+
} else {
54+
neighborCol--;
55+
}
56+
}
57+
}
58+
59+
private void extracted(char[][] board, int n, int[] count, int neighborRow, int neighborCol) {
60+
while (neighborCol < n) {
61+
if (board[neighborRow][neighborCol] == 'p') {
62+
count[0]++;
63+
break;
64+
} else if (board[neighborRow][neighborCol] == 'B') {
65+
break;
66+
} else {
67+
neighborCol++;
68+
}
69+
}
70+
}
71+
72+
private void extracted1(char[][] board, int[] count, int neighborRow, int neighborCol) {
73+
while (neighborRow >= 0) {
74+
if (board[neighborRow][neighborCol] == 'p') {
75+
count[0]++;
76+
break;
77+
} else if (board[neighborRow][neighborCol] == 'B') {
78+
break;
79+
} else {
80+
neighborRow--;
81+
}
82+
}
83+
}
84+
85+
private void extracted1(char[][] board, int m, int[] count, int neighborRow, int neighborCol) {
86+
while (neighborRow < m) {
87+
if (board[neighborRow][neighborCol] == 'p') {
88+
count[0]++;
89+
break;
90+
} else if (board[neighborRow][neighborCol] == 'B') {
91+
break;
92+
} else {
93+
neighborRow++;
94+
}
95+
}
8096
}
8197
}

0 commit comments

Comments
 (0)