关于阵列中最深坑的混淆

时间:2017-10-22 17:43:30

标签: algorithm

我把这个问题作为面试的先决条件,

  

给出了由N个整数组成的非空零索引数组A.一个   该数组中的凹坑是任何整数三元组(P,Q,R),使得:0≤   P< Q< R< N;

     

序列[A [P],A [P + 1],...,A [Q]]严格减少,即A [P]>   A [P + 1]> ...> A [Q];

     

序列A [Q],A [Q + 1],...,A [R]严格增加,即A [Q] <1。   A [Q + 1]&lt; ......&lt; A [R]。

     

坑的深度(P,Q,R)是最小值{A [P] - A [Q],A [R] -   A [Q]}。例如,考虑由10个元素组成的数组A.   的是:

A[0] =  0
A[1] =  1
A[2] =  3
A[3] = -2
A[4] =  0
A[5] =  1
A[6] =  0
A[7] = -3
A[8] =  2
A[9] =  3
     

Triplet(2,3,4)是此数组中的凹坑之一,因为序列   [A [2],A [3]]严格减少(3> -2)和序列[A [3],A [4]]   严格地增加(-2 <0)。其深度为min {A [2] - A [3],A [4] -   A [3]} = 2。

     

Triplet(2,3,5)是另一个深度为3的坑。

     

Triplet(5,7,8)是另一个深度为4的坑。没有坑   这个阵列比4更深(即深度更大)。

它表示Triplet(5,7,8)的最深坑深度为4。

但不是三胞胎(2,7,9)有最深的凹坑深度6?

三联体(2,7,9)的对应值是(3,-3,3),它也满足上述条件,即

1) 0 ≤ P < Q < R < N
2) A[P] > A[P+1] > ... > A[Q] and A[Q] < A[Q+1] < ... < A[R]

所以在这种情况下,min {A [P] - A [Q],A [R] - A [Q]}是6。

我在这里缺少什么?

PS 如果您认为此帖子不属于此论坛,请指出我应该在哪里发布。

3 个答案:

答案 0 :(得分:1)

查看P的{​​{1}}到Q的序列。

2 to 7

序列[A [P],A [P + 1],...,A [Q]]严格减少,即A [P]> A [P + 1]> ...&gt; A [Q];

规则说这应该是一个递减的序列。但事实并非如此。 3 -2 0 1 0 -33>-2此处序列中断。

来自-2 is not greater than 0。没有问题,因为序列正在增加。 7 to 9

答案 1 :(得分:0)

  

迅速解决最深坑问题:

 func solution(_ array: [Int]) -> Int {

        //guaranty we have at least three elements
        if array.isEmpty  {
            print("isEmpty")
            return -1
        }

        if array.count < 3 {
            print("is less than 3")
            return -1
        }

        //extremum point; max or min points
        var extremumPoints = [Int]()

        //adding first element
        extremumPoints.append(array[0])

        //calculate extremum points for 1 to one before last element
        for i in 1..<(array.count - 1) {

            let isRelativeExtremum = ((array[i] - array[i - 1]) * (array[i] - array[i + 1])) > 0
            //we call a point semi-extremum if a point is equal to previous element or next element and not equal to previous element or next element
            let isSemiExtremum = ((array[i] != array[i - 1]) && (array[i] == array[i + 1])) || ((array[i] != array[i + 1]) && (array[i] == array[i - 1]))

            if isRelativeExtremum || isSemiExtremum {
                extremumPoints.append(array[i])
            }
        }

        //adding last element
        extremumPoints.append(array[array.count - 1])

        //we will hold depthes in this array
        var depthes = [Int]()

        for i in 1..<(extremumPoints.count - 1) {

            let isBottomOfaPit = extremumPoints[i] < extremumPoints[i - 1] && extremumPoints[i] < extremumPoints[i + 1]

            if isBottomOfaPit {

                let d1 = extremumPoints[i - 1] - extremumPoints[i]
                let d2 = extremumPoints[i + 1] - extremumPoints[i]
                let d = min(d1, d2)
                depthes.append(d)
            }

        }

        //deepest pit
        let deepestPit = depthes.max()
        return deepestPit ?? -1
    }

// ****************************

let A = [0,1,3,-2,0,1,0,-3,2,3]
let deepestPit = solution(A)
print(deepestPit) // 4

答案 2 :(得分:0)

def deepest(A):     
    def check(p, q, r, A):
        if A[p] > A[q] and A[q] < A[r]:
            return min(A[p] - A[q], A[r] - A[q])
        else:
            return -1
    max_depth = 0
    for i in range(1, len(A) - 2):
        if A[i-1] > A[i] < A[i + 1]:
            p = i
            r = i
            while 0 <= p and r <= len(A) - 1:
                depth = check(p, i, r, A)
                max_depth = max(max_depth, depth)
                p -= 1
                r += 1
    return max_depth