Skip to content

Commit

Permalink
Improved arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
javadev authored Mar 31, 2024
1 parent 6a500ee commit 77bd502
Show file tree
Hide file tree
Showing 13 changed files with 14 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@ 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
for (rod in rods) {
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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
Expand Down
4 changes: 1 addition & 3 deletions src/main/kotlin/g1001_1100/s1024_video_stitching/Solution.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<IntArray>, 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]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<IntArray>): 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -13,7 +12,7 @@ class Solution {
val stk: Deque<Char> = 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]++
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<IntArray>): 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<Int>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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<Int>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Int, Int>

Expand All @@ -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]
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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]
)
Expand Down

0 comments on commit 77bd502

Please sign in to comment.