Skip to content

Improved arrays #623

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ package g1501_1600.s1579_remove_max_number_of_edges_to_keep_graph_fully_traversa

// #Hard #Graph #Union_Find #2023_06_14_Time_942_ms_(32.52%)_Space_92.5_MB_(100.00%)

import java.util.Arrays

class Solution {
fun maxNumEdgesToRemove(n: Int, edges: Array<IntArray>): Int {
Arrays.sort(edges) { a: IntArray, b: IntArray -> b[0] - a[0] }
edges.sortWith { a: IntArray, b: IntArray -> b[0] - a[0] }
val alice = IntArray(n + 1)
val rankAlice = IntArray(n + 1)
val bob = IntArray(n + 1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package g1601_1700.s1632_rank_transform_of_a_matrix
// #Hard #Array #Greedy #Matrix #Graph #Union_Find #Topological_Sort
// #2023_06_17_Time_807_ms_(100.00%)_Space_96.5_MB_(100.00%)

import java.util.Arrays

class Solution {
fun matrixRankTransform(matrix: Array<IntArray>): Array<IntArray> {
val rowCount = matrix.size
Expand Down Expand Up @@ -49,7 +47,7 @@ class Solution {
} else {
val rowCount = matrix.size
val ufind = IntArray(rowCount + matrix[0].size)
Arrays.fill(ufind, -1)
ufind.fill(-1)
for (nIdx in startIdx until endIdx) {
val r = nums[nIdx].toInt() shr 16 and 0xFFFF
val c = nums[nIdx].toInt() and 0xFFFF
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ package g1601_1700.s1665_minimum_initial_energy_to_finish_tasks

// #Hard #Array #Sorting #Greedy #2023_06_15_Time_823_ms_(100.00%)_Space_114.6_MB_(100.00%)

import java.util.Arrays

class Solution {
fun minimumEffort(tasks: Array<IntArray>): Int {
Arrays.sort(tasks) { a: IntArray, b: IntArray -> a[1] - a[0] - b[1] + b[0] }
tasks.sortWith { a: IntArray, b: IntArray -> a[1] - a[0] - b[1] + b[0] }
var prev = 0
for (item in tasks) {
prev = Math.max(prev + item[0], item[1])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@ package g1601_1700.s1691_maximum_height_by_stacking_cuboids
// #Hard #Array #Dynamic_Programming #Sorting
// #2023_06_15_Time_187_ms_(100.00%)_Space_38.6_MB_(100.00%)

import java.util.Arrays

class Solution {
fun maxHeight(cuboids: Array<IntArray>): Int {
for (a in cuboids) {
a.sort()
}
Arrays.sort(
cuboids
) { a: IntArray, b: IntArray ->

cuboids.sortWith sort@{ a: IntArray, b: IntArray ->
if (a[0] != b[0]) {
return@sort a[0] - b[0]
} else if (a[1] != b[1]) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package g1601_1700.s1697_checking_existence_of_edge_length_limited_paths
// #Hard #Array #Sorting #Graph #Union_Find
// #2023_06_15_Time_1411_ms_(72.90%)_Space_101.6_MB_(99.07%)

import java.util.Arrays

class Solution {
private class Dsu(n: Int) {
private val parent: IntArray
Expand Down Expand Up @@ -32,12 +30,12 @@ class Solution {
}

fun distanceLimitedPathsExist(n: Int, edgeList: Array<IntArray>, queries: Array<IntArray>): BooleanArray {
Arrays.sort(edgeList) { o1: IntArray, o2: IntArray -> Integer.compare(o1[2], o2[2]) }
edgeList.sortWith { o1: IntArray, o2: IntArray -> Integer.compare(o1[2], o2[2]) }
val data = Array(queries.size) { IntArray(4) }
for (i in queries.indices) {
data[i] = intArrayOf(queries[i][0], queries[i][1], queries[i][2], i)
}
Arrays.sort(data) { o1: IntArray, o2: IntArray -> Integer.compare(o1[2], o2[2]) }
data.sortWith { o1: IntArray, o2: IntArray -> Integer.compare(o1[2], o2[2]) }
val d = Dsu(n)
var j = 0
val ans = BooleanArray(queries.size)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@ package g1701_1800.s1710_maximum_units_on_a_truck

// #Easy #Array #Sorting #Greedy #2023_06_16_Time_228_ms_(100.00%)_Space_38.5_MB_(100.00%)

import java.util.Arrays

@Suppress("NAME_SHADOWING")
class Solution {
fun maximumUnits(boxTypes: Array<IntArray>, truckSize: Int): Int {
var truckSize = truckSize
Arrays.sort(boxTypes) { b1: IntArray, b2: IntArray -> Integer.compare(b2[1], b1[1]) }
boxTypes.sortWith { b1: IntArray, b2: IntArray -> Integer.compare(b2[1], b1[1]) }
var maxUnits = 0
var i = 0
while (truckSize > 0 && i < boxTypes.size) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Solution {
}
}
val n = events.size
Arrays.sort(events, { a: IntArray, b: IntArray -> a[0].compareTo(b[0]) })
events.sortWith { a: IntArray, b: IntArray -> a[0].compareTo(b[0]) }
val memo = Array(n) { IntArray(k + 1) }
return dfs(events, 0, k, memo)
}
Expand Down
5 changes: 2 additions & 3 deletions src/main/kotlin/g1801_1900/s1847_closest_room/Solution.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package g1801_1900.s1847_closest_room

// #Hard #Array #Sorting #Binary_Search #2023_06_22_Time_1179_ms_(100.00%)_Space_92.1_MB_(100.00%)

import java.util.Arrays
import java.util.TreeSet

class Solution {
Expand All @@ -12,8 +11,8 @@ class Solution {
for (i in 0 until numQuery) {
queries[i] = intArrayOf(queries[i][0], queries[i][1], i)
}
Arrays.sort(rooms) { a: IntArray, b: IntArray -> if (a[1] != b[1]) a[1] - b[1] else a[0] - b[0] }
Arrays.sort(queries) { a: IntArray, b: IntArray -> if (a[1] != b[1]) a[1] - b[1] else a[0] - b[0] }
rooms.sortWith { a: IntArray, b: IntArray -> if (a[1] != b[1]) a[1] - b[1] else a[0] - b[0] }
queries.sortWith { a: IntArray, b: IntArray -> if (a[1] != b[1]) a[1] - b[1] else a[0] - b[0] }
val roomIds = TreeSet<Int>()
val result = IntArray(numQuery)
var j = numRoom - 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package g1801_1900.s1851_minimum_interval_to_include_each_query
// #Hard #Array #Sorting #Binary_Search #Heap_Priority_Queue #Line_Sweep
// #2023_06_22_Time_1612_ms_(87.50%)_Space_129.5_MB_(75.00%)

import java.util.Arrays
import java.util.PriorityQueue

class Solution {
Expand All @@ -13,8 +12,8 @@ class Solution {
for (i in 0 until numQuery) {
queriesWithIndex[i] = intArrayOf(queries[i], i)
}
Arrays.sort(intervals, { a: IntArray, b: IntArray -> a[0].compareTo(b[0]) })
Arrays.sort(queriesWithIndex, { a: IntArray, b: IntArray -> a[0].compareTo(b[0]) })
intervals.sortWith { a: IntArray, b: IntArray -> a[0].compareTo(b[0]) }
queriesWithIndex.sortWith { a: IntArray, b: IntArray -> a[0].compareTo(b[0]) }
val minHeap = PriorityQueue({ a: IntArray, b: IntArray -> (a[1] - a[0]).compareTo(b[1] - b[0]) })
val result = IntArray(numQuery)
var j = 0
Expand Down
Loading