Skip to content

Commit de06da4

Browse files
authored
Added tasks 1025, 1026, 1027, 1028
1 parent 21fec79 commit de06da4

File tree

13 files changed

+399
-0
lines changed

13 files changed

+399
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1750,6 +1750,10 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.12'
17501750
| # | Title | Difficulty | Tag | Time, ms | Time, %
17511751
|------|----------------|-------------|-------------|----------|---------
17521752
| 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
1753+
| 1028 |[Recover a Tree From Preorder Traversal](src/main/kotlin/g1001_1100/s1028_recover_a_tree_from_preorder_traversal/Solution.kt)| Hard | String, Depth_First_Search, Tree, Binary_Tree | 246 | 100.00
1754+
| 1027 |[Longest Arithmetic Subsequence](src/main/kotlin/g1001_1100/s1027_longest_arithmetic_subsequence/Solution.kt)| Medium | Array, Hash_Table, Dynamic_Programming, Binary_Search | 330 | 100.00
1755+
| 1026 |[Maximum Difference Between Node and Ancestor](src/main/kotlin/g1001_1100/s1026_maximum_difference_between_node_and_ancestor/Solution.kt)| Medium | Depth_First_Search, Tree, Binary_Tree | 155 | 77.78
1756+
| 1025 |[Divisor Game](src/main/kotlin/g1001_1100/s1025_divisor_game/Solution.kt)| Easy | Dynamic_Programming, Math, Game_Theory, Brainteaser | 114 | 93.33
17531757
| 1024 |[Video Stitching](src/main/kotlin/g1001_1100/s1024_video_stitching/Solution.kt)| Medium | Array, Dynamic_Programming, Greedy | 141 | 100.00
17541758
| 1023 |[Camelcase Matching](src/main/kotlin/g1001_1100/s1023_camelcase_matching/Solution.kt)| Medium | String, Two_Pointers, Trie, String_Matching | 149 | 60.00
17551759
| 1022 |[Sum of Root To Leaf Binary Numbers](src/main/kotlin/g1001_1100/s1022_sum_of_root_to_leaf_binary_numbers/Solution.kt)| Easy | Depth_First_Search, Tree, Binary_Tree | 158 | 88.89
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package g1001_1100.s1025_divisor_game
2+
3+
// #Easy #Dynamic_Programming #Math #Game_Theory #Brainteaser
4+
// #2023_05_23_Time_114_ms_(93.33%)_Space_34.2_MB_(13.33%)
5+
6+
class Solution {
7+
fun divisorGame(n: Int): Boolean {
8+
return n % 2 == 0
9+
}
10+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
1025\. Divisor Game
2+
3+
Easy
4+
5+
Alice and Bob take turns playing a game, with Alice starting first.
6+
7+
Initially, there is a number `n` on the chalkboard. On each player's turn, that player makes a move consisting of:
8+
9+
* Choosing any `x` with `0 < x < n` and `n % x == 0`.
10+
* Replacing the number `n` on the chalkboard with `n - x`.
11+
12+
Also, if a player cannot make a move, they lose the game.
13+
14+
Return `true` _if and only if Alice wins the game, assuming both players play optimally_.
15+
16+
**Example 1:**
17+
18+
**Input:** n = 2
19+
20+
**Output:** true
21+
22+
**Explanation:** Alice chooses 1, and Bob has no more moves.
23+
24+
**Example 2:**
25+
26+
**Input:** n = 3
27+
28+
**Output:** false
29+
30+
**Explanation:** Alice chooses 1, Bob chooses 1, and Alice has no more moves.
31+
32+
**Constraints:**
33+
34+
* `1 <= n <= 1000`
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package g1001_1100.s1026_maximum_difference_between_node_and_ancestor
2+
3+
// #Medium #Depth_First_Search #Tree #Binary_Tree
4+
// #2023_05_23_Time_155_ms_(77.78%)_Space_36.6_MB_(48.15%)
5+
6+
import com_github_leetcode.TreeNode
7+
import kotlin.math.abs
8+
9+
/*
10+
* Example:
11+
* var ti = TreeNode(5)
12+
* var v = ti.`val`
13+
* Definition for a binary tree node.
14+
* class TreeNode(var `val`: Int) {
15+
* var left: TreeNode? = null
16+
* var right: TreeNode? = null
17+
* }
18+
*/
19+
class Solution {
20+
private var max = 0
21+
fun maxAncestorDiff(root: TreeNode?): Int {
22+
traverse(root, -1, -1)
23+
return max
24+
}
25+
26+
private fun traverse(root: TreeNode?, maxAncestor: Int, minAncestor: Int) {
27+
if (root == null) {
28+
return
29+
}
30+
if (maxAncestor == -1) {
31+
traverse(root.left, root.`val`, root.`val`)
32+
traverse(root.right, root.`val`, root.`val`)
33+
}
34+
if (maxAncestor != -1) {
35+
max = max.coerceAtLeast(abs(maxAncestor - root.`val`))
36+
max = max.coerceAtLeast(abs(minAncestor - root.`val`))
37+
traverse(root.left, root.`val`.coerceAtLeast(maxAncestor), root.`val`.coerceAtMost(minAncestor))
38+
traverse(root.right, root.`val`.coerceAtLeast(maxAncestor), root.`val`.coerceAtMost(minAncestor))
39+
}
40+
}
41+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
1026\. Maximum Difference Between Node and Ancestor
2+
3+
Medium
4+
5+
Given the `root` of a binary tree, find the maximum value `v` for which there exist **different** nodes `a` and `b` where `v = |a.val - b.val|` and `a` is an ancestor of `b`.
6+
7+
A node `a` is an ancestor of `b` if either: any child of `a` is equal to `b` or any child of `a` is an ancestor of `b`.
8+
9+
**Example 1:**
10+
11+
![](https://assets.leetcode.com/uploads/2020/11/09/tmp-tree.jpg)
12+
13+
**Input:** root = [8,3,10,1,6,null,14,null,null,4,7,13]
14+
15+
**Output:** 7
16+
17+
**Explanation:** We have various ancestor-node differences, some of which are given below :
18+
19+
|8 - 3| = 5
20+
21+
|3 - 7| = 4
22+
23+
|8 - 1| = 7
24+
25+
|10 - 13| = 3
26+
27+
Among all possible differences, the maximum value of 7 is obtained by |8 - 1| = 7.
28+
29+
**Example 2:**
30+
31+
![](https://assets.leetcode.com/uploads/2020/11/09/tmp-tree-1.jpg)
32+
33+
**Input:** root = [1,null,2,null,0,3]
34+
35+
**Output:** 3
36+
37+
**Constraints:**
38+
39+
* The number of nodes in the tree is in the range `[2, 5000]`.
40+
* <code>0 <= Node.val <= 10<sup>5</sup></code>
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package g1001_1100.s1027_longest_arithmetic_subsequence
2+
3+
// #Medium #Array #Hash_Table #Dynamic_Programming #Binary_Search
4+
// #2023_05_23_Time_330_ms_(100.00%)_Space_101.4_MB_(16.67%)
5+
6+
import java.util.Arrays
7+
8+
class Solution {
9+
fun longestArithSeqLength(nums: IntArray): Int {
10+
val max = maxElement(nums)
11+
val min = minElement(nums)
12+
val diff = max - min
13+
val n = nums.size
14+
val dp = Array(n) { IntArray(2 * diff + 2) }
15+
for (d in dp) {
16+
Arrays.fill(d, 1)
17+
}
18+
var ans = 0
19+
for (i in 0 until n) {
20+
for (j in i - 1 downTo 0) {
21+
val difference = nums[i] - nums[j] + diff
22+
val temp = dp[j][difference]
23+
dp[i][difference] = Math.max(dp[i][difference], temp + 1)
24+
if (ans < dp[i][difference]) {
25+
ans = dp[i][difference]
26+
}
27+
}
28+
}
29+
return ans
30+
}
31+
32+
private fun maxElement(arr: IntArray): Int {
33+
var max = Int.MIN_VALUE
34+
for (e in arr) {
35+
if (max < e) {
36+
max = e
37+
}
38+
}
39+
return max
40+
}
41+
42+
private fun minElement(arr: IntArray): Int {
43+
var min = Int.MAX_VALUE
44+
for (e in arr) {
45+
if (min > e) {
46+
min = e
47+
}
48+
}
49+
return min
50+
}
51+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
1027\. Longest Arithmetic Subsequence
2+
3+
Medium
4+
5+
Given an array `nums` of integers, return _the length of the longest arithmetic subsequence in_ `nums`.
6+
7+
**Note** that:
8+
9+
* A **subsequence** is an array that can be derived from another array by deleting some or no elements without changing the order of the remaining elements.
10+
* A sequence `seq` is arithmetic if `seq[i + 1] - seq[i]` are all the same value (for `0 <= i < seq.length - 1`).
11+
12+
**Example 1:**
13+
14+
**Input:** nums = [3,6,9,12]
15+
16+
**Output:** 4 **Explanation: ** The whole array is an arithmetic sequence with steps of length = 3.
17+
18+
**Example 2:**
19+
20+
**Input:** nums = [9,4,7,2,10]
21+
22+
**Output:** 3 **Explanation: ** The longest arithmetic subsequence is [4,7,10].
23+
24+
**Example 3:**
25+
26+
**Input:** nums = [20,1,15,3,10,5,8]
27+
28+
**Output:** 4 **Explanation: ** The longest arithmetic subsequence is [20,15,10,5].
29+
30+
**Constraints:**
31+
32+
* `2 <= nums.length <= 1000`
33+
* `0 <= nums[i] <= 500`
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package g1001_1100.s1028_recover_a_tree_from_preorder_traversal
2+
3+
// #Hard #String #Depth_First_Search #Tree #Binary_Tree
4+
// #2023_05_23_Time_246_ms_(100.00%)_Space_49.3_MB_(100.00%)
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 ptr = 0
20+
fun recoverFromPreorder(traversal: String): TreeNode? {
21+
return find(traversal, 0)
22+
}
23+
24+
private fun find(traversal: String, level: Int): TreeNode? {
25+
if (ptr == traversal.length) {
26+
return null
27+
}
28+
var i = ptr
29+
var count = 0
30+
while (traversal[i] == '-') {
31+
count++
32+
i++
33+
}
34+
return if (count == level) {
35+
val start = i
36+
while (i < traversal.length && traversal[i] != '-') {
37+
i++
38+
}
39+
val `val` = traversal.substring(start, i).toInt()
40+
ptr = i
41+
val root = TreeNode(`val`)
42+
root.left = find(traversal, level + 1)
43+
root.right = find(traversal, level + 1)
44+
root
45+
} else {
46+
null
47+
}
48+
}
49+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
1028\. Recover a Tree From Preorder Traversal
2+
3+
Hard
4+
5+
We run a preorder depth-first search (DFS) on the `root` of a binary tree.
6+
7+
At each node in this traversal, we output `D` dashes (where `D` is the depth of this node), then we output the value of this node. If the depth of a node is `D`, the depth of its immediate child is `D + 1`. The depth of the `root` node is `0`.
8+
9+
If a node has only one child, that child is guaranteed to be **the left child**.
10+
11+
Given the output `traversal` of this traversal, recover the tree and return _its_ `root`.
12+
13+
**Example 1:**
14+
15+
![](https://assets.leetcode.com/uploads/2019/04/08/recover-a-tree-from-preorder-traversal.png)
16+
17+
**Input:** traversal = "1-2--3--4-5--6--7"
18+
19+
**Output:** [1,2,5,3,4,6,7]
20+
21+
**Example 2:**
22+
23+
![](https://assets.leetcode.com/uploads/2019/04/11/screen-shot-2019-04-10-at-114101-pm.png)
24+
25+
**Input:** traversal = "1-2--3---4-5--6---7"
26+
27+
**Output:** [1,2,5,3,null,6,null,4,null,7]
28+
29+
**Example 3:**
30+
31+
![](https://assets.leetcode.com/uploads/2019/04/11/screen-shot-2019-04-10-at-114955-pm.png)
32+
33+
**Input:** traversal = "1-401--349---90--88"
34+
35+
**Output:** [1,401,null,349,88,90]
36+
37+
**Constraints:**
38+
39+
* The number of nodes in the original tree is in the range `[1, 1000]`.
40+
* <code>1 <= Node.val <= 10<sup>9</sup></code>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package g1001_1100.s1025_divisor_game
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 divisorGame() {
10+
assertThat(Solution().divisorGame(2), equalTo(true))
11+
}
12+
13+
@Test
14+
fun divisorGame2() {
15+
assertThat(Solution().divisorGame(3), equalTo(false))
16+
}
17+
}

0 commit comments

Comments
 (0)