diff --git a/src/main/kotlin/g3301_3400/s3392_count_subarrays_of_length_three_with_a_condition/Solution.kt b/src/main/kotlin/g3301_3400/s3392_count_subarrays_of_length_three_with_a_condition/Solution.kt index d2c77b3b3..5f46a884c 100644 --- a/src/main/kotlin/g3301_3400/s3392_count_subarrays_of_length_three_with_a_condition/Solution.kt +++ b/src/main/kotlin/g3301_3400/s3392_count_subarrays_of_length_three_with_a_condition/Solution.kt @@ -1,6 +1,6 @@ package g3301_3400.s3392_count_subarrays_of_length_three_with_a_condition -// #Easy #2024_12_22_Time_3_ms_(100.00%)_Space_45_MB_(100.00%) +// #Easy #Array #2024_12_22_Time_3_ms_(100.00%)_Space_45_MB_(100.00%) class Solution { fun countSubarrays(nums: IntArray): Int { diff --git a/src/main/kotlin/g3301_3400/s3393_count_paths_with_the_given_xor_value/Solution.kt b/src/main/kotlin/g3301_3400/s3393_count_paths_with_the_given_xor_value/Solution.kt index b908c0e62..836942d45 100644 --- a/src/main/kotlin/g3301_3400/s3393_count_paths_with_the_given_xor_value/Solution.kt +++ b/src/main/kotlin/g3301_3400/s3393_count_paths_with_the_given_xor_value/Solution.kt @@ -1,6 +1,7 @@ package g3301_3400.s3393_count_paths_with_the_given_xor_value -// #Medium #2024_12_30_Time_57_(68.42%)_Space_73.12_(52.63%) +// #Medium #Array #Dynamic_Programming #Math #Matrix #Bit_Manipulation +// #2024_12_30_Time_57_(68.42%)_Space_73.12_(52.63%) class Solution { private var m = -1 diff --git a/src/main/kotlin/g3301_3400/s3394_check_if_grid_can_be_cut_into_sections/Solution.kt b/src/main/kotlin/g3301_3400/s3394_check_if_grid_can_be_cut_into_sections/Solution.kt index cd5245c6d..a3eb74212 100644 --- a/src/main/kotlin/g3301_3400/s3394_check_if_grid_can_be_cut_into_sections/Solution.kt +++ b/src/main/kotlin/g3301_3400/s3394_check_if_grid_can_be_cut_into_sections/Solution.kt @@ -1,55 +1,43 @@ package g3301_3400.s3394_check_if_grid_can_be_cut_into_sections -// #Medium #2024_12_22_Time_298_ms_(100.00%)_Space_132.4_MB_(100.00%) +// #Medium #Geometry #Line_Sweep #2025_01_06_Time_61_(100.00%)_Space_152.17_(45.00%) import kotlin.math.max @Suppress("unused") class Solution { - fun checkValidCuts(n: Int, rectangles: Array): Boolean { - val m = rectangles.size - val xAxis = Array(m) { IntArray(2) } - val yAxis = Array(m) { IntArray(2) } - var ind = 0 - for (axis in rectangles) { - val startX = axis[0] - val startY = axis[1] - val endX = axis[2] - val endY = axis[3] - xAxis[ind] = intArrayOf(startX, endX) - yAxis[ind] = intArrayOf(startY, endY) - ind++ + fun checkValidCuts(m: Int, rectangles: Array): Boolean { + val n = rectangles.size + val start = LongArray(n) + for (i in 0..( - Comparator { a: IntArray, b: IntArray -> if (a[0] == b[0]) a[1] - b[1] else a[0] - b[0] }, - ) - - yAxis.sortWith( - Comparator { a: IntArray, b: IntArray -> if (a[0] == b[0]) a[1] - b[1] else a[0] - b[0] }, - ) - val verticalCuts = findSections(xAxis) - if (verticalCuts > 2) { + start.sort() + if (validate(start)) { return true } - val horizontalCuts = findSections(yAxis) - return horizontalCuts > 2 + for (i in 0..): Int { - var end = axis[0][1] - var sections = 1 - for (i in 1.. axis[i][0]) { - end = max(end, axis[i][1]) - } else { - sections++ - end = axis[i][1] - } - if (sections > 2) { - return sections + private fun validate(arr: LongArray): Boolean { + var cut = 0 + val n = arr.size + var max = arr[0].toInt() and MASK + for (i in 0..= max && ++cut == 2) { + return true } + max = max(max.toDouble(), (arr[i] and MASK.toLong()).toInt().toDouble()).toInt() } - return sections + return false + } + + companion object { + private val MASK = (1 shl 30) - 1 } } diff --git a/src/main/kotlin/g3301_3400/s3395_subsequences_with_a_unique_middle_mode_i/Solution.kt b/src/main/kotlin/g3301_3400/s3395_subsequences_with_a_unique_middle_mode_i/Solution.kt index 4f21cdd3f..1c44826bb 100644 --- a/src/main/kotlin/g3301_3400/s3395_subsequences_with_a_unique_middle_mode_i/Solution.kt +++ b/src/main/kotlin/g3301_3400/s3395_subsequences_with_a_unique_middle_mode_i/Solution.kt @@ -1,190 +1,74 @@ package g3301_3400.s3395_subsequences_with_a_unique_middle_mode_i -// #Hard #2024_12_22_Time_485_ms_(100.00%)_Space_50.6_MB_(100.00%) +// #Hard #Array #Hash_Table #Math #Combinatorics #2025_01_06_Time_49_(100.00%)_Space_41.14_(100.00%) class Solution { - fun subsequencesWithMiddleMode(a: IntArray): Int { - val n = a.size - // Create a dictionary to store indices of each number - val dict: MutableMap> = HashMap() - for (i in 0.. ArrayList() }.add(i) - } - var ans = 0L - // Iterate over each unique number and its indices - for (entry in dict.entries) { - val b: MutableList = entry.value - val m = b.size - for (k in 0.. = midEntry.value - val m = b.size - for (tmpEntry in dict.entries) { - if (midEntry.key != tmpEntry.key) { - val c: MutableList = tmpEntry.value - val size = c.size - var k = 0 - var j = 0 - while (k < m) { - val i: Int = b[k] - val r = m - 1 - k - val u = i - k - val v = (n - 1 - i) - r - while (j < size && c[j] < i) { - j++ - } - val x = j - val y = size - x - dif = - ( - ( - dif + - convert(k, 1) * - convert(x, 1) % - MOD - * convert(y, 1) % - MOD - * convert(v - y, 1) % - MOD - ) % - MOD - ) - dif = - ( - ( - dif + - convert(k, 1) * - convert(y, 2) % - MOD - * convert(u - x, 1) % - MOD - ) % - MOD - ) - dif = - ( - ( - dif + convert(k, 1) * convert(x, 1) % MOD * convert( - y, - 2, - ) % MOD - ) % - MOD - ) - - dif = - ( - ( - dif + - convert(r, 1) * - convert(x, 1) % - MOD - * convert(y, 1) % - MOD - * convert(u - x, 1) % - MOD - ) % - MOD - ) - dif = - ( - ( - dif + - convert(r, 1) * - convert(x, 2) % - MOD - * convert(v - y, 1) % - MOD - ) % - MOD - ) - dif = - ( - ( - dif + convert(r, 1) * convert(x, 2) % MOD * convert( - y, - 1, - ) % MOD - ) % - MOD - ) - k++ - } - } + val n = nums.size + val newNums = IntArray(n) + val map: MutableMap = HashMap(n) + var m = 0 + var index = 0 + for (x in nums) { + var id = map[x] + if (id == null) { + id = m++ + map.put(x, id) } + newNums[index++] = id } - return ((ans - dif + MOD) % MOD).toInt() - } - - private fun convert(n: Int, k: Int): Long { - if (k > n) { + if (m == n) { return 0 } - if (k == 0 || k == n) { - return 1 + val rightCount = IntArray(m) + for (x in newNums) { + rightCount[x]++ } - var res: Long = 1 - for (i in 0..= 2) { + val right = n - (left + 1) + val leftX = leftCount[x] + val rightX = rightCount[x] + ans -= c2[left - leftX] * c2[right - rightX] + for (y in 0..): Int { diff --git a/src/main/kotlin/g3401_3500/s3403_find_the_lexicographically_largest_string_from_the_box_i/Solution.kt b/src/main/kotlin/g3401_3500/s3403_find_the_lexicographically_largest_string_from_the_box_i/Solution.kt index 1d38bf7ec..933cbce4a 100644 --- a/src/main/kotlin/g3401_3500/s3403_find_the_lexicographically_largest_string_from_the_box_i/Solution.kt +++ b/src/main/kotlin/g3401_3500/s3403_find_the_lexicographically_largest_string_from_the_box_i/Solution.kt @@ -1,6 +1,6 @@ package g3401_3500.s3403_find_the_lexicographically_largest_string_from_the_box_i -// #Medium #2024_12_29_Time_22_(100.00%)_Space_38.72_(100.00%) +// #Medium #String #Two_Pointers #Enumeration #2024_12_29_Time_22_(100.00%)_Space_38.72_(100.00%) import kotlin.math.min diff --git a/src/main/kotlin/g3401_3500/s3404_count_special_subsequences/Solution.kt b/src/main/kotlin/g3401_3500/s3404_count_special_subsequences/Solution.kt index b55ff4356..d59c552d8 100644 --- a/src/main/kotlin/g3401_3500/s3404_count_special_subsequences/Solution.kt +++ b/src/main/kotlin/g3401_3500/s3404_count_special_subsequences/Solution.kt @@ -1,6 +1,7 @@ package g3401_3500.s3404_count_special_subsequences -// #Medium #2024_12_29_Time_276_(100.00%)_Space_60.90_(100.00%) +// #Medium #Array #Hash_Table #Math #Enumeration +// #2024_12_29_Time_276_(100.00%)_Space_60.90_(100.00%) class Solution { fun numberOfSubsequences(nums: IntArray): Long { diff --git a/src/main/kotlin/g3401_3500/s3405_count_the_number_of_arrays_with_k_matching_adjacent_elements/Solution.kt b/src/main/kotlin/g3401_3500/s3405_count_the_number_of_arrays_with_k_matching_adjacent_elements/Solution.kt index 53aa6c48f..afb467879 100644 --- a/src/main/kotlin/g3401_3500/s3405_count_the_number_of_arrays_with_k_matching_adjacent_elements/Solution.kt +++ b/src/main/kotlin/g3401_3500/s3405_count_the_number_of_arrays_with_k_matching_adjacent_elements/Solution.kt @@ -1,6 +1,6 @@ package g3401_3500.s3405_count_the_number_of_arrays_with_k_matching_adjacent_elements -// #Hard #2024_12_29_Time_57_(100.00%)_Space_37.63_(100.00%) +// #Hard #Math #Combinatorics #2024_12_29_Time_57_(100.00%)_Space_37.63_(100.00%) class Solution { fun countGoodArrays(n: Int, m: Int, k: Int): Int {