Skip to content

Commit 81d138d

Browse files
authored
Added tasks 220, 222, 223, 224.
1 parent 58b988a commit 81d138d

File tree

13 files changed

+392
-0
lines changed

13 files changed

+392
-0
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.5'
173173

174174
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
175175
|-|-|-|-|-|-
176+
| 0222 |[Count Complete Tree Nodes](src/main/kotlin/g0201_0300/s0222_count_complete_tree_nodes/Solution.kt)| |||
176177

177178
#### Day 11
178179

@@ -1581,7 +1582,11 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.5'
15811582
| 0234 |[Palindrome Linked List](src/main/kotlin/g0201_0300/s0234_palindrome_linked_list/Solution.kt)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Two_Pointers, Stack, Linked_List, Recursion, Level_2_Day_3_Linked_List, Udemy_Linked_List | 641 | 79.53
15821583
| 0230 |[Kth Smallest Element in a BST](src/main/kotlin/g0201_0300/s0230_kth_smallest_element_in_a_bst/Solution.kt)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Tree, Binary_Tree, Binary_Search_Tree, Data_Structure_II_Day_17_Tree, Level_2_Day_9_Binary_Search_Tree | 393 | 33.33
15831584
| 0226 |[Invert Binary Tree](src/main/kotlin/g0201_0300/s0226_invert_binary_tree/Solution.kt)| Easy | Top_100_Liked_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Data_Structure_I_Day_12_Tree, Level_2_Day_6_Tree, Udemy_Tree_Stack_Queue | 233 | 54.90
1585+
| 0224 |[Basic Calculator](src/main/kotlin/g0201_0300/s0224_basic_calculator/Solution.kt)| Hard | String, Math, Stack, Recursion | 294 | 93.33
1586+
| 0223 |[Rectangle Area](src/main/kotlin/g0201_0300/s0223_rectangle_area/Solution.kt)| Medium | Math, Geometry | 291 | 66.67
1587+
| 0222 |[Count Complete Tree Nodes](src/main/kotlin/g0201_0300/s0222_count_complete_tree_nodes/Solution.kt)| |||
15841588
| 0221 |[Maximal Square](src/main/kotlin/g0201_0300/s0221_maximal_square/Solution.kt)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Matrix, Dynamic_Programming_I_Day_16 | 614 | 44.00
1589+
| 0220 |[Contains Duplicate III](src/main/kotlin/g0201_0300/s0220_contains_duplicate_iii/Solution.kt)| Medium | Array, Sorting, Sliding_Window, Ordered_Set, Bucket_Sort | 921 | 72.22
15851590
| 0219 |[Contains Duplicate II](src/main/kotlin/g0201_0300/s0219_contains_duplicate_ii/Solution.kt)| Easy | Array, Hash_Table, Sliding_Window | 813 | 80.46
15861591
| 0218 |[The Skyline Problem](src/main/kotlin/g0201_0300/s0218_the_skyline_problem/Solution.kt)| Hard | Top_Interview_Questions, Array, Heap_Priority_Queue, Ordered_Set, Divide_and_Conquer, Segment_Tree, Binary_Indexed_Tree, Line_Sweep | 365 | 93.14
15871592
| 0217 |[Contains Duplicate](src/main/kotlin/g0201_0300/s0217_contains_duplicate/Solution.kt)| Easy | Top_Interview_Questions, Array, Hash_Table, Sorting, Data_Structure_I_Day_1_Array, Programming_Skills_I_Day_11_Containers_and_Libraries, Udemy_Arrays | 719 | 73.49
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package g0201_0300.s0220_contains_duplicate_iii
2+
3+
// #Medium #Array #Sorting #Sliding_Window #Ordered_Set #Bucket_Sort
4+
// #2022_10_25_Time_921_ms_(72.22%)_Space_77.4_MB_(41.67%)
5+
6+
class Solution {
7+
private fun getId(i: Long, w: Long): Long {
8+
return if (i < 0) (i + 1) / w - 1 else i / w
9+
}
10+
11+
fun containsNearbyAlmostDuplicate(nums: IntArray, k: Int, t: Int): Boolean {
12+
if (t < 0) {
13+
return false
14+
}
15+
val d: MutableMap<Long, Long> = HashMap()
16+
val w = t.toLong() + 1
17+
for (i in nums.indices) {
18+
val m = getId(nums[i].toLong(), w)
19+
if (d.containsKey(m)) {
20+
return true
21+
}
22+
if (d.containsKey(m - 1) && Math.abs(nums[i] - d[m - 1]!!) < w) {
23+
return true
24+
}
25+
if (d.containsKey(m + 1) && Math.abs(nums[i] - d[m + 1]!!) < w) {
26+
return true
27+
}
28+
d[m] = nums[i].toLong()
29+
if (i >= k) {
30+
d.remove(getId(nums[i - k].toLong(), w))
31+
}
32+
}
33+
return false
34+
}
35+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
220\. Contains Duplicate III
2+
3+
Hard
4+
5+
You are given an integer array `nums` and two integers `indexDiff` and `valueDiff`.
6+
7+
Find a pair of indices `(i, j)` such that:
8+
9+
* `i != j`,
10+
* `abs(i - j) <= indexDiff`.
11+
* `abs(nums[i] - nums[j]) <= valueDiff`, and
12+
13+
Return `true` _if such pair exists or_ `false` _otherwise_.
14+
15+
**Example 1:**
16+
17+
**Input:** nums = [1,2,3,1], indexDiff = 3, valueDiff = 0
18+
19+
**Output:** true
20+
21+
**Explanation:** We can choose (i, j) = (0, 3).
22+
23+
We satisfy the three conditions:
24+
25+
i != j --> 0 != 3
26+
27+
abs(i - j) <= indexDiff -->
28+
29+
abs(0 - 3) <= 3
30+
31+
abs(nums[i] - nums[j]) <= valueDiff --> abs(1 - 1) <= 0
32+
33+
**Example 2:**
34+
35+
**Input:** nums = [1,5,9,1,5,9], indexDiff = 2, valueDiff = 3
36+
37+
**Output:** false
38+
39+
**Explanation:** After trying all the possible pairs (i, j), we cannot satisfy the three conditions, so we return false.
40+
41+
**Constraints:**
42+
43+
* <code>2 <= nums.length <= 10<sup>5</sup></code>
44+
* <code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code>
45+
* `1 <= indexDiff <= nums.length`
46+
* <code>0 <= valueDiff <= 10<sup>9</sup></code>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package g0201_0300.s0222_count_complete_tree_nodes
2+
3+
import com_github_leetcode.TreeNode
4+
5+
/*
6+
* Example:
7+
* var ti = TreeNode(5)
8+
* var v = ti.`val`
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+
class Solution {
16+
fun countNodes(root: TreeNode?): Int {
17+
if (root == null) {
18+
return 0
19+
}
20+
val leftHeight = leftHeight(root)
21+
val rightHeight = rightHeight(root)
22+
// case 1: When Height(Left sub-tree) = Height(right sub-tree) 2^h - 1
23+
return if (leftHeight == rightHeight) {
24+
(1 shl leftHeight) - 1
25+
} else {
26+
1 + countNodes(root.left) + countNodes(root.right)
27+
}
28+
}
29+
30+
private fun leftHeight(root: TreeNode?): Int {
31+
return if (root == null) {
32+
0
33+
} else 1 + leftHeight(root.left)
34+
}
35+
36+
private fun rightHeight(root: TreeNode?): Int {
37+
return if (root == null) {
38+
0
39+
} else 1 + rightHeight(root.right)
40+
}
41+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
222\. Count Complete Tree Nodes
2+
3+
Medium
4+
5+
Given the `root` of a **complete** binary tree, return the number of the nodes in the tree.
6+
7+
According to **[Wikipedia](http://en.wikipedia.org/wiki/Binary_tree#Types_of_binary_trees)**, every level, except possibly the last, is completely filled in a complete binary tree, and all nodes in the last level are as far left as possible. It can have between `1` and <code>2<sup>h</sup></code> nodes inclusive at the last level `h`.
8+
9+
Design an algorithm that runs in less than `O(n)` time complexity.
10+
11+
**Example 1:**
12+
13+
![](https://assets.leetcode.com/uploads/2021/01/14/complete.jpg)
14+
15+
**Input:** root = [1,2,3,4,5,6]
16+
17+
**Output:** 6
18+
19+
**Example 2:**
20+
21+
**Input:** root = []
22+
23+
**Output:** 0
24+
25+
**Example 3:**
26+
27+
**Input:** root = [1]
28+
29+
**Output:** 1
30+
31+
**Constraints:**
32+
33+
* The number of nodes in the tree is in the range <code>[0, 5 * 10<sup>4</sup>]</code>.
34+
* <code>0 <= Node.val <= 5 * 10<sup>4</sup></code>
35+
* The tree is guaranteed to be **complete**.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package g0201_0300.s0223_rectangle_area
2+
3+
// #Medium #Math #Geometry #2022_10_25_Time_291_ms_(66.67%)_Space_37.8_MB_(55.56%)
4+
5+
@Suppress("kotlin:S107")
6+
class Solution {
7+
fun computeArea(ax1: Int, ay1: Int, ax2: Int, ay2: Int, bx1: Int, by1: Int, bx2: Int, by2: Int): Int {
8+
val left = Math.max(ax1, bx1).toLong()
9+
val right = Math.min(ax2, bx2).toLong()
10+
val top = Math.min(ay2, by2).toLong()
11+
val bottom = Math.max(ay1, by1).toLong()
12+
var area = (right - left) * (top - bottom)
13+
// if not overlaping, either of these two will be non-posittive
14+
// if right - left = 0, are will automtically be 0 as well
15+
if (right - left < 0 || top - bottom < 0) {
16+
area = 0
17+
}
18+
return ((ax2 - ax1) * (ay2 - ay1) + (bx2 - bx1) * (by2 - by1) - area).toInt()
19+
}
20+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
223\. Rectangle Area
2+
3+
Medium
4+
5+
Given the coordinates of two **rectilinear** rectangles in a 2D plane, return _the total area covered by the two rectangles_.
6+
7+
The first rectangle is defined by its **bottom-left** corner `(ax1, ay1)` and its **top-right** corner `(ax2, ay2)`.
8+
9+
The second rectangle is defined by its **bottom-left** corner `(bx1, by1)` and its **top-right** corner `(bx2, by2)`.
10+
11+
**Example 1:**
12+
13+
![Rectangle Area](https://assets.leetcode.com/uploads/2021/05/08/rectangle-plane.png)
14+
15+
**Input:** ax1 = -3, ay1 = 0, ax2 = 3, ay2 = 4, bx1 = 0, by1 = -1, bx2 = 9, by2 = 2
16+
17+
**Output:** 45
18+
19+
**Example 2:**
20+
21+
**Input:** ax1 = -2, ay1 = -2, ax2 = 2, ay2 = 2, bx1 = -2, by1 = -2, bx2 = 2, by2 = 2
22+
23+
**Output:** 16
24+
25+
**Constraints:**
26+
27+
* <code>-10<sup>4</sup> <= ax1 <= ax2 <= 10<sup>4</sup></code>
28+
* <code>-10<sup>4</sup> <= ay1 <= ay2 <= 10<sup>4</sup></code>
29+
* <code>-10<sup>4</sup> <= bx1 <= bx2 <= 10<sup>4</sup></code>
30+
* <code>-10<sup>4</sup> <= by1 <= by2 <= 10<sup>4</sup></code>
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package g0201_0300.s0224_basic_calculator
2+
3+
// #Hard #String #Math #Stack #Recursion #2022_10_25_Time_294_ms_(93.33%)_Space_40.3_MB_(90.00%)
4+
5+
class Solution {
6+
private var i = 0
7+
fun calculate(s: String): Int {
8+
val ca = s.toCharArray()
9+
return helper(ca)
10+
}
11+
12+
private fun helper(ca: CharArray): Int {
13+
var num = 0
14+
var prenum = 0
15+
var isPlus = true
16+
while (i < ca.size) {
17+
val c = ca[i]
18+
if (c != ' ') {
19+
if (c >= '0' && c <= '9') {
20+
num = if (num == 0) {
21+
c.code - '0'.code
22+
} else {
23+
num * 10 + c.code - '0'.code
24+
}
25+
} else if (c == '+') {
26+
prenum += num * if (isPlus) 1 else -1
27+
isPlus = true
28+
num = 0
29+
} else if (c == '-') {
30+
prenum += num * if (isPlus) 1 else -1
31+
num = 0
32+
isPlus = false
33+
} else if (c == '(') {
34+
i++
35+
prenum += helper(ca) * if (isPlus) 1 else -1
36+
isPlus = true
37+
num = 0
38+
} else if (c == ')') {
39+
return prenum + num * if (isPlus) 1 else -1
40+
}
41+
}
42+
i++
43+
}
44+
return prenum + num * if (isPlus) 1 else -1
45+
}
46+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
224\. Basic Calculator
2+
3+
Hard
4+
5+
Given a string `s` representing a valid expression, implement a basic calculator to evaluate it, and return _the result of the evaluation_.
6+
7+
**Note:** You are **not** allowed to use any built-in function which evaluates strings as mathematical expressions, such as `eval()`.
8+
9+
**Example 1:**
10+
11+
**Input:** s = "1 + 1"
12+
13+
**Output:** 2
14+
15+
**Example 2:**
16+
17+
**Input:** s = " 2-1 + 2 "
18+
19+
**Output:** 3
20+
21+
**Example 3:**
22+
23+
**Input:** s = "(1+(4+5+2)-3)+(6+8)"
24+
25+
**Output:** 23
26+
27+
**Constraints:**
28+
29+
* <code>1 <= s.length <= 3 * 10<sup>5</sup></code>
30+
* `s` consists of digits, `'+'`, `'-'`, `'('`, `')'`, and `' '`.
31+
* `s` represents a valid expression.
32+
* `'+'` is **not** used as a unary operation (i.e., `"+1"` and `"+(2 + 3)"` is invalid).
33+
* `'-'` could be used as a unary operation (i.e., `"-1"` and `"-(2 + 3)"` is valid).
34+
* There will be no two consecutive operators in the input.
35+
* Every number and running calculation will fit in a signed 32-bit integer.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package g0201_0300.s0220_contains_duplicate_iii
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 containsNearbyAlmostDuplicate() {
10+
assertThat(
11+
Solution().containsNearbyAlmostDuplicate(intArrayOf(1, 2, 3, 1), 3, 0),
12+
equalTo(true)
13+
)
14+
}
15+
16+
@Test
17+
fun containsNearbyAlmostDuplicate2() {
18+
assertThat(
19+
Solution().containsNearbyAlmostDuplicate(intArrayOf(1, 0, 1, 1), 1, 2),
20+
equalTo(true)
21+
)
22+
}
23+
24+
@Test
25+
fun containsNearbyAlmostDuplicate3() {
26+
assertThat(
27+
Solution().containsNearbyAlmostDuplicate(intArrayOf(1, 5, 9, 1, 5, 9), 2, 3),
28+
equalTo(false)
29+
)
30+
}
31+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package g0201_0300.s0222_count_complete_tree_nodes
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 countNodes() {
11+
val leftNodeLeftNode = TreeNode(4)
12+
val leftNodeRightNode = TreeNode(5)
13+
val leftNode = TreeNode(2, leftNodeLeftNode, leftNodeRightNode)
14+
val rightNodeLeftNode = TreeNode(6)
15+
val rightNode = TreeNode(3, rightNodeLeftNode, null)
16+
val root = TreeNode(1, leftNode, rightNode)
17+
assertThat(Solution().countNodes(root), equalTo(6))
18+
}
19+
20+
@Test
21+
fun countNodes2() {
22+
assertThat(Solution().countNodes(null), equalTo(0))
23+
}
24+
25+
@Test
26+
fun countNodes3() {
27+
assertThat(Solution().countNodes(TreeNode(1)), equalTo(1))
28+
}
29+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package g0201_0300.s0223_rectangle_area
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 rectangleArea() {
10+
assertThat(Solution().computeArea(-3, 0, 3, 4, 0, -1, 9, 2), equalTo(45))
11+
}
12+
13+
@Test
14+
fun rectangleArea2() {
15+
assertThat(Solution().computeArea(-2, -2, 2, 2, -2, -2, 2, 2), equalTo(16))
16+
}
17+
}

0 commit comments

Comments
 (0)