Skip to content

Commit fa8f384

Browse files
authored
Improved min and max
1 parent 5d01592 commit fa8f384

File tree

28 files changed

+48
-52
lines changed
  • src/main/kotlin
    • g2801_2900
    • g2901_3000
      • s2908_minimum_sum_of_mountain_triplets_i
      • s2909_minimum_sum_of_mountain_triplets_ii
      • s2911_minimum_changes_to_make_k_semi_palindromes
      • s2926_maximum_balanced_subsequence_sum
      • s2944_minimum_number_of_coins_for_fruits
      • s2954_count_the_number_of_infection_sequences
      • s2958_length_of_longest_subarray_with_at_most_k_frequency
    • g3001_3100
    • g3101_3200
    • g3201_3300
    • g3301_3400
      • s3327_check_if_dfs_strings_are_palindromes
      • s3372_maximize_the_number_of_target_nodes_after_connecting_trees_i
      • s3394_check_if_grid_can_be_cut_into_sections
    • g3401_3500
    • g3501_3600/s3508_implement_router

28 files changed

+48
-52
lines changed

src/main/kotlin/g2801_2900/s2865_beautiful_towers_i/Solution.kt

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ class Solution {
1010
var ans = maxHeights[pickId].toLong()
1111
var min = maxHeights[pickId].toLong()
1212
for (i in pickId - 1 downTo 0) {
13-
min = min(min.toDouble(), maxHeights[i].toDouble()).toLong()
13+
min = min(min, maxHeights[i].toLong())
1414
ans += min
1515
}
1616
min = maxHeights[pickId].toLong()
1717
for (i in pickId + 1 until maxHeights.size) {
18-
min = min(min.toDouble(), maxHeights[i].toDouble()).toLong()
18+
min = min(min, maxHeights[i].toLong())
1919
ans += min
2020
}
2121
return ans
@@ -30,7 +30,7 @@ class Solution {
3030
maxHeights[i] >= maxHeights[i + 1]
3131
)
3232
) {
33-
ans = max(ans.toDouble(), `fun`(maxHeights, i).toDouble()).toLong()
33+
ans = max(ans, `fun`(maxHeights, i))
3434
}
3535
}
3636
return ans

src/main/kotlin/g2801_2900/s2874_maximum_value_of_an_ordered_triplet_ii/Solution.kt

+4-4
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ class Solution {
1010
var tempMax = nums[0]
1111
for (i in 1 until diff.size - 1) {
1212
diff[i] = tempMax - nums[i]
13-
tempMax = max(tempMax.toDouble(), nums[i].toDouble()).toInt()
13+
tempMax = max(tempMax, nums[i])
1414
}
1515
var max = Long.MIN_VALUE
1616
tempMax = nums[nums.size - 1]
1717
for (i in nums.size - 2 downTo 1) {
18-
max = max(max.toDouble(), (tempMax.toLong() * diff[i]).toDouble()).toLong()
19-
tempMax = max(tempMax.toDouble(), nums[i].toDouble()).toInt()
18+
max = max(max, tempMax.toLong() * diff[i])
19+
tempMax = max(tempMax, nums[i])
2020
}
21-
return max(max.toDouble(), 0.0).toLong()
21+
return max(max, 0)
2222
}
2323
}

src/main/kotlin/g2801_2900/s2896_apply_operations_to_make_two_strings_equal/Solution.kt

+3-5
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,12 @@ class Solution {
2121
}
2222
val dp = IntArray(m)
2323
dp[0] = 0
24-
dp[1] = min(x.toDouble(), (diffs[1] - diffs[0]).toDouble()).toInt()
24+
dp[1] = min(x, diffs[1] - diffs[0])
2525
for (i in 2 until m) {
2626
if ((i and 1) == 1) {
27-
dp[i] = min((dp[i - 1] + x).toDouble(), (dp[i - 2] + diffs[i] - diffs[i - 1]).toDouble())
28-
.toInt()
27+
dp[i] = min(dp[i - 1] + x, dp[i - 2] + diffs[i] - diffs[i - 1])
2928
} else {
30-
dp[i] = min(dp[i - 1].toDouble(), (dp[i - 2] + diffs[i] - diffs[i - 1]).toDouble())
31-
.toInt()
29+
dp[i] = min(dp[i - 1], dp[i - 2] + diffs[i] - diffs[i - 1])
3230
}
3331
}
3432
return dp[m - 1]

src/main/kotlin/g2901_3000/s2908_minimum_sum_of_mountain_triplets_i/Solution.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class Solution {
1515
for (k in j + 1 until nums.size) {
1616
if (nums[i] < nums[j] && nums[k] < nums[j]) {
1717
val min = nums[i] + nums[k] + nums[j]
18-
output = min(min.toDouble(), output.toDouble()).toInt()
18+
output = min(min, output)
1919
}
2020
}
2121
}

src/main/kotlin/g2901_3000/s2909_minimum_sum_of_mountain_triplets_ii/Solution.kt

+1-2
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@ class Solution {
3232
var ans = Int.MAX_VALUE
3333
for (i in 0 until n) {
3434
if (leftSmallest[i] != -1 && rightSmallest[i] != -1) {
35-
ans = min(ans.toDouble(), (leftSmallest[i] + rightSmallest[i] + nums[i]).toDouble())
36-
.toInt()
35+
ans = min(ans, leftSmallest[i] + rightSmallest[i] + nums[i])
3736
}
3837
}
3938
if (ans == Int.MAX_VALUE) {

src/main/kotlin/g2901_3000/s2911_minimum_changes_to_make_k_semi_palindromes/Solution.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class Solution {
2828
}
2929
var min = INF
3030
for (j in (k - 1) * 2 until (i - 1)) {
31-
min = min(min.toDouble(), (calc(j, k - 1) + change(j, i)).toDouble()).toInt()
31+
min = min(min, calc(j, k - 1) + change(j, i))
3232
}
3333
dp[i][k] = min
3434
return min

src/main/kotlin/g2901_3000/s2926_maximum_balanced_subsequence_sum/Solution.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ class Solution {
6868
var index = index
6969
var result: Long = 0
7070
while (index > 0) {
71-
result = max(tree[index].toDouble(), result.toDouble()).toLong()
71+
result = max(tree[index], result)
7272
index -= lowbit(index)
7373
}
7474
return result

src/main/kotlin/g2901_3000/s2944_minimum_number_of_coins_for_fruits/Solution.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class Solution {
1616
if (acquired + 1 < n) {
1717
var min = Int.MAX_VALUE
1818
for (j in acquired + 1 downTo i + 1) {
19-
min = min(min.toDouble(), dp[j].toDouble()).toInt()
19+
min = min(min, dp[j])
2020
}
2121
dp[i] = prices[i] + min
2222
} else {

src/main/kotlin/g2901_3000/s2954_count_the_number_of_infection_sequences/Solution.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class Solution {
4040
var res: Long = 1
4141
for (i in 1 until sick.size) {
4242
val group = sick[i] - sick[i - 1] - 1
43-
res = res * modPow(2, max(0.0, (group - 1).toDouble()).toInt(), MOD) % MOD
43+
res = res * modPow(2, max(0, group - 1), MOD) % MOD
4444
res = res * binomCoeff(sick[i] - i, group) % MOD
4545
}
4646
return (res * binomCoeff(n - sick.size, n - sick[sick.size - 1] - 1) % MOD).toInt()

src/main/kotlin/g2901_3000/s2958_length_of_longest_subarray_with_at_most_k_frequency/Solution.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ class Solution {
1010
var m1 = Int.MIN_VALUE
1111
var m2 = Int.MAX_VALUE
1212
for (num in nums) {
13-
m1 = max(m1.toDouble(), num.toDouble()).toInt()
14-
m2 = min(m2.toDouble(), num.toDouble()).toInt()
13+
m1 = max(m1, num)
14+
m2 = min(m2, num)
1515
}
1616
var max = 0
1717
val f = IntArray(m1 - m2 + 1)

src/main/kotlin/g3001_3100/s3039_apply_operations_to_make_string_empty/Solution.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class Solution {
1414
val sb = StringBuilder()
1515
for (c in ar) {
1616
freq[c.code - 'a'.code]++
17-
max = max(freq[c.code - 'a'.code].toDouble(), max.toDouble()).toInt()
17+
max = max(freq[c.code - 'a'.code], max)
1818
}
1919
for (i in n - 1 downTo 0) {
2020
if (freq[ar[i].code - 'a'.code] == max) {

src/main/kotlin/g3001_3100/s3074_apple_redistribution_into_boxes/Solution.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class Solution {
1515
var max = 0
1616
for (j in capacity) {
1717
count[j]++
18-
max = max(max.toDouble(), j.toDouble()).toInt()
18+
max = max(max, j)
1919
}
2020
for (i in max downTo 0) {
2121
if (count[i] >= 1) {

src/main/kotlin/g3001_3100/s3076_shortest_uncommon_substring_in_an_array/Solution.kt

+1-2
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ class Solution {
2828
val curLen = search(
2929
cs,
3030
i,
31-
min(m.toDouble(), (i + resultLen).toDouble())
32-
.toInt(),
31+
min(m, (i + resultLen)),
3332
k,
3433
)
3534
if (curLen != -1) {

src/main/kotlin/g3001_3100/s3086_minimum_moves_to_pick_k_ones/Solution.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class Solution {
5353
val area1 = (mid - l + 1).toLong() * median
5454
val area2 = (r - mid).toLong() * median
5555
val curRes = area1 - sum1 + sum2 - area2
56-
res = min(res.toDouble(), curRes.toDouble()).toLong()
56+
res = min(res, curRes)
5757
l++
5858
}
5959
res += 2L * maxChanges

src/main/kotlin/g3001_3100/s3092_most_frequent_ids/Solution.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class Solution {
1010
var max = Int.MIN_VALUE
1111
val n = nums.size
1212
for (num in nums) {
13-
max = max(max.toDouble(), num.toDouble()).toInt()
13+
max = max(max, num)
1414
}
1515
val bins = LongArray(max + 1)
1616
var mostFrequentID = 0

src/main/kotlin/g3101_3200/s3121_count_the_number_of_special_characters_ii/Solution.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class Solution {
1414
for (i in word.indices) {
1515
val a = word[i]
1616
if (a.code < 91) {
17-
capital[a.code - 65] = min(capital[a.code - 65].toDouble(), i.toDouble()).toInt()
17+
capital[a.code - 65] = min(capital[a.code - 65], i)
1818
} else {
1919
small[a.code - 97] = i
2020
}

src/main/kotlin/g3101_3200/s3161_block_placement_queries/Solution.kt

+3-3
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ class Solution {
7070
right.parent[x] = next
7171
bit.update(next, next - pre)
7272
} else {
73-
val maxGap = max(bit.query(pre).toDouble(), (x - pre).toDouble()).toInt()
73+
val maxGap = max(bit.query(pre), x - pre)
7474
ans[index--] = maxGap >= q[2]
7575
}
7676
}
@@ -83,7 +83,7 @@ class Solution {
8383
fun update(i: Int, v: Int) {
8484
var i = i
8585
while (i < n) {
86-
tree[i] = max(tree[i].toDouble(), v.toDouble()).toInt()
86+
tree[i] = max(tree[i], v)
8787
i += i and -i
8888
}
8989
}
@@ -92,7 +92,7 @@ class Solution {
9292
var i = i
9393
var result = 0
9494
while (i > 0) {
95-
result = max(result.toDouble(), tree[i].toDouble()).toInt()
95+
result = max(result, tree[i])
9696
i = i and i - 1
9797
}
9898
return result

src/main/kotlin/g3101_3200/s3196_maximize_total_cost_of_alternating_subarrays/Solution.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class Solution {
1010
var addResult = nums[0].toLong()
1111
var subResult = nums[0].toLong()
1212
for (i in 1 until n) {
13-
val tempAdd = (max(addResult.toDouble(), subResult.toDouble()) + nums[i]).toLong()
13+
val tempAdd = max(addResult, subResult) + nums[i]
1414
val tempSub = addResult - nums[i]
1515
addResult = tempAdd
1616
subResult = tempSub

src/main/kotlin/g3201_3300/s3207_maximum_points_after_enemy_battles/Solution.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class Solution {
99
val n = enemyEnergies.size
1010
var min = enemyEnergies[0]
1111
for (i in 1 until n) {
12-
min = min(min.toDouble(), enemyEnergies[i].toDouble()).toInt()
12+
min = min(min, enemyEnergies[i])
1313
}
1414
if (currentEnergy == 0 || currentEnergy < min) {
1515
return 0

src/main/kotlin/g3201_3300/s3272_find_the_count_of_good_integers/Solution.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ class Solution {
7373

7474
private fun allKPalindromes(n: Int, k: Int): List<String> {
7575
val ans = StringBuilder(n)
76-
ans.append("0".repeat(max(0.0, n.toDouble()).toInt()))
76+
ans.append("0".repeat(max(0, n)))
7777
val rem = IntArray(n)
7878
rem[0] = 1
7979
for (i in 1 until n) {

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class Solution {
6666
for (i in 2..<m - 2) {
6767
var len = 0
6868
if (i < right) {
69-
len = min(lens[2 * center - i].toDouble(), (right - i).toDouble()).toInt()
69+
len = min(lens[2 * center - i], right - i)
7070
}
7171
while (t[i + len + 1] == t[i - len - 1]) {
7272
len++

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ class Solution {
8282
run {
8383
var i = 0
8484
while (k != 0 && i < m) {
85-
max = max(max.toDouble(), b[i][k - 1].toDouble()).toInt()
85+
max = max(max, b[i][k - 1])
8686
i++
8787
}
8888
}

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@ class Solution {
3232
if (start >= max && ++cut == 2) {
3333
return true
3434
}
35-
max = max(max.toDouble(), (arr[i] and MASK.toLong()).toInt().toDouble()).toInt()
35+
max = max(max, (arr[i] and MASK.toLong()).toInt())
3636
}
3737
return false
3838
}
3939

4040
companion object {
41-
private val MASK = (1 shl 30) - 1
41+
private const val MASK = (1 shl 30) - 1
4242
}
4343
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class Solution {
2626
currGCD = gcd(currGCD, nums[j])
2727
currLCM = lcm(currLCM, nums[j])
2828
if (currPro == currLCM * currGCD) {
29-
maxL = max(maxL.toDouble(), (j - i + 1).toDouble()).toInt()
29+
maxL = max(maxL, j - i + 1)
3030
}
3131
}
3232
}

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

+8-8
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,31 @@ import kotlin.math.max
77

88
class Solution {
99
fun maximumCoins(coins: Array<IntArray>, k: Int): Long {
10-
coins.sortWith { a: IntArray?, b: IntArray? -> a!![0] - b!![0] }
10+
coins.sortWith { a: IntArray, b: IntArray -> a[0] - b[0] }
1111
val n = coins.size
1212
var res: Long = 0
1313
var cur: Long = 0
1414
var j = 0
1515
for (ints in coins) {
1616
while (j < n && coins[j][1] <= ints[0] + k - 1) {
17-
cur += (coins[j][1] - coins[j][0] + 1).toLong() * coins[j][2]
17+
cur += (coins[j][1] - coins[j][0] + 1) * coins[j][2]
1818
j++
1919
}
2020
if (j < n) {
21-
val part = max(0.0, (ints[0] + k - 1 - coins[j][0] + 1).toDouble()).toLong() * coins[j][2]
22-
res = max(res.toDouble(), (cur + part).toDouble()).toLong()
21+
val part = max(0, ints[0] + k - 1 - coins[j][0] + 1) * coins[j][2]
22+
res = max(res, cur + part)
2323
}
24-
cur -= (ints[1] - ints[0] + 1).toLong() * ints[2]
24+
cur -= (ints[1] - ints[0] + 1) * ints[2]
2525
}
2626
cur = 0
2727
j = 0
2828
for (coin in coins) {
29-
cur += (coin[1] - coin[0] + 1).toLong() * coin[2]
29+
cur += (coin[1] - coin[0] + 1) * coin[2]
3030
while (coins[j][1] < coin[1] - k + 1) {
31-
cur -= (coins[j][1] - coins[j][0] + 1).toLong() * coins[j][2]
31+
cur -= (coins[j][1] - coins[j][0] + 1) * coins[j][2]
3232
j++
3333
}
34-
val part = max(0.0, (coin[1] - k - coins[j][0] + 1).toDouble()).toLong() * coins[j][2]
34+
val part = max(0, coin[1] - k - coins[j][0] + 1) * coins[j][2]
3535
res = max(res, (cur - part))
3636
}
3737
return res

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class Solution {
3131
for (j in 0..<m) {
3232
if (g[i][j] == 1) {
3333
for (d in 0..3) {
34-
res = max(res.toDouble(), dp(i, j, 1, d, 1).toDouble()).toInt()
34+
res = max(res, dp(i, j, 1, d, 1))
3535
}
3636
}
3737
}

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class Solution {
2020
seg[size + i] = 0
2121
}
2222
for (i in size - 1 downTo 1) {
23-
seg[i] = max(seg[i shl 1].toDouble(), seg[i shl 1 or 1].toDouble()).toInt()
23+
seg[i] = max(seg[i shl 1], seg[i shl 1 or 1])
2424
}
2525
var ans = 0
2626
for (f in fruits) {
@@ -46,7 +46,7 @@ class Solution {
4646
seg[i] = `val`
4747
i /= 2
4848
while (i > 0) {
49-
seg[i] = max(seg[i shl 1].toDouble(), seg[i shl 1 or 1].toDouble()).toInt()
49+
seg[i] = max(seg[i shl 1], seg[i shl 1 or 1])
5050
i /= 2
5151
}
5252
}

src/main/kotlin/g3501_3600/s3508_implement_router/Router.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class Router(private val size: Int) {
5151
return true
5252
}
5353

54-
fun forwardPacket(): IntArray? {
54+
fun forwardPacket(): IntArray {
5555
if (q.isEmpty()) {
5656
return intArrayOf()
5757
}
@@ -85,7 +85,7 @@ class Router(private val size: Int) {
8585
if (lower == -1 || higher == -1) {
8686
0
8787
} else {
88-
max(0.0, (higher - lower + 1).toDouble()).toInt()
88+
max(0, higher - lower + 1)
8989
}
9090
} else {
9191
0

0 commit comments

Comments
 (0)