Skip to content

Commit 9d3926b

Browse files
authored
Added tasks 701, 703, 704, 705
1 parent 13ddf25 commit 9d3926b

File tree

13 files changed

+348
-0
lines changed

13 files changed

+348
-0
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.9'
368368

369369
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
370370
|-|-|-|-|-|-
371+
| 0704 |[Binary Search](src/main/kotlin/g0701_0800/s0704_binary_search/Solution.kt)| Easy | Array, Binary_Search | 261 | 77.91
371372
| 0278 |[First Bad Version](src.save/main/kotlin/g0201_0300/s0278_first_bad_version/Solution.kt)| Easy | Binary_Search, Interactive | 349 | 76.86
372373

373374
#### Day 8 Binary Search Tree
@@ -597,6 +598,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.9'
597598

598599
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
599600
|-|-|-|-|-|-
601+
| 0704 |[Binary Search](src/main/kotlin/g0701_0800/s0704_binary_search/Solution.kt)| Easy | Array, Binary_Search | 261 | 77.91
600602
| 0033 |[Search in Rotated Sorted Array](src.save/main/kotlin/g0001_0100/s0033_search_in_rotated_sorted_array/Solution.kt)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Binary_Search | 184 | 86.08
601603
| 0153 |[Find Minimum in Rotated Sorted Array](src.save/main/kotlin/g0101_0200/s0153_find_minimum_in_rotated_sorted_array/Solution.kt)| Medium | Top_100_Liked_Questions, Array, Binary_Search | 262 | 60.96
602604

@@ -693,6 +695,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.9'
693695
| 0111 |[Minimum Depth of Binary Tree](src.save/main/kotlin/g0101_0200/s0111_minimum_depth_of_binary_tree/Solution.kt)| Easy | Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree | 525 | 90.51
694696
| 0104 |[Maximum Depth of Binary Tree](src.save/main/kotlin/g0101_0200/s0104_maximum_depth_of_binary_tree/Solution.kt)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree | 236 | 83.39
695697
| 0110 |[Balanced Binary Tree](src.save/main/kotlin/g0101_0200/s0110_balanced_binary_tree/Solution.kt)| Easy | Depth_First_Search, Tree, Binary_Tree | 310 | 63.63
698+
| 0701 |[Insert into a Binary Search Tree](src/main/kotlin/g0701_0800/s0701_insert_into_a_binary_search_tree/Solution.kt)| Medium | Tree, Binary_Tree, Binary_Search_Tree | 311 | 79.03
696699
| 0297 |[Serialize and Deserialize Binary Tree](src.save/main/kotlin/g0201_0300/s0297_serialize_and_deserialize_binary_tree/Codec.kt)| Hard | Top_Interview_Questions, String, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Design | 475 | 78.85
697700
| 0124 |[Binary Tree Maximum Path Sum](src.save/main/kotlin/g0101_0200/s0124_binary_tree_maximum_path_sum/Solution.kt)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Dynamic_Programming, Depth_First_Search, Tree, Binary_Tree | 331 | 74.42
698701
| 0098 |[Validate Binary Search Tree](src.save/main/kotlin/g0001_0100/s0098_validate_binary_search_tree/Solution.kt)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Tree, Binary_Tree, Binary_Search_Tree | 330 | 41.38
@@ -857,6 +860,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.9'
857860
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
858861
|-|-|-|-|-|-
859862
| 0700 |[Search in a Binary Search Tree](src/main/kotlin/g0601_0700/s0700_search_in_a_binary_search_tree/Solution.kt)| Easy | Tree, Binary_Tree, Binary_Search_Tree | 251 | 88.31
863+
| 0701 |[Insert into a Binary Search Tree](src/main/kotlin/g0701_0800/s0701_insert_into_a_binary_search_tree/Solution.kt)| Medium | Tree, Binary_Tree, Binary_Search_Tree | 311 | 79.03
860864

861865
#### Day 14 Tree
862866

@@ -1021,6 +1025,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.9'
10211025

10221026
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
10231027
|-|-|-|-|-|-
1028+
| 0704 |[Binary Search](src/main/kotlin/g0701_0800/s0704_binary_search/Solution.kt)| Easy | Array, Binary_Search | 261 | 77.91
10241029
| 0278 |[First Bad Version](src.save/main/kotlin/g0201_0300/s0278_first_bad_version/Solution.kt)| Easy | Binary_Search, Interactive | 349 | 76.86
10251030
| 0035 |[Search Insert Position](src.save/main/kotlin/g0001_0100/s0035_search_insert_position/Solution.kt)| Easy | Top_100_Liked_Questions, Array, Binary_Search | 267 | 50.32
10261031

@@ -1268,6 +1273,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.9'
12681273

12691274
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
12701275
|-|-|-|-|-|-
1276+
| 0704 |[Binary Search](src/main/kotlin/g0701_0800/s0704_binary_search/Solution.kt)| Easy | Array, Binary_Search | 261 | 77.91
12711277
| 0374 |[Guess Number Higher or Lower](src.save/main/kotlin/g0301_0400/s0374_guess_number_higher_or_lower/Solution.kt)| Easy | Binary_Search, Interactive | 134 | 94.19
12721278

12731279
#### Day 2
@@ -1681,6 +1687,10 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.9'
16811687
| 0864 |[Shortest Path to Get All Keys](src/main/kotlin/g0801_0900/s0864_shortest_path_to_get_all_keys/Solution.kt)| Hard | Breadth_First_Search, Bit_Manipulation | 176 | 100.00
16821688
| 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
16831689
| 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
1690+
| 0705 |[Design HashSet](src/main/kotlin/g0701_0800/s0705_design_hashset/MyHashSet.kt)| Easy | Array, Hash_Table, Design, Linked_List, Hash_Function | 385 | 75.61
1691+
| 0704 |[Binary Search](src/main/kotlin/g0701_0800/s0704_binary_search/Solution.kt)| Easy | Array, Binary_Search, Algorithm_I_Day_1_Binary_Search, Binary_Search_I_Day_1, Level_1_Day_7_Binary_Search, Udemy_Binary_Search | 261 | 77.91
1692+
| 0703 |[Kth Largest Element in a Stream](src/main/kotlin/g0701_0800/s0703_kth_largest_element_in_a_stream/KthLargest.kt)| Easy | Tree, Binary_Tree, Design, Heap_Priority_Queue, Binary_Search_Tree, Data_Stream | 286 | 95.45
1693+
| 0701 |[Insert into a Binary Search Tree](src/main/kotlin/g0701_0800/s0701_insert_into_a_binary_search_tree/Solution.kt)| Medium | Tree, Binary_Tree, Binary_Search_Tree, Data_Structure_I_Day_13_Tree, Udemy_Tree_Stack_Queue | 311 | 79.03
16841694
| 0700 |[Search in a Binary Search Tree](src/main/kotlin/g0601_0700/s0700_search_in_a_binary_search_tree/Solution.kt)| Easy | Tree, Binary_Tree, Binary_Search_Tree, Data_Structure_I_Day_13_Tree | 251 | 88.31
16851695
| 0699 |[Falling Squares](src/main/kotlin/g0601_0700/s0699_falling_squares/Solution.kt)| Hard | Array, Ordered_Set, Segment_Tree | 293 | 100.00
16861696
| 0698 |[Partition to K Equal Sum Subsets](src/main/kotlin/g0601_0700/s0698_partition_to_k_equal_sum_subsets/Solution.kt)| Medium | Array, Dynamic_Programming, Bit_Manipulation, Backtracking, Bitmask, Memoization | 191 | 100.00
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package g0701_0800.s0701_insert_into_a_binary_search_tree
2+
3+
// #Medium #Tree #Binary_Tree #Binary_Search_Tree #Data_Structure_I_Day_13_Tree
4+
// #Udemy_Tree_Stack_Queue #2023_02_23_Time_311_ms_(79.03%)_Space_38.5_MB_(11.29%)
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 insertIntoBST(
20+
root: TreeNode?,
21+
value: Int
22+
): TreeNode? {
23+
if (root == null) {
24+
return TreeNode(value)
25+
}
26+
27+
when {
28+
root.value < value -> root.right = insertIntoBST(root.right, value)
29+
root.value > value -> root.left = insertIntoBST(root.left, value)
30+
}
31+
32+
return root
33+
}
34+
private val TreeNode.value get() = `val`
35+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
701\. Insert into a Binary Search Tree
2+
3+
Medium
4+
5+
You are given the `root` node of a binary search tree (BST) and a `value` to insert into the tree. Return _the root node of the BST after the insertion_. It is **guaranteed** that the new value does not exist in the original BST.
6+
7+
**Notice** that there may exist multiple valid ways for the insertion, as long as the tree remains a BST after insertion. You can return **any of them**.
8+
9+
**Example 1:**
10+
11+
![](https://assets.leetcode.com/uploads/2020/10/05/insertbst.jpg)
12+
13+
**Input:** root = [4,2,7,1,3], val = 5
14+
15+
**Output:** [4,2,7,1,3,5]
16+
17+
**Explanation:** Another accepted tree is: ![](https://assets.leetcode.com/uploads/2020/10/05/bst.jpg)
18+
19+
**Example 2:**
20+
21+
**Input:** root = [40,20,60,10,30,50,70], val = 25
22+
23+
**Output:** [40,20,60,10,30,50,70,null,null,25]
24+
25+
**Example 3:**
26+
27+
**Input:** root = [4,2,7,1,3,null,null,null,null,null,null], val = 5
28+
29+
**Output:** [4,2,7,1,3,5]
30+
31+
**Constraints:**
32+
33+
* The number of nodes in the tree will be in the range <code>[0, 10<sup>4</sup>]</code>.
34+
* <code>-10<sup>8</sup> <= Node.val <= 10<sup>8</sup></code>
35+
* All the values `Node.val` are **unique**.
36+
* <code>-10<sup>8</sup> <= val <= 10<sup>8</sup></code>
37+
* It's **guaranteed** that `val` does not exist in the original BST.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package g0701_0800.s0703_kth_largest_element_in_a_stream
2+
3+
// #Easy #Tree #Binary_Tree #Design #Heap_Priority_Queue #Binary_Search_Tree #Data_Stream
4+
// #2023_02_23_Time_286_ms_(95.45%)_Space_59.4_MB_(32.95%)
5+
6+
import java.util.PriorityQueue
7+
8+
class KthLargest(private val maxSize: Int, nums: IntArray) {
9+
private val heap: PriorityQueue<Int> = PriorityQueue()
10+
11+
init {
12+
for (num in nums) {
13+
add(num)
14+
}
15+
}
16+
17+
fun add(`val`: Int): Int {
18+
if (heap.size < maxSize) {
19+
heap.add(`val`)
20+
} else if (heap.peek() < `val`) {
21+
heap.add(`val`)
22+
heap.poll()
23+
}
24+
return heap.peek()
25+
}
26+
}
27+
28+
/*
29+
* Your KthLargest object will be instantiated and called as such:
30+
* var obj = KthLargest(k, nums)
31+
* var param_1 = obj.add(`val`)
32+
*/
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
703\. Kth Largest Element in a Stream
2+
3+
Easy
4+
5+
Design a class to find the <code>k<sup>th</sup></code> largest element in a stream. Note that it is the <code>k<sup>th</sup></code> largest element in the sorted order, not the <code>k<sup>th</sup></code> distinct element.
6+
7+
Implement `KthLargest` class:
8+
9+
* `KthLargest(int k, int[] nums)` Initializes the object with the integer `k` and the stream of integers `nums`.
10+
* `int add(int val)` Appends the integer `val` to the stream and returns the element representing the <code>k<sup>th</sup></code> largest element in the stream.
11+
12+
**Example 1:**
13+
14+
**Input**
15+
16+
["KthLargest", "add", "add", "add", "add", "add"]
17+
[[3, [4, 5, 8, 2]], [3], [5], [10], [9], [4]]
18+
19+
**Output:** [null, 4, 5, 5, 8, 8]
20+
21+
**Explanation:**
22+
23+
KthLargest kthLargest = new KthLargest(3, [4, 5, 8, 2]);
24+
kthLargest.add(3); // return 4
25+
kthLargest.add(5); // return 5
26+
kthLargest.add(10); // return 5
27+
kthLargest.add(9); // return 8
28+
kthLargest.add(4); // return 8
29+
30+
**Constraints:**
31+
32+
* <code>1 <= k <= 10<sup>4</sup></code>
33+
* <code>0 <= nums.length <= 10<sup>4</sup></code>
34+
* <code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code>
35+
* <code>-10<sup>4</sup> <= val <= 10<sup>4</sup></code>
36+
* At most <code>10<sup>4</sup></code> calls will be made to `add`.
37+
* It is guaranteed that there will be at least `k` elements in the array when you search for the <code>k<sup>th</sup></code> element.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package g0701_0800.s0704_binary_search
2+
3+
// #Easy #Array #Binary_Search #Algorithm_I_Day_1_Binary_Search #Binary_Search_I_Day_1
4+
// #Level_1_Day_7_Binary_Search #Udemy_Binary_Search
5+
// #2023_02_23_Time_261_ms_(77.91%)_Space_38.7_MB_(34.19%)
6+
7+
class Solution {
8+
fun search(nums: IntArray, target: Int): Int {
9+
val index = nums.binarySearch(target)
10+
return if (index >= 0) index else -1
11+
}
12+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
704\. Binary Search
2+
3+
Easy
4+
5+
Given an array of integers `nums` which is sorted in ascending order, and an integer `target`, write a function to search `target` in `nums`. If `target` exists, then return its index. Otherwise, return `-1`.
6+
7+
You must write an algorithm with `O(log n)` runtime complexity.
8+
9+
**Example 1:**
10+
11+
**Input:** nums = [-1,0,3,5,9,12], target = 9
12+
13+
**Output:** 4
14+
15+
**Explanation:** 9 exists in nums and its index is 4
16+
17+
**Example 2:**
18+
19+
**Input:** nums = [-1,0,3,5,9,12], target = 2
20+
21+
**Output:** -1
22+
23+
**Explanation:** 2 does not exist in nums so return -1
24+
25+
**Constraints:**
26+
27+
* <code>1 <= nums.length <= 10<sup>4</sup></code>
28+
* <code>-10<sup>4</sup> < nums[i], target < 10<sup>4</sup></code>
29+
* All the integers in `nums` are **unique**.
30+
* `nums` is sorted in ascending order.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package g0701_0800.s0705_design_hashset
2+
3+
// #Easy #Array #Hash_Table #Design #Linked_List #Hash_Function
4+
// #2023_02_23_Time_385_ms_(75.61%)_Space_111.9_MB_(7.32%)
5+
6+
class MyHashSet {
7+
private val arr: BooleanArray = BooleanArray(1000001)
8+
9+
fun add(key: Int) {
10+
arr[key] = true
11+
}
12+
13+
fun remove(key: Int) {
14+
arr[key] = false
15+
}
16+
17+
operator fun contains(key: Int): Boolean {
18+
return arr[key]
19+
}
20+
}
21+
22+
/*
23+
* Your MyHashSet object will be instantiated and called as such:
24+
* var obj = MyHashSet()
25+
* obj.add(key)
26+
* obj.remove(key)
27+
* var param_3 = obj.contains(key)
28+
*/
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
705\. Design HashSet
2+
3+
Easy
4+
5+
Design a HashSet without using any built-in hash table libraries.
6+
7+
Implement `MyHashSet` class:
8+
9+
* `void add(key)` Inserts the value `key` into the HashSet.
10+
* `bool contains(key)` Returns whether the value `key` exists in the HashSet or not.
11+
* `void remove(key)` Removes the value `key` in the HashSet. If `key` does not exist in the HashSet, do nothing.
12+
13+
**Example 1:**
14+
15+
**Input**
16+
17+
["MyHashSet", "add", "add", "contains", "contains", "add", "contains", "remove", "contains"]
18+
[[], [1], [2], [1], [3], [2], [2], [2], [2]]
19+
20+
**Output:** [null, null, null, true, false, null, true, null, false]
21+
22+
**Explanation:**
23+
24+
MyHashSet myHashSet = new MyHashSet();
25+
myHashSet.add(1); // set = [1]
26+
myHashSet.add(2); // set = [1, 2]
27+
myHashSet.contains(1); // return True
28+
myHashSet.contains(3); // return False, (not found)
29+
myHashSet.add(2); // set = [1, 2]
30+
myHashSet.contains(2); // return True
31+
myHashSet.remove(2); // set = [1]
32+
myHashSet.contains(2); // return False, (already removed)
33+
34+
**Constraints:**
35+
36+
* <code>0 <= key <= 10<sup>6</sup></code>
37+
* At most <code>10<sup>4</sup></code> calls will be made to `add`, `remove`, and `contains`.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package g0701_0800.s0701_insert_into_a_binary_search_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 insertIntoBST() {
11+
val treeNode: TreeNode? = TreeNode.create(listOf(4, 2, 7, 1, 3))
12+
val expected: TreeNode? = TreeNode.create(listOf(4, 2, 7, 1, 3, 5))
13+
assertThat(
14+
Solution().insertIntoBST(treeNode, 5).toString(), equalTo(expected.toString())
15+
)
16+
}
17+
18+
@Test
19+
fun insertIntoBST2() {
20+
val treeNode: TreeNode? = TreeNode.create(listOf(40, 20, 60, 10, 30, 50, 70))
21+
val expected: TreeNode? = TreeNode.create(listOf(40, 20, 60, 10, 30, 50, 70, null, null, 25))
22+
assertThat(
23+
Solution().insertIntoBST(treeNode, 25).toString(),
24+
equalTo(expected.toString())
25+
)
26+
}
27+
28+
@Test
29+
fun insertIntoBST3() {
30+
val treeNode: TreeNode? = TreeNode.create(listOf(4, 2, 7, 1, 3, null, null, null, null, null, null))
31+
val expected: TreeNode? = TreeNode.create(listOf(4, 2, 7, 1, 3, 5))
32+
assertThat(
33+
Solution().insertIntoBST(treeNode, 5).toString(), equalTo(expected.toString())
34+
)
35+
}
36+
}

0 commit comments

Comments
 (0)