Skip to content

Commit 6a78076

Browse files
authored
Added tasks 733, 735, 736, 738, 740.
1 parent c6a2b0e commit 6a78076

File tree

15 files changed

+493
-0
lines changed

15 files changed

+493
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package g0701_0800.s0733_flood_fill;
2+
3+
// #Easy #Array #Depth_First_Search #Breadth_First_Search #Matrix
4+
5+
public class Solution {
6+
public int[][] floodFill(int[][] image, int sr, int sc, int newColor) {
7+
int o = image[sr][sc];
8+
helper(image, sr, sc, newColor, o);
9+
return image;
10+
}
11+
12+
private void helper(int[][] img, int r, int c, int n, int o) {
13+
if (r >= img.length
14+
|| c >= img[0].length
15+
|| r < 0
16+
|| c < 0
17+
|| img[r][c] == n
18+
|| img[r][c] != o) {
19+
return;
20+
}
21+
img[r][c] = n;
22+
helper(img, r + 1, c, n, o);
23+
helper(img, r - 1, c, n, o);
24+
helper(img, r, c + 1, n, o);
25+
helper(img, r, c - 1, n, o);
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
733\. Flood Fill
2+
3+
Easy
4+
5+
An image is represented by an `m x n` integer grid `image` where `image[i][j]` represents the pixel value of the image.
6+
7+
You are also given three integers `sr`, `sc`, and `newColor`. You should perform a **flood fill** on the image starting from the pixel `image[sr][sc]`.
8+
9+
To perform a **flood fill**, consider the starting pixel, plus any pixels connected **4-directionally** to the starting pixel of the same color as the starting pixel, plus any pixels connected **4-directionally** to those pixels (also with the same color), and so on. Replace the color of all of the aforementioned pixels with `newColor`.
10+
11+
Return _the modified image after performing the flood fill_.
12+
13+
**Example 1:**
14+
15+
![](https://assets.leetcode.com/uploads/2021/06/01/flood1-grid.jpg)
16+
17+
**Input:** image = [[1,1,1],[1,1,0],[1,0,1]], sr = 1, sc = 1, newColor = 2
18+
19+
**Output:** [[2,2,2],[2,2,0],[2,0,1]]
20+
21+
**Explanation:** From the center of the image with position (sr, sc) = (1, 1) (i.e., the red pixel), all pixels connected by a path of the same color as the starting pixel (i.e., the blue pixels) are colored with the new color. Note the bottom corner is not colored 2, because it is not 4-directionally connected to the starting pixel.
22+
23+
**Example 2:**
24+
25+
**Input:** image = [[0,0,0],[0,0,0]], sr = 0, sc = 0, newColor = 2
26+
27+
**Output:** [[2,2,2],[2,2,2]]
28+
29+
**Constraints:**
30+
31+
* `m == image.length`
32+
* `n == image[i].length`
33+
* `1 <= m, n <= 50`
34+
* <code>0 <= image[i][j], newColor < 2<sup>16</sup></code>
35+
* `0 <= sr < m`
36+
* `0 <= sc < n`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package g0701_0800.s0735_asteroid_collision;
2+
3+
// #Medium #Array #Stack
4+
5+
import java.util.Deque;
6+
import java.util.LinkedList;
7+
8+
public class Solution {
9+
public int[] asteroidCollision(int[] asteroids) {
10+
Deque<Integer> stack = new LinkedList<>();
11+
for (int a : asteroids) {
12+
if (a > 0) {
13+
stack.addLast(a);
14+
} else {
15+
if (!stack.isEmpty() && stack.peekLast() > 0) {
16+
if (stack.peekLast() == Math.abs(a)) {
17+
stack.pollLast();
18+
} else {
19+
while (!stack.isEmpty()
20+
&& stack.peekLast() > 0
21+
&& stack.peekLast() < Math.abs(a)) {
22+
stack.pollLast();
23+
}
24+
if (!stack.isEmpty()
25+
&& stack.peekLast() > 0
26+
&& stack.peekLast() == Math.abs(a)) {
27+
stack.pollLast();
28+
} else if (stack.isEmpty() || stack.peekLast() < 0) {
29+
stack.addLast(a);
30+
}
31+
}
32+
} else {
33+
stack.addLast(a);
34+
}
35+
}
36+
}
37+
int[] ans = new int[stack.size()];
38+
for (int i = stack.size() - 1; i >= 0; i--) {
39+
ans[i] = stack.pollLast();
40+
}
41+
return ans;
42+
}
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
735\. Asteroid Collision
2+
3+
Medium
4+
5+
We are given an array `asteroids` of integers representing asteroids in a row.
6+
7+
For each asteroid, the absolute value represents its size, and the sign represents its direction (positive meaning right, negative meaning left). Each asteroid moves at the same speed.
8+
9+
Find out the state of the asteroids after all collisions. If two asteroids meet, the smaller one will explode. If both are the same size, both will explode. Two asteroids moving in the same direction will never meet.
10+
11+
**Example 1:**
12+
13+
**Input:** asteroids = [5,10,-5]
14+
15+
**Output:** [5,10]
16+
17+
**Explanation:** The 10 and -5 collide resulting in 10. The 5 and 10 never collide.
18+
19+
**Example 2:**
20+
21+
**Input:** asteroids = [8,-8]
22+
23+
**Output:** []
24+
25+
**Explanation:** The 8 and -8 collide exploding each other.
26+
27+
**Example 3:**
28+
29+
**Input:** asteroids = [10,2,-5]
30+
31+
**Output:** [10]
32+
33+
**Explanation:** The 2 and -5 collide resulting in -5. The 10 and -5 collide resulting in 10.
34+
35+
**Constraints:**
36+
37+
* <code>2 <= asteroids.length <= 10<sup>4</sup></code>
38+
* `-1000 <= asteroids[i] <= 1000`
39+
* `asteroids[i] != 0`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package g0701_0800.s0736_parse_lisp_expression;
2+
3+
// #Hard #String #Hash_Table #Stack #Recursion
4+
5+
import java.util.Deque;
6+
import java.util.HashMap;
7+
import java.util.LinkedList;
8+
import java.util.Map;
9+
10+
public class Solution {
11+
static class Exp {
12+
Deque<Exp> exps;
13+
String op;
14+
Exp parent;
15+
16+
public Exp(Exp from) {
17+
this.exps = new LinkedList<>();
18+
this.parent = from;
19+
}
20+
21+
private int evaluate(Map<String, Integer> vars) {
22+
if (op.equalsIgnoreCase("add")) {
23+
return exps.pop().evaluate(vars) + exps.pop().evaluate(vars);
24+
} else if (op.equalsIgnoreCase("mult")) {
25+
return exps.pop().evaluate(vars) * exps.pop().evaluate(vars);
26+
} else if (op.equalsIgnoreCase("let")) {
27+
Map<String, Integer> nextVars = new HashMap<>(vars);
28+
while (exps.size() > 1) {
29+
String varName = exps.pop().op;
30+
int val = exps.pop().evaluate(nextVars);
31+
nextVars.put(varName, val);
32+
}
33+
return exps.pop().evaluate(nextVars);
34+
} else {
35+
if (Character.isLetter(op.charAt(0))) {
36+
return vars.get(op);
37+
} else {
38+
return Integer.parseInt(op);
39+
}
40+
}
41+
}
42+
}
43+
44+
private Exp buildTree(String exp) {
45+
Exp root = new Exp(null);
46+
Exp cur = root;
47+
int n = exp.length() - 1;
48+
while (n >= 0) {
49+
char c = exp.charAt(n);
50+
if (c == ')') {
51+
Exp next = new Exp(cur);
52+
cur.exps.push(next);
53+
cur = next;
54+
} else if (c == '(') {
55+
cur.op = cur.exps.pop().op;
56+
cur = cur.parent;
57+
} else if (c != ' ') {
58+
int pre = n;
59+
while (pre >= 0 && exp.charAt(pre) != '(' && exp.charAt(pre) != ' ') {
60+
pre--;
61+
}
62+
Exp next = new Exp(cur);
63+
next.op = exp.substring(pre + 1, n + 1);
64+
cur.exps.push(next);
65+
n = pre + 1;
66+
}
67+
n--;
68+
}
69+
return root.exps.pop();
70+
}
71+
72+
public int evaluate(String exp) {
73+
return buildTree(exp).evaluate(new HashMap<>());
74+
}
75+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
736\. Parse Lisp Expression
2+
3+
Hard
4+
5+
You are given a string expression representing a Lisp-like expression to return the integer value of.
6+
7+
The syntax for these expressions is given as follows.
8+
9+
* An expression is either an integer, let expression, add expression, mult expression, or an assigned variable. Expressions always evaluate to a single integer.
10+
* (An integer could be positive or negative.)
11+
* A let expression takes the form <code>"(let v<sub>1</sub> e<sub>1</sub> v<sub>2</sub> e<sub>2</sub> ... v<sub>n</sub> e<sub>n</sub> expr)"</code>, where let is always the string `"let"`, then there are one or more pairs of alternating variables and expressions, meaning that the first variable <code>v<sub>1</sub></code> is assigned the value of the expression <code>e<sub>1</sub></code>, the second variable <code>v<sub>2</sub></code> is assigned the value of the expression <code>e<sub>2</sub></code>, and so on sequentially; and then the value of this let expression is the value of the expression `expr`.
12+
* An add expression takes the form <code>"(add e<sub>1</sub> e<sub>2</sub>)"</code> where add is always the string `"add"`, there are always two expressions <code>e<sub>1</sub></code>, <code>e<sub>2</sub></code> and the result is the addition of the evaluation of <code>e<sub>1</sub></code> and the evaluation of <code>e<sub>2</sub></code>.
13+
* A mult expression takes the form <code>"(mult e<sub>1</sub> e<sub>2</sub>)"</code> where mult is always the string `"mult"`, there are always two expressions <code>e<sub>1</sub></code>, <code>e<sub>2</sub></code> and the result is the multiplication of the evaluation of e1 and the evaluation of e2.
14+
* For this question, we will use a smaller subset of variable names. A variable starts with a lowercase letter, then zero or more lowercase letters or digits. Additionally, for your convenience, the names `"add"`, `"let"`, and `"mult"` are protected and will never be used as variable names.
15+
* Finally, there is the concept of scope. When an expression of a variable name is evaluated, within the context of that evaluation, the innermost scope (in terms of parentheses) is checked first for the value of that variable, and then outer scopes are checked sequentially. It is guaranteed that every expression is legal. Please see the examples for more details on the scope.
16+
17+
**Example 1:**
18+
19+
**Input:** expression = "(let x 2 (mult x (let x 3 y 4 (add x y))))"
20+
21+
**Output:** 14
22+
23+
**Explanation:** In the expression (add x y), when checking for the value of the variable x, we check from the innermost scope to the outermost in the context of the variable we are trying to evaluate. Since x = 3 is found first, the value of x is 3.
24+
25+
**Example 2:**
26+
27+
**Input:** expression = "(let x 3 x 2 x)"
28+
29+
**Output:** 2
30+
31+
**Explanation:** Assignment in let statements is processed sequentially.
32+
33+
**Example 3:**
34+
35+
**Input:** expression = "(let x 1 y 2 x (add x y) (add x y))"
36+
37+
**Output:** 5
38+
39+
**Explanation:** The first (add x y) evaluates as 3, and is assigned to x. The second (add x y) evaluates as 3+2 = 5.
40+
41+
**Constraints:**
42+
43+
* `1 <= expression.length <= 2000`
44+
* There are no leading or trailing spaces in `expression`.
45+
* All tokens are separated by a single space in `expression`.
46+
* The answer and all intermediate calculations of that answer are guaranteed to fit in a **32-bit** integer.
47+
* The expression is guaranteed to be legal and evaluate to an integer.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package g0701_0800.s0738_monotone_increasing_digits;
2+
3+
// #Medium #Math #Greedy
4+
5+
public class Solution {
6+
public int monotoneIncreasingDigits(int n) {
7+
for (int i = 10; n / i > 0; i *= 10) {
8+
int digit = (n / i) % 10;
9+
int endnum = n % i;
10+
int firstendnum = endnum * 10 / i;
11+
if (digit > firstendnum) {
12+
n -= endnum + 1;
13+
}
14+
}
15+
return (n);
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
738\. Monotone Increasing Digits
2+
3+
Medium
4+
5+
An integer has **monotone increasing digits** if and only if each pair of adjacent digits `x` and `y` satisfy `x <= y`.
6+
7+
Given an integer `n`, return _the largest number that is less than or equal to_ `n` _with **monotone increasing digits**_.
8+
9+
**Example 1:**
10+
11+
**Input:** n = 10
12+
13+
**Output:** 9
14+
15+
**Example 2:**
16+
17+
**Input:** n = 1234
18+
19+
**Output:** 1234
20+
21+
**Example 3:**
22+
23+
**Input:** n = 332
24+
25+
**Output:** 299
26+
27+
**Constraints:**
28+
29+
* <code>0 <= n <= 10<sup>9</sup></code>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g0701_0800.s0740_delete_and_earn;
2+
3+
// #Medium #Array #Hash_Table #Dynamic_Programming
4+
5+
public class Solution {
6+
public int deleteAndEarn(int[] nums) {
7+
int[] sum = new int[10001];
8+
int min = 10001;
9+
int max = 0;
10+
for (int num : nums) {
11+
sum[num] += num;
12+
min = Math.min(num, min);
13+
max = Math.max(num, max);
14+
}
15+
16+
int[] dp = new int[max + 1];
17+
dp[min] = sum[min];
18+
for (int i = min + 1; i <= max; i++) {
19+
dp[i] = Math.max(dp[i - 1], dp[i - 2] + sum[i]);
20+
}
21+
return dp[max];
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
740\. Delete and Earn
2+
3+
Medium
4+
5+
You are given an integer array `nums`. You want to maximize the number of points you get by performing the following operation any number of times:
6+
7+
* Pick any `nums[i]` and delete it to earn `nums[i]` points. Afterwards, you must delete **every** element equal to `nums[i] - 1` and **every** element equal to `nums[i] + 1`.
8+
9+
Return _the **maximum number of points** you can earn by applying the above operation some number of times_.
10+
11+
**Example 1:**
12+
13+
**Input:** nums = [3,4,2]
14+
15+
**Output:** 6
16+
17+
**Explanation:** You can perform the following operations:
18+
19+
- Delete 4 to earn 4 points. Consequently, 3 is also deleted. nums = [2].
20+
21+
- Delete 2 to earn 2 points. nums = [].
22+
23+
You earn a total of 6 points.
24+
25+
**Example 2:**
26+
27+
**Input:** nums = [2,2,3,3,3,4]
28+
29+
**Output:** 9
30+
31+
**Explanation:** You can perform the following operations:
32+
33+
- Delete a 3 to earn 3 points. All 2's and 4's are also deleted. nums = [3,3].
34+
35+
- Delete a 3 again to earn 3 points. nums = [3].
36+
37+
- Delete a 3 once more to earn 3 points. nums = [].
38+
39+
You earn a total of 9 points.
40+
41+
**Constraints:**
42+
43+
* <code>1 <= nums.length <= 2 * 10<sup>4</sup></code>
44+
* <code>1 <= nums[i] <= 10<sup>4</sup></code>

0 commit comments

Comments
 (0)