Skip to content

Commit

Permalink
Improved tasks 3394, 3395, 3396, 3399, 3404
Browse files Browse the repository at this point in the history
  • Loading branch information
javadev committed Dec 30, 2024
1 parent dabacd5 commit e589447
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import kotlin.math.max
class Solution {
fun checkValidCuts(n: Int, rectangles: Array<IntArray>): Boolean {
val m = rectangles.size
val xAxis = Array<IntArray?>(m) { IntArray(2) }
val yAxis = Array<IntArray?>(m) { IntArray(2) }
val xAxis = Array<IntArray>(m) { IntArray(2) }
val yAxis = Array<IntArray>(m) { IntArray(2) }
var ind = 0
for (axis in rectangles) {
val startX = axis[0]
Expand All @@ -21,8 +21,8 @@ class Solution {
ind++
}

xAxis.sortWith<IntArray?>(
Comparator { a: IntArray?, b: IntArray? -> if (a!![0] == b!![0]) a[1] - b[1] else a[0] - b[0] },
xAxis.sortWith<IntArray>(
Comparator { a: IntArray, b: IntArray -> if (a[0] == b[0]) a[1] - b[1] else a[0] - b[0] },
)

yAxis.sortWith<IntArray?>(
Expand All @@ -36,15 +36,15 @@ class Solution {
return horizontalCuts > 2
}

private fun findSections(axis: Array<IntArray?>): Int {
var end = axis[0]!![1]
private fun findSections(axis: Array<IntArray>): Int {
var end = axis[0][1]
var sections = 1
for (i in 1..<axis.size) {
if (end > axis[i]!![0]) {
end = max(end, axis[i]!![1])
if (end > axis[i][0]) {
end = max(end, axis[i][1])
} else {
sections++
end = axis[i]!![1]
end = axis[i][1]
}
if (sections > 2) {
return sections
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ class Solution {
fun subsequencesWithMiddleMode(a: IntArray): Int {
val n = a.size
// Create a dictionary to store indices of each number
val dict: MutableMap<Int?, MutableList<Int?>?> = HashMap<Int?, MutableList<Int?>?>()
val dict: MutableMap<Int, MutableList<Int>> = HashMap()
for (i in 0..<n) {
dict.computeIfAbsent(a[i]) { _: Int? -> java.util.ArrayList<Int?>() }!!.add(i)
dict.computeIfAbsent(a[i]) { _: Int -> ArrayList<Int>() }.add(i)
}
var ans = 0L
// Iterate over each unique number and its indices
for (entry in dict.entries) {
val b: MutableList<Int?> = entry.value!!
val b: MutableList<Int> = entry.value
val m = b.size
for (k in 0..<m) {
val i: Int = b[k]!!
val i: Int = b[k]
val r = m - 1 - k
val u = i - k
val v = (n - 1 - i) - r
Expand Down Expand Up @@ -71,20 +71,20 @@ class Solution {
var dif: Long = 0
// Principle of inclusion-exclusion
for (midEntry in dict.entries) {
val b: MutableList<Int?> = midEntry.value!!
val b: MutableList<Int> = midEntry.value
val m = b.size
for (tmpEntry in dict.entries) {
if (midEntry.key != tmpEntry.key) {
val c: MutableList<Int?> = tmpEntry.value!!
val c: MutableList<Int> = tmpEntry.value
val size = c.size
var k = 0
var j = 0
while (k < m) {
val i: Int = b[k]!!
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) {
while (j < size && c[j] < i) {
j++
}
val x = j
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import kotlin.math.min

class Solution {
fun minimumOperations(nums: IntArray): Int {
val map: MutableMap<Int?, Int?> = HashMap<Int?, Int?>()
val map: MutableMap<Int, Int> = HashMap()
var dupct = 0
for (num in nums) {
map.put(num, map.getOrDefault(num, 0)!! + 1)
map.put(num, map.getOrDefault(num, 0) + 1)
if (map[num] == 2) {
dupct++
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Solution {
if (flips <= numOps) {
return 1
}
val seg: MutableList<Int> = ArrayList<Int>()
val seg: MutableList<Int> = ArrayList()
var count = 1
var max = 1
for (i in 1..<b.size) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ package g3401_3500.s3404_count_special_subsequences

class Solution {
fun numberOfSubsequences(nums: IntArray): Long {
val freq: MutableMap<Double?, Int?> = HashMap<Double?, Int?>()
val freq: MutableMap<Double, Int> = HashMap()
var ans: Long = 0
for (r in 4..<nums.size) {
var p = 0
val q = r - 2
while (p < q - 1) {
val key = nums[p].toDouble() / nums[q]
freq.put(key, freq.getOrDefault(key, 0)!! + 1)
freq.put(key, freq.getOrDefault(key, 0) + 1)
++p
}
for (s in r + 2..<nums.size) {
ans += freq.getOrDefault(nums[s].toDouble() / nums[r], 0)!!.toLong()
ans += freq.getOrDefault(nums[s].toDouble() / nums[r], 0).toLong()
}
}
return ans
Expand Down

0 comments on commit e589447

Please sign in to comment.