题目
Given an array A, partition it into two (contiguous) subarrays left and right so that:
- Every element in left is less than or equal to every element in right.left and right are non-empty.left has the smallest possible size.
- Return the length of left after such a partitioning. It is guaranteed that such a partitioning exists.
思路
左侧的最大值比右侧所有的值小。遍历数组,记录左侧最大值,当遍历到的值比左侧最大值大是,则该值加入左侧数组,并且更新左侧最大值;当遍历到的值比左侧最大值大时,更新当前最大值。
代码
func partitionDisjoint(A []int) int {
leftbigest := A[0]
curBigest := leftbigest
leftLong := 1
for i:=1 ; i < len(A) ; i ++ {
if (A[i] < leftbigest) {
leftbigest = curBigest
leftLong = i+1
} else if(A[i] > curBigest){
curBigest = A[i]
}
}
return leftLong
}
网友评论