Skip to content

Commit e855640

Browse files
authored
Improved tasks 2060, 2257
1 parent 4a6da3b commit e855640

File tree

3 files changed

+74
-53
lines changed
  • src/main/java
    • g2001_2100/s2060_check_if_an_original_string_exists_given_two_encoded_strings
    • g2201_2300/s2257_count_unguarded_cells_in_the_grid
    • g2501_2600/s2538_difference_between_maximum_and_minimum_price_sum

3 files changed

+74
-53
lines changed

src/main/java/g2001_2100/s2060_check_if_an_original_string_exists_given_two_encoded_strings/Solution.java

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -67,24 +67,8 @@ private void dfs(int i1, int i2, int diff) {
6767
x2++;
6868
}
6969
if (diff == 0) {
70-
char c1 = s1.charAt(i1);
71-
char c2 = s2.charAt(i2);
72-
if (Character.isLetter(c1) && Character.isLetter(c2)) {
73-
if (c1 != c2) {
74-
return;
75-
}
76-
dfs(i1 + 1, i2 + 1, diff);
70+
if (extracted(i1, i2, diff, indexNums1, indexNums2)) {
7771
return;
78-
} else {
79-
if (!indexNums1.isEmpty()) {
80-
for (int[] num1Item : indexNums1) {
81-
dfs(num1Item[0] + 1, i2, diff + num1Item[1]);
82-
}
83-
} else {
84-
for (int[] num2Item : indexNums2) {
85-
dfs(i1, num2Item[0] + 1, diff - num2Item[1]);
86-
}
87-
}
8872
}
8973
} else if (diff > 0) {
9074
if (Character.isLetter(s2.charAt(i2))) {
@@ -105,4 +89,28 @@ private void dfs(int i1, int i2, int diff) {
10589
}
10690
memo[i1][i2][diff + 999] = stringMatched;
10791
}
92+
93+
private boolean extracted(
94+
int i1, int i2, int diff, List<int[]> indexNums1, List<int[]> indexNums2) {
95+
char c1 = s1.charAt(i1);
96+
char c2 = s2.charAt(i2);
97+
if (Character.isLetter(c1) && Character.isLetter(c2)) {
98+
if (c1 != c2) {
99+
return true;
100+
}
101+
dfs(i1 + 1, i2 + 1, diff);
102+
return true;
103+
} else {
104+
if (!indexNums1.isEmpty()) {
105+
for (int[] num1Item : indexNums1) {
106+
dfs(num1Item[0] + 1, i2, diff + num1Item[1]);
107+
}
108+
} else {
109+
for (int[] num2Item : indexNums2) {
110+
dfs(i1, num2Item[0] + 1, diff - num2Item[1]);
111+
}
112+
}
113+
}
114+
return false;
115+
}
108116
}

src/main/java/g2201_2300/s2257_count_unguarded_cells_in_the_grid/Solution.java

Lines changed: 48 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -19,48 +19,16 @@ public int countUnguarded(int m, int n, int[][] guards, int[][] walls) {
1919
for (int i = 0; i <= guards.length - 1; i++) {
2020
int currentRow = guards[i][0];
2121
int currentCol = guards[i][1] - 1;
22-
while (currentCol >= 0) {
23-
if (matrix[currentRow][currentCol] != 'W'
24-
&& matrix[currentRow][currentCol] != 'G') {
25-
matrix[currentRow][currentCol] = 'R';
26-
} else {
27-
break;
28-
}
29-
currentCol--;
30-
}
22+
extracted(matrix, currentRow, currentCol);
3123
currentRow = guards[i][0];
3224
currentCol = guards[i][1] + 1;
33-
while (currentCol <= n - 1) {
34-
if (matrix[currentRow][currentCol] != 'W'
35-
&& matrix[currentRow][currentCol] != 'G') {
36-
matrix[currentRow][currentCol] = 'R';
37-
} else {
38-
break;
39-
}
40-
currentCol++;
41-
}
25+
extracted(n, matrix, currentRow, currentCol);
4226
currentRow = guards[i][0] - 1;
4327
currentCol = guards[i][1];
44-
while (currentRow >= 0) {
45-
if (matrix[currentRow][currentCol] != 'W'
46-
&& matrix[currentRow][currentCol] != 'G') {
47-
matrix[currentRow][currentCol] = 'R';
48-
} else {
49-
break;
50-
}
51-
currentRow--;
52-
}
28+
extracted1(matrix, currentRow, currentCol);
5329
currentRow = guards[i][0] + 1;
5430
currentCol = guards[i][1];
55-
while (currentRow <= m - 1) {
56-
if (matrix[currentRow][currentCol] != 'W'
57-
&& matrix[currentRow][currentCol] != 'G') {
58-
matrix[currentRow][currentCol] = 'R';
59-
} else {
60-
break;
61-
}
62-
currentRow++;
63-
}
31+
extracted1(m, matrix, currentRow, currentCol);
6432
}
6533
for (int i = 0; i <= m - 1; i++) {
6634
for (int j = 0; j <= n - 1; j++) {
@@ -71,4 +39,48 @@ public int countUnguarded(int m, int n, int[][] guards, int[][] walls) {
7139
}
7240
return result;
7341
}
42+
43+
private void extracted1(int m, char[][] matrix, int currentRow, int currentCol) {
44+
while (currentRow <= m - 1) {
45+
if (matrix[currentRow][currentCol] != 'W' && matrix[currentRow][currentCol] != 'G') {
46+
matrix[currentRow][currentCol] = 'R';
47+
} else {
48+
break;
49+
}
50+
currentRow++;
51+
}
52+
}
53+
54+
private void extracted1(char[][] matrix, int currentRow, int currentCol) {
55+
while (currentRow >= 0) {
56+
if (matrix[currentRow][currentCol] != 'W' && matrix[currentRow][currentCol] != 'G') {
57+
matrix[currentRow][currentCol] = 'R';
58+
} else {
59+
break;
60+
}
61+
currentRow--;
62+
}
63+
}
64+
65+
private void extracted(int n, char[][] matrix, int currentRow, int currentCol) {
66+
while (currentCol <= n - 1) {
67+
if (matrix[currentRow][currentCol] != 'W' && matrix[currentRow][currentCol] != 'G') {
68+
matrix[currentRow][currentCol] = 'R';
69+
} else {
70+
break;
71+
}
72+
currentCol++;
73+
}
74+
}
75+
76+
private void extracted(char[][] matrix, int currentRow, int currentCol) {
77+
while (currentCol >= 0) {
78+
if (matrix[currentRow][currentCol] != 'W' && matrix[currentRow][currentCol] != 'G') {
79+
matrix[currentRow][currentCol] = 'R';
80+
} else {
81+
break;
82+
}
83+
currentCol--;
84+
}
85+
}
7486
}

src/main/java/g2501_2600/s2538_difference_between_maximum_and_minimum_price_sum/Solution.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import java.util.ArrayList;
77

8+
@SuppressWarnings("unchecked")
89
public class Solution {
910
private ArrayList<Integer>[] tree;
1011
private int[] price;

0 commit comments

Comments
 (0)