How to solve this problem? — Permutation Algorithm

Правка en1, от Name123, 2019-10-17 03:32:54

Given an array A that is a permutation of n numbers [1-n]. Find the number of subarrarys S that meets the following condition max(S) — min(S) = length(S) — 1.

Example 1:

Input: [4, 3, 1, 2, 5] Output: 10 Explanation: subarrays that meets the condition are [4] [3] [1] [2] [5] [4 3] [1 2] [3 1 2] [4 3 1 2] [4 3 1 2 5]

There are 10 subarray that meets the condition, so the answer should be 10.

Is there a better than O(n^2) solution?

Теги #subarray, #permutation

История

 
 
 
 
Правки
 
 
  Rev. Язык Кто Когда Δ Комментарий
en1 Английский Name123 2019-10-17 03:32:54 520 Initial revision (published)