TopcoderArchive
Archive
Algorithm problem

NewPlatformJumper

Problem statement, definition, constraints, and public examples.

Problem Statement

A new game called Platform Jumper is almost ready to be released. We don't know yet what the aim of the game will be, but we know that the game will consist of N platforms numbered 1 to N. You are given a int[] height, where the i-th element (1-indexed) is the height of platform i.

The main character can only jump from platform i to platform j if |height[i] - height[j]| + |i - j| does not exceed jumperLimit and j is greater than i. The developer of the game is afraid the game will be too easy if the main character can make many different jumps, so he has asked you to determine how many jumps are possible. Two jumps are considered different if either the starting platform or the ending platform are different. Return the number of possible different jumps.

Definition

Class:
NewPlatformJumper
Method:
howMany
Parameters:
int[], int
Returns:
int
Method signature:
int howMany(int[] height, int jumperLimit)
(be sure your method is public)

Notes

  • |x| represents the absolute value of x.

Constraints

  • height will contain between 1 and 50 elements, inclusive.
  • Each element of height will be between 1 and 1,000,000, inclusive.
  • jumperLimit will be between 1 and 1,000,000, inclusive.

Examples

  1. {6,5,4}
    3
    Returns: 2
    Jump from platform 1 to platform 2 is possible because |6-5|+|1-2|=2<3. Jump from platform 2 to platform 3 is possible because |5-4|+|2-3|=2<3. Jump from platform 1 to platform 3 is impossible because |6-4|+|1-3|=4>3.
  2. {1,1,1,1}
    3
    Returns: 6
    Every jump from a platform to a higher-numbered platform is possible.
  3. {1,1,1,1,1}
    1
    Returns: 4
    It is only possible to jump to adjacent platforms.
  4. {3,141,55,92,617}
    30
    Returns: 0
  5. {135474,456465,245614,756766,234535,233455,73456,356766,566777,255357,443336,233467,588664,256656}
    31415
    Returns: 17
Back to all problems