美文网首页
915. Partition Array into Disjoi

915. Partition Array into Disjoi

作者: 消魂泪 | 来源:发表于2019-11-01 15:49 被阅读0次

    题目

    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
    }
    

    相关文章

      网友评论

          本文标题:915. Partition Array into Disjoi

          本文链接:https://www.haomeiwen.com/subject/zyzfbctx.html