Skip to content

Commit 9084100

Browse files
authored
Added tasks 572, 575, 576, 581
1 parent 6548725 commit 9084100

File tree

13 files changed

+367
-0
lines changed

13 files changed

+367
-0
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1219,6 +1219,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.8'
12191219
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
12201220
|-|-|-|-|-|-
12211221
| 0117 |[Populating Next Right Pointers in Each Node II](src/main/kotlin/g0101_0200/s0117_populating_next_right_pointers_in_each_node_ii/Solution.kt)| Medium | Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Linked_List | 199 | 94.67
1222+
| 0572 |[Subtree of Another Tree](src/main/kotlin/g0501_0600/s0572_subtree_of_another_tree/Solution.kt)| Easy | Depth_First_Search, Tree, Binary_Tree, Hash_Function, String_Matching | 214 | 92.39
12221223

12231224
#### Day 8 Breadth First Search Depth First Search
12241225

@@ -1658,6 +1659,10 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.8'
16581659
| 0763 |[Partition Labels](src/main/kotlin/g0701_0800/s0763_partition_labels/Solution.kt)| Medium | Top_100_Liked_Questions, String, Hash_Table, Greedy, Two_Pointers, Data_Structure_II_Day_7_String | 235 | 84.75
16591660
| 0739 |[Daily Temperatures](src/main/kotlin/g0701_0800/s0739_daily_temperatures/Solution.kt)| Medium | Top_100_Liked_Questions, Array, Stack, Monotonic_Stack, Programming_Skills_II_Day_6 | 936 | 80.54
16601661
| 0647 |[Palindromic Substrings](src/main/kotlin/g0601_0700/s0647_palindromic_substrings/Solution.kt)| Medium | Top_100_Liked_Questions, String, Dynamic_Programming | 266 | 67.83
1662+
| 0581 |[Shortest Unsorted Continuous Subarray](src/main/kotlin/g0501_0600/s0581_shortest_unsorted_continuous_subarray/Solution.kt)| Medium | Array, Sorting, Greedy, Two_Pointers, Stack, Monotonic_Stack | 246 | 100.00
1663+
| 0576 |[Out of Boundary Paths](src/main/kotlin/g0501_0600/s0576_out_of_boundary_paths/Solution.kt)| Medium | Dynamic_Programming | 153 | 100.00
1664+
| 0575 |[Distribute Candies](src/main/kotlin/g0501_0600/s0575_distribute_candies/Solution.kt)| Easy | Array, Hash_Table | 538 | 76.92
1665+
| 0572 |[Subtree of Another Tree](src/main/kotlin/g0501_0600/s0572_subtree_of_another_tree/Solution.kt)| Easy | Depth_First_Search, Tree, Binary_Tree, Hash_Function, String_Matching, Algorithm_II_Day_7_Breadth_First_Search_Depth_First_Search | 214 | 92.39
16611666
| 0567 |[Permutation in String](src/main/kotlin/g0501_0600/s0567_permutation_in_string/Solution.kt)| Medium | String, Hash_Table, Two_Pointers, Sliding_Window, Algorithm_I_Day_6_Sliding_Window | 169 | 100.00
16621667
| 0566 |[Reshape the Matrix](src/main/kotlin/g0501_0600/s0566_reshape_the_matrix/Solution.kt)| Easy | Array, Matrix, Simulation, Data_Structure_I_Day_4_Array, Programming_Skills_I_Day_7_Array | 239 | 99.05
16631668
| 0565 |[Array Nesting](src/main/kotlin/g0501_0600/s0565_array_nesting/Solution.kt)| Medium | Array, Depth_First_Search | 553 | 100.00
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package g0501_0600.s0572_subtree_of_another_tree
2+
3+
import com_github_leetcode.TreeNode
4+
5+
// #Easy #Depth_First_Search #Tree #Binary_Tree #Hash_Function #String_Matching
6+
// #Algorithm_II_Day_7_Breadth_First_Search_Depth_First_Search
7+
// #2023_01_23_Time_214_ms_(92.39%)_Space_38.4_MB_(45.65%)
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 fun isSubtreeFound(root: TreeNode?, subRoot: TreeNode?): Boolean {
21+
if (root == null && subRoot == null) {
22+
return true
23+
}
24+
if (root == null || subRoot == null) {
25+
return false
26+
}
27+
return if (root.`val` === subRoot.`val`) {
28+
isSubtreeFound(root.left, subRoot.left) && isSubtree(root.right, subRoot.right)
29+
} else {
30+
false
31+
}
32+
}
33+
34+
fun isSubtree(root: TreeNode?, subRoot: TreeNode?): Boolean {
35+
if (root == null && subRoot == null) {
36+
return true
37+
}
38+
return if (root == null || subRoot == null) {
39+
false
40+
} else isSubtreeFound(root, subRoot) ||
41+
isSubtree(root.left, subRoot) ||
42+
isSubtree(root.right, subRoot)
43+
}
44+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
572\. Subtree of Another Tree
2+
3+
Easy
4+
5+
Given the roots of two binary trees `root` and `subRoot`, return `true` if there is a subtree of `root` with the same structure and node values of `subRoot` and `false` otherwise.
6+
7+
A subtree of a binary tree `tree` is a tree that consists of a node in `tree` and all of this node's descendants. The tree `tree` could also be considered as a subtree of itself.
8+
9+
**Example 1:**
10+
11+
![](https://assets.leetcode.com/uploads/2021/04/28/subtree1-tree.jpg)
12+
13+
**Input:** root = [3,4,5,1,2], subRoot = [4,1,2]
14+
15+
**Output:** true
16+
17+
**Example 2:**
18+
19+
![](https://assets.leetcode.com/uploads/2021/04/28/subtree2-tree.jpg)
20+
21+
**Input:** root = [3,4,5,1,2,null,null,null,null,0], subRoot = [4,1,2]
22+
23+
**Output:** false
24+
25+
**Constraints:**
26+
27+
* The number of nodes in the `root` tree is in the range `[1, 2000]`.
28+
* The number of nodes in the `subRoot` tree is in the range `[1, 1000]`.
29+
* <code>-10<sup>4</sup> <= root.val <= 10<sup>4</sup></code>
30+
* <code>-10<sup>4</sup> <= subRoot.val <= 10<sup>4</sup></code>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package g0501_0600.s0575_distribute_candies
2+
3+
// #Easy #Array #Hash_Table #2023_01_23_Time_538_ms_(76.92%)_Space_47.3_MB_(69.23%)
4+
5+
internal class Solution {
6+
fun distributeCandies(candyType: IntArray): Int {
7+
val s: MutableSet<Int> = HashSet()
8+
for (i in candyType) {
9+
if (!s.contains(i)) {
10+
s.add(i)
11+
}
12+
}
13+
val canEat = candyType.size / 2
14+
return if (s.size >= canEat) {
15+
canEat
16+
} else {
17+
s.size
18+
}
19+
}
20+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
575\. Distribute Candies
2+
3+
Easy
4+
5+
Alice has `n` candies, where the <code>i<sup>th</sup></code> candy is of type `candyType[i]`. Alice noticed that she started to gain weight, so she visited a doctor.
6+
7+
The doctor advised Alice to only eat `n / 2` of the candies she has (`n` is always even). Alice likes her candies very much, and she wants to eat the maximum number of different types of candies while still following the doctor's advice.
8+
9+
Given the integer array `candyType` of length `n`, return _the **maximum** number of different types of candies she can eat if she only eats_ `n / 2` _of them_.
10+
11+
**Example 1:**
12+
13+
**Input:** candyType = [1,1,2,2,3,3]
14+
15+
**Output:** 3
16+
17+
**Explanation:** Alice can only eat 6 / 2 = 3 candies. Since there are only 3 types, she can eat one of each type.
18+
19+
**Example 2:**
20+
21+
**Input:** candyType = [1,1,2,3]
22+
23+
**Output:** 2
24+
25+
**Explanation:** Alice can only eat 4 / 2 = 2 candies. Whether she eats types [1,2], [1,3], or [2,3], she still can only eat 2 different types.
26+
27+
**Example 3:**
28+
29+
**Input:** candyType = [6,6,6,6]
30+
31+
**Output:** 1
32+
33+
**Explanation:** Alice can only eat 4 / 2 = 2 candies. Even though she can eat 2 candies, she only has 1 type.
34+
35+
**Constraints:**
36+
37+
* `n == candyType.length`
38+
* <code>2 <= n <= 10<sup>4</sup></code>
39+
* `n` is even.
40+
* <code>-10<sup>5</sup> <= candyType[i] <= 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 g0501_0600.s0576_out_of_boundary_paths
2+
3+
// #Medium #Dynamic_Programming #2023_01_23_Time_153_ms_(100.00%)_Space_34.7_MB_(80.00%)
4+
5+
import java.util.Arrays
6+
7+
class Solution {
8+
private val dRowCol = arrayOf(intArrayOf(1, 0), intArrayOf(-1, 0), intArrayOf(0, 1), intArrayOf(0, -1))
9+
private fun dfs(
10+
m: Int,
11+
n: Int,
12+
remainingMoves: Int,
13+
currRow: Int,
14+
currCol: Int,
15+
cache: Array<Array<IntArray>>
16+
): Int {
17+
if (currRow < 0 || currRow == m || currCol < 0 || currCol == n) {
18+
return 1
19+
}
20+
if (remainingMoves == 0) {
21+
return 0
22+
}
23+
if (cache[currRow][currCol][remainingMoves] == -1) {
24+
var paths = 0
25+
for (i in 0..3) {
26+
val newRow = currRow + dRowCol[i][0]
27+
val newCol = currCol + dRowCol[i][1]
28+
val m1 = 1000000007
29+
paths = (paths + dfs(m, n, remainingMoves - 1, newRow, newCol, cache)) % m1
30+
}
31+
cache[currRow][currCol][remainingMoves] = paths
32+
}
33+
return cache[currRow][currCol][remainingMoves]
34+
}
35+
36+
fun findPaths(m: Int, n: Int, maxMoves: Int, startRow: Int, startCol: Int): Int {
37+
val cache = Array(m) {
38+
Array(n) {
39+
IntArray(
40+
maxMoves + 1
41+
)
42+
}
43+
}
44+
for (c1 in cache) {
45+
for (c2 in c1) {
46+
Arrays.fill(c2, -1)
47+
}
48+
}
49+
return dfs(m, n, maxMoves, startRow, startCol, cache)
50+
}
51+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
576\. Out of Boundary Paths
2+
3+
Medium
4+
5+
There is an `m x n` grid with a ball. The ball is initially at the position `[startRow, startColumn]`. You are allowed to move the ball to one of the four adjacent cells in the grid (possibly out of the grid crossing the grid boundary). You can apply **at most** `maxMove` moves to the ball.
6+
7+
Given the five integers `m`, `n`, `maxMove`, `startRow`, `startColumn`, return the number of paths to move the ball out of the grid boundary. Since the answer can be very large, return it **modulo** <code>10<sup>9</sup> + 7</code>.
8+
9+
**Example 1:**
10+
11+
![](https://assets.leetcode.com/uploads/2021/04/28/out_of_boundary_paths_1.png)
12+
13+
**Input:** m = 2, n = 2, maxMove = 2, startRow = 0, startColumn = 0
14+
15+
**Output:** 6
16+
17+
**Example 2:**
18+
19+
![](https://assets.leetcode.com/uploads/2021/04/28/out_of_boundary_paths_2.png)
20+
21+
**Input:** m = 1, n = 3, maxMove = 3, startRow = 0, startColumn = 1
22+
23+
**Output:** 12
24+
25+
**Constraints:**
26+
27+
* `1 <= m, n <= 50`
28+
* `0 <= maxMove <= 50`
29+
* `0 <= startRow < m`
30+
* `0 <= startColumn < n`
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package g0501_0600.s0581_shortest_unsorted_continuous_subarray
2+
3+
// #Medium #Array #Sorting #Greedy #Two_Pointers #Stack #Monotonic_Stack
4+
// #2023_01_30_Time_246_ms_(100.00%)_Space_39.4_MB_(50.00%)
5+
6+
class Solution {
7+
fun findUnsortedSubarray(nums: IntArray): Int {
8+
var end = -2
9+
var max = Int.MIN_VALUE
10+
for (i in nums.indices) {
11+
max = Math.max(max, nums[i])
12+
if (nums[i] < max) {
13+
end = i
14+
}
15+
}
16+
var start = -1
17+
var min = Int.MAX_VALUE
18+
for (i in nums.indices.reversed()) {
19+
min = Math.min(min, nums[i])
20+
if (nums[i] > min) {
21+
start = i
22+
}
23+
}
24+
return end - start + 1
25+
}
26+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
581\. Shortest Unsorted Continuous Subarray
2+
3+
Medium
4+
5+
Given an integer array `nums`, you need to find one **continuous subarray** that if you only sort this subarray in ascending order, then the whole array will be sorted in ascending order.
6+
7+
Return _the shortest such subarray and output its length_.
8+
9+
**Example 1:**
10+
11+
**Input:** nums = [2,6,4,8,10,9,15]
12+
13+
**Output:** 5
14+
15+
**Explanation:** You need to sort [6, 4, 8, 10, 9] in ascending order to make the whole array sorted in ascending order.
16+
17+
**Example 2:**
18+
19+
**Input:** nums = [1,2,3,4]
20+
21+
**Output:** 0
22+
23+
**Example 3:**
24+
25+
**Input:** nums = [1]
26+
27+
**Output:** 0
28+
29+
**Constraints:**
30+
31+
* <code>1 <= nums.length <= 10<sup>4</sup></code>
32+
* <code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code>
33+
34+
**Follow up:** Can you solve it in `O(n)` time complexity?
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package g0501_0600.s0572_subtree_of_another_tree
2+
3+
import com_github_leetcode.TreeNode
4+
import org.hamcrest.CoreMatchers.equalTo
5+
import org.hamcrest.MatcherAssert.assertThat
6+
import org.junit.jupiter.api.Test
7+
8+
internal class SolutionTest {
9+
@Test
10+
fun isSubtree() {
11+
val treeNode: TreeNode = TreeNode.create(listOf(3, 4, 5, 1, 2))!!
12+
val subTree: TreeNode = TreeNode.create(listOf(4, 1, 2))!!
13+
assertThat(Solution().isSubtree(treeNode, subTree), equalTo(true))
14+
}
15+
16+
@Test
17+
fun isSubtree2() {
18+
val treeNode: TreeNode = TreeNode.create(
19+
listOf(3, 4, 5, 1, 2, null, null, null, null, 0)
20+
)!!
21+
val subTree: TreeNode = TreeNode.create(listOf(4, 1, 2))!!
22+
assertThat(Solution().isSubtree(treeNode, subTree), equalTo(false))
23+
}
24+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package g0501_0600.s0575_distribute_candies
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 distributeCandies() {
10+
assertThat(Solution().distributeCandies(intArrayOf(1, 1, 2, 2, 3, 3)), equalTo(3))
11+
}
12+
13+
@Test
14+
fun distributeCandies2() {
15+
assertThat(Solution().distributeCandies(intArrayOf(1, 1, 2, 3)), equalTo(2))
16+
}
17+
18+
@Test
19+
fun distributeCandies3() {
20+
assertThat(Solution().distributeCandies(intArrayOf(6, 6, 6, 6)), equalTo(1))
21+
}
22+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package g0501_0600.s0576_out_of_boundary_paths
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 findPaths() {
10+
assertThat(Solution().findPaths(2, 2, 2, 0, 0), equalTo(6))
11+
}
12+
13+
@Test
14+
fun findPaths2() {
15+
assertThat(Solution().findPaths(1, 3, 3, 0, 1), equalTo(12))
16+
}
17+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package g0501_0600.s0581_shortest_unsorted_continuous_subarray
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 findUnsortedSubarray() {
10+
assertThat(
11+
Solution().findUnsortedSubarray(intArrayOf(2, 6, 4, 8, 10, 9, 15)), equalTo(5)
12+
)
13+
}
14+
15+
@Test
16+
fun findUnsortedSubarray2() {
17+
assertThat(Solution().findUnsortedSubarray(intArrayOf(1, 2, 3, 4)), equalTo(0))
18+
}
19+
20+
@Test
21+
fun findUnsortedSubarray3() {
22+
assertThat(Solution().findUnsortedSubarray(intArrayOf(1)), equalTo(0))
23+
}
24+
}

0 commit comments

Comments
 (0)