Skip to content

Commit dae8b90

Browse files
authored
Added tasks 936, 937, 938, 939
1 parent fed1bfe commit dae8b90

File tree

13 files changed

+475
-0
lines changed

13 files changed

+475
-0
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.12'
492492
| 0103 |[Binary Tree Zigzag Level Order Traversal](src/main/kotlin/g0101_0200/s0103_binary_tree_zigzag_level_order_traversal/Solution.kt)| Medium | Top_Interview_Questions, Breadth_First_Search, Tree, Binary_Tree | 316 | 34.25
493493
| 0108 |[Convert Sorted Array to Binary Search Tree](src/main/kotlin/g0101_0200/s0108_convert_sorted_array_to_binary_search_tree/Solution.kt)| Easy | Top_Interview_Questions, Array, Tree, Binary_Tree, Binary_Search_Tree, Divide_and_Conquer | 334 | 35.39
494494
| 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 | 307 | 43.93
495+
| 0938 |[Range Sum of BST](src/main/kotlin/g0901_1000/s0938_range_sum_of_bst/Solution.kt)| Easy | Depth_First_Search, Tree, Binary_Tree, Binary_Search_Tree | 356 | 55.36
495496
| 0100 |[Same Tree](src/main/kotlin/g0001_0100/s0100_same_tree/Solution.kt)| Easy | Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree | 208 | 72.24
496497
| 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 | 233 | 54.90
497498
| 0111 |[Minimum Depth of Binary Tree](src/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
@@ -1732,6 +1733,10 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.12'
17321733
|------|----------------|-------------|-------------|----------|---------
17331734
| 1143 |[Longest Common Subsequence](src/main/kotlin/g1101_1200/s1143_longest_common_subsequence/Solution.kt)| Medium | Top_100_Liked_Questions, String, Dynamic_Programming, Algorithm_II_Day_17_Dynamic_Programming, Dynamic_Programming_I_Day_19, Udemy_Dynamic_Programming | 307 | 38.36
17341735
| 0994 |[Rotting Oranges](src/main/kotlin/g0901_1000/s0994_rotting_oranges/Solution.kt)| Medium | Array, Breadth_First_Search, Matrix, Algorithm_I_Day_9_Breadth_First_Search_Depth_First_Search, Level_2_Day_10_Graph/BFS/DFS | 308 | 57.93
1736+
| 0939 |[Minimum Area Rectangle](src/main/kotlin/g0901_1000/s0939_minimum_area_rectangle/Solution.kt)| Medium | Array, Hash_Table, Math, Sorting, Geometry | 461 | 100.00
1737+
| 0938 |[Range Sum of BST](src/main/kotlin/g0901_1000/s0938_range_sum_of_bst/Solution.kt)| Easy | Depth_First_Search, Tree, Binary_Tree, Binary_Search_Tree, Udemy_Tree_Stack_Queue | 356 | 55.36
1738+
| 0937 |[Reorder Data in Log Files](src/main/kotlin/g0901_1000/s0937_reorder_data_in_log_files/Solution.kt)| Easy | Array, String, Sorting | 205 | 81.82
1739+
| 0936 |[Stamping The Sequence](src/main/kotlin/g0901_1000/s0936_stamping_the_sequence/Solution.kt)| Hard | String, Greedy, Stack, Queue | 196 | 100.00
17351740
| 0935 |[Knight Dialer](src/main/kotlin/g0901_1000/s0935_knight_dialer/Solution.kt)| Medium | Dynamic_Programming | 160 | 100.00
17361741
| 0934 |[Shortest Bridge](src/main/kotlin/g0901_1000/s0934_shortest_bridge/Solution.kt)| Medium | Array, Depth_First_Search, Breadth_First_Search, Matrix, Graph_Theory_I_Day_6_Matrix_Related_Problems | 301 | 80.95
17371742
| 0933 |[Number of Recent Calls](src/main/kotlin/g0901_1000/s0933_number_of_recent_calls/RecentCounter.kt)| Easy | Design, Queue, Data_Stream | 476 | 82.50
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package g0901_1000.s0936_stamping_the_sequence
2+
3+
// #Hard #String #Greedy #Stack #Queue #2023_04_28_Time_196_ms_(100.00%)_Space_40.3_MB_(100.00%)
4+
5+
@Suppress("NAME_SHADOWING")
6+
class Solution {
7+
fun movesToStamp(stamp: String, target: String): IntArray {
8+
val moves: MutableList<Int> = ArrayList()
9+
val s = stamp.toCharArray()
10+
val t = target.toCharArray()
11+
var stars = 0
12+
val visited = BooleanArray(target.length)
13+
while (stars < target.length) {
14+
var doneReplace = false
15+
for (i in 0..target.length - stamp.length) {
16+
if (!visited[i] && canReplace(t, i, s)) {
17+
stars = doReplace(t, i, s, stars)
18+
doneReplace = true
19+
visited[i] = true
20+
moves.add(i)
21+
if (stars == t.size) {
22+
break
23+
}
24+
}
25+
}
26+
if (!doneReplace) {
27+
return IntArray(0)
28+
}
29+
}
30+
val result = IntArray(moves.size)
31+
for (i in moves.indices) {
32+
result[i] = moves[moves.size - i - 1]
33+
}
34+
return result
35+
}
36+
37+
private fun canReplace(t: CharArray, i: Int, s: CharArray): Boolean {
38+
for (j in s.indices) {
39+
if (t[i + j] != '*' && t[i + j] != s[j]) {
40+
return false
41+
}
42+
}
43+
return true
44+
}
45+
46+
private fun doReplace(t: CharArray, i: Int, s: CharArray, stars: Int): Int {
47+
var stars = stars
48+
for (j in s.indices) {
49+
if (t[i + j] != '*') {
50+
t[i + j] = '*'
51+
stars++
52+
}
53+
}
54+
return stars
55+
}
56+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
936\. Stamping The Sequence
2+
3+
Hard
4+
5+
You are given two strings `stamp` and `target`. Initially, there is a string `s` of length `target.length` with all `s[i] == '?'`.
6+
7+
In one turn, you can place `stamp` over `s` and replace every letter in the `s` with the corresponding letter from `stamp`.
8+
9+
* For example, if `stamp = "abc"` and `target = "abcba"`, then `s` is `"?????"` initially. In one turn you can:
10+
11+
* place `stamp` at index `0` of `s` to obtain `"abc??"`,
12+
* place `stamp` at index `1` of `s` to obtain `"?abc?"`, or
13+
* place `stamp` at index `2` of `s` to obtain `"??abc"`.
14+
15+
Note that `stamp` must be fully contained in the boundaries of `s` in order to stamp (i.e., you cannot place `stamp` at index `3` of `s`).
16+
17+
We want to convert `s` to `target` using **at most** `10 * target.length` turns.
18+
19+
Return _an array of the index of the left-most letter being stamped at each turn_. If we cannot obtain `target` from `s` within `10 * target.length` turns, return an empty array.
20+
21+
**Example 1:**
22+
23+
**Input:** stamp = "abc", target = "ababc"
24+
25+
**Output:** [0,2]
26+
27+
**Explanation:** Initially s = "?????".
28+
29+
- Place stamp at index 0 to get "abc??".
30+
31+
- Place stamp at index 2 to get "ababc".
32+
33+
[1,0,2] would also be accepted as an answer, as well as some other answers.
34+
35+
**Example 2:**
36+
37+
**Input:** stamp = "abca", target = "aabcaca"
38+
39+
**Output:** [3,0,1]
40+
41+
**Explanation:** Initially s = "???????".
42+
43+
- Place stamp at index 3 to get "???abca".
44+
45+
- Place stamp at index 0 to get "abcabca".
46+
47+
- Place stamp at index 1 to get "aabcaca".
48+
49+
**Constraints:**
50+
51+
* `1 <= stamp.length <= target.length <= 1000`
52+
* `stamp` and `target` consist of lowercase English letters.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package g0901_1000.s0937_reorder_data_in_log_files
2+
3+
// #Easy #Array #String #Sorting #2023_04_28_Time_205_ms_(81.82%)_Space_44_MB_(9.09%)
4+
5+
import java.util.Collections
6+
7+
class Solution {
8+
fun reorderLogFiles(logs: Array<String>): Array<String?> {
9+
val digi: MutableList<String> = ArrayList()
10+
val word: MutableList<String> = ArrayList()
11+
for (s in logs) {
12+
if (Character.isDigit(s[s.length - 1])) digi.add(s) else word.add(s)
13+
}
14+
Collections.sort(
15+
word,
16+
Comparator { s1, s2 ->
17+
val firstSpacePosition = s1.indexOf(" ")
18+
val firstWord = s1.substring(firstSpacePosition, s1.length)
19+
val secondSpacePosition = s2.indexOf(" ")
20+
val secondWord = s2.substring(secondSpacePosition, s2.length)
21+
if (firstWord.compareTo(secondWord) == 0) {
22+
val firstSpacePosition1 = s1.indexOf(" ")
23+
val firstWord1 = s1.substring(0, firstSpacePosition1)
24+
val secondSpacePosition1 = s2.indexOf(" ")
25+
val secondWord1 = s2.substring(0, secondSpacePosition1)
26+
return@Comparator firstWord1.compareTo(secondWord1)
27+
}
28+
firstWord.compareTo(secondWord)
29+
}
30+
)
31+
val result = arrayOfNulls<String>(digi.size + word.size)
32+
var `in` = 0
33+
for (s in word) result[`in`++] = s
34+
for (s in digi) result[`in`++] = s
35+
return result
36+
}
37+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
937\. Reorder Data in Log Files
2+
3+
Medium
4+
5+
You are given an array of `logs`. Each log is a space-delimited string of words, where the first word is the **identifier**.
6+
7+
There are two types of logs:
8+
9+
* **Letter-logs**: All words (except the identifier) consist of lowercase English letters.
10+
* **Digit-logs**: All words (except the identifier) consist of digits.
11+
12+
Reorder these logs so that:
13+
14+
1. The **letter-logs** come before all **digit-logs**.
15+
2. The **letter-logs** are sorted lexicographically by their contents. If their contents are the same, then sort them lexicographically by their identifiers.
16+
3. The **digit-logs** maintain their relative ordering.
17+
18+
Return _the final order of the logs_.
19+
20+
**Example 1:**
21+
22+
**Input:** logs = ["dig1 8 1 5 1","let1 art can","dig2 3 6","let2 own kit dig","let3 art zero"]
23+
24+
**Output:** ["let1 art can","let3 art zero","let2 own kit dig","dig1 8 1 5 1","dig2 3 6"]
25+
26+
**Explanation:**
27+
28+
The letter-log contents are all different, so their ordering is "art can", "art zero", "own kit dig".
29+
30+
The digit-logs have a relative order of "dig1 8 1 5 1", "dig2 3 6".
31+
32+
**Example 2:**
33+
34+
**Input:** logs = ["a1 9 2 3 1","g1 act car","zo4 4 7","ab1 off key dog","a8 act zoo"]
35+
36+
**Output:** ["g1 act car","a8 act zoo","ab1 off key dog","a1 9 2 3 1","zo4 4 7"]
37+
38+
**Constraints:**
39+
40+
* `1 <= logs.length <= 100`
41+
* `3 <= logs[i].length <= 100`
42+
* All the tokens of `logs[i]` are separated by a **single** space.
43+
* `logs[i]` is guaranteed to have an identifier and at least one word after the identifier.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package g0901_1000.s0938_range_sum_of_bst
2+
3+
// #Easy #Depth_First_Search #Tree #Binary_Tree #Binary_Search_Tree #Udemy_Tree_Stack_Queue
4+
// #2023_04_29_Time_356_ms_(55.36%)_Space_83.3_MB_(5.36%)
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+
internal class Solution {
19+
fun rangeSumBST(root: TreeNode?, low: Int, high: Int): Int {
20+
var ans = 0
21+
if (root == null) return 0
22+
if (root.`val` in low..high) {
23+
ans += root.`val`
24+
}
25+
if (root.`val` in low..high) {
26+
ans += rangeSumBST(root.left, low, high)
27+
ans += rangeSumBST(root.right, low, high)
28+
} else if (root.`val` >= low && root.`val` >= high) {
29+
ans += rangeSumBST(root.left, low, high)
30+
} else {
31+
ans += rangeSumBST(root.right, low, high)
32+
}
33+
return ans
34+
}
35+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
938\. Range Sum of BST
2+
3+
Easy
4+
5+
Given the `root` node of a binary search tree and two integers `low` and `high`, return _the sum of values of all nodes with a value in the **inclusive** range_ `[low, high]`.
6+
7+
**Example 1:**
8+
9+
![](https://assets.leetcode.com/uploads/2020/11/05/bst1.jpg)
10+
11+
**Input:** root = [10,5,15,3,7,null,18], low = 7, high = 15
12+
13+
**Output:** 32
14+
15+
**Explanation:** Nodes 7, 10, and 15 are in the range [7, 15]. 7 + 10 + 15 = 32.
16+
17+
**Example 2:**
18+
19+
![](https://assets.leetcode.com/uploads/2020/11/05/bst2.jpg)
20+
21+
**Input:** root = [10,5,15,3,7,13,18,1,null,6], low = 6, high = 10
22+
23+
**Output:** 23
24+
25+
**Explanation:** Nodes 6, 7, and 10 are in the range [6, 10]. 6 + 7 + 10 = 23.
26+
27+
**Constraints:**
28+
29+
* The number of nodes in the tree is in the range <code>[1, 2 * 10<sup>4</sup>]</code>.
30+
* <code>1 <= Node.val <= 10<sup>5</sup></code>
31+
* <code>1 <= low <= high <= 10<sup>5</sup></code>
32+
* All `Node.val` are **unique**.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package g0901_1000.s0939_minimum_area_rectangle
2+
3+
// #Medium #Array #Hash_Table #Math #Sorting #Geometry
4+
// #2023_04_29_Time_461_ms_(100.00%)_Space_74.8_MB_(20.00%)
5+
6+
import java.util.Arrays
7+
import kotlin.math.abs
8+
9+
class Solution {
10+
fun minAreaRect(points: Array<IntArray>): Int {
11+
if (points.size < 4) {
12+
return 0
13+
}
14+
val map: MutableMap<Int, MutableSet<Int>> = HashMap()
15+
for (p in points) {
16+
map.putIfAbsent(p[0], HashSet())
17+
map[p[0]]!!.add(p[1])
18+
}
19+
Arrays.sort(
20+
points
21+
) { a: IntArray, b: IntArray ->
22+
if (a[0] == b[0]) Integer.compare(
23+
a[1],
24+
b[1]
25+
) else Integer.compare(a[0], b[0])
26+
}
27+
var min = Int.MAX_VALUE
28+
for (i in 0 until points.size - 2) {
29+
for (j in i + 1 until points.size - 1) {
30+
val p1 = points[i]
31+
val p2 = points[j]
32+
val area = abs((p1[0] - p2[0]) * (p1[1] - p2[1]))
33+
if (area >= min || area == 0) {
34+
continue
35+
}
36+
if (map[p1[0]]!!.contains(p2[1]) && map[p2[0]]!!.contains(p1[1])) {
37+
min = area
38+
}
39+
}
40+
}
41+
return if (min == Int.MAX_VALUE) 0 else min
42+
}
43+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
939\. Minimum Area Rectangle
2+
3+
Medium
4+
5+
You are given an array of points in the **X-Y** plane `points` where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code>.
6+
7+
Return _the minimum area of a rectangle formed from these points, with sides parallel to the X and Y axes_. If there is not any such rectangle, return `0`.
8+
9+
**Example 1:**
10+
11+
![](https://assets.leetcode.com/uploads/2021/08/03/rec1.JPG)
12+
13+
**Input:** points = [[1,1],[1,3],[3,1],[3,3],[2,2]]
14+
15+
**Output:** 4
16+
17+
**Example 2:**
18+
19+
![](https://assets.leetcode.com/uploads/2021/08/03/rec2.JPG)
20+
21+
**Input:** points = [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
22+
23+
**Output:** 2
24+
25+
**Constraints:**
26+
27+
* `1 <= points.length <= 500`
28+
* `points[i].length == 2`
29+
* <code>0 <= x<sub>i</sub>, y<sub>i</sub> <= 4 * 10<sup>4</sup></code>
30+
* All the given points are **unique**.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package g0901_1000.s0936_stamping_the_sequence
2+
3+
import com_github_leetcode.CommonUtils
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 movesToStamp() {
11+
assertThat(
12+
CommonUtils().compareArray(
13+
Solution().movesToStamp("abc", "ababc"), intArrayOf(0, 2)
14+
),
15+
equalTo(true)
16+
)
17+
}
18+
19+
@Test
20+
fun movesToStamp2() {
21+
assertThat(
22+
CommonUtils().compareArray(
23+
Solution().movesToStamp("abca", "aabcaca"), intArrayOf(3, 0, 1)
24+
),
25+
equalTo(true)
26+
)
27+
}
28+
}

0 commit comments

Comments
 (0)