Skip to content

Commit 874914b

Browse files
authored
Added tasks 1036, 1037, 1038, 1039, 1040.
1 parent 4c67c5f commit 874914b

File tree

15 files changed

+462
-0
lines changed

15 files changed

+462
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package g1001_1100.s1036_escape_a_large_maze;
2+
3+
// #Hard #Array #Hash_Table #Depth_First_Search #Breadth_First_Search
4+
// #2022_02_27_Time_115_ms_(73.46%)_Space_79.4_MB_(60.66%)
5+
6+
import java.util.HashSet;
7+
import java.util.Set;
8+
9+
public class Solution {
10+
public boolean isEscapePossible(int[][] blocked, int[] source, int[] target) {
11+
if (blocked.length == 0) {
12+
return true;
13+
}
14+
Set<Integer> blocks = new HashSet<>();
15+
for (int[] b : blocked) {
16+
if (target[0] * 1000000 + target[1] != b[0] * 1000000 + b[1]) {
17+
blocks.add(b[0] * 1000000 + b[1]);
18+
}
19+
}
20+
return dfs(blocks, source, source[0], source[1], new HashSet<>(), target)
21+
&& dfs(blocks, target, target[0], target[1], new HashSet<>(), source);
22+
}
23+
24+
private boolean dfs(
25+
Set<Integer> blocks, int[] start, int i, int j, Set<Integer> visited, int[] target) {
26+
if (i < 0
27+
|| j < 0
28+
|| i > 999999
29+
|| j > 999999
30+
|| blocks.contains(i * 1000000 + j)
31+
|| visited.contains(i * 1000000 + j)) {
32+
return false;
33+
}
34+
if (i == target[0] && j == target[1]) {
35+
return true;
36+
}
37+
visited.add(i * 1000000 + j);
38+
if (visited.size() > blocks.size() * (blocks.size() + 1)) {
39+
return true;
40+
}
41+
return dfs(blocks, start, i + 1, j, visited, target)
42+
|| dfs(blocks, start, i - 1, j, visited, target)
43+
|| dfs(blocks, start, i, j + 1, visited, target)
44+
|| dfs(blocks, start, i, j - 1, visited, target);
45+
}
46+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
1036\. Escape a Large Maze
2+
3+
Hard
4+
5+
There is a 1 million by 1 million grid on an XY-plane, and the coordinates of each grid square are `(x, y)`.
6+
7+
We start at the <code>source = [s<sub>x</sub>, s<sub>y</sub>]</code> square and want to reach the <code>target = [t<sub>x</sub>, t<sub>y</sub>]</code> square. There is also an array of `blocked` squares, where each <code>blocked[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents a blocked square with coordinates <code>(x<sub>i</sub>, y<sub>i</sub>)</code>.
8+
9+
Each move, we can walk one square north, east, south, or west if the square is **not** in the array of `blocked` squares. We are also not allowed to walk outside of the grid.
10+
11+
Return `true` _if and only if it is possible to reach the_ `target` _square from the_ `source` _square through a sequence of valid moves_.
12+
13+
**Example 1:**
14+
15+
**Input:** blocked = [[0,1],[1,0]], source = [0,0], target = [0,2]
16+
17+
**Output:** false
18+
19+
**Explanation:** The target square is inaccessible starting from the source square because we cannot move.
20+
21+
We cannot move north or east because those squares are blocked.
22+
23+
We cannot move south or west because we cannot go outside of the grid.
24+
25+
**Example 2:**
26+
27+
**Input:** blocked = [], source = [0,0], target = [999999,999999]
28+
29+
**Output:** true
30+
31+
**Explanation:** Because there are no blocked cells, it is possible to reach the target square.
32+
33+
**Constraints:**
34+
35+
* `0 <= blocked.length <= 200`
36+
* `blocked[i].length == 2`
37+
* <code>0 <= x<sub>i</sub>, y<sub>i</sub> < 10<sup>6</sup></code>
38+
* `source.length == target.length == 2`
39+
* <code>0 <= s<sub>x</sub>, s<sub>y</sub>, t<sub>x</sub>, t<sub>y</sub> < 10<sup>6</sup></code>
40+
* `source != target`
41+
* It is guaranteed that `source` and `target` are not blocked.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package g1001_1100.s1037_valid_boomerang;
2+
3+
// #Easy #Math #Geometry #2022_02_27_Time_0_ms_(100.00%)_Space_41.7_MB_(21.15%)
4+
5+
public class Solution {
6+
public boolean isBoomerang(int[][] points) {
7+
return (points[1][1] - points[0][1]) * (points[2][0] - points[0][0])
8+
!= (points[2][1] - points[0][1]) * (points[1][0] - points[0][0]);
9+
}
10+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
1037\. Valid Boomerang
2+
3+
Easy
4+
5+
Given an array `points` where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents a point on the **X-Y** plane, return `true` _if these points are a **boomerang**_.
6+
7+
A **boomerang** is a set of three points that are **all distinct** and **not in a straight line**.
8+
9+
**Example 1:**
10+
11+
**Input:** points = [[1,1],[2,3],[3,2]]
12+
13+
**Output:** true
14+
15+
**Example 2:**
16+
17+
**Input:** points = [[1,1],[2,2],[3,3]]
18+
19+
**Output:** false
20+
21+
**Constraints:**
22+
23+
* `points.length == 3`
24+
* `points[i].length == 2`
25+
* <code>0 <= x<sub>i</sub>, y<sub>i</sub> <= 100</code>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package g1001_1100.s1038_binary_search_tree_to_greater_sum_tree;
2+
3+
// #Medium #Depth_First_Search #Tree #Binary_Tree #Binary_Search_Tree
4+
// #2022_02_27_Time_0_ms_(100.00%)_Space_42.2_MB_(6.86%)
5+
6+
import com_github_leetcode.TreeNode;
7+
8+
public class Solution {
9+
private int greaterSum = 0;
10+
11+
public TreeNode bstToGst(TreeNode root) {
12+
if (root.right != null) {
13+
bstToGst(root.right);
14+
}
15+
greaterSum = root.val = greaterSum + root.val;
16+
if (root.left != null) {
17+
bstToGst(root.left);
18+
}
19+
return root;
20+
}
21+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
1037\. Valid Boomerang
2+
3+
Easy
4+
5+
Given an array `points` where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents a point on the **X-Y** plane, return `true` _if these points are a **boomerang**_.
6+
7+
A **boomerang** is a set of three points that are **all distinct** and **not in a straight line**.
8+
9+
**Example 1:**
10+
11+
**Input:** points = [[1,1],[2,3],[3,2]]
12+
13+
**Output:** true
14+
15+
**Example 2:**
16+
17+
**Input:** points = [[1,1],[2,2],[3,3]]
18+
19+
**Output:** false
20+
21+
**Constraints:**
22+
23+
* `points.length == 3`
24+
* `points[i].length == 2`
25+
* <code>0 <= x<sub>i</sub>, y<sub>i</sub> <= 100</code>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package g1001_1100.s1039_minimum_score_triangulation_of_polygon;
2+
3+
// #Medium #Array #Dynamic_Programming #2022_02_27_Time_6_ms_(38.60%)_Space_42.2_MB_(8.48%)
4+
5+
import java.util.Arrays;
6+
7+
public class Solution {
8+
9+
private static final int[][] DP = new int[101][101];
10+
11+
public int minScoreTriangulation(int[] values) {
12+
int n = values.length;
13+
for (int[] row : DP) {
14+
Arrays.fill(row, -1);
15+
}
16+
return util(values, 1, n - 1);
17+
}
18+
19+
private int util(int[] values, int i, int j) {
20+
if (i >= j) {
21+
return 0;
22+
}
23+
if (DP[i][j] != -1) {
24+
return DP[i][j];
25+
}
26+
27+
int ans = Integer.MAX_VALUE;
28+
29+
for (int k = i; k <= j - 1; k++) {
30+
int temp =
31+
util(values, i, k)
32+
+ util(values, k + 1, j)
33+
+ (values[i - 1] * values[k] * values[j]);
34+
35+
ans = Math.min(ans, temp);
36+
DP[i][j] = ans;
37+
}
38+
39+
return DP[i][j];
40+
}
41+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
1039\. Minimum Score Triangulation of Polygon
2+
3+
Medium
4+
5+
You have a convex `n`\-sided polygon where each vertex has an integer value. You are given an integer array `values` where `values[i]` is the value of the <code>i<sup>th</sup></code> vertex (i.e., **clockwise order**).
6+
7+
You will **triangulate** the polygon into `n - 2` triangles. For each triangle, the value of that triangle is the product of the values of its vertices, and the total score of the triangulation is the sum of these values over all `n - 2` triangles in the triangulation.
8+
9+
Return _the smallest possible total score that you can achieve with some triangulation of the polygon_.
10+
11+
**Example 1:**
12+
13+
![](https://assets.leetcode.com/uploads/2021/02/25/shape1.jpg)
14+
15+
**Input:** values = [1,2,3]
16+
17+
**Output:** 6
18+
19+
**Explanation:** The polygon is already triangulated, and the score of the only triangle is 6.
20+
21+
**Example 2:**
22+
23+
![](https://assets.leetcode.com/uploads/2021/02/25/shape2.jpg)
24+
25+
**Input:** values = [3,7,4,5]
26+
27+
**Output:** 144
28+
29+
**Explanation:** There are two triangulations, with possible scores: 3\*7\*5 + 4\*5\*7 = 245, or 3\*4\*5 + 3\*4\*7 = 144. The minimum score is 144.
30+
31+
**Example 3:**
32+
33+
![](https://assets.leetcode.com/uploads/2021/02/25/shape3.jpg)
34+
35+
**Input:** values = [1,3,1,4,1,5]
36+
37+
**Output:** 13
38+
39+
**Explanation:** The minimum score triangulation has score 1\*1\*3 + 1\*1\*4 + 1\*1\*5 + 1\*1\*1 = 13.
40+
41+
**Constraints:**
42+
43+
* `n == values.length`
44+
* `3 <= n <= 50`
45+
* `1 <= values[i] <= 100`
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package g1001_1100.s1040_moving_stones_until_consecutive_ii;
2+
3+
// #Medium #Array #Math #Sorting #Two_Pointers #2022_02_27_Time_8_ms_(55.00%)_Space_49_MB_(31.67%)
4+
5+
import java.util.Arrays;
6+
7+
public class Solution {
8+
public int[] numMovesStonesII(int[] stones) {
9+
int n = stones.length;
10+
int[] ans = new int[2];
11+
int i = 0;
12+
int j = 0;
13+
int wsize;
14+
int scount;
15+
int minMoves = Integer.MAX_VALUE;
16+
Arrays.sort(stones);
17+
while (j < n) {
18+
wsize = stones[j] - stones[i] + 1;
19+
scount = j - i + 1;
20+
if (wsize > n) {
21+
i++;
22+
continue;
23+
}
24+
25+
if (wsize == n - 1 && scount == n - 1) {
26+
minMoves = Math.min(minMoves, 2);
27+
} else {
28+
minMoves = Math.min(minMoves, n - scount);
29+
}
30+
31+
j++;
32+
}
33+
ans[0] = minMoves;
34+
int maxMoves = 0;
35+
if (stones[1] == stones[0] + 1 || stones[n - 1] == stones[n - 2] + 1) {
36+
maxMoves = stones[n - 1] - stones[0] + 1 - n;
37+
} else {
38+
maxMoves =
39+
Math.max(
40+
((stones[n - 1] - stones[1]) - (n - 1) + 1),
41+
((stones[n - 2] - stones[0]) - (n - 1) + 1));
42+
}
43+
44+
ans[1] = maxMoves;
45+
return ans;
46+
}
47+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
1040\. Moving Stones Until Consecutive II
2+
3+
Medium
4+
5+
There are some stones in different positions on the X-axis. You are given an integer array `stones`, the positions of the stones.
6+
7+
Call a stone an **endpoint stone** if it has the smallest or largest position. In one move, you pick up an **endpoint stone** and move it to an unoccupied position so that it is no longer an **endpoint stone**.
8+
9+
* In particular, if the stones are at say, `stones = [1,2,5]`, you cannot move the endpoint stone at position `5`, since moving it to any position (such as `0`, or `3`) will still keep that stone as an endpoint stone.
10+
11+
The game ends when you cannot make any more moves (i.e., the stones are in three consecutive positions).
12+
13+
Return _an integer array_ `answer` _of length_ `2` _where_:
14+
15+
* `answer[0]` _is the minimum number of moves you can play, and_
16+
* `answer[1]` _is the maximum number of moves you can play_.
17+
18+
**Example 1:**
19+
20+
**Input:** stones = [7,4,9]
21+
22+
**Output:** [1,2]
23+
24+
**Explanation:** We can move 4 -> 8 for one move to finish the game. Or, we can move 9 -> 5, 4 -> 6 for two moves to finish the game.
25+
26+
**Example 2:**
27+
28+
**Input:** stones = [6,5,4,3,10]
29+
30+
**Output:** [2,3]
31+
32+
**Explanation:** We can move 3 -> 8 then 10 -> 7 to finish the game.
33+
34+
Or, we can move 3 -> 7, 4 -> 8, 5 -> 9 to finish the game.
35+
36+
Notice we cannot move 10 -> 2 to finish the game, because that would be an illegal move.
37+
38+
**Constraints:**
39+
40+
* <code>3 <= stones.length <= 10<sup>4</sup></code>
41+
* <code>1 <= stones[i] <= 10<sup>9</sup></code>
42+
* All the values of `stones` are **unique**.

0 commit comments

Comments
 (0)