The max product of consecutive elements in an array

后端 未结 8 1499
面向向阳花
面向向阳花 2021-01-30 03:13

I was asked this algorithm question during my onsite interview. Since I was not asked to sign NDA, I post it here for an answer.

Given an array of REAL

8条回答
  •  野的像风
    2021-01-30 03:55

    Ignoring negative numbers for the moment...

    Let A[i..j] mean A[i]*A[i+1]*...*A[j]

    The problem is to find max(A[i..j])

    Notice that A[i..j] = A[0..j] / A[0..i-1]

    So if we calculate A[0..x] for all x.

    We can then determine max(A[i..j]) = max(A[0..x]) / min(A[0..y])

提交回复
热议问题