All problems
0018MediumArrayDivide and ConquerSortingHeap (Priority Queue)Quickselect

Kth Highest Daily Swing

Tracked in this browser only
Write code

Trains the technique from

LeetCode 215Kth Largest Element in an Array

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

A rooftop weather station writes down how far the temperature moved from one day to the next. swings[i] is the figure recorded on day i in whole degrees, and it runs negative on a day that cooled off.

Line the recorded figures up from highest to lowest and return the figure standing at position k of that line, where position 1 is the highest.

Two days that recorded the same figure each take a place of their own in the line, so a repeated figure occupies several consecutive positions. If four days all recorded 5, then positions 1 through 4 of the line every one of them hold 5. Read the request as a position in that full line, not as a rank among the different figures the station saw: it is the k-th figure counting downward with repeats intact, and asking for the k-th value that differs from the ones above it gives a different, wrong answer.

Arranging the entire log just to read one position off it is more work than the question needs.

Examples

Example 1

Input
swings = [6, -2, 6, 1, -5, 9], k = 3
Output
6

Highest to lowest the line reads 9, 6, 6, 1, -2, -5. Position 3 holds the second of the two sixes.

Example 2

Input
swings = [5, 5, 5, 2], k = 2
Output
5

Three fives fill positions 1, 2 and 3, so position 2 holds 5. Only two figures ever appeared, and treating the request as a rank among them would wrongly report 2.

Example 3

Input
swings = [-7], k = 1
Output
-7

A one-day log leaves a line of length one, and its only entry answers every legal request.

Constraints

  • 1 <= k <= swings.length <= 10^5
  • -10^4 <= swings[i] <= 10^4

The signature

The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def kth_highest_swing(swings: list[int], k: int) -> int:
Java
public int kthHighestSwing(int[] swings, int k)
September 7
Apply