Skip to content

Commit 71a0bb2

Browse files
authored
Added tasks 450, 451, 452, 453.
1 parent feb9207 commit 71a0bb2

File tree

13 files changed

+372
-0
lines changed

13 files changed

+372
-0
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1166,6 +1166,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.7'
11661166
|-|-|-|-|-|-
11671167
| 0199 |[Binary Tree Right Side View](src.save/main/kotlin/g0101_0200/s0199_binary_tree_right_side_view/Solution.kt)| Medium | Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree | 194 | 92.89
11681168
| 0113 |[Path Sum II](src.save/main/kotlin/g0101_0200/s0113_path_sum_ii/Solution.kt)| Medium | Depth_First_Search, Tree, Binary_Tree, Backtracking | 364 | 78.67
1169+
| 0450 |[Delete Node in a BST](src/main/kotlin/g0401_0500/s0450_delete_node_in_a_bst/Solution.kt)| Medium | Tree, Binary_Tree, Binary_Search_Tree | 257 | 84.62
11691170

11701171
#### Day 17 Tree
11711172

@@ -1197,6 +1198,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.7'
11971198

11981199
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
11991200
|-|-|-|-|-|-
1201+
| 0451 |[Sort Characters By Frequency](src/main/kotlin/g0401_0500/s0451_sort_characters_by_frequency/Solution.kt)| Medium | String, Hash_Table, Sorting, Heap_Priority_Queue, Counting, Bucket_Sort | 288 | 81.72
12001202

12011203
### Algorithm I
12021204

@@ -1635,6 +1637,10 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.7'
16351637
| 0560 |[Subarray Sum Equals K](src/main/kotlin/g0501_0600/s0560_subarray_sum_equals_k/Solution.kt)| Medium | Top_100_Liked_Questions, Array, Hash_Table, Prefix_Sum, Data_Structure_II_Day_5_Array | 692 | 53.27
16361638
| 0543 |[Diameter of Binary Tree](src/main/kotlin/g0501_0600/s0543_diameter_of_binary_tree/Solution.kt)| Easy | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree, Level_2_Day_7_Tree, Udemy_Tree_Stack_Queue | 307 | 43.93
16371639
| 0494 |[Target Sum](src/main/kotlin/g0401_0500/s0494_target_sum/Solution.kt)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Backtracking | 308 | 89.61
1640+
| 0453 |[Minimum Moves to Equal Array Elements](src/main/kotlin/g0401_0500/s0453_minimum_moves_to_equal_array_elements/Solution.kt)| Medium | Array, Math | 282 | 100.00
1641+
| 0452 |[Minimum Number of Arrows to Burst Balloons](src/main/kotlin/g0401_0500/s0452_minimum_number_of_arrows_to_burst_balloons/Solution.kt)| Medium | Array, Sorting, Greedy | 934 | 100.00
1642+
| 0451 |[Sort Characters By Frequency](src/main/kotlin/g0401_0500/s0451_sort_characters_by_frequency/Solution.kt)| Medium | String, Hash_Table, Sorting, Heap_Priority_Queue, Counting, Bucket_Sort, Data_Structure_II_Day_21_Heap_Priority_Queue | 288 | 81.72
1643+
| 0450 |[Delete Node in a BST](src/main/kotlin/g0401_0500/s0450_delete_node_in_a_bst/Solution.kt)| Medium | Tree, Binary_Tree, Binary_Search_Tree, Data_Structure_II_Day_16_Tree | 257 | 84.62
16381644
| 0449 |[Serialize and Deserialize BST](src/main/kotlin/g0401_0500/s0449_serialize_and_deserialize_bst/Codec.kt)| Medium | String, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Design, Binary_Search_Tree | 233 | 100.00
16391645
| 0448 |[Find All Numbers Disappeared in an Array](src/main/kotlin/g0401_0500/s0448_find_all_numbers_disappeared_in_an_array/Solution.kt)| Easy | Array, Hash_Table, Udemy_Arrays | 394 | 100.00
16401646
| 0447 |[Number of Boomerangs](src/main/kotlin/g0401_0500/s0447_number_of_boomerangs/Solution.kt)| Medium | Array, Hash_Table, Math | 308 | 100.00
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package g0401_0500.s0450_delete_node_in_a_bst
2+
3+
// #Medium #Tree #Binary_Tree #Binary_Search_Tree #Data_Structure_II_Day_16_Tree
4+
// #2022_12_25_Time_257_ms_(84.62%)_Space_38.6_MB_(92.31%)
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+
fun deleteNode(root: TreeNode?, key: Int): TreeNode? {
20+
if (root == null) return root
21+
// find the correct node
22+
if (key < root.`val`) {
23+
root.left = deleteNode(root.left, key)
24+
} else if (key > root.`val`) {
25+
root.right = deleteNode(root.right, key)
26+
} else {
27+
// case 1 - both children are null
28+
if (root.left == null && root.right == null) {
29+
return null
30+
} else if (root.left != null && root.right != null) { // case 2 - both children are NOT null
31+
val successor = minimum(root.right!!) // inorder successor
32+
root.right = deleteNode(root.right, successor)
33+
root.`val` = successor
34+
} else { // case 3 - only one of the child is null
35+
return root.left ?: root.right
36+
}
37+
}
38+
return root
39+
}
40+
41+
private fun minimum(root: TreeNode): Int {
42+
var node = root
43+
while (node.left != null) {
44+
node = node.left!!
45+
}
46+
return node.`val`
47+
}
48+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
450\. Delete Node in a BST
2+
3+
Medium
4+
5+
Given a root node reference of a BST and a key, delete the node with the given key in the BST. Return _the **root node reference** (possibly updated) of the BST_.
6+
7+
Basically, the deletion can be divided into two stages:
8+
9+
1. Search for a node to remove.
10+
2. If the node is found, delete the node.
11+
12+
**Example 1:**
13+
14+
![](https://assets.leetcode.com/uploads/2020/09/04/del_node_1.jpg)
15+
16+
**Input:** root = [5,3,6,2,4,null,7], key = 3
17+
18+
**Output:** [5,4,6,2,null,null,7]
19+
20+
**Explanation:** Given key to delete is 3. So we find the node with value 3 and delete it.
21+
22+
One valid answer is [5,4,6,2,null,null,7], shown in the above BST.
23+
24+
Please notice that another valid answer is [5,2,6,null,4,null,7] and it's also accepted.
25+
26+
![](https://assets.leetcode.com/uploads/2020/09/04/del_node_supp.jpg)
27+
28+
**Example 2:**
29+
30+
**Input:** root = [5,3,6,2,4,null,7], key = 0
31+
32+
**Output:** [5,3,6,2,4,null,7]
33+
34+
**Explanation:** The tree does not contain a node with value = 0.
35+
36+
**Example 3:**
37+
38+
**Input:** root = [], key = 0
39+
40+
**Output:** []
41+
42+
**Constraints:**
43+
44+
* The number of nodes in the tree is in the range <code>[0, 10<sup>4</sup>]</code>.
45+
* <code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code>
46+
* Each node has a **unique** value.
47+
* `root` is a valid binary search tree.
48+
* <code>-10<sup>5</sup> <= key <= 10<sup>5</sup></code>
49+
50+
**Follow up:** Could you solve it with time complexity `O(height of tree)`?
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package g0401_0500.s0451_sort_characters_by_frequency
2+
3+
// #Medium #String #Hash_Table #Sorting #Heap_Priority_Queue #Counting #Bucket_Sort
4+
// #Data_Structure_II_Day_21_Heap_Priority_Queue
5+
// #2022_12_25_Time_288_ms_(81.72%)_Space_39.4_MB_(83.06%)
6+
7+
class Solution {
8+
fun frequencySort(s: String) = s.groupingBy { it }.eachCount().entries.sortedByDescending { it.value }
9+
.joinToString("") {
10+
StringBuilder().apply { for (i in 1..it.value) append(it.key) }.toString()
11+
}
12+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
451\. Sort Characters By Frequency
2+
3+
Medium
4+
5+
Given a string `s`, sort it in **decreasing order** based on the **frequency** of the characters. The **frequency** of a character is the number of times it appears in the string.
6+
7+
Return _the sorted string_. If there are multiple answers, return _any of them_.
8+
9+
**Example 1:**
10+
11+
**Input:** s = "tree"
12+
13+
**Output:** "eert"
14+
15+
**Explanation:** 'e' appears twice while 'r' and 't' both appear once. So 'e' must appear before both 'r' and 't'. Therefore "eetr" is also a valid answer.
16+
17+
**Example 2:**
18+
19+
**Input:** s = "cccaaa"
20+
21+
**Output:** "aaaccc"
22+
23+
**Explanation:** Both 'c' and 'a' appear three times, so both "cccaaa" and "aaaccc" are valid answers. Note that "cacaca" is incorrect, as the same characters must be together.
24+
25+
**Example 3:**
26+
27+
**Input:** s = "Aabb"
28+
29+
**Output:** "bbAa"
30+
31+
**Explanation:** "bbaA" is also a valid answer, but "Aabb" is incorrect. Note that 'A' and 'a' are treated as two different characters.
32+
33+
**Constraints:**
34+
35+
* <code>1 <= s.length <= 5 * 10<sup>5</sup></code>
36+
* `s` consists of uppercase and lowercase English letters and digits.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package g0401_0500.s0452_minimum_number_of_arrows_to_burst_balloons
2+
3+
// #Medium #Array #Sorting #Greedy #2022_12_25_Time_934_ms_(100.00%)_Space_87_MB_(100.00%)
4+
5+
import java.util.Arrays
6+
7+
class Solution {
8+
/*
9+
* I'm glad to have come up with this solution on my own on 10/13/2021:
10+
* we'll have to sort the
11+
* balloons by its ending points, a counter case to this is below:
12+
* {{0, 6}, {0, 9}, {7, 8}}
13+
* if we sort by starting points, then it becomes:
14+
* {0, 6}, {0, 9}, {7, 8}
15+
* this way, if we shoot 9,
16+
* {0, 6} won't be burst however, if we sort by ending points, then it becomes:
17+
* {0, 6}, {7, 8}, {0, 9}, then we shoot at 6, then at 8, this gives us the result of bursting all balloons.
18+
*/
19+
fun findMinArrowShots(points: Array<IntArray>): Int {
20+
Arrays.sort(
21+
points
22+
) { a: IntArray, b: IntArray ->
23+
Integer.compare(
24+
a[1],
25+
b[1]
26+
)
27+
}
28+
var minArrows = 1
29+
var end = points[0][1].toLong()
30+
for (i in 1 until points.size) {
31+
if (points[i][0] > end) {
32+
minArrows++
33+
end = points[i][1].toLong()
34+
}
35+
}
36+
return minArrows
37+
}
38+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
452\. Minimum Number of Arrows to Burst Balloons
2+
3+
Medium
4+
5+
There are some spherical balloons taped onto a flat wall that represents the XY-plane. The balloons are represented as a 2D integer array `points` where <code>points[i] = [x<sub>start</sub>, x<sub>end</sub>]</code> denotes a balloon whose **horizontal diameter** stretches between <code>x<sub>start</sub></code> and <code>x<sub>end</sub></code>. You do not know the exact y-coordinates of the balloons.
6+
7+
Arrows can be shot up **directly vertically** (in the positive y-direction) from different points along the x-axis. A balloon with <code>x<sub>start</sub></code> and <code>x<sub>end</sub></code> is **burst** by an arrow shot at `x` if <code>x<sub>start</sub> <= x <= x<sub>end</sub></code>. There is **no limit** to the number of arrows that can be shot. A shot arrow keeps traveling up infinitely, bursting any balloons in its path.
8+
9+
Given the array `points`, return _the **minimum** number of arrows that must be shot to burst all balloons_.
10+
11+
**Example 1:**
12+
13+
**Input:** points = [[10,16],[2,8],[1,6],[7,12]]
14+
15+
**Output:** 2
16+
17+
**Explanation:** The balloons can be burst by 2 arrows:
18+
19+
- Shoot an arrow at x = 6, bursting the balloons [2,8] and [1,6].
20+
21+
- Shoot an arrow at x = 11, bursting the balloons [10,16] and [7,12].
22+
23+
**Example 2:**
24+
25+
**Input:** points = [[1,2],[3,4],[5,6],[7,8]]
26+
27+
**Output:** 4
28+
29+
**Explanation:** One arrow needs to be shot for each balloon for a total of 4 arrows.
30+
31+
**Example 3:**
32+
33+
**Input:** points = [[1,2],[2,3],[3,4],[4,5]]
34+
35+
**Output:** 2
36+
37+
**Explanation:** The balloons can be burst by 2 arrows:
38+
39+
- Shoot an arrow at x = 2, bursting the balloons [1,2] and [2,3].
40+
41+
- Shoot an arrow at x = 4, bursting the balloons [3,4] and [4,5].
42+
43+
**Constraints:**
44+
45+
* <code>1 <= points.length <= 10<sup>5</sup></code>
46+
* `points[i].length == 2`
47+
* <code>-2<sup>31</sup> <= x<sub>start</sub> < x<sub>end</sub> <= 2<sup>31</sup> - 1</code>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package g0401_0500.s0453_minimum_moves_to_equal_array_elements
2+
3+
// #Medium #Array #Math #2022_12_25_Time_282_ms_(100.00%)_Space_38.7_MB_(88.89%)
4+
5+
class Solution {
6+
fun minMoves(nums: IntArray): Int {
7+
var min = nums[0]
8+
var sum = nums[0]
9+
// determining the total sum and smallest element of the input array
10+
for (i in 1..nums.size - 1) {
11+
sum += nums[i]
12+
min = Math.min(min, nums[i])
13+
}
14+
return sum - min * nums.size
15+
}
16+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
453\. Minimum Moves to Equal Array Elements
2+
3+
Medium
4+
5+
Given an integer array `nums` of size `n`, return _the minimum number of moves required to make all array elements equal_.
6+
7+
In one move, you can increment `n - 1` elements of the array by `1`.
8+
9+
**Example 1:**
10+
11+
**Input:** nums = [1,2,3]
12+
13+
**Output:** 3
14+
15+
**Explanation:** Only three moves are needed (remember each move increments two elements): [1,2,3] => [2,3,3] => [3,4,3] => [4,4,4]
16+
17+
**Example 2:**
18+
19+
**Input:** nums = [1,1,1]
20+
21+
**Output:** 0
22+
23+
**Constraints:**
24+
25+
* `n == nums.length`
26+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
27+
* <code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code>
28+
* The answer is guaranteed to fit in a **32-bit** integer.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package g0401_0500.s0450_delete_node_in_a_bst
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 deleteNode() {
11+
val input: TreeNode? = TreeNode.create(listOf(5, 3, 6, 2, 4, null, 7))
12+
val expected: TreeNode? = TreeNode.create(listOf(5, 4, 6, 2, null, null, 7))
13+
assertThat(Solution().deleteNode(input, 3).toString(), equalTo(expected.toString()))
14+
}
15+
16+
@Test
17+
fun deleteNode2() {
18+
val input: TreeNode? = TreeNode.create(listOf(5, 3, 6, 2, 4, null, 7))
19+
val expected: TreeNode? = TreeNode.create(listOf(5, 3, 6, 2, 4, null, 7))
20+
assertThat(Solution().deleteNode(input, 0).toString(), equalTo(expected.toString()))
21+
}
22+
23+
@Test
24+
fun deleteNode3() {
25+
assertThat(Solution().deleteNode(null, 0), equalTo(null))
26+
}
27+
}

0 commit comments

Comments
 (0)