Sliding Window Maximum

A long array A[] is given to you. There is a sliding window
of size w which is moving from the very left of the array to
the very right. You can only see the w numbers in the window.
Each time the sliding window moves rightwards by one position.

 * Example:
 * [1  3  -1] -3  5  3  6  7
 *  1 [3  -1  -3] 5  3  6  7
 *  1  3 [-1  -3  5] 3  6  7
 *  1  3  -1 [-3  5  3] 6  7
 *  1  3  -1  -3 [5  3  6] 7
 *  1  3  -1  -3  5 [3  6  7]

Input: A long array A[], and a window width w
Output: An array B[], B[i] is the maximum value of from A[i+w-1]
Requirement: find a good optimal way to get B[i]