func partitionDisjoint(nums []int) int {
n := len(nums)
leftMax := make([]int, n)
rightMin := make([]int, n)
max := nums[0]
for i, num := range nums {
if num > max {
max = num
}
leftMax[i] = max
}
min := nums[n-1]
for i := n - 1; i >= 0; i-- {
num := nums[i]
if num < min {
min = num
}
rightMin[i] = min
}
for i := 1; i < n; i++ {
if leftMax[i-1] <= rightMin[i] {
return i
}
}
return 0
}
-
Solution adopted from here
-
We maintain a
leftMax
andrightMin
array that holds the max value seen from the left and min value seen from the right at an indexi
. -
The solution we require occurs when the max value seen from the left is lesser than the min value seen from the right.