Skip to content

Commit 47eff80

Browse files
authored
Added tasks 2017, 2018, 2019, 2022, 2023.
1 parent 9b93fc0 commit 47eff80

File tree

15 files changed

+664
-0
lines changed

15 files changed

+664
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package g2001_2100.s2017_grid_game;
2+
3+
// #Medium #Array #Matrix #Prefix_Sum #2022_05_24_Time_10_ms_(41.46%)_Space_95.2_MB_(32.32%)
4+
5+
public class Solution {
6+
public long gridGame(int[][] grid) {
7+
int n = grid[0].length;
8+
long[] cum0 = new long[n + 1];
9+
long[] cum1 = new long[n + 1];
10+
for (int i = 0; i < n; i++) {
11+
cum0[i + 1] = cum0[i] + grid[0][i];
12+
cum1[i + 1] = cum1[i] + grid[1][i];
13+
}
14+
long ans = Long.MAX_VALUE;
15+
for (int i = 0; i < n; i++) {
16+
ans = Math.min(ans, Math.max(cum0[n] - cum0[i + 1], cum1[i]));
17+
}
18+
return ans;
19+
}
20+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
2017\. Grid Game
2+
3+
Medium
4+
5+
You are given a **0-indexed** 2D array `grid` of size `2 x n`, where `grid[r][c]` represents the number of points at position `(r, c)` on the matrix. Two robots are playing a game on this matrix.
6+
7+
Both robots initially start at `(0, 0)` and want to reach `(1, n-1)`. Each robot may only move to the **right** (`(r, c)` to `(r, c + 1)`) or **down** (`(r, c)` to `(r + 1, c)`).
8+
9+
At the start of the game, the **first** robot moves from `(0, 0)` to `(1, n-1)`, collecting all the points from the cells on its path. For all cells `(r, c)` traversed on the path, `grid[r][c]` is set to `0`. Then, the **second** robot moves from `(0, 0)` to `(1, n-1)`, collecting the points on its path. Note that their paths may intersect with one another.
10+
11+
The **first** robot wants to **minimize** the number of points collected by the **second** robot. In contrast, the **second** robot wants to **maximize** the number of points it collects. If both robots play **optimally**, return _the **number of points** collected by the **second** robot._
12+
13+
**Example 1:**
14+
15+
![](https://assets.leetcode.com/uploads/2021/09/08/a1.png)
16+
17+
**Input:** grid = [[2,5,4],[1,5,1]]
18+
19+
**Output:** 4
20+
21+
**Explanation:** The optimal path taken by the first robot is shown in red, and the optimal path taken by the second robot is shown in blue.
22+
23+
The cells visited by the first robot are set to 0.
24+
25+
The second robot will collect 0 + 0 + 4 + 0 = 4 points.
26+
27+
**Example 2:**
28+
29+
![](https://assets.leetcode.com/uploads/2021/09/08/a2.png)
30+
31+
**Input:** grid = [[3,3,1],[8,5,2]]
32+
33+
**Output:** 4
34+
35+
**Explanation:** The optimal path taken by the first robot is shown in red, and the optimal path taken by the second robot is shown in blue.
36+
37+
The cells visited by the first robot are set to 0.
38+
39+
The second robot will collect 0 + 3 + 1 + 0 = 4 points.
40+
41+
**Example 3:**
42+
43+
![](https://assets.leetcode.com/uploads/2021/09/08/a3.png)
44+
45+
**Input:** grid = [[1,3,1,15],[1,3,3,1]]
46+
47+
**Output:** 7
48+
49+
**Explanation:** The optimal path taken by the first robot is shown in red, and the optimal path taken by the second robot is shown in blue.
50+
51+
The cells visited by the first robot are set to 0.
52+
53+
The second robot will collect 0 + 1 + 3 + 3 + 0 = 7 points.
54+
55+
**Constraints:**
56+
57+
* `grid.length == 2`
58+
* `n == grid[r].length`
59+
* <code>1 <= n <= 5 * 10<sup>4</sup></code>
60+
* <code>1 <= grid[r][c] <= 10<sup>5</sup></code>
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package g2001_2100.s2018_check_if_word_can_be_placed_in_crossword;
2+
3+
// #Medium #Array #Matrix #Enumeration #2022_05_24_Time_11_ms_(52.76%)_Space_106.7_MB_(34.26%)
4+
5+
public class Solution {
6+
public boolean placeWordInCrossword(char[][] board, String word) {
7+
int m = board.length;
8+
int n = board[0].length;
9+
for (int i = 0; i < m; i++) {
10+
for (int j = 0; j < n; j++) {
11+
if ((board[i][j] == ' ' || board[i][j] == word.charAt(0))
12+
&& (canPlaceTopDown(word, board, i, j)
13+
|| canPlaceLeftRight(word, board, i, j)
14+
|| canPlaceBottomUp(word, board, i, j)
15+
|| canPlaceRightLeft(word, board, i, j))) {
16+
return true;
17+
}
18+
}
19+
}
20+
return false;
21+
}
22+
23+
private boolean canPlaceRightLeft(String word, char[][] board, int row, int col) {
24+
if (col + 1 < board[0].length
25+
&& (Character.isLowerCase(board[row][col + 1]) || board[row][col + 1] == ' ')) {
26+
return false;
27+
}
28+
int k = 0;
29+
int j = col;
30+
for (; j >= 0 && k < word.length(); j--) {
31+
if (board[row][j] != word.charAt(k) && board[row][j] != ' ') {
32+
return false;
33+
} else {
34+
k++;
35+
}
36+
}
37+
return k == word.length() && (j < 0 || board[row][j] == '#');
38+
}
39+
40+
private boolean canPlaceBottomUp(String word, char[][] board, int row, int col) {
41+
if (row + 1 < board.length
42+
&& (Character.isLowerCase(board[row + 1][col]) || board[row + 1][col] == ' ')) {
43+
return false;
44+
}
45+
int k = 0;
46+
int i = row;
47+
for (; i >= 0 && k < word.length(); i--) {
48+
if (board[i][col] != word.charAt(k) && board[i][col] != ' ') {
49+
return false;
50+
} else {
51+
k++;
52+
}
53+
}
54+
return k == word.length() && (i < 0 || board[i][col] == '#');
55+
}
56+
57+
private boolean canPlaceLeftRight(String word, char[][] board, int row, int col) {
58+
if (col > 0 && (Character.isLowerCase(board[row][col - 1]) || board[row][col - 1] == ' ')) {
59+
return false;
60+
}
61+
int k = 0;
62+
int j = col;
63+
for (; j < board[0].length && k < word.length(); j++) {
64+
if (board[row][j] != word.charAt(k) && board[row][j] != ' ') {
65+
return false;
66+
} else {
67+
k++;
68+
}
69+
}
70+
return k == word.length() && (j == board[0].length || board[row][j] == '#');
71+
}
72+
73+
private boolean canPlaceTopDown(String word, char[][] board, int row, int col) {
74+
if (row > 0 && (Character.isLowerCase(board[row - 1][col]) || board[row - 1][col] == ' ')) {
75+
return false;
76+
}
77+
int k = 0;
78+
int i = row;
79+
for (; i < board.length && k < word.length(); i++) {
80+
if (board[i][col] != word.charAt(k) && board[i][col] != ' ') {
81+
return false;
82+
} else {
83+
k++;
84+
}
85+
}
86+
return k == word.length() && (i == board.length || board[i][col] == '#');
87+
}
88+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
2018\. Check if Word Can Be Placed In Crossword
2+
3+
Medium
4+
5+
You are given an `m x n` matrix `board`, representing the **current** state of a crossword puzzle. The crossword contains lowercase English letters (from solved words), `' '` to represent any **empty** cells, and `'#'` to represent any **blocked** cells.
6+
7+
A word can be placed **horizontally** (left to right **or** right to left) or **vertically** (top to bottom **or** bottom to top) in the board if:
8+
9+
* It does not occupy a cell containing the character `'#'`.
10+
* The cell each letter is placed in must either be `' '` (empty) or **match** the letter already on the `board`.
11+
* There must not be any empty cells `' '` or other lowercase letters **directly left or right** of the word if the word was placed **horizontally**.
12+
* There must not be any empty cells `' '` or other lowercase letters **directly above or below** the word if the word was placed **vertically**.
13+
14+
Given a string `word`, return `true` _if_ `word` _can be placed in_ `board`_, or_ `false` _**otherwise**_.
15+
16+
**Example 1:**
17+
18+
![](https://assets.leetcode.com/uploads/2021/10/04/crossword-ex1-1.png)
19+
20+
**Input:** board = [["#", " ", "#"], [" ", " ", "#"], ["#", "c", " "]], word = "abc"
21+
22+
**Output:** true
23+
24+
**Explanation:** The word "abc" can be placed as shown above (top to bottom).
25+
26+
**Example 2:**
27+
28+
![](https://assets.leetcode.com/uploads/2021/10/04/crossword-ex2-1.png)
29+
30+
**Input:** board = [[" ", "#", "a"], [" ", "#", "c"], [" ", "#", "a"]], word = "ac"
31+
32+
**Output:** false
33+
34+
**Explanation:** It is impossible to place the word because there will always be a space/letter above or below it.
35+
36+
**Example 3:**
37+
38+
![](https://assets.leetcode.com/uploads/2021/10/04/crossword-ex3-1.png)
39+
40+
**Input:** board = [["#", " ", "#"], [" ", " ", "#"], ["#", " ", "c"]], word = "ca"
41+
42+
**Output:** true
43+
44+
**Explanation:** The word "ca" can be placed as shown above (right to left).
45+
46+
**Constraints:**
47+
48+
* `m == board.length`
49+
* `n == board[i].length`
50+
* <code>1 <= m * n <= 2 * 10<sup>5</sup></code>
51+
* `board[i][j]` will be `' '`, `'#'`, or a lowercase English letter.
52+
* `1 <= word.length <= max(m, n)`
53+
* `word` will contain only lowercase English letters.
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package g2001_2100.s2019_the_score_of_students_solving_math_expression;
2+
3+
// #Hard #Array #String #Dynamic_Programming #Math #Stack #Memoization
4+
// #2022_05_24_Time_435_ms_(89.93%)_Space_47_MB_(87.77%)
5+
6+
import java.util.ArrayDeque;
7+
import java.util.HashSet;
8+
9+
public class Solution {
10+
private HashSet<?>[][] dp;
11+
12+
public int scoreOfStudents(String s, int[] answers) {
13+
ArrayDeque<Integer> st = new ArrayDeque<>();
14+
int n = s.length();
15+
int i = 0;
16+
dp = new HashSet<?>[n][n];
17+
while (i < n) {
18+
if (s.charAt(i) - '0' >= 0 && s.charAt(i) - '9' <= 0) {
19+
st.push(s.charAt(i) - '0');
20+
i++;
21+
} else if (s.charAt(i) == '*') {
22+
int cur = st.pop() * (s.charAt(i + 1) - '0');
23+
i += 2;
24+
st.push(cur);
25+
} else {
26+
i++;
27+
}
28+
}
29+
int res = 0;
30+
int ret = 0;
31+
while (!st.isEmpty()) {
32+
res += st.pop();
33+
}
34+
HashSet<Integer> wrong = opts(0, n - 1, s);
35+
for (int ans : answers) {
36+
if (ans == res) {
37+
ret += 5;
38+
} else if (wrong.contains(ans)) {
39+
ret += 2;
40+
}
41+
}
42+
return ret;
43+
}
44+
45+
private HashSet<Integer> opts(int i, int j, String s) {
46+
if (dp[i][j] != null) {
47+
return (HashSet<Integer>) dp[i][j];
48+
}
49+
if (i == j) {
50+
HashSet<Integer> res = new HashSet<>();
51+
res.add(s.charAt(i) - '0');
52+
dp[i][j] = res;
53+
return res;
54+
}
55+
HashSet<Integer> res = new HashSet<>();
56+
for (int x = i + 1; x < j; x += 2) {
57+
char op = s.charAt(x);
58+
HashSet<Integer> left = opts(i, x - 1, s);
59+
HashSet<Integer> right = opts(x + 1, j, s);
60+
if (op == '*') {
61+
for (int l : left) {
62+
for (int r : right) {
63+
if (l * r <= 1000) {
64+
res.add(l * r);
65+
}
66+
}
67+
}
68+
} else {
69+
for (int l : left) {
70+
for (int r : right) {
71+
if (l + r <= 1000) {
72+
res.add(l + r);
73+
}
74+
}
75+
}
76+
}
77+
}
78+
dp[i][j] = res;
79+
return res;
80+
}
81+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
2019\. The Score of Students Solving Math Expression
2+
3+
Hard
4+
5+
You are given a string `s` that contains digits `0-9`, addition symbols `'+'`, and multiplication symbols `'*'` **only**, representing a **valid** math expression of **single digit numbers** (e.g., `3+5*2`). This expression was given to `n` elementary school students. The students were instructed to get the answer of the expression by following this **order of operations**:
6+
7+
1. Compute **multiplication**, reading from **left to right**; Then,
8+
2. Compute **addition**, reading from **left to right**.
9+
10+
You are given an integer array `answers` of length `n`, which are the submitted answers of the students in no particular order. You are asked to grade the `answers`, by following these **rules**:
11+
12+
* If an answer **equals** the correct answer of the expression, this student will be rewarded `5` points;
13+
* Otherwise, if the answer **could be interpreted** as if the student applied the operators **in the wrong order** but had **correct arithmetic**, this student will be rewarded `2` points;
14+
* Otherwise, this student will be rewarded `0` points.
15+
16+
Return _the sum of the points of the students_.
17+
18+
**Example 1:**
19+
20+
![](https://assets.leetcode.com/uploads/2021/09/17/student_solving_math.png)
21+
22+
**Input:** s = "7+3\*1\*2", answers = [20,13,42]
23+
24+
**Output:** 7
25+
26+
**Explanation:** As illustrated above, the correct answer of the expression is 13, therefore one student is rewarded 5 points: [20,**13**,42]
27+
28+
A student might have applied the operators in this wrong order: ((7+3)\*1)\*2 = 20. Therefore one student is rewarded 2 points: [**20**,13,42]
29+
30+
The points for the students are: [2,5,0]. The sum of the points is 2+5+0=7.
31+
32+
**Example 2:**
33+
34+
**Input:** s = "3+5\*2", answers = [13,0,10,13,13,16,16]
35+
36+
**Output:** 19
37+
38+
**Explanation:** The correct answer of the expression is 13, therefore three students are rewarded 5 points each: [**13**,0,10,**13**,**13**,16,16]
39+
40+
A student might have applied the operators in this wrong order: ((3+5)\*2 = 16. Therefore two students are rewarded 2 points: [13,0,10,13,13,**16**,**16**]
41+
42+
The points for the students are: [5,0,0,5,5,2,2]. The sum of the points is 5+0+0+5+5+2+2=19.
43+
44+
**Example 3:**
45+
46+
**Input:** s = "6+0\*1", answers = [12,9,6,4,8,6]
47+
48+
**Output:** 10
49+
50+
**Explanation:** The correct answer of the expression is 6.
51+
52+
If a student had incorrectly done (6+0)\*1, the answer would also be 6.
53+
54+
By the rules of grading, the students will still be rewarded 5 points (as they got the correct answer), not 2 points.
55+
56+
The points for the students are: [0,0,5,0,0,5]. The sum of the points is 10.
57+
58+
**Constraints:**
59+
60+
* `3 <= s.length <= 31`
61+
* `s` represents a valid expression that contains only digits `0-9`, `'+'`, and `'*'` only.
62+
* All the integer operands in the expression are in the **inclusive** range `[0, 9]`.
63+
* `1 <=` The count of all operators (`'+'` and `'*'`) in the math expression `<= 15`
64+
* Test data are generated such that the correct answer of the expression is in the range of `[0, 1000]`.
65+
* `n == answers.length`
66+
* <code>1 <= n <= 10<sup>4</sup></code>
67+
* `0 <= answers[i] <= 1000`
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package g2001_2100.s2022_convert_1d_array_into_2d_array;
2+
3+
// #Easy #Array #Matrix #Simulation #2022_05_25_Time_8_ms_(40.41%)_Space_119.4_MB_(53.92%)
4+
5+
public class Solution {
6+
public int[][] construct2DArray(int[] original, int m, int n) {
7+
int size = original.length;
8+
if (m * n != size) {
9+
return new int[][] {};
10+
}
11+
int[][] ans = new int[m][n];
12+
int k = 0;
13+
for (int i = 0; i < m; i++) {
14+
for (int j = 0; j < n; j++) {
15+
ans[i][j] = original[k++];
16+
}
17+
}
18+
return ans;
19+
}
20+
}

0 commit comments

Comments
 (0)