Skip to content

Improved arrays #634

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 3 commits into from
Apr 28, 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 @@ -6,43 +6,38 @@ package g1501_1600.s1594_maximum_non_negative_product_in_a_matrix
class Solution {
private class Tuple(var max: Long, var min: Long)

fun maxProductPath(grid: Array<IntArray?>?): Int {
fun maxProductPath(grid: Array<IntArray>): Int {
// DP
if (grid == null || grid.size == 0 || grid[0] == null || grid[0]!!.size == 0) {
if (grid.isEmpty() || grid[0].isEmpty()) {
return 0
}
val rows = grid.size
val cols = grid[0]!!.size
val dp = Array(rows) { arrayOfNulls<Tuple>(cols) }
for (i in 0 until rows) {
for (j in 0 until cols) {
dp[i][j] = Tuple(1, 1)
}
}
val cols = grid[0].size
val dp = Array(rows) { Array(cols) { Tuple(1, 1) } }
// Init first row and column
dp[0][0]!!.max = grid[0]!![0].toLong()
dp[0][0]!!.min = grid[0]!![0].toLong()
dp[0][0].max = grid[0][0].toLong()
dp[0][0].min = grid[0][0].toLong()
for (i in 1 until rows) {
dp[i][0]!!.max = grid[i]!![0] * dp[i - 1][0]!!.max
dp[i][0]!!.min = grid[i]!![0] * dp[i - 1][0]!!.min
dp[i][0].max = grid[i][0] * dp[i - 1][0].max
dp[i][0].min = grid[i][0] * dp[i - 1][0].min
}
for (i in 1 until cols) {
dp[0][i]!!.max = grid[0]!![i] * dp[0][i - 1]!!.max
dp[0][i]!!.min = grid[0]!![i] * dp[0][i - 1]!!.min
dp[0][i].max = grid[0][i] * dp[0][i - 1].max
dp[0][i].min = grid[0][i] * dp[0][i - 1].min
}
// DP
for (i in 1 until rows) {
for (j in 1 until cols) {
val up1 = dp[i - 1][j]!!.max * grid[i]!![j]
val up2 = dp[i - 1][j]!!.min * grid[i]!![j]
val left1 = dp[i][j - 1]!!.max * grid[i]!![j]
val left2 = dp[i][j - 1]!!.min * grid[i]!![j]
dp[i][j]!!.max = Math.max(up1, Math.max(up2, Math.max(left1, left2)))
dp[i][j]!!.min = Math.min(up1, Math.min(up2, Math.min(left1, left2)))
val up1 = dp[i - 1][j].max * grid[i][j]
val up2 = dp[i - 1][j].min * grid[i][j]
val left1 = dp[i][j - 1].max * grid[i][j]
val left2 = dp[i][j - 1].min * grid[i][j]
dp[i][j].max = Math.max(up1, Math.max(up2, Math.max(left1, left2)))
dp[i][j].min = Math.min(up1, Math.min(up2, Math.min(left1, left2)))
}
}
return if (dp[rows - 1][cols - 1]!!.max < 0) {
return if (dp[rows - 1][cols - 1].max < 0) {
-1
} else (dp[rows - 1][cols - 1]!!.max % (1e9 + 7)).toInt()
} else (dp[rows - 1][cols - 1].max % (1e9 + 7)).toInt()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@ package g1801_1900.s1886_determine_whether_matrix_can_be_obtained_by_rotation
// #Easy #Array #Matrix #Programming_Skills_II_Day_7
// #2023_06_22_Time_147_ms_(85.71%)_Space_34.1_MB_(100.00%)

import java.util.Arrays

class Solution {
fun findRotation(mat: Array<IntArray>, target: Array<IntArray?>?): Boolean {
fun findRotation(mat: Array<IntArray>, target: Array<IntArray>): Boolean {
for (i in 0..3) {
if (Arrays.deepEquals(mat, target)) {
if (mat.contentDeepEquals(target)) {
return true
}
rotate(mat)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package g1901_2000.s1942_the_number_of_the_smallest_unoccupied_chair
// #Medium #Array #Heap_Priority_Queue #Ordered_Set
// #2023_06_20_Time_549_ms_(100.00%)_Space_63.6_MB_(100.00%)

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

class Solution {
Expand All @@ -15,9 +14,8 @@ class Solution {
all[2 * i] = Person(i, times[i][0], false, true)
all[2 * i + 1] = Person(i, times[i][1], true, false)
}
Arrays.sort(
all
) { a: Person?, b: Person? ->

all.sortWith { a: Person?, b: Person? ->
val i = if (a!!.leave) -1 else 1
val j = if (b!!.leave) -1 else 1
if (a.time == b.time) i - j else a.time - b.time
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,17 @@ package g1901_2000.s1985_find_the_kth_largest_integer_in_the_array
// #Medium #Array #String #Sorting #Heap_Priority_Queue #Divide_and_Conquer #Quickselect
// #2023_06_21_Time_397_ms_(100.00%)_Space_55.3_MB_(66.67%)

import java.util.Arrays

class Solution {
fun kthLargestNumber(nums: Array<String>, k: Int): String {
Arrays.sort(nums) { n1: String, n2: String -> compareStringInt(n2, n1) }
nums.sortWith { n1: String, n2: String -> compareStringInt(n2, n1) }
return nums[k - 1]
}

private fun compareStringInt(n1: String, n2: String): Int {
if (n1.length != n2.length) {
return if (n1.length < n2.length) -1 else 1
}
for (i in 0 until n1.length) {
for (i in n1.indices) {
val n1Digit = n1[i].code - '0'.code
val n2Digit = n2[i].code - '0'.code
if (n1Digit > n2Digit) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ package g1901_2000.s1996_the_number_of_weak_characters_in_the_game
// #Medium #Array #Sorting #Greedy #Stack #Monotonic_Stack
// #2023_06_21_Time_1234_ms_(100.00%)_Space_98.2_MB_(100.00%)

import java.util.Arrays

class Solution {
fun numberOfWeakCharacters(properties: Array<IntArray>): Int {
Arrays.sort(properties) { a: IntArray, b: IntArray -> if (a[0] == b[0]) b[1] - a[1] else a[0] - b[0] }
properties.sortWith { a: IntArray, b: IntArray -> if (a[0] == b[0]) b[1] - a[1] else a[0] - b[0] }
var max = properties[properties.size - 1][1]
var count = 0
for (i in properties.size - 2 downTo 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ package g2001_2100.s2008_maximum_earnings_from_taxi
// #Medium #Array #Dynamic_Programming #Sorting #Binary_Search
// #2023_06_23_Time_1008_ms_(100.00%)_Space_67.3_MB_(100.00%)

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

@Suppress("UNUSED_PARAMETER")
class Solution {
fun maxTaxiEarnings(n: Int, rides: Array<IntArray>): Long {
// Sort based on start time
Arrays.sort(rides) { a: IntArray, b: IntArray ->
rides.sortWith { a: IntArray, b: IntArray ->
a[0] - b[0]
}
var max: Long = 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ package g2001_2100.s2054_two_best_non_overlapping_events
// #Medium #Array #Dynamic_Programming #Sorting #Binary_Search #Heap_Priority_Queue
// #2023_06_25_Time_851_ms_(100.00%)_Space_108.7_MB_(50.00%)

import java.util.Arrays

class Solution {
fun maxTwoEvents(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] }
val max = IntArray(events.size)
for (i in events.indices.reversed()) {
if (i == events.size - 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@ package g2001_2100.s2092_find_all_people_with_secret
// #Hard #Sorting #Depth_First_Search #Breadth_First_Search #Graph #Union_Find
// #2023_06_28_Time_1086_ms_(100.00%)_Space_104.2_MB_(100.00%)

import java.util.Arrays

@Suppress("NAME_SHADOWING")
class Solution {
fun findAllPeople(n: Int, meetings: Array<IntArray>, firstPerson: Int): List<Int> {
Arrays.sort(meetings) { a: IntArray, b: IntArray -> a[2] - b[2] }
meetings.sortWith { a: IntArray, b: IntArray -> a[2] - b[2] }
val uf = UF(n)
// base
uf.union(0, firstPerson)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package g2101_2200.s2136_earliest_possible_day_of_full_bloom

// #Hard #Array #Sorting #Greedy #2023_06_25_Time_968_ms_(100.00%)_Space_57.2_MB_(100.00%)

import java.util.Arrays
import java.util.Collections

class Solution {
Expand All @@ -15,7 +14,7 @@ class Solution {
for (i in 0 until n) {
arr[i] = Seed(plantTime[i], growTime[i])
}
Arrays.sort(arr, Collections.reverseOrder())
arr.sortWith(Collections.reverseOrder())
var ans = arr[0]!!.plantTime + arr[0]!!.growTime
var lastPlantDay = arr[0]!!.plantTime
for (i in 1 until n) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package g2201_2300.s2250_count_number_of_rectangles_containing_each_point
// #Medium #Array #Sorting #Binary_Search #Binary_Indexed_Tree
// #2023_06_27_Time_967_ms_(100.00%)_Space_85.4_MB_(100.00%)

import java.util.Arrays

class Solution {
fun countRectangles(rectangles: Array<IntArray>, points: Array<IntArray>): IntArray {
val n = rectangles.size
Expand All @@ -14,7 +12,7 @@ class Solution {
for (i in 0 until q) {
es[n + i] = intArrayOf(points[i][0], points[i][1], i)
}
Arrays.sort(es) { x: IntArray?, y: IntArray? -> if (x!![0] != y!![0]) -(x[0] - y[0]) else x.size - y.size }
es.sortWith { x: IntArray?, y: IntArray? -> if (x!![0] != y!![0]) -(x[0] - y[0]) else x.size - y.size }
val ct = IntArray(101)
val ans = IntArray(q)
for (e in es) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,20 @@ package g2201_2300.s2251_number_of_flowers_in_full_bloom
// #Hard #Array #Hash_Table #Sorting #Binary_Search #Prefix_Sum #Ordered_Set
// #2023_06_28_Time_973_ms_(100.00%)_Space_88.6_MB_(100.00%)

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

class Solution {
fun fullBloomFlowers(flowers: Array<IntArray>, persons: IntArray): IntArray {
Arrays.sort(flowers, { a: IntArray, b: IntArray -> a[0].compareTo(b[0]) })
flowers.sortWith { a: IntArray, b: IntArray -> a[0].compareTo(b[0]) }
val ans = IntArray(persons.size)
val pq = PriorityQueue({ a: Pair, b: Pair -> a.j.compareTo(b.j) })
val pq = PriorityQueue { a: Pair, b: Pair -> a.j.compareTo(b.j) }
var j = 0
val t = Array(persons.size) { IntArray(2) }
for (i in persons.indices) {
t[i][0] = persons[i]
t[i][1] = i
}
Arrays.sort(t, { a: IntArray, b: IntArray -> a[0].compareTo(b[0]) })
t.sortWith { a: IntArray, b: IntArray -> a[0].compareTo(b[0]) }
for (ints in t) {
while (pq.isNotEmpty()) {
if (pq.peek().j < ints[0]) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ package g2201_2300.s2271_maximum_white_tiles_covered_by_a_carpet
// #Medium #Array #Sorting #Greedy #Binary_Search #Prefix_Sum
// #2023_06_28_Time_692_ms_(100.00%)_Space_66.2_MB_(100.00%)

import java.util.Arrays

class Solution {
fun maximumWhiteTiles(tiles: Array<IntArray>, carpetLength: Int): Int {
Arrays.sort(tiles, { x: IntArray, y: IntArray -> x[0].compareTo(y[0]) })
tiles.sortWith { x: IntArray, y: IntArray -> x[0].compareTo(y[0]) }
var currentCover = Math.min(tiles[0][1] - tiles[0][0] + 1, carpetLength)
var maxCover = currentCover
var head = 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@ package g2201_2300.s2280_minimum_lines_to_represent_a_line_chart
// #Medium #Array #Math #Sorting #Geometry #Number_Theory
// #2023_06_28_Time_765_ms_(100.00%)_Space_98.8_MB_(100.00%)

import java.util.Arrays

class Solution {
fun minimumLines(stockPrices: Array<IntArray>): Int {
if (stockPrices.size == 1) {
return 0
}
Arrays.sort(stockPrices) { a: IntArray, b: IntArray -> a[0] - b[0] }
stockPrices.sortWith { a: IntArray, b: IntArray -> a[0] - b[0] }
// multiply with 1.0 to make it double and multiply with 100 for making it big so that
// difference won't come out to be very less and after division it become 0.
// failing for one of the case without multiply 100
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@ package g2401_2500.s2402_meeting_rooms_iii
// #Hard #Array #Sorting #Heap_Priority_Queue
// #2023_07_03_Time_976_ms_(100.00%)_Space_108.7_MB_(66.67%)

import java.util.Arrays

class Solution {
fun mostBooked(n: Int, meetings: Array<IntArray>): Int {
val counts = IntArray(n)
val endTimes = LongArray(n)
Arrays.sort(meetings) { a: IntArray, b: IntArray -> Integer.compare(a[0], b[0]) }
meetings.sortWith { a: IntArray, b: IntArray -> Integer.compare(a[0], b[0]) }
for (meeting in meetings) {
val id = findRoomId(endTimes, meeting[0])
counts[id]++
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@ package g2401_2500.s2463_minimum_total_distance_traveled
// #Hard #Array #Dynamic_Programming #Sorting
// #2023_07_05_Time_153_ms_(100.00%)_Space_37_MB_(100.00%)

import java.util.Arrays

class Solution {
fun minimumTotalDistance(robot: List<Int>, f: Array<IntArray>): Long {
// sort factories :
// 1. move all factories with 0-capacity to the end
// 2. sort everything else by x-position in asc order
Arrays.sort(f) { a: IntArray, b: IntArray -> if (a[1] == 0) 1 else if (b[1] == 0) -1 else a[0] - b[0] }
f.sortWith { a: IntArray, b: IntArray -> if (a[1] == 0) 1 else if (b[1] == 0) -1 else a[0] - b[0] }
// Sort robots by x-position in asc order
// As we don't know the implementation of the List that is passed, it is better to map it to
// an array explicitly
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ package g2501_2600.s2503_maximum_number_of_points_from_grid_queries
// #2023_07_04_Time_581_ms_(100.00%)_Space_62.6_MB_(100.00%)

import java.util.ArrayDeque
import java.util.Arrays
import java.util.PriorityQueue
import java.util.Queue

Expand All @@ -19,9 +18,9 @@ class Solution {
for (i in queries.indices) {
index[i] = i
}
Arrays.sort(index, { o: Int?, m: Int? -> queries[o!!].compareTo(queries[m!!]) })
index.sortWith { o: Int?, m: Int? -> queries[o!!].compareTo(queries[m!!]) }
val q1: Queue<IntArray> = ArrayDeque()
val q2 = PriorityQueue({ a: IntArray, b: IntArray -> a[2].compareTo(b[2]) })
val q2 = PriorityQueue { a: IntArray, b: IntArray -> a[2].compareTo(b[2]) }
q2.offer(intArrayOf(0, 0, grid[0][0]))
val visited = Array(r) { BooleanArray(c) }
var count = 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package g2501_2600.s2542_maximum_subsequence_score
// #Medium #Array #Sorting #Greedy #Heap_Priority_Queue
// #2023_07_04_Time_780_ms_(81.97%)_Space_56.7_MB_(99.45%)

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

class Solution {
Expand All @@ -15,9 +14,8 @@ class Solution {
for (i in 0 until n) {
nums[i] = PairInfo(nums1[i], nums2[i])
}
Arrays.sort(
nums
) { a: PairInfo?, b: PairInfo? ->

nums.sortWith sort@{ a: PairInfo?, b: PairInfo? ->
if (a!!.val2 == b!!.val2) {
return@sort a.val1 - b.val1
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ package g2501_2600.s2545_sort_the_students_by_their_kth_score

// #Medium #Array #Sorting #Matrix #2023_07_04_Time_442_ms_(100.00%)_Space_53.3_MB_(100.00%)

import java.util.Arrays

class Solution {
fun sortTheStudents(score: Array<IntArray>, k: Int): Array<IntArray> {
Arrays.sort(score) { o1: IntArray, o2: IntArray -> o2[k] - o1[k] }
score.sortWith { o1: IntArray, o2: IntArray -> o2[k] - o1[k] }
return score
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@ package g2501_2600.s2580_count_ways_to_group_overlapping_ranges

// #Medium #Array #Sorting #2023_07_10_Time_669_ms_(100.00%)_Space_122.8_MB_(50.00%)

import java.util.Arrays

@Suppress("NAME_SHADOWING")
class Solution {
fun countWays(ranges: Array<IntArray>): Int {
var cnt = 1
Arrays.sort(ranges) { a, b -> if (a[0] != b[0]) a[0] - b[0] else a[1] - b[1] }
ranges.sortWith { a, b -> if (a[0] != b[0]) a[0] - b[0] else a[1] - b[1] }
var curr = ranges[0]
for (i in 1 until ranges.size) {
if (ranges[i][1] < curr[0] || ranges[i][0] > curr[1]) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@ package g2501_2600.s2589_minimum_time_to_complete_all_tasks
// #Hard #Array #Sorting #Greedy #Binary_Search #Stack
// #2023_07_12_Time_422_ms_(100.00%)_Space_58.7_MB_(50.00%)

import java.util.Arrays

class Solution {
fun findMinimumTime(tasks: Array<IntArray>): Int {
var res = 0
val arr = BooleanArray(2001)
Arrays.sort(tasks) { a: IntArray, b: IntArray ->
tasks.sortWith { a: IntArray, b: IntArray ->
a[1] - b[1]
}
for (task in tasks) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package g2701_2800.s2719_count_of_integers

// #Hard #String #Dynamic_Programming #Math #2023_08_02_Time_208_ms_(100.00%)_Space_38_MB_(68.42%)

import java.util.Arrays

class Solution {
private lateinit var dp: Array<Array<Array<IntArray>>>
private fun countStrings(i: Int, tight1: Boolean, tight2: Boolean, sum: Int, num1: String, num2: String): Int {
Expand Down Expand Up @@ -41,7 +39,7 @@ class Solution {
for (dim1 in dp) {
for (dim2 in dim1) {
for (dim3 in dim2) {
Arrays.fill(dim3, -1)
dim3.fill(-1)
}
}
}
Expand Down
Loading
Loading