diff --git a/src/main/kotlin/g0901_1000/s0939_minimum_area_rectangle/Solution.kt b/src/main/kotlin/g0901_1000/s0939_minimum_area_rectangle/Solution.kt index a7f4a0421..e17587ad8 100644 --- a/src/main/kotlin/g0901_1000/s0939_minimum_area_rectangle/Solution.kt +++ b/src/main/kotlin/g0901_1000/s0939_minimum_area_rectangle/Solution.kt @@ -3,7 +3,6 @@ package g0901_1000.s0939_minimum_area_rectangle // #Medium #Array #Hash_Table #Math #Sorting #Geometry // #2023_04_29_Time_461_ms_(100.00%)_Space_74.8_MB_(20.00%) -import java.util.Arrays import kotlin.math.abs class Solution { @@ -16,9 +15,7 @@ class Solution { map.putIfAbsent(p[0], HashSet()) map.getValue(p[0]).add(p[1]) } - Arrays.sort( - points - ) { a: IntArray, b: IntArray -> + points.sortWith { a: IntArray, b: IntArray -> if (a[0] == b[0]) Integer.compare( a[1], b[1] diff --git a/src/main/kotlin/g0901_1000/s0956_tallest_billboard/Solution.kt b/src/main/kotlin/g0901_1000/s0956_tallest_billboard/Solution.kt index b42265b5f..7f6a6aba9 100644 --- a/src/main/kotlin/g0901_1000/s0956_tallest_billboard/Solution.kt +++ b/src/main/kotlin/g0901_1000/s0956_tallest_billboard/Solution.kt @@ -2,8 +2,6 @@ package g0901_1000.s0956_tallest_billboard // #Hard #Array #Dynamic_Programming #2023_05_03_Time_182_ms_(100.00%)_Space_49.8_MB_(100.00%) -import java.util.Arrays - class Solution { fun tallestBillboard(rods: IntArray): Int { var maxDiff = 0 @@ -11,7 +9,7 @@ class Solution { maxDiff += rod } val dp = IntArray(maxDiff + 1) - Arrays.fill(dp, -1) + dp.fill(-1) dp[0] = 0 for (l in rods) { val dpOld = IntArray(maxDiff + 1) diff --git a/src/main/kotlin/g0901_1000/s0957_prison_cells_after_n_days/Solution.kt b/src/main/kotlin/g0901_1000/s0957_prison_cells_after_n_days/Solution.kt index dbb6f493a..6952341c6 100644 --- a/src/main/kotlin/g0901_1000/s0957_prison_cells_after_n_days/Solution.kt +++ b/src/main/kotlin/g0901_1000/s0957_prison_cells_after_n_days/Solution.kt @@ -3,8 +3,6 @@ package g0901_1000.s0957_prison_cells_after_n_days // #Medium #Array #Hash_Table #Math #Bit_Manipulation // #2023_05_03_Time_172_ms_(100.00%)_Space_36.2_MB_(50.00%) -import java.util.Arrays - @Suppress("NAME_SHADOWING") class Solution { fun prisonAfterNDays(cells: IntArray, n: Int): IntArray { @@ -20,7 +18,7 @@ class Solution { day++ n-- val next = getNextDay(prev) - if (Arrays.equals(next, first)) { + if (next.contentEquals(first)) { period = day - 1 n %= period } diff --git a/src/main/kotlin/g1001_1100/s1024_video_stitching/Solution.kt b/src/main/kotlin/g1001_1100/s1024_video_stitching/Solution.kt index 62f8a8ccf..77e38de84 100644 --- a/src/main/kotlin/g1001_1100/s1024_video_stitching/Solution.kt +++ b/src/main/kotlin/g1001_1100/s1024_video_stitching/Solution.kt @@ -3,11 +3,9 @@ package g1001_1100.s1024_video_stitching // #Medium #Array #Dynamic_Programming #Greedy // #2023_05_22_Time_141_ms_(100.00%)_Space_34.8_MB_(100.00%) -import java.util.Arrays - class Solution { fun videoStitching(clips: Array, time: Int): Int { - Arrays.sort(clips) { a: IntArray, b: IntArray -> + clips.sortWith { a: IntArray, b: IntArray -> if (a[0] == b[0] ) a[1] - b[1] else a[0] - b[0] } diff --git a/src/main/kotlin/g1001_1100/s1027_longest_arithmetic_subsequence/Solution.kt b/src/main/kotlin/g1001_1100/s1027_longest_arithmetic_subsequence/Solution.kt index 908d1113b..ddb5c6a42 100644 --- a/src/main/kotlin/g1001_1100/s1027_longest_arithmetic_subsequence/Solution.kt +++ b/src/main/kotlin/g1001_1100/s1027_longest_arithmetic_subsequence/Solution.kt @@ -3,8 +3,6 @@ package g1001_1100.s1027_longest_arithmetic_subsequence // #Medium #Array #Hash_Table #Dynamic_Programming #Binary_Search // #2023_05_23_Time_330_ms_(100.00%)_Space_101.4_MB_(16.67%) -import java.util.Arrays - class Solution { fun longestArithSeqLength(nums: IntArray): Int { val max = maxElement(nums) @@ -13,7 +11,7 @@ class Solution { val n = nums.size val dp = Array(n) { IntArray(2 * diff + 2) } for (d in dp) { - Arrays.fill(d, 1) + d.fill(1) } var ans = 0 for (i in 0 until n) { diff --git a/src/main/kotlin/g1001_1100/s1029_two_city_scheduling/Solution.kt b/src/main/kotlin/g1001_1100/s1029_two_city_scheduling/Solution.kt index 5d18d1593..e586f6301 100644 --- a/src/main/kotlin/g1001_1100/s1029_two_city_scheduling/Solution.kt +++ b/src/main/kotlin/g1001_1100/s1029_two_city_scheduling/Solution.kt @@ -2,11 +2,9 @@ package g1001_1100.s1029_two_city_scheduling // #Medium #Array #Sorting #Greedy #2023_05_24_Time_148_ms_(100.00%)_Space_35.4_MB_(92.31%) -import java.util.Arrays - class Solution { fun twoCitySchedCost(costs: Array): Int { - Arrays.sort(costs) { a: IntArray, b: IntArray -> + costs.sortWith { a: IntArray, b: IntArray -> a[0] - a[1] - (b[0] - b[1]) } var cost = 0 diff --git a/src/main/kotlin/g1001_1100/s1081_smallest_subsequence_of_distinct_characters/Solution.kt b/src/main/kotlin/g1001_1100/s1081_smallest_subsequence_of_distinct_characters/Solution.kt index 4c25f41d3..91fc91919 100644 --- a/src/main/kotlin/g1001_1100/s1081_smallest_subsequence_of_distinct_characters/Solution.kt +++ b/src/main/kotlin/g1001_1100/s1081_smallest_subsequence_of_distinct_characters/Solution.kt @@ -3,7 +3,6 @@ package g1001_1100.s1081_smallest_subsequence_of_distinct_characters // #Medium #String #Greedy #Stack #Monotonic_Stack // #2023_06_02_Time_146_ms_(100.00%)_Space_34_MB_(100.00%) -import java.util.Arrays import java.util.Deque import java.util.LinkedList @@ -13,7 +12,7 @@ class Solution { val stk: Deque = LinkedList() val freq = IntArray(26) val exist = BooleanArray(26) - Arrays.fill(exist, false) + exist.fill(false) for (ch in s.toCharArray()) { freq[ch.code - 'a'.code]++ } diff --git a/src/main/kotlin/g1201_1300/s1235_maximum_profit_in_job_scheduling/Solution.kt b/src/main/kotlin/g1201_1300/s1235_maximum_profit_in_job_scheduling/Solution.kt index 993ec1c63..5de6ffdc3 100644 --- a/src/main/kotlin/g1201_1300/s1235_maximum_profit_in_job_scheduling/Solution.kt +++ b/src/main/kotlin/g1201_1300/s1235_maximum_profit_in_job_scheduling/Solution.kt @@ -3,8 +3,6 @@ package g1201_1300.s1235_maximum_profit_in_job_scheduling // #Hard #Array #Dynamic_Programming #Sorting #Binary_Search // #2023_06_09_Time_370_ms_(100.00%)_Space_49.5_MB_(84.00%) -import java.util.Arrays - class Solution { fun jobScheduling(startTime: IntArray, endTime: IntArray, profit: IntArray): Int { val n = startTime.size @@ -14,7 +12,7 @@ class Solution { time[i][1] = endTime[i] time[i][2] = profit[i] } - Arrays.sort(time, { a: IntArray, b: IntArray -> a[1].compareTo(b[1]) }) + time.sortWith { a: IntArray, b: IntArray -> a[1].compareTo(b[1]) } val maxP = Array(n) { IntArray(2) } var lastPos = -1 var currProfit: Int diff --git a/src/main/kotlin/g1301_1400/s1353_maximum_number_of_events_that_can_be_attended/Solution.kt b/src/main/kotlin/g1301_1400/s1353_maximum_number_of_events_that_can_be_attended/Solution.kt index cbbcea505..6f69bd291 100644 --- a/src/main/kotlin/g1301_1400/s1353_maximum_number_of_events_that_can_be_attended/Solution.kt +++ b/src/main/kotlin/g1301_1400/s1353_maximum_number_of_events_that_can_be_attended/Solution.kt @@ -3,12 +3,11 @@ package g1301_1400.s1353_maximum_number_of_events_that_can_be_attended // #Medium #Array #Greedy #Heap_Priority_Queue // #2023_06_06_Time_728_ms_(100.00%)_Space_103.1_MB_(80.00%) -import java.util.Arrays import java.util.PriorityQueue class Solution { fun maxEvents(events: Array): Int { - Arrays.sort(events) { a: IntArray, b: IntArray -> a[0] - b[0] } + events.sortWith { a: IntArray, b: IntArray -> a[0] - b[0] } var ans = 0 var i = 0 val pq = PriorityQueue() diff --git a/src/main/kotlin/g1301_1400/s1366_rank_teams_by_votes/Solution.kt b/src/main/kotlin/g1301_1400/s1366_rank_teams_by_votes/Solution.kt index d061dbaa4..5d2b185c6 100644 --- a/src/main/kotlin/g1301_1400/s1366_rank_teams_by_votes/Solution.kt +++ b/src/main/kotlin/g1301_1400/s1366_rank_teams_by_votes/Solution.kt @@ -3,8 +3,6 @@ package g1301_1400.s1366_rank_teams_by_votes // #Medium #Array #String #Hash_Table #Sorting #Counting // #2023_06_06_Time_179_ms_(100.00%)_Space_36.9_MB_(93.33%) -import java.util.Arrays - class Solution { internal class Node(var c: Char) { var count = IntArray(26) @@ -20,9 +18,8 @@ class Solution { nodes[vote[i].code - 'A'.code]!!.count[i]++ } } - Arrays.sort( - nodes - ) { o1: Node?, o2: Node? -> + + nodes.sortWith sort@{ o1: Node?, o2: Node? -> for (i in 0..25) { if (o1!!.count[i] != o2!!.count[i]) { return@sort o2.count[i] - o1.count[i] diff --git a/src/main/kotlin/g1301_1400/s1383_maximum_performance_of_a_team/Solution.kt b/src/main/kotlin/g1301_1400/s1383_maximum_performance_of_a_team/Solution.kt index 52fc7256b..3df98fd49 100644 --- a/src/main/kotlin/g1301_1400/s1383_maximum_performance_of_a_team/Solution.kt +++ b/src/main/kotlin/g1301_1400/s1383_maximum_performance_of_a_team/Solution.kt @@ -3,7 +3,6 @@ package g1301_1400.s1383_maximum_performance_of_a_team // #Hard #Array #Sorting #Greedy #Heap_Priority_Queue // #2023_06_06_Time_427_ms_(100.00%)_Space_50.2_MB_(100.00%) -import java.util.Arrays import java.util.PriorityQueue class Solution { @@ -13,7 +12,7 @@ class Solution { engineers[i][0] = speed[i] engineers[i][1] = efficiency[i] } - Arrays.sort(engineers) { engineer1: IntArray, engineer2: IntArray -> engineer2[1] - engineer1[1] } + engineers.sortWith { engineer1: IntArray, engineer2: IntArray -> engineer2[1] - engineer1[1] } var speedSum: Long = 0 var maximumPerformance: Long = 0 val minHeap = PriorityQueue() diff --git a/src/main/kotlin/g1301_1400/s1387_sort_integers_by_the_power_value/Solution.kt b/src/main/kotlin/g1301_1400/s1387_sort_integers_by_the_power_value/Solution.kt index 4b1468a6e..97abcd67d 100644 --- a/src/main/kotlin/g1301_1400/s1387_sort_integers_by_the_power_value/Solution.kt +++ b/src/main/kotlin/g1301_1400/s1387_sort_integers_by_the_power_value/Solution.kt @@ -3,8 +3,6 @@ package g1301_1400.s1387_sort_integers_by_the_power_value // #Medium #Dynamic_Programming #Sorting #Memoization // #2023_06_06_Time_370_ms_(100.00%)_Space_39.8_MB_(100.00%) -import java.util.Arrays - class Solution { private lateinit var cacheMap: MutableMap @@ -16,7 +14,7 @@ class Solution { arr[i][0] = lo + i arr[i][1] = getStepCount(lo + i) } - Arrays.sort(arr) { a: IntArray, b: IntArray -> a[1].compareTo(b[1]) } + arr.sortWith { a: IntArray, b: IntArray -> a[1].compareTo(b[1]) } return arr[k - 1][0] } diff --git a/src/main/kotlin/g1401_1500/s1489_find_critical_and_pseudo_critical_edges_in_minimum_spanning_tree/Solution.kt b/src/main/kotlin/g1401_1500/s1489_find_critical_and_pseudo_critical_edges_in_minimum_spanning_tree/Solution.kt index 0f076115a..aa79fbb5e 100644 --- a/src/main/kotlin/g1401_1500/s1489_find_critical_and_pseudo_critical_edges_in_minimum_spanning_tree/Solution.kt +++ b/src/main/kotlin/g1401_1500/s1489_find_critical_and_pseudo_critical_edges_in_minimum_spanning_tree/Solution.kt @@ -3,7 +3,6 @@ package g1401_1500.s1489_find_critical_and_pseudo_critical_edges_in_minimum_span // #Hard #Sorting #Graph #Union_Find #Minimum_Spanning_Tree #Strongly_Connected_Component // #2023_06_13_Time_342_ms_(100.00%)_Space_39.1_MB_(100.00%) -import java.util.Arrays import java.util.LinkedList class Solution { @@ -25,7 +24,7 @@ class Solution { mst[i] = LinkedList() } val mstSet = BooleanArray(edges.size) - Arrays.sort(edges) { a: IntArray, b: IntArray -> + edges.sortWith { a: IntArray, b: IntArray -> Integer.compare( a[2], b[2] )