483. Find Elevation by Rank
Find Elevation by Rank

A survey records elevations at points on a square field. Rank all points by elevation from lowest to highest and find the elevation at the requested position. Count equal elevations separately. Negative elevations are below sea level.

Example of ranking survey elevations from lowest to highest

Method Signature

int findElevation(List<String> elevations, int position)
  • elevations: each string represents one row of comma-separated integers, such as "-4,2,9", with no spaces.
  • position: the 1-based rank of the required reading.
  • Return the elevation value at that rank.

Constraints

  • Let sideLength = elevations.size(), where 1 ≤ sideLength ≤ 300.
  • Each row contains exactly sideLength integers.
  • Every elevation is between -1,000,000,000 and 1,000,000,000, inclusive.
  • Elevations never decrease from left to right or from top to bottom.
  • 1 ≤ position ≤ sideLength * sideLength.
  • Use better than O(sideLength * sideLength) additional space.

Examples

Example 1

findElevation(elevations = List.of("-9,-1,6", "-4,3,10", "2,8,15"), position = 5)

Output: 3

The ordered readings are -9, -4, -1, 2, 3, 6, 8, 10, 15. The fifth reading is 3.

Example 2

findElevation(elevations = List.of("-7,-2", "-2,5"), position = 3)

Output: -2

The ordered readings are -7, -2, -2, 5. The repeated reading -2 occupies positions 2 and 3.

Example 3

findElevation(elevations = List.of("14"), position = 1)

Output: 14

There is only one survey point.



Please use Laptop/Desktop or any other large screen to add/edit code.