Skip to content

Commit f2cf9d5

Browse files
authored
Added tasks 539, 540, 541, 542
1 parent 04fde26 commit f2cf9d5

File tree

13 files changed

+393
-0
lines changed

13 files changed

+393
-0
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.8'
277277

278278
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
279279
|-|-|-|-|-|-
280+
| 0542 |[01 Matrix](src/main/kotlin/g0501_0600/s0542_01_matrix/Solution.kt)| Medium | Array, Dynamic_Programming, Breadth_First_Search, Matrix | 441 | 94.06
280281

281282
#### Day 6 Matrix Related Problems
282283

@@ -1123,6 +1124,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.8'
11231124

11241125
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
11251126
|-|-|-|-|-|-
1127+
| 0542 |[01 Matrix](src/main/kotlin/g0501_0600/s0542_01_matrix/Solution.kt)| Medium | Array, Dynamic_Programming, Breadth_First_Search, Matrix | 441 | 94.06
11261128
| 0994 |[Rotting Oranges](src/main/kotlin/g0901_1000/s0994_rotting_oranges/Solution.kt)| Medium | Array, Breadth_First_Search, Matrix | 308 | 57.93
11271129

11281130
#### Day 10 Recursion Backtracking
@@ -1431,6 +1433,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.8'
14311433

14321434
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
14331435
|-|-|-|-|-|-
1436+
| 0540 |[Single Element in a Sorted Array](src/main/kotlin/g0501_0600/s0540_single_element_in_a_sorted_array/Solution.kt)| Medium | Array, Binary_Search | 274 | 86.67
14341437

14351438
#### Day 10
14361439

@@ -1649,6 +1652,10 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.8'
16491652
| 0647 |[Palindromic Substrings](src/main/kotlin/g0601_0700/s0647_palindromic_substrings/Solution.kt)| Medium | Top_100_Liked_Questions, String, Dynamic_Programming | 266 | 67.83
16501653
| 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
16511654
| 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
1655+
| 0542 |[01 Matrix](src/main/kotlin/g0501_0600/s0542_01_matrix/Solution.kt)| Medium | Array, Dynamic_Programming, Breadth_First_Search, Matrix, Algorithm_I_Day_9_Breadth_First_Search_Depth_First_Search, Graph_Theory_I_Day_5_Matrix_Related_Problems | 441 | 94.06
1656+
| 0541 |[Reverse String II](src/main/kotlin/g0501_0600/s0541_reverse_string_ii/Solution.kt)| Easy | String, Two_Pointers | 200 | 83.33
1657+
| 0540 |[Single Element in a Sorted Array](src/main/kotlin/g0501_0600/s0540_single_element_in_a_sorted_array/Solution.kt)| Medium | Array, Binary_Search, Binary_Search_II_Day_9 | 274 | 86.67
1658+
| 0539 |[Minimum Time Difference](src/main/kotlin/g0501_0600/s0539_minimum_time_difference/Solution.kt)| Medium | Array, String, Math, Sorting | 183 | 100.00
16521659
| 0538 |[Convert BST to Greater Tree](src/main/kotlin/g0501_0600/s0538_convert_bst_to_greater_tree/Solution.kt)| Medium | Depth_First_Search, Tree, Binary_Tree, Binary_Search_Tree | 252 | 77.78
16531660
| 0537 |[Complex Number Multiplication](src/main/kotlin/g0501_0600/s0537_complex_number_multiplication/Solution.kt)| Medium | String, Math, Simulation | 171 | 75.00
16541661
| 0535 |[Encode and Decode TinyURL](src/main/kotlin/g0501_0600/s0535_encode_and_decode_tinyurl/Codec.kt)| Medium | String, Hash_Table, Design, Hash_Function | 183 | 81.25
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package g0501_0600.s0539_minimum_time_difference
2+
3+
// #Medium #Array #String #Math #Sorting #2023_01_16_Time_183_ms_(100.00%)_Space_36_MB_(96.23%)
4+
5+
class Solution {
6+
fun findMinDifference(timePoints: List<String>): Int {
7+
return if (timePoints.size < 300) {
8+
smallInputSize(timePoints)
9+
} else largeInputSize(timePoints)
10+
}
11+
12+
private fun largeInputSize(timePoints: List<String>): Int {
13+
val times = BooleanArray(60 * 24)
14+
for (time in timePoints) {
15+
val hours = time.substring(0, 2).toInt()
16+
val minutes = time.substring(3, 5).toInt()
17+
if (times[hours * 60 + minutes]) {
18+
return 0
19+
}
20+
times[hours * 60 + minutes] = true
21+
}
22+
var prev = -1
23+
var min = 60 * 24
24+
for (i in 0 until times.size + times.size / 2) {
25+
if (i < times.size) {
26+
if (times[i] && prev == -1) {
27+
prev = i
28+
} else if (times[i]) {
29+
min = Math.min(min, i - prev)
30+
prev = i
31+
}
32+
} else {
33+
if (times[i - times.size] && prev == -1) {
34+
prev = i
35+
} else if (times[i - times.size]) {
36+
min = Math.min(min, i - prev)
37+
prev = i
38+
}
39+
}
40+
}
41+
return min
42+
}
43+
44+
private fun smallInputSize(timePoints: List<String>): Int {
45+
val times = IntArray(timePoints.size)
46+
var j = 0
47+
for (time in timePoints) {
48+
val hours = time.substring(0, 2).toInt()
49+
val minutes = time.substring(3, 5).toInt()
50+
times[j++] = hours * 60 + minutes
51+
}
52+
times.sort()
53+
var min = 60 * 24
54+
for (i in 1..times.size) {
55+
min = if (i == times.size) {
56+
Math.min(min, times[0] + 60 * 24 - times[times.size - 1])
57+
} else {
58+
Math.min(min, times[i] - times[i - 1])
59+
}
60+
if (min == 0) {
61+
return 0
62+
}
63+
}
64+
return min
65+
}
66+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
539\. Minimum Time Difference
2+
3+
Medium
4+
5+
Given a list of 24-hour clock time points in **"HH:MM"** format, return _the minimum **minutes** difference between any two time-points in the list_.
6+
7+
**Example 1:**
8+
9+
**Input:** timePoints = ["23:59","00:00"]
10+
11+
**Output:** 1
12+
13+
**Example 2:**
14+
15+
**Input:** timePoints = ["00:00","23:59","00:00"]
16+
17+
**Output:** 0
18+
19+
**Constraints:**
20+
21+
* <code>2 <= timePoints.length <= 2 * 10<sup>4</sup></code>
22+
* `timePoints[i]` is in the format **"HH:MM"**.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package g0501_0600.s0540_single_element_in_a_sorted_array
2+
3+
// #Medium #Array #Binary_Search #Binary_Search_II_Day_9
4+
// #2023_01_16_Time_274_ms_(86.67%)_Space_44.4_MB_(83.33%)
5+
6+
class Solution {
7+
fun singleNonDuplicate(nums: IntArray): Int {
8+
var start = 0
9+
var end = nums.size - 1
10+
while (start < end) {
11+
val mid = start + (end - start) / 2
12+
if (mid + 1 < nums.size && nums[mid] != nums[mid + 1] && mid - 1 >= 0 && nums[mid] != nums[mid - 1]) {
13+
return nums[mid]
14+
} else if (mid + 1 < nums.size && nums[mid] == nums[mid + 1] && mid % 2 == 0) {
15+
start = mid + 1
16+
} else if (mid - 1 >= 0 && nums[mid] == nums[mid - 1] && mid % 2 == 1) {
17+
start = mid + 1
18+
} else {
19+
end = mid - 1
20+
}
21+
}
22+
return nums[start]
23+
}
24+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
540\. Single Element in a Sorted Array
2+
3+
Medium
4+
5+
You are given a sorted array consisting of only integers where every element appears exactly twice, except for one element which appears exactly once.
6+
7+
Return _the single element that appears only once_.
8+
9+
Your solution must run in `O(log n)` time and `O(1)` space.
10+
11+
**Example 1:**
12+
13+
**Input:** nums = [1,1,2,3,3,4,4,8,8]
14+
15+
**Output:** 2
16+
17+
**Example 2:**
18+
19+
**Input:** nums = [3,3,7,7,10,11,11]
20+
21+
**Output:** 10
22+
23+
**Constraints:**
24+
25+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
26+
* <code>0 <= nums[i] <= 10<sup>5</sup></code>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package g0501_0600.s0541_reverse_string_ii
2+
3+
// #Easy #String #Two_Pointers #2023_01_17_Time_200_ms_(83.33%)_Space_35.7_MB_(86.67%)
4+
5+
class Solution {
6+
fun reverseStr(s: String, k: Int): String {
7+
val res = StringBuilder()
8+
var p1 = 0
9+
var p2 = 2 * k - 1
10+
if (s.length < k) {
11+
res.append(reverse(s))
12+
return res.toString()
13+
}
14+
while (p1 < s.length) {
15+
if (s.length < p1 + k) {
16+
res.append(reverse(s.substring(p1)))
17+
} else {
18+
res.append(reverse(s.substring(p1, p1 + k)))
19+
if (s.length < p2 + 1) {
20+
res.append(s.substring(p1 + k))
21+
} else {
22+
res.append(s, p1 + k, p2 + 1)
23+
}
24+
}
25+
p1 = p1 + 2 * k
26+
p2 = p2 + 2 * k
27+
}
28+
return res.toString()
29+
}
30+
31+
private fun reverse(s: String): String {
32+
val sb = StringBuilder()
33+
sb.append(s)
34+
sb.reverse()
35+
return sb.toString()
36+
}
37+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
541\. Reverse String II
2+
3+
Easy
4+
5+
Given a string `s` and an integer `k`, reverse the first `k` characters for every `2k` characters counting from the start of the string.
6+
7+
If there are fewer than `k` characters left, reverse all of them. If there are less than `2k` but greater than or equal to `k` characters, then reverse the first `k` characters and leave the other as original.
8+
9+
**Example 1:**
10+
11+
**Input:** s = "abcdefg", k = 2
12+
13+
**Output:** "bacdfeg"
14+
15+
**Example 2:**
16+
17+
**Input:** s = "abcd", k = 2
18+
19+
**Output:** "bacd"
20+
21+
**Constraints:**
22+
23+
* <code>1 <= s.length <= 10<sup>4</sup></code>
24+
* `s` consists of only lowercase English letters.
25+
* <code>1 <= k <= 10<sup>4</sup></code>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package g0501_0600.s0542_01_matrix
2+
3+
// #Medium #Array #Dynamic_Programming #Breadth_First_Search #Matrix
4+
// #Algorithm_I_Day_9_Breadth_First_Search_Depth_First_Search
5+
// #Graph_Theory_I_Day_5_Matrix_Related_Problems
6+
// #2023_01_17_Time_441_ms_(94.06%)_Space_54_MB_(78.22%)
7+
8+
import java.util.Arrays
9+
10+
class Solution {
11+
fun updateMatrix(mat: Array<IntArray>): Array<IntArray> {
12+
val dist = Array(mat.size) { IntArray(mat[0].size) }
13+
for (i in mat.indices) {
14+
Arrays.fill(dist[i], Int.MAX_VALUE - 100000)
15+
}
16+
for (i in mat.indices) {
17+
for (j in mat[0].indices) {
18+
if (mat[i][j] == 0) {
19+
dist[i][j] = 0
20+
} else {
21+
if (i > 0) {
22+
dist[i][j] = Math.min(dist[i][j], dist[i - 1][j] + 1)
23+
}
24+
if (j > 0) {
25+
dist[i][j] = Math.min(dist[i][j], dist[i][j - 1] + 1)
26+
}
27+
}
28+
}
29+
}
30+
for (i in mat.indices.reversed()) {
31+
for (j in mat[0].indices.reversed()) {
32+
if (i < mat.size - 1) {
33+
dist[i][j] = Math.min(dist[i][j], dist[i + 1][j] + 1)
34+
}
35+
if (j < mat[0].size - 1) {
36+
dist[i][j] = Math.min(dist[i][j], dist[i][j + 1] + 1)
37+
}
38+
}
39+
}
40+
return dist
41+
}
42+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
542\. 01 Matrix
2+
3+
Medium
4+
5+
Given an `m x n` binary matrix `mat`, return _the distance of the nearest_ `0` _for each cell_.
6+
7+
The distance between two adjacent cells is `1`.
8+
9+
**Example 1:**
10+
11+
![](https://assets.leetcode.com/uploads/2021/04/24/01-1-grid.jpg)
12+
13+
**Input:** mat = [[0,0,0],[0,1,0],[0,0,0]]
14+
15+
**Output:** [[0,0,0],[0,1,0],[0,0,0]]
16+
17+
**Example 2:**
18+
19+
![](https://assets.leetcode.com/uploads/2021/04/24/01-2-grid.jpg)
20+
21+
**Input:** mat = [[0,0,0],[0,1,0],[1,1,1]]
22+
23+
**Output:** [[0,0,0],[0,1,0],[1,2,1]]
24+
25+
**Constraints:**
26+
27+
* `m == mat.length`
28+
* `n == mat[i].length`
29+
* <code>1 <= m, n <= 10<sup>4</sup></code>
30+
* <code>1 <= m * n <= 10<sup>4</sup></code>
31+
* `mat[i][j]` is either `0` or `1`.
32+
* There is at least one `0` in `mat`.
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package g0501_0600.s0539_minimum_time_difference
2+
3+
import org.hamcrest.CoreMatchers.equalTo
4+
import org.hamcrest.MatcherAssert.assertThat
5+
import org.junit.jupiter.api.Test
6+
import java.util.stream.IntStream
7+
8+
internal class SolutionTest {
9+
@Test
10+
fun findMinDifference() {
11+
assertThat(Solution().findMinDifference(listOf("23:59", "00:00")), equalTo(1))
12+
}
13+
14+
@Test
15+
fun findMinDifference2() {
16+
assertThat(
17+
Solution().findMinDifference(listOf("00:00", "23:59", "00:00")),
18+
equalTo(0)
19+
)
20+
}
21+
22+
@Test
23+
fun findMinDifference3() {
24+
val timePoints: MutableList<String> = ArrayList()
25+
IntStream.rangeClosed(0, 310 / 2)
26+
.forEach { index: Int ->
27+
timePoints.add("23:59")
28+
timePoints.add("00:00")
29+
}
30+
assertThat(Solution().findMinDifference(timePoints), equalTo(0))
31+
}
32+
33+
@Test
34+
fun findMinDifference4() {
35+
val timePoints: MutableList<String> = ArrayList()
36+
var index = 0
37+
while (index < 301) {
38+
val hour = index / 60 % 24
39+
val minute = index % 60
40+
timePoints.add(
41+
String.format(
42+
"%s:%s",
43+
if (hour < 10) "0$hour" else hour, if (minute < 10) "0$minute" else minute
44+
)
45+
)
46+
index++
47+
}
48+
assertThat(Solution().findMinDifference(timePoints), equalTo(1))
49+
}
50+
}

0 commit comments

Comments
 (0)