Skip to content

Improved min and max #797

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 1 commit into from
Apr 12, 2025
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 @@ -10,12 +10,12 @@ class Solution {
var ans = maxHeights[pickId].toLong()
var min = maxHeights[pickId].toLong()
for (i in pickId - 1 downTo 0) {
min = min(min.toDouble(), maxHeights[i].toDouble()).toLong()
min = min(min, maxHeights[i].toLong())
ans += min
}
min = maxHeights[pickId].toLong()
for (i in pickId + 1 until maxHeights.size) {
min = min(min.toDouble(), maxHeights[i].toDouble()).toLong()
min = min(min, maxHeights[i].toLong())
ans += min
}
return ans
Expand All @@ -30,7 +30,7 @@ class Solution {
maxHeights[i] >= maxHeights[i + 1]
)
) {
ans = max(ans.toDouble(), `fun`(maxHeights, i).toDouble()).toLong()
ans = max(ans, `fun`(maxHeights, i))
}
}
return ans
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ class Solution {
var tempMax = nums[0]
for (i in 1 until diff.size - 1) {
diff[i] = tempMax - nums[i]
tempMax = max(tempMax.toDouble(), nums[i].toDouble()).toInt()
tempMax = max(tempMax, nums[i])
}
var max = Long.MIN_VALUE
tempMax = nums[nums.size - 1]
for (i in nums.size - 2 downTo 1) {
max = max(max.toDouble(), (tempMax.toLong() * diff[i]).toDouble()).toLong()
tempMax = max(tempMax.toDouble(), nums[i].toDouble()).toInt()
max = max(max, tempMax.toLong() * diff[i])
tempMax = max(tempMax, nums[i])
}
return max(max.toDouble(), 0.0).toLong()
return max(max, 0)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,12 @@ class Solution {
}
val dp = IntArray(m)
dp[0] = 0
dp[1] = min(x.toDouble(), (diffs[1] - diffs[0]).toDouble()).toInt()
dp[1] = min(x, diffs[1] - diffs[0])
for (i in 2 until m) {
if ((i and 1) == 1) {
dp[i] = min((dp[i - 1] + x).toDouble(), (dp[i - 2] + diffs[i] - diffs[i - 1]).toDouble())
.toInt()
dp[i] = min(dp[i - 1] + x, dp[i - 2] + diffs[i] - diffs[i - 1])
} else {
dp[i] = min(dp[i - 1].toDouble(), (dp[i - 2] + diffs[i] - diffs[i - 1]).toDouble())
.toInt()
dp[i] = min(dp[i - 1], dp[i - 2] + diffs[i] - diffs[i - 1])
}
}
return dp[m - 1]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Solution {
for (k in j + 1 until nums.size) {
if (nums[i] < nums[j] && nums[k] < nums[j]) {
val min = nums[i] + nums[k] + nums[j]
output = min(min.toDouble(), output.toDouble()).toInt()
output = min(min, output)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ class Solution {
var ans = Int.MAX_VALUE
for (i in 0 until n) {
if (leftSmallest[i] != -1 && rightSmallest[i] != -1) {
ans = min(ans.toDouble(), (leftSmallest[i] + rightSmallest[i] + nums[i]).toDouble())
.toInt()
ans = min(ans, leftSmallest[i] + rightSmallest[i] + nums[i])
}
}
if (ans == Int.MAX_VALUE) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Solution {
}
var min = INF
for (j in (k - 1) * 2 until (i - 1)) {
min = min(min.toDouble(), (calc(j, k - 1) + change(j, i)).toDouble()).toInt()
min = min(min, calc(j, k - 1) + change(j, i))
}
dp[i][k] = min
return min
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class Solution {
var index = index
var result: Long = 0
while (index > 0) {
result = max(tree[index].toDouble(), result.toDouble()).toLong()
result = max(tree[index], result)
index -= lowbit(index)
}
return result
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Solution {
if (acquired + 1 < n) {
var min = Int.MAX_VALUE
for (j in acquired + 1 downTo i + 1) {
min = min(min.toDouble(), dp[j].toDouble()).toInt()
min = min(min, dp[j])
}
dp[i] = prices[i] + min
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class Solution {
var res: Long = 1
for (i in 1 until sick.size) {
val group = sick[i] - sick[i - 1] - 1
res = res * modPow(2, max(0.0, (group - 1).toDouble()).toInt(), MOD) % MOD
res = res * modPow(2, max(0, group - 1), MOD) % MOD
res = res * binomCoeff(sick[i] - i, group) % MOD
}
return (res * binomCoeff(n - sick.size, n - sick[sick.size - 1] - 1) % MOD).toInt()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ class Solution {
var m1 = Int.MIN_VALUE
var m2 = Int.MAX_VALUE
for (num in nums) {
m1 = max(m1.toDouble(), num.toDouble()).toInt()
m2 = min(m2.toDouble(), num.toDouble()).toInt()
m1 = max(m1, num)
m2 = min(m2, num)
}
var max = 0
val f = IntArray(m1 - m2 + 1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Solution {
val sb = StringBuilder()
for (c in ar) {
freq[c.code - 'a'.code]++
max = max(freq[c.code - 'a'.code].toDouble(), max.toDouble()).toInt()
max = max(freq[c.code - 'a'.code], max)
}
for (i in n - 1 downTo 0) {
if (freq[ar[i].code - 'a'.code] == max) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Solution {
var max = 0
for (j in capacity) {
count[j]++
max = max(max.toDouble(), j.toDouble()).toInt()
max = max(max, j)
}
for (i in max downTo 0) {
if (count[i] >= 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ class Solution {
val curLen = search(
cs,
i,
min(m.toDouble(), (i + resultLen).toDouble())
.toInt(),
min(m, (i + resultLen)),
k,
)
if (curLen != -1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class Solution {
val area1 = (mid - l + 1).toLong() * median
val area2 = (r - mid).toLong() * median
val curRes = area1 - sum1 + sum2 - area2
res = min(res.toDouble(), curRes.toDouble()).toLong()
res = min(res, curRes)
l++
}
res += 2L * maxChanges
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Solution {
var max = Int.MIN_VALUE
val n = nums.size
for (num in nums) {
max = max(max.toDouble(), num.toDouble()).toInt()
max = max(max, num)
}
val bins = LongArray(max + 1)
var mostFrequentID = 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Solution {
for (i in word.indices) {
val a = word[i]
if (a.code < 91) {
capital[a.code - 65] = min(capital[a.code - 65].toDouble(), i.toDouble()).toInt()
capital[a.code - 65] = min(capital[a.code - 65], i)
} else {
small[a.code - 97] = i
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class Solution {
right.parent[x] = next
bit.update(next, next - pre)
} else {
val maxGap = max(bit.query(pre).toDouble(), (x - pre).toDouble()).toInt()
val maxGap = max(bit.query(pre), x - pre)
ans[index--] = maxGap >= q[2]
}
}
Expand All @@ -83,7 +83,7 @@ class Solution {
fun update(i: Int, v: Int) {
var i = i
while (i < n) {
tree[i] = max(tree[i].toDouble(), v.toDouble()).toInt()
tree[i] = max(tree[i], v)
i += i and -i
}
}
Expand All @@ -92,7 +92,7 @@ class Solution {
var i = i
var result = 0
while (i > 0) {
result = max(result.toDouble(), tree[i].toDouble()).toInt()
result = max(result, tree[i])
i = i and i - 1
}
return result
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Solution {
var addResult = nums[0].toLong()
var subResult = nums[0].toLong()
for (i in 1 until n) {
val tempAdd = (max(addResult.toDouble(), subResult.toDouble()) + nums[i]).toLong()
val tempAdd = max(addResult, subResult) + nums[i]
val tempSub = addResult - nums[i]
addResult = tempAdd
subResult = tempSub
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Solution {
val n = enemyEnergies.size
var min = enemyEnergies[0]
for (i in 1 until n) {
min = min(min.toDouble(), enemyEnergies[i].toDouble()).toInt()
min = min(min, enemyEnergies[i])
}
if (currentEnergy == 0 || currentEnergy < min) {
return 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class Solution {

private fun allKPalindromes(n: Int, k: Int): List<String> {
val ans = StringBuilder(n)
ans.append("0".repeat(max(0.0, n.toDouble()).toInt()))
ans.append("0".repeat(max(0, n)))
val rem = IntArray(n)
rem[0] = 1
for (i in 1 until n) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class Solution {
for (i in 2..<m - 2) {
var len = 0
if (i < right) {
len = min(lens[2 * center - i].toDouble(), (right - i).toDouble()).toInt()
len = min(lens[2 * center - i], right - i)
}
while (t[i + len + 1] == t[i - len - 1]) {
len++
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class Solution {
run {
var i = 0
while (k != 0 && i < m) {
max = max(max.toDouble(), b[i][k - 1].toDouble()).toInt()
max = max(max, b[i][k - 1])
i++
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ class Solution {
if (start >= max && ++cut == 2) {
return true
}
max = max(max.toDouble(), (arr[i] and MASK.toLong()).toInt().toDouble()).toInt()
max = max(max, (arr[i] and MASK.toLong()).toInt())
}
return false
}

companion object {
private val MASK = (1 shl 30) - 1
private const val MASK = (1 shl 30) - 1
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Solution {
currGCD = gcd(currGCD, nums[j])
currLCM = lcm(currLCM, nums[j])
if (currPro == currLCM * currGCD) {
maxL = max(maxL.toDouble(), (j - i + 1).toDouble()).toInt()
maxL = max(maxL, j - i + 1)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,31 @@ import kotlin.math.max

class Solution {
fun maximumCoins(coins: Array<IntArray>, k: Int): Long {
coins.sortWith { a: IntArray?, b: IntArray? -> a!![0] - b!![0] }
coins.sortWith { a: IntArray, b: IntArray -> a[0] - b[0] }
val n = coins.size
var res: Long = 0
var cur: Long = 0
var j = 0
for (ints in coins) {
while (j < n && coins[j][1] <= ints[0] + k - 1) {
cur += (coins[j][1] - coins[j][0] + 1).toLong() * coins[j][2]
cur += (coins[j][1] - coins[j][0] + 1) * coins[j][2]
j++
}
if (j < n) {
val part = max(0.0, (ints[0] + k - 1 - coins[j][0] + 1).toDouble()).toLong() * coins[j][2]
res = max(res.toDouble(), (cur + part).toDouble()).toLong()
val part = max(0, ints[0] + k - 1 - coins[j][0] + 1) * coins[j][2]
res = max(res, cur + part)
}
cur -= (ints[1] - ints[0] + 1).toLong() * ints[2]
cur -= (ints[1] - ints[0] + 1) * ints[2]
}
cur = 0
j = 0
for (coin in coins) {
cur += (coin[1] - coin[0] + 1).toLong() * coin[2]
cur += (coin[1] - coin[0] + 1) * coin[2]
while (coins[j][1] < coin[1] - k + 1) {
cur -= (coins[j][1] - coins[j][0] + 1).toLong() * coins[j][2]
cur -= (coins[j][1] - coins[j][0] + 1) * coins[j][2]
j++
}
val part = max(0.0, (coin[1] - k - coins[j][0] + 1).toDouble()).toLong() * coins[j][2]
val part = max(0, coin[1] - k - coins[j][0] + 1) * coins[j][2]
res = max(res, (cur - part))
}
return res
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class Solution {
for (j in 0..<m) {
if (g[i][j] == 1) {
for (d in 0..3) {
res = max(res.toDouble(), dp(i, j, 1, d, 1).toDouble()).toInt()
res = max(res, dp(i, j, 1, d, 1))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Solution {
seg[size + i] = 0
}
for (i in size - 1 downTo 1) {
seg[i] = max(seg[i shl 1].toDouble(), seg[i shl 1 or 1].toDouble()).toInt()
seg[i] = max(seg[i shl 1], seg[i shl 1 or 1])
}
var ans = 0
for (f in fruits) {
Expand All @@ -46,7 +46,7 @@ class Solution {
seg[i] = `val`
i /= 2
while (i > 0) {
seg[i] = max(seg[i shl 1].toDouble(), seg[i shl 1 or 1].toDouble()).toInt()
seg[i] = max(seg[i shl 1], seg[i shl 1 or 1])
i /= 2
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/g3501_3600/s3508_implement_router/Router.kt
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class Router(private val size: Int) {
return true
}

fun forwardPacket(): IntArray? {
fun forwardPacket(): IntArray {
if (q.isEmpty()) {
return intArrayOf()
}
Expand Down Expand Up @@ -85,7 +85,7 @@ class Router(private val size: Int) {
if (lower == -1 || higher == -1) {
0
} else {
max(0.0, (higher - lower + 1).toDouble()).toInt()
max(0, higher - lower + 1)
}
} else {
0
Expand Down