Skip to content

Commit 051bd03

Browse files
authored
Added tasks 1037, 1038, 1039, 1040
1 parent 35c8e79 commit 051bd03

File tree

13 files changed

+334
-0
lines changed

13 files changed

+334
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1779,6 +1779,10 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.12'
17791779
| 1155 |[Number of Dice Rolls With Target Sum](src/main/kotlin/g1101_1200/s1155_number_of_dice_rolls_with_target_sum/Solution.kt)| Medium | Dynamic_Programming | 158 | 80.95
17801780
| 1154 |[Day of the Year](src/main/kotlin/g1101_1200/s1154_day_of_the_year/Solution.kt)| Easy | String, Math | 317 | 70.00
17811781
| 1143 |[Longest Common Subsequence](src/main/kotlin/g1101_1200/s1143_longest_common_subsequence/Solution.kt)| Medium | Top_100_Liked_Questions, String, Dynamic_Programming, Algorithm_II_Day_17_Dynamic_Programming, Dynamic_Programming_I_Day_19, Udemy_Dynamic_Programming | 307 | 38.36
1782+
| 1040 |[Moving Stones Until Consecutive II](src/main/kotlin/g1001_1100/s1040_moving_stones_until_consecutive_ii/Solution.kt)| Medium | Array, Math, Sorting, Two_Pointers | 287 | 50.00
1783+
| 1039 |[Minimum Score Triangulation of Polygon](src/main/kotlin/g1001_1100/s1039_minimum_score_triangulation_of_polygon/Solution.kt)| Medium | Array, Dynamic_Programming | 147 | 100.00
1784+
| 1038 |[Binary Search Tree to Greater Sum Tree](src/main/kotlin/g1001_1100/s1038_binary_search_tree_to_greater_sum_tree/Solution.kt)| Medium | Depth_First_Search, Tree, Binary_Tree, Binary_Search_Tree | 123 | 91.67
1785+
| 1037 |[Valid Boomerang](src/main/kotlin/g1001_1100/s1037_valid_boomerang/Solution.kt)| Easy | Array, Math, Geometry | 126 | 100.00
17821786
| 1036 |[Escape a Large Maze](src/main/kotlin/g1001_1100/s1036_escape_a_large_maze/Solution.kt)| Hard | Array, Hash_Table, Depth_First_Search, Breadth_First_Search | 387 | 100.00
17831787
| 1035 |[Uncrossed Lines](src/main/kotlin/g1001_1100/s1035_uncrossed_lines/Solution.kt)| Medium | Array, Dynamic_Programming | 162 | 93.33
17841788
| 1034 |[Coloring A Border](src/main/kotlin/g1001_1100/s1034_coloring_a_border/Solution.kt)| Medium | Array, Depth_First_Search, Breadth_First_Search, Matrix | 332 | 100.00
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package g1001_1100.s1037_valid_boomerang
2+
3+
// #Easy #Array #Math #Geometry #2023_05_26_Time_126_ms_(100.00%)_Space_34.8_MB_(60.00%)
4+
5+
class Solution {
6+
fun isBoomerang(points: Array<IntArray>): Boolean {
7+
return (
8+
(points[1][1] - points[0][1]) * (points[2][0] - points[0][0])
9+
!= (points[2][1] - points[0][1]) * (points[1][0] - points[0][0])
10+
)
11+
}
12+
}
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: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
// #2023_05_26_Time_123_ms_(91.67%)_Space_34.7_MB_(58.33%)
5+
6+
import com_github_leetcode.TreeNode
7+
8+
/*
9+
* Example:
10+
* var ti = TreeNode(5)
11+
* var v = ti.`val`
12+
* Definition for a binary tree node.
13+
* class TreeNode(var `val`: Int) {
14+
* var left: TreeNode? = null
15+
* var right: TreeNode? = null
16+
* }
17+
*/
18+
class Solution {
19+
private var greaterSum = 0
20+
fun bstToGst(root: TreeNode?): TreeNode {
21+
if (root!!.right != null) {
22+
bstToGst(root.right!!)
23+
}
24+
root.`val` = greaterSum + root.`val`
25+
greaterSum = root.`val`
26+
if (root.left != null) {
27+
bstToGst(root.left!!)
28+
}
29+
return root
30+
}
31+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
1038\. Binary Search Tree to Greater Sum Tree
2+
3+
Medium
4+
5+
Given the `root` of a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original BST is changed to the original key plus the sum of all keys greater than the original key in BST.
6+
7+
As a reminder, a _binary search tree_ is a tree that satisfies these constraints:
8+
9+
* The left subtree of a node contains only nodes with keys **less than** the node's key.
10+
* The right subtree of a node contains only nodes with keys **greater than** the node's key.
11+
* Both the left and right subtrees must also be binary search trees.
12+
13+
**Example 1:**
14+
15+
![](https://assets.leetcode.com/uploads/2019/05/02/tree.png)
16+
17+
**Input:** root = [4,1,6,0,2,5,7,null,null,null,3,null,null,null,8]
18+
19+
**Output:** [30,36,21,36,35,26,15,null,null,null,33,null,null,null,8]
20+
21+
**Example 2:**
22+
23+
**Input:** root = [0,null,1]
24+
25+
**Output:** [1,null,1]
26+
27+
**Constraints:**
28+
29+
* The number of nodes in the tree is in the range `[1, 100]`.
30+
* `0 <= Node.val <= 100`
31+
* All the values in the tree are **unique**.
32+
33+
**Note:** This question is the same as 538: [https://leetcode.com/problems/convert-bst-to-greater-tree/](https://leetcode.com/problems/convert-bst-to-greater-tree/)
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package g1001_1100.s1039_minimum_score_triangulation_of_polygon
2+
3+
// #Medium #Array #Dynamic_Programming #2023_05_26_Time_147_ms_(100.00%)_Space_38.9_MB_(50.00%)
4+
5+
class Solution() {
6+
private val dp = Array(101) { IntArray(101) }
7+
fun minScoreTriangulation(values: IntArray): Int {
8+
val n = values.size
9+
for (row: IntArray? in dp) {
10+
row!!.fill(-1)
11+
}
12+
return util(values, 1, n - 1)
13+
}
14+
15+
private fun util(values: IntArray, i: Int, j: Int): Int {
16+
if (i >= j) {
17+
return 0
18+
}
19+
if (dp[i][j] != -1) {
20+
return dp[i][j]
21+
}
22+
var ans = Int.MAX_VALUE
23+
for (k in i until j) {
24+
val temp = (
25+
util(values, i, k) +
26+
util(values, k + 1, j) +
27+
(values[i - 1] * values[k] * values[j])
28+
)
29+
ans = ans.coerceAtMost(temp)
30+
dp[i][j] = ans
31+
}
32+
return dp[i][j]
33+
}
34+
}
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: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package g1001_1100.s1040_moving_stones_until_consecutive_ii
2+
3+
// #Medium #Array #Math #Sorting #Two_Pointers
4+
// #2023_05_26_Time_287_ms_(50.00%)_Space_50.2_MB_(100.00%)
5+
6+
class Solution {
7+
fun numMovesStonesII(a: IntArray): IntArray? {
8+
a.sort()
9+
var i = 0
10+
val n = a.size
11+
var low = n
12+
val high = (a[n - 1] - n + 2 - a[1]).coerceAtLeast(a[n - 2] - a[0] - n + 2)
13+
for (j in 0 until n) {
14+
while (a[j] - a[i] >= n) ++i
15+
low = if (j - i + 1 == n - 1 && a[j] - a[i] == n - 2) low.coerceAtMost(2)
16+
else low.coerceAtMost(n - (j - i + 1))
17+
}
18+
return intArrayOf(low, high)
19+
}
20+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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. Or, we can move 3 -> 7, 4 -> 8, 5 -> 9 to finish the game. Notice we cannot move 10 -> 2 to finish the game, because that would be an illegal move.
33+
34+
**Constraints:**
35+
36+
* <code>3 <= stones.length <= 10<sup>4</sup></code>
37+
* <code>1 <= stones[i] <= 10<sup>9</sup></code>
38+
* All the values of `stones` are **unique**.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package g1001_1100.s1037_valid_boomerang
2+
3+
import org.hamcrest.CoreMatchers.equalTo
4+
import org.hamcrest.MatcherAssert.assertThat
5+
import org.junit.jupiter.api.Test
6+
7+
internal class SolutionTest {
8+
@Test
9+
fun isBoomerang() {
10+
assertThat(Solution().isBoomerang(arrayOf(intArrayOf(1, 1), intArrayOf(2, 3), intArrayOf(3, 2))), equalTo(true))
11+
}
12+
13+
@Test
14+
fun isBoomerang2() {
15+
assertThat(
16+
Solution().isBoomerang(arrayOf(intArrayOf(1, 1), intArrayOf(2, 2), intArrayOf(3, 3))), equalTo(false)
17+
)
18+
}
19+
}

0 commit comments

Comments
 (0)