Max Distance

题目描述

Given an array A of integers, find the maximum of j - i subjected to the constraint of A[i] <= A[j].

If there is no solution possible, return -1.

解题方法

brute force

O(n^2)的做法很容易想到

不行就sort大法

O(nlogn)

  • sort, 而且sort的时候我们还有保存原有的元素的index

Reference