Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Algorithm 关于阵列中最深坑的混淆_Algorithm - Fatal编程技术网

Algorithm 关于阵列中最深坑的混淆

Algorithm 关于阵列中最深坑的混淆,algorithm,Algorithm,我把这个问题作为面试的先决条件 给出了一个由N个整数组成的非空零索引数组。A. 此数组中的pit是任何三重整数(P,Q,R),因此:0≤ PA[Q] 序列A[Q],A[Q+1],…,A[R]严格递增,即A[Q]< A[Q+1]-2但-2不大于0序列在此中断。 从7到9。没有问题,因为序列正在增加-3 swift中最深坑问题的答案: def(A): def检查(p、q、r、A): 如果A[p]>A[q]和A[q]A[i]p=1搞砸了。它是存储在数组中的值,需要遵循规则。@Ankit是的,必须考虑其

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

给出了一个由N个整数组成的非空零索引数组。A. 此数组中的pit是任何三重整数(P,Q,R),因此:0≤ P 序列[A[p],A[p+1],…,A[Q]]严格递减,即A[p]> A[P+1]>…>A[Q]

序列A[Q],A[Q+1],…,A[R]严格递增,即A[Q]< A[Q+1]<… 坑的深度(p,Q,R)是min{a[p]− A[Q],A[R]− A[Q]}。例如,考虑数组A由10个元素组成 即:

三重态(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

三元组(2,3,5)是另一个深度为3的坑

三重体(5,7,8)是另一个深度为4的坑。这里没有坑 此阵列深度(即深度大于)大于4

它说三重体(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]
1)0≤ PA[P+1]>…>A[Q]和A[Q]
所以在这个例子中,min{A[p]− A[Q],A[R]− A[Q]}是6

我错过了什么


p.S.如果你认为这篇文章不属于这个论坛,那么请指出我应该在哪里发布。

请参见
p
Q
的顺序,查看
2到7

它是
3-21010-3

序列[A[P],A[P+1],…,A[Q]]严格递减,即A[P]>A[P+1]>…>A[Q]; 规则说这应该是一个递减序列。但事实并非如此<代码>3>-2
-2不大于0
序列在此中断。

7到9
。没有问题,因为序列正在增加<代码>-3 swift中最深坑问题的答案:

def(A):
def检查(p、q、r、A):
如果A[p]>A[q]和A[q]A[i]虽然0序列
[A[2],A[3],…,A[7]]
并没有严格地递减
A[3]
。gottcha。谢谢你指出,我把
A[p]>A[p+1]
p>p=1
搞砸了。它是存储在数组中的值,需要遵循规则。@Ankit是的,必须考虑其间的整个序列。这是我的答案。
1) 0 ≤ P < Q < R < N
2) A[P] > A[P+1] > ... > A[Q] and A[Q] < A[Q+1] < ... < A[R]
 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
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