Skip to content

Commit 39bb359

Browse files
authored
Added tasks 434, 435, 436, 440.
1 parent 9a41fcb commit 39bb359

File tree

13 files changed

+344
-0
lines changed

13 files changed

+344
-0
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1077,6 +1077,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.7'
10771077
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
10781078
|-|-|-|-|-|-
10791079
| 0240 |[Search a 2D Matrix II](src.save/main/kotlin/g0201_0300/s0240_search_a_2d_matrix_ii/Solution.kt)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Binary_Search, Matrix, Divide_and_Conquer | 460 | 66.08
1080+
| 0435 |[Non-overlapping Intervals](src/main/kotlin/g0401_0500/s0435_non_overlapping_intervals/Solution.kt)| Medium | Array, Dynamic_Programming, Sorting, Greedy | 1040 | 85.07
10801081

10811082
#### Day 5 Array
10821083

@@ -1566,6 +1567,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.7'
15661567

15671568
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
15681569
|-|-|-|-|-|-
1570+
| 0436 |[Find Right Interval](src/main/kotlin/g0401_0500/s0436_find_right_interval/Solution.kt)| Medium | Array, Sorting, Binary_Search | 333 | 100.00
15691571

15701572
#### Day 12
15711573

@@ -1628,8 +1630,12 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.7'
16281630
| 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
16291631
| 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
16301632
| 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
1633+
| 0440 |[K-th Smallest in Lexicographical Order](src/main/kotlin/g0401_0500/s0440_k_th_smallest_in_lexicographical_order/Solution.kt)| Hard | Trie | 149 | 100.00
16311634
| 0438 |[Find All Anagrams in a String](src/main/kotlin/g0401_0500/s0438_find_all_anagrams_in_a_string/Solution.kt)| Medium | Top_100_Liked_Questions, String, Hash_Table, Sliding_Window, Algorithm_II_Day_5_Sliding_Window, Programming_Skills_II_Day_12, Level_1_Day_12_Sliding_Window/Two_Pointer | 561 | 54.68
16321635
| 0437 |[Path Sum III](src/main/kotlin/g0401_0500/s0437_path_sum_iii/Solution.kt)| Medium | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree, Level_2_Day_7_Tree | 403 | 54.12
1636+
| 0436 |[Find Right Interval](src/main/kotlin/g0401_0500/s0436_find_right_interval/Solution.kt)| Medium | Array, Sorting, Binary_Search, Binary_Search_II_Day_11 | 333 | 100.00
1637+
| 0435 |[Non-overlapping Intervals](src/main/kotlin/g0401_0500/s0435_non_overlapping_intervals/Solution.kt)| Medium | Array, Dynamic_Programming, Sorting, Greedy, Data_Structure_II_Day_4_Array | 1040 | 85.07
1638+
| 0434 |[Number of Segments in a String](src/main/kotlin/g0401_0500/s0434_number_of_segments_in_a_string/Solution.kt)| Easy | String | 167 | 80.00
16331639
| 0433 |[Minimum Genetic Mutation](src/main/kotlin/g0401_0500/s0433_minimum_genetic_mutation/Solution.kt)| Medium | String, Hash_Table, Breadth_First_Search, Graph_Theory_I_Day_12_Breadth_First_Search | 204 | 82.08
16341640
| 0432 |[All O\`one Data Structure](src/main/kotlin/g0401_0500/s0432_all_oone_data_structure/AllOne.kt)| Hard | Hash_Table, Design, Linked_List, Doubly_Linked_List | 1200 | 100.00
16351641
| 0430 |[Flatten a Multilevel Doubly Linked List](src/main/kotlin/g0401_0500/s0430_flatten_a_multilevel_doubly_linked_list/Solution.kt)| Medium | Depth_First_Search, Linked_List, Doubly_Linked_List | 194 | 97.44
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package g0401_0500.s0434_number_of_segments_in_a_string
2+
3+
// #Easy #String #2022_12_22_Time_167_ms_(80.00%)_Space_35.4_MB_(46.67%)
4+
5+
class Solution {
6+
fun countSegments(s: String): Int {
7+
var s = s
8+
s = s.trim { it <= ' ' }
9+
if (s.length == 0) {
10+
return 0
11+
}
12+
val splitted = s.split(" ".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
13+
var result = 0
14+
for (value in splitted) {
15+
if (value.length > 0) {
16+
result++
17+
}
18+
}
19+
return result
20+
}
21+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
434\. Number of Segments in a String
2+
3+
Easy
4+
5+
Given a string `s`, return _the number of segments in the string_.
6+
7+
A **segment** is defined to be a contiguous sequence of **non-space characters**.
8+
9+
**Example 1:**
10+
11+
**Input:** s = "Hello, my name is John"
12+
13+
**Output:** 5
14+
15+
**Explanation:** The five segments are ["Hello,", "my", "name", "is", "John"]
16+
17+
**Example 2:**
18+
19+
**Input:** s = "Hello"
20+
21+
**Output:** 1
22+
23+
**Constraints:**
24+
25+
* `0 <= s.length <= 300`
26+
* `s` consists of lowercase and uppercase English letters, digits, or one of the following characters `"!@#$%^&*()_+-=',.:"`.
27+
* The only space character in `s` is `' '`.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package g0401_0500.s0435_non_overlapping_intervals
2+
3+
// #Medium #Array #Dynamic_Programming #Sorting #Greedy #Data_Structure_II_Day_4_Array
4+
// #2022_12_22_Time_1040_ms_(85.07%)_Space_117.9_MB_(82.09%)
5+
6+
import java.util.Arrays
7+
8+
class Solution {
9+
fun eraseOverlapIntervals(intervals: Array<IntArray>): Int {
10+
Arrays.sort(intervals) { a: IntArray, b: IntArray ->
11+
if (a[0] != b[0]
12+
) a[0] - b[0] else a[1] - b[1]
13+
}
14+
var erasures = 0
15+
var end = intervals[0][1]
16+
for (i in 1 until intervals.size) {
17+
end = if (intervals[i][0] < end) {
18+
erasures++
19+
Math.min(end, intervals[i][1])
20+
} else {
21+
intervals[i][1]
22+
}
23+
}
24+
return erasures
25+
}
26+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
435\. Non-overlapping Intervals
2+
3+
Medium
4+
5+
Given an array of intervals `intervals` where <code>intervals[i] = [start<sub>i</sub>, end<sub>i</sub>]</code>, return _the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping_.
6+
7+
**Example 1:**
8+
9+
**Input:** intervals = [[1,2],[2,3],[3,4],[1,3]]
10+
11+
**Output:** 1
12+
13+
**Explanation:** [1,3] can be removed and the rest of the intervals are non-overlapping.
14+
15+
**Example 2:**
16+
17+
**Input:** intervals = [[1,2],[1,2],[1,2]]
18+
19+
**Output:** 2
20+
21+
**Explanation:** You need to remove two [1,2] to make the rest of the intervals non-overlapping.
22+
23+
**Example 3:**
24+
25+
**Input:** intervals = [[1,2],[2,3]]
26+
27+
**Output:** 0
28+
29+
**Explanation:** You don't need to remove any of the intervals since they're already non-overlapping.
30+
31+
**Constraints:**
32+
33+
* <code>1 <= intervals.length <= 10<sup>5</sup></code>
34+
* `intervals[i].length == 2`
35+
* <code>-5 * 10<sup>4</sup> <= start<sub>i</sub> < end<sub>i</sub> <= 5 * 10<sup>4</sup></code>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package g0401_0500.s0436_find_right_interval
2+
3+
// #Medium #Array #Sorting #Binary_Search #Binary_Search_II_Day_11
4+
// #2022_12_22_Time_333_ms_(100.00%)_Space_49.7_MB_(90.00%)
5+
6+
import java.util.function.Function
7+
8+
class Solution {
9+
private fun findminmax(num: Array<IntArray>): IntArray {
10+
var min = num[0][0]
11+
var max = num[0][0]
12+
for (i in 1 until num.size) {
13+
min = Math.min(min, num[i][0])
14+
max = Math.max(max, num[i][0])
15+
}
16+
return intArrayOf(min, max)
17+
}
18+
19+
fun findRightInterval(intervals: Array<IntArray>): IntArray {
20+
if (intervals.size <= 1) {
21+
return intArrayOf(-1)
22+
}
23+
val n = intervals.size
24+
val result = IntArray(n)
25+
val map: MutableMap<Int, Int?> = HashMap()
26+
for (i in 0 until n) {
27+
map[intervals[i][0]] = i
28+
}
29+
val minmax = findminmax(intervals)
30+
for (i in minmax[1] - 1 downTo minmax[0] + 1) {
31+
map.computeIfAbsent(i, Function { k: Int -> map[k + 1] })
32+
}
33+
for (i in 0 until n) {
34+
result[i] = map.getOrDefault(intervals[i][1], -1)!!
35+
}
36+
return result
37+
}
38+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
436\. Find Right Interval
2+
3+
Medium
4+
5+
You are given an array of `intervals`, where <code>intervals[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> and each <code>start<sub>i</sub></code> is **unique**.
6+
7+
The **right interval** for an interval `i` is an interval `j` such that <code>start<sub>j</sub> >= end<sub>i</sub></code> and <code>start<sub>j</sub></code> is **minimized**. Note that `i` may equal `j`.
8+
9+
Return _an array of **right interval** indices for each interval `i`_. If no **right interval** exists for interval `i`, then put `-1` at index `i`.
10+
11+
**Example 1:**
12+
13+
**Input:** intervals = [[1,2]]
14+
15+
**Output:** [-1]
16+
17+
**Explanation:** There is only one interval in the collection, so it outputs -1.
18+
19+
**Example 2:**
20+
21+
**Input:** intervals = [[3,4],[2,3],[1,2]]
22+
23+
**Output:** [-1,0,1]
24+
25+
**Explanation:** There is no right interval for [3,4].
26+
27+
The right interval for [2,3] is [3,4] since start<sub>0</sub> = 3 is the smallest start that is >= end<sub>1</sub> = 3.
28+
29+
The right interval for [1,2] is [2,3] since start<sub>1</sub> = 2 is the smallest start that is >= end<sub>2</sub> = 2.
30+
31+
**Example 3:**
32+
33+
**Input:** intervals = [[1,4],[2,3],[3,4]]
34+
35+
**Output:** [-1,2,-1]
36+
37+
**Explanation:** There is no right interval for [1,4] and [3,4]. The right interval for [2,3] is [3,4] since start<sub>2</sub> = 3 is the smallest start that is >= end<sub>1</sub> = 3.
38+
39+
**Constraints:**
40+
41+
* <code>1 <= intervals.length <= 2 * 10<sup>4</sup></code>
42+
* `intervals[i].length == 2`
43+
* <code>-10<sup>6</sup> <= start<sub>i</sub> <= end<sub>i</sub> <= 10<sup>6</sup></code>
44+
* The start point of each interval is **unique**.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package g0401_0500.s0440_k_th_smallest_in_lexicographical_order
2+
3+
// #Hard #Trie #2022_12_22_Time_149_ms_(100.00%)_Space_32.5_MB_(100.00%)
4+
5+
class Solution {
6+
fun findKthNumber(n: Int, k: Int): Int {
7+
var k = k
8+
var curr = 1
9+
k = k - 1
10+
while (k > 0) {
11+
val steps = calSteps(n, curr.toLong(), curr + 1L)
12+
if (steps <= k) {
13+
curr += 1
14+
k -= steps
15+
} else {
16+
curr *= 10
17+
k -= 1
18+
}
19+
}
20+
return curr
21+
}
22+
23+
// use long in case of overflow
24+
private fun calSteps(n: Int, n1: Long, n2: Long): Int {
25+
var n1 = n1
26+
var n2 = n2
27+
var steps: Long = 0
28+
while (n1 <= n) {
29+
steps += Math.min(n + 1L, n2) - n1
30+
n1 *= 10
31+
n2 *= 10
32+
}
33+
return steps.toInt()
34+
}
35+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
440\. K-th Smallest in Lexicographical Order
2+
3+
Hard
4+
5+
Given two integers `n` and `k`, return _the_ <code>k<sup>th</sup></code> _lexicographically smallest integer in the range_ `[1, n]`.
6+
7+
**Example 1:**
8+
9+
**Input:** n = 13, k = 2
10+
11+
**Output:** 10
12+
13+
**Explanation:** The lexicographical order is [1, 10, 11, 12, 13, 2, 3, 4, 5, 6, 7, 8, 9], so the second smallest number is 10.
14+
15+
**Example 2:**
16+
17+
**Input:** n = 1, k = 1
18+
19+
**Output:** 1
20+
21+
**Constraints:**
22+
23+
* <code>1 <= k <= n <= 10<sup>9</sup></code>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package g0401_0500.s0434_number_of_segments_in_a_string
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 countSegments() {
10+
assertThat(Solution().countSegments("Hello, my name is John"), equalTo(5))
11+
}
12+
13+
@Test
14+
fun countSegments2() {
15+
assertThat(Solution().countSegments("Hello"), equalTo(1))
16+
}
17+
}

0 commit comments

Comments
 (0)