×

EASY

# EXPLANATION

It is easy to see that in order to make the least number of stops, one should cover as much distance as possible each time. We can compute this in linear time, by maintaining two pointers p1 and p2 such that a[p2] - a[p1] <= M, but a[p2+1] - a[p1] > M. We can decrement p1 and adjust p2 accordingly.

Thus we get get for each point i, dp[i], which is the minimum number of steps to reach the destination from location i. To count the number of ways, we have dp[i] = sum dp[j], such that dp[j] = dp[i+1], and a[j] - a[i] <= M. We can easily compute the dp[] array in linear time as well.

This question is marked "community wiki".

19.1k348495534
accept rate: 36%

So if the input is 5 6 0 4 5 6 10 Would the array dp={4,2,2,1,1}? or array dp={3,1,1,1}? In the above explanation: "Thus we get get for each point i, dp[i], which is the minimum number of steps to reach the destination from location i."

Does dp[i] show us the minimum number of steps from i to destination or the number of ways from i to destination?

(25 Dec '12, 14:13)

I seem to have the same problem as @jaskaran_1

(27 Jan '13, 23:22) 4★
 toggle preview community wiki:
Preview

By Email:

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• image?![alt text](/path/img.jpg "title")
• numbered list: 1. Foo 2. Bar
• to add a line break simply add two spaces to where you would like the new line to be.
• basic HTML tags are also supported
• mathemetical formulas in Latex between \$ symbol

Question tags:

×14,850
×3,291
×12
×12

question asked: 28 Nov '12, 18:38

question was seen: 2,981 times

last updated: 27 Jan '13, 23:22