Skip to content

Commit 509a5d1

Browse files
authored
Improved arrays
1 parent 7441d3a commit 509a5d1

File tree

5 files changed

+8
-15
lines changed

5 files changed

+8
-15
lines changed

src.save/main/kotlin/g0401_0500/s0493_reverse_pairs/Solution.kt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ package g0401_0500.s0493_reverse_pairs
33
// #Hard #Array #Binary_Search #Ordered_Set #Divide_and_Conquer #Segment_Tree #Binary_Indexed_Tree
44
// #Merge_Sort #2023_01_04_Time_887_ms_(66.67%)_Space_73.9_MB_(66.67%)
55

6-
import java.util.Arrays
7-
86
class Solution {
97
fun reversePairs(nums: IntArray): Int {
108
return mergeSort(nums, 0, nums.size - 1)
@@ -25,7 +23,7 @@ class Solution {
2523
}
2624
cnt += j - (mid + 1)
2725
}
28-
Arrays.sort(nums, start, end + 1)
26+
nums.sort(start, end + 1)
2927
return cnt
3028
}
3129
}

src.save/test/kotlin/g0001_0100/s0026_remove_duplicates_from_sorted_array/SolutionTest.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,19 @@ package g0001_0100.s0026_remove_duplicates_from_sorted_array
33
import org.hamcrest.CoreMatchers.equalTo
44
import org.hamcrest.MatcherAssert.assertThat
55
import org.junit.jupiter.api.Test
6-
import java.util.Arrays
76

87
internal class SolutionTest {
98
@Test
109
fun removeDuplicates() {
1110
val array = intArrayOf(1, 1, 2)
1211
val end = Solution().removeDuplicates(array)
13-
assertThat(Arrays.toString(array.copyOfRange(0, end)), equalTo("[1, 2]"))
12+
assertThat(array.copyOfRange(0, end), equalTo(intArrayOf(1, 2)))
1413
}
1514

1615
@Test
1716
fun removeDuplicates2() {
1817
val array = intArrayOf(0, 0, 1, 1, 1, 2, 2, 3, 3, 4)
1918
val end = Solution().removeDuplicates(array)
20-
assertThat(Arrays.toString(array.copyOfRange(0, end)), equalTo("[0, 1, 2, 3, 4]"))
19+
assertThat(array.copyOfRange(0, end), equalTo(intArrayOf(0, 1, 2, 3, 4)))
2120
}
2221
}

src.save/test/kotlin/g0001_0100/s0027_remove_element/SolutionTest.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,19 @@ package g0001_0100.s0027_remove_element
33
import org.hamcrest.CoreMatchers.equalTo
44
import org.hamcrest.MatcherAssert.assertThat
55
import org.junit.jupiter.api.Test
6-
import java.util.Arrays
76

87
internal class SolutionTest {
98
@Test
109
fun removeElement() {
1110
val original = intArrayOf(3, 2, 2, 3)
1211
val end = Solution().removeElement(original, 3)
13-
assertThat(Arrays.copyOfRange(original, 0, end), equalTo(intArrayOf(2, 2)))
12+
assertThat(original.copyOfRange(0, end), equalTo(intArrayOf(2, 2)))
1413
}
1514

1615
@Test
1716
fun removeElement2() {
1817
val original = intArrayOf(0, 1, 2, 2, 3, 0, 4, 2)
1918
val end = Solution().removeElement(original, 2)
20-
assertThat(Arrays.copyOfRange(original, 0, end), equalTo(intArrayOf(0, 1, 4, 0, 3)))
19+
assertThat(original.copyOfRange(0, end), equalTo(intArrayOf(0, 1, 4, 0, 3)))
2120
}
2221
}

src.save/test/kotlin/g0001_0100/s0080_remove_duplicates_from_sorted_array_ii/SolutionTest.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,19 @@ package g0001_0100.s0080_remove_duplicates_from_sorted_array_ii
33
import org.hamcrest.CoreMatchers.equalTo
44
import org.hamcrest.MatcherAssert.assertThat
55
import org.junit.jupiter.api.Test
6-
import java.util.Arrays
76

87
internal class SolutionTest {
98
@Test
109
fun removeDuplicates() {
1110
val array = intArrayOf(1, 1, 1, 2, 2, 3)
1211
val end = Solution().removeDuplicates(array)
13-
assertThat(Arrays.copyOfRange(array, 0, end), equalTo(intArrayOf(1, 1, 2, 2, 3)))
12+
assertThat(array.copyOfRange(0, end), equalTo(intArrayOf(1, 1, 2, 2, 3)))
1413
}
1514

1615
@Test
1716
fun removeDuplicates2() {
1817
val array = intArrayOf(0, 0, 1, 1, 1, 1, 2, 3, 3)
1918
val end = Solution().removeDuplicates(array)
20-
assertThat(Arrays.copyOfRange(array, 0, end), equalTo(intArrayOf(0, 0, 1, 1, 2, 3, 3)))
19+
assertThat(array.copyOfRange(0, end), equalTo(intArrayOf(0, 0, 1, 1, 2, 3, 3)))
2120
}
2221
}

src/main/kotlin/g0701_0800/s0787_cheapest_flights_within_k_stops/Solution.kt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,13 @@ package g0701_0800.s0787_cheapest_flights_within_k_stops
33
// #Medium #Dynamic_Programming #Depth_First_Search #Breadth_First_Search #Heap_Priority_Queue
44
// #Graph #Shortest_Path #2023_03_13_Time_185_ms_(99.20%)_Space_36.6_MB_(89.64%)
55

6-
import java.util.Arrays
7-
86
class Solution {
97
fun findCheapestPrice(n: Int, flights: Array<IntArray>, src: Int, dst: Int, k: Int): Int {
108
// k + 2 becase there are total of k(intermediate stops) + 1(src) + 1(dst)
119
// dp[i][j] = cost to reach j using atmost i edges from src
1210
val dp = Array(k + 2) { IntArray(n) }
1311
for (row in dp) {
14-
Arrays.fill(row, Int.MAX_VALUE)
12+
row.fill(Int.MAX_VALUE)
1513
}
1614
// cost to reach src is always 0
1715
for (i in 0..k + 1) {

0 commit comments

Comments
 (0)