kt: add «1574. Shortest Subarray to be Removed to Make Array Sorted»

URL:	https://leetcode.com/problems/shortest-subarray-to-be-removed-to-make-array-sorted/
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2024-11-15 23:25:03 +01:00
parent 784547b4a2
commit 970f7c1060
Signed by: mfocko
SSH key fingerprint: SHA256:icm0fIOSJUpy5+1x23sfr+hLtF9UhY8VpMC7H4WFJP8

View file

@ -0,0 +1,22 @@
class Solution {
fun findLengthOfShortestSubarray(arr: IntArray): Int {
var (left, right) = 0 to arr.size - 1
// shift right while ensorted
while (right > 0 && arr[right] >= arr[right - 1]) {
right -= 1
}
var shortest = right
while (left < right && (left < 1 || arr[left - 1] <= arr[left])) {
while (right < arr.size && arr[left] > arr[right]) {
right += 1
}
shortest = listOf(shortest, right - left - 1).min()
left += 1
}
return shortest
}
}