Skip to content

Commit e589447

Browse files
committed
Improved tasks 3394, 3395, 3396, 3399, 3404
1 parent dabacd5 commit e589447

File tree

5 files changed

+23
-23
lines changed
  • src/main/kotlin
    • g3301_3400
      • s3394_check_if_grid_can_be_cut_into_sections
      • s3395_subsequences_with_a_unique_middle_mode_i
      • s3396_minimum_number_of_operations_to_make_elements_in_array_distinct
      • s3399_smallest_substring_with_identical_characters_ii
    • g3401_3500/s3404_count_special_subsequences

5 files changed

+23
-23
lines changed

src/main/kotlin/g3301_3400/s3394_check_if_grid_can_be_cut_into_sections/Solution.kt

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import kotlin.math.max
88
class Solution {
99
fun checkValidCuts(n: Int, rectangles: Array<IntArray>): Boolean {
1010
val m = rectangles.size
11-
val xAxis = Array<IntArray?>(m) { IntArray(2) }
12-
val yAxis = Array<IntArray?>(m) { IntArray(2) }
11+
val xAxis = Array<IntArray>(m) { IntArray(2) }
12+
val yAxis = Array<IntArray>(m) { IntArray(2) }
1313
var ind = 0
1414
for (axis in rectangles) {
1515
val startX = axis[0]
@@ -21,8 +21,8 @@ class Solution {
2121
ind++
2222
}
2323

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

2828
yAxis.sortWith<IntArray?>(
@@ -36,15 +36,15 @@ class Solution {
3636
return horizontalCuts > 2
3737
}
3838

39-
private fun findSections(axis: Array<IntArray?>): Int {
40-
var end = axis[0]!![1]
39+
private fun findSections(axis: Array<IntArray>): Int {
40+
var end = axis[0][1]
4141
var sections = 1
4242
for (i in 1..<axis.size) {
43-
if (end > axis[i]!![0]) {
44-
end = max(end, axis[i]!![1])
43+
if (end > axis[i][0]) {
44+
end = max(end, axis[i][1])
4545
} else {
4646
sections++
47-
end = axis[i]!![1]
47+
end = axis[i][1]
4848
}
4949
if (sections > 2) {
5050
return sections

src/main/kotlin/g3301_3400/s3395_subsequences_with_a_unique_middle_mode_i/Solution.kt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,17 @@ class Solution {
66
fun subsequencesWithMiddleMode(a: IntArray): Int {
77
val n = a.size
88
// Create a dictionary to store indices of each number
9-
val dict: MutableMap<Int?, MutableList<Int?>?> = HashMap<Int?, MutableList<Int?>?>()
9+
val dict: MutableMap<Int, MutableList<Int>> = HashMap()
1010
for (i in 0..<n) {
11-
dict.computeIfAbsent(a[i]) { _: Int? -> java.util.ArrayList<Int?>() }!!.add(i)
11+
dict.computeIfAbsent(a[i]) { _: Int -> ArrayList<Int>() }.add(i)
1212
}
1313
var ans = 0L
1414
// Iterate over each unique number and its indices
1515
for (entry in dict.entries) {
16-
val b: MutableList<Int?> = entry.value!!
16+
val b: MutableList<Int> = entry.value
1717
val m = b.size
1818
for (k in 0..<m) {
19-
val i: Int = b[k]!!
19+
val i: Int = b[k]
2020
val r = m - 1 - k
2121
val u = i - k
2222
val v = (n - 1 - i) - r
@@ -71,20 +71,20 @@ class Solution {
7171
var dif: Long = 0
7272
// Principle of inclusion-exclusion
7373
for (midEntry in dict.entries) {
74-
val b: MutableList<Int?> = midEntry.value!!
74+
val b: MutableList<Int> = midEntry.value
7575
val m = b.size
7676
for (tmpEntry in dict.entries) {
7777
if (midEntry.key != tmpEntry.key) {
78-
val c: MutableList<Int?> = tmpEntry.value!!
78+
val c: MutableList<Int> = tmpEntry.value
7979
val size = c.size
8080
var k = 0
8181
var j = 0
8282
while (k < m) {
83-
val i: Int = b[k]!!
83+
val i: Int = b[k]
8484
val r = m - 1 - k
8585
val u = i - k
8686
val v = (n - 1 - i) - r
87-
while (j < size && c[j]!! < i) {
87+
while (j < size && c[j] < i) {
8888
j++
8989
}
9090
val x = j

src/main/kotlin/g3301_3400/s3396_minimum_number_of_operations_to_make_elements_in_array_distinct/Solution.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ import kotlin.math.min
66

77
class Solution {
88
fun minimumOperations(nums: IntArray): Int {
9-
val map: MutableMap<Int?, Int?> = HashMap<Int?, Int?>()
9+
val map: MutableMap<Int, Int> = HashMap()
1010
var dupct = 0
1111
for (num in nums) {
12-
map.put(num, map.getOrDefault(num, 0)!! + 1)
12+
map.put(num, map.getOrDefault(num, 0) + 1)
1313
if (map[num] == 2) {
1414
dupct++
1515
}

src/main/kotlin/g3301_3400/s3399_smallest_substring_with_identical_characters_ii/Solution.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class Solution {
2424
if (flips <= numOps) {
2525
return 1
2626
}
27-
val seg: MutableList<Int> = ArrayList<Int>()
27+
val seg: MutableList<Int> = ArrayList()
2828
var count = 1
2929
var max = 1
3030
for (i in 1..<b.size) {

src/main/kotlin/g3401_3500/s3404_count_special_subsequences/Solution.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@ package g3401_3500.s3404_count_special_subsequences
44

55
class Solution {
66
fun numberOfSubsequences(nums: IntArray): Long {
7-
val freq: MutableMap<Double?, Int?> = HashMap<Double?, Int?>()
7+
val freq: MutableMap<Double, Int> = HashMap()
88
var ans: Long = 0
99
for (r in 4..<nums.size) {
1010
var p = 0
1111
val q = r - 2
1212
while (p < q - 1) {
1313
val key = nums[p].toDouble() / nums[q]
14-
freq.put(key, freq.getOrDefault(key, 0)!! + 1)
14+
freq.put(key, freq.getOrDefault(key, 0) + 1)
1515
++p
1616
}
1717
for (s in r + 2..<nums.size) {
18-
ans += freq.getOrDefault(nums[s].toDouble() / nums[r], 0)!!.toLong()
18+
ans += freq.getOrDefault(nums[s].toDouble() / nums[r], 0).toLong()
1919
}
2020
}
2121
return ans

0 commit comments

Comments
 (0)