Skip to content

Commit feb9207

Browse files
authored
Added tasks 446, 447, 448, 449.
1 parent 6e4a282 commit feb9207

File tree

13 files changed

+383
-0
lines changed

13 files changed

+383
-0
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -810,6 +810,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.7'
810810
| 0075 |[Sort Colors](src.save/main/kotlin/g0001_0100/s0075_sort_colors/Solution.kt)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Two_Pointers | 198 | 85.66
811811
| 0066 |[Plus One](src.save/main/kotlin/g0001_0100/s0066_plus_one/Solution.kt)| Easy | Top_Interview_Questions, Array, Math | 303 | 35.18
812812
| 0238 |[Product of Array Except Self](src.save/main/kotlin/g0201_0300/s0238_product_of_array_except_self/Solution.kt)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Prefix_Sum | 669 | 48.96
813+
| 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 | 394 | 100.00
813814
| 0442 |[Find All Duplicates in an Array](src/main/kotlin/g0401_0500/s0442_find_all_duplicates_in_an_array/Solution.kt)| Medium | Array, Hash_Table | 480 | 73.81
814815
| 0041 |[First Missing Positive](src.save/main/kotlin/g0001_0100/s0041_first_missing_positive/Solution.kt)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table | 345 | 100.00
815816
| 0239 |[Sliding Window Maximum](src.save/main/kotlin/g0201_0300/s0239_sliding_window_maximum/Solution.kt)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Array, Heap_Priority_Queue, Sliding_Window, Queue, Monotonic_Queue | 1059 | 86.14
@@ -1634,6 +1635,10 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.7'
16341635
| 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
16351636
| 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
16361637
| 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
1638+
| 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
1639+
| 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
1640+
| 0447 |[Number of Boomerangs](src/main/kotlin/g0401_0500/s0447_number_of_boomerangs/Solution.kt)| Medium | Array, Hash_Table, Math | 308 | 100.00
1641+
| 0446 |[Arithmetic Slices II - Subsequence](src/main/kotlin/g0401_0500/s0446_arithmetic_slices_ii_subsequence/Solution.kt)| Hard | Array, Dynamic_Programming | 312 | 100.00
16371642
| 0445 |[Add Two Numbers II](src/main/kotlin/g0401_0500/s0445_add_two_numbers_ii/Solution.kt)| Medium | Math, Stack, Linked_List, Programming_Skills_II_Day_15, Udemy_Linked_List | 240 | 82.61
16381643
| 0443 |[String Compression](src/main/kotlin/g0401_0500/s0443_string_compression/Solution.kt)| Medium | String, Two_Pointers | 198 | 92.68
16391644
| 0442 |[Find All Duplicates in an Array](src/main/kotlin/g0401_0500/s0442_find_all_duplicates_in_an_array/Solution.kt)| Medium | Array, Hash_Table, Udemy_Arrays | 480 | 73.81
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package g0401_0500.s0446_arithmetic_slices_ii_subsequence
2+
3+
// #Hard #Array #Dynamic_Programming #2022_12_24_Time_312_ms_(100.00%)_Space_45.3_MB_(98.59%)
4+
5+
class Solution {
6+
fun numberOfArithmeticSlices(arr: IntArray): Int {
7+
val indexes: MutableMap<Long, MutableList<Int>> = HashMap()
8+
val length = Array(arr.size) { IntArray(arr.size) }
9+
var count = 0
10+
for (i in arr.indices) {
11+
for (j in i + 1 until arr.size) {
12+
val ix = indexes[arr[i] - (arr[j] - arr[i].toLong())] ?: continue
13+
for (k in ix) {
14+
length[i][j] += length[k][i] + 1
15+
}
16+
count += length[i][j]
17+
}
18+
indexes.computeIfAbsent(
19+
arr[i].toLong()
20+
) { _: Long? -> ArrayList() }.add(i)
21+
}
22+
return count
23+
}
24+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
446\. Arithmetic Slices II - Subsequence
2+
3+
Hard
4+
5+
Given an integer array `nums`, return _the number of all the **arithmetic subsequences** of_ `nums`.
6+
7+
A sequence of numbers is called arithmetic if it consists of **at least three elements** and if the difference between any two consecutive elements is the same.
8+
9+
* For example, `[1, 3, 5, 7, 9]`, `[7, 7, 7, 7]`, and `[3, -1, -5, -9]` are arithmetic sequences.
10+
* For example, `[1, 1, 2, 5, 7]` is not an arithmetic sequence.
11+
12+
A **subsequence** of an array is a sequence that can be formed by removing some elements (possibly none) of the array.
13+
14+
* For example, `[2,5,10]` is a subsequence of `[1,2,1,**2**,4,1,**5**,**10**]`.
15+
16+
The test cases are generated so that the answer fits in **32-bit** integer.
17+
18+
**Example 1:**
19+
20+
**Input:** nums = [2,4,6,8,10]
21+
22+
**Output:** 7
23+
24+
**Explanation:** All arithmetic subsequence slices are:
25+
26+
[2,4,6]
27+
28+
[4,6,8]
29+
30+
[6,8,10]
31+
32+
[2,4,6,8]
33+
34+
[4,6,8,10]
35+
36+
[2,4,6,8,10]
37+
38+
[2,6,10]
39+
40+
**Example 2:**
41+
42+
**Input:** nums = [7,7,7,7,7]
43+
44+
**Output:** 16
45+
46+
**Explanation:** Any subsequence of this array is arithmetic.
47+
48+
**Constraints:**
49+
50+
* `1 <= nums.length <= 1000`
51+
* <code>-2<sup>31</sup> <= nums[i] <= 2<sup>31</sup> - 1</code>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package g0401_0500.s0447_number_of_boomerangs
2+
3+
// #Medium #Array #Hash_Table #Math #2022_12_24_Time_308_ms_(100.00%)_Space_41.1_MB_(100.00%)
4+
5+
class Solution {
6+
fun numberOfBoomerangs(points: Array<IntArray>): Int {
7+
val m: HashMap<Int, Int> = HashMap()
8+
var ans = 0
9+
for (i in points.indices) {
10+
for (j in points.indices) {
11+
if (i == j) {
12+
continue
13+
}
14+
val dis = dist(points[i], points[j])
15+
val prev = m.getOrDefault(dis, 0)
16+
if (prev >= 1) {
17+
ans += prev * 2
18+
}
19+
m[dis] = prev + 1
20+
}
21+
m.clear()
22+
}
23+
return ans
24+
}
25+
26+
private fun dist(d1: IntArray, d2: IntArray): Int {
27+
return (d1[0] - d2[0]) * (d1[0] - d2[0]) + (d1[1] - d2[1]) * (d1[1] - d2[1])
28+
}
29+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
447\. Number of Boomerangs
2+
3+
Medium
4+
5+
You are given `n` `points` in the plane that are all **distinct**, where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code>. A **boomerang** is a tuple of points `(i, j, k)` such that the distance between `i` and `j` equals the distance between `i` and `k` **(the order of the tuple matters)**.
6+
7+
Return _the number of boomerangs_.
8+
9+
**Example 1:**
10+
11+
**Input:** points = [[0,0],[1,0],[2,0]]
12+
13+
**Output:** 2
14+
15+
**Explanation:** The two boomerangs are [[1,0],[0,0],[2,0]] and [[1,0],[2,0],[0,0]].
16+
17+
**Example 2:**
18+
19+
**Input:** points = [[1,1],[2,2],[3,3]]
20+
21+
**Output:** 2
22+
23+
**Example 3:**
24+
25+
**Input:** points = [[1,1]]
26+
27+
**Output:** 0
28+
29+
**Constraints:**
30+
31+
* `n == points.length`
32+
* `1 <= n <= 500`
33+
* `points[i].length == 2`
34+
* <code>-10<sup>4</sup> <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>4</sup></code>
35+
* All the points are **unique**.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package g0401_0500.s0448_find_all_numbers_disappeared_in_an_array
2+
3+
// #Easy #Array #Hash_Table #Udemy_Arrays #2022_12_24_Time_394_ms_(100.00%)_Space_46_MB_(94.74%)
4+
5+
class Solution {
6+
fun findDisappearedNumbers(nums: IntArray): List<Int> {
7+
val arr = IntArray(nums.size)
8+
var counter = 0
9+
for (i in nums.indices) {
10+
arr[nums[counter] - 1] = 1
11+
counter++
12+
}
13+
val res: MutableList<Int> = ArrayList()
14+
for (i in arr.indices) {
15+
if (arr[i] == 0) {
16+
res.add(i + 1)
17+
}
18+
}
19+
return res
20+
}
21+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
448\. Find All Numbers Disappeared in an Array
2+
3+
Easy
4+
5+
Given an array `nums` of `n` integers where `nums[i]` is in the range `[1, n]`, return _an array of all the integers in the range_ `[1, n]` _that do not appear in_ `nums`.
6+
7+
**Example 1:**
8+
9+
**Input:** nums = [4,3,2,7,8,2,3,1]
10+
11+
**Output:** [5,6]
12+
13+
**Example 2:**
14+
15+
**Input:** nums = [1,1]
16+
17+
**Output:** [2]
18+
19+
**Constraints:**
20+
21+
* `n == nums.length`
22+
* <code>1 <= n <= 10<sup>5</sup></code>
23+
* `1 <= nums[i] <= n`
24+
25+
**Follow up:** Could you do it without extra space and in `O(n)` runtime? You may assume the returned list does not count as extra space.
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package g0401_0500.s0449_serialize_and_deserialize_bst
2+
3+
// #Medium #String #Depth_First_Search #Breadth_First_Search #Tree #Binary_Tree #Design
4+
// #Binary_Search_Tree #2022_12_24_Time_233_ms_(100.00%)_Space_36.3_MB_(100.00%)
5+
6+
import com_github_leetcode.TreeNode
7+
8+
/*
9+
* Definition for a binary tree node.
10+
* class TreeNode(var `val`: Int) {
11+
* var left: TreeNode? = null
12+
* var right: TreeNode? = null
13+
* }
14+
*/
15+
16+
class Codec {
17+
private var cur = 0
18+
19+
// Encodes a tree to a single string.
20+
fun serialize(root: TreeNode?): String {
21+
val sb = StringBuilder()
22+
serializeDfs(root, sb)
23+
return sb.toString()
24+
}
25+
26+
private fun serializeDfs(root: TreeNode?, sb: StringBuilder) {
27+
if (root == null) {
28+
sb.append(SPLIT)
29+
return
30+
}
31+
sb.append((root.`val` + MIN).toChar())
32+
serializeDfs(root.left, sb)
33+
serializeDfs(root.right, sb)
34+
}
35+
36+
// Decodes your encoded data to tree.
37+
fun deserialize(data: String): TreeNode? {
38+
cur = 0
39+
return deserializeDFS(data.toCharArray())
40+
}
41+
42+
private fun deserializeDFS(data: CharArray): TreeNode? {
43+
if (data[cur] == SPLIT) {
44+
cur++
45+
return null
46+
}
47+
val node = TreeNode(data[cur++].code - MIN)
48+
node.left = deserializeDFS(data)
49+
node.right = deserializeDFS(data)
50+
return node
51+
}
52+
53+
companion object {
54+
private const val SPLIT = 0.toChar()
55+
private const val MIN = 1
56+
}
57+
}
58+
59+
/*
60+
* Your Codec object will be instantiated and called as such:
61+
* val ser = Codec()
62+
* val deser = Codec()
63+
* val tree: String = ser.serialize(root)
64+
* val ans = deser.deserialize(tree)
65+
* return ans
66+
*/
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
449\. Serialize and Deserialize BST
2+
3+
Medium
4+
5+
Serialization is converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection link to be reconstructed later in the same or another computer environment.
6+
7+
Design an algorithm to serialize and deserialize a **binary search tree**. There is no restriction on how your serialization/deserialization algorithm should work. You need to ensure that a binary search tree can be serialized to a string, and this string can be deserialized to the original tree structure.
8+
9+
**The encoded string should be as compact as possible.**
10+
11+
**Example 1:**
12+
13+
**Input:** root = [2,1,3]
14+
15+
**Output:** [2,1,3]
16+
17+
**Example 2:**
18+
19+
**Input:** root = []
20+
21+
**Output:** []
22+
23+
**Constraints:**
24+
25+
* The number of nodes in the tree is in the range <code>[0, 10<sup>4</sup>]</code>.
26+
* <code>0 <= Node.val <= 10<sup>4</sup></code>
27+
* The input tree is **guaranteed** to be a binary search tree.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package g0401_0500.s0446_arithmetic_slices_ii_subsequence
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 numberOfArithmeticSlices() {
10+
assertThat(Solution().numberOfArithmeticSlices(intArrayOf(2, 4, 6, 8, 10)), equalTo(7))
11+
}
12+
13+
@Test
14+
fun numberOfArithmeticSlices2() {
15+
assertThat(Solution().numberOfArithmeticSlices(intArrayOf(7, 7, 7, 7, 7)), equalTo(16))
16+
}
17+
}

0 commit comments

Comments
 (0)