Longest Arithmetic Subsequence
Real-world version: Longest Constant-Step Firing LadderThe statement here is our own: the same inputs, outputs and technique.
A glass studio keeps readings, the peak kiln temperature of every firing it has run, listed oldest firing first.
A ladder is a selection of two or more firings, kept in the order they appear in readings, such that the change from each selected firing to the next selected one is the same signed amount every time. The firings in a ladder need not be neighbours in the log, and that shared amount may be negative or zero.
Return the number of firings in the longest ladder the log admits.
Examples
Example 1
- Input
- readings = [412, 60, 205, 118, 350, 176]
- Output
- 3
The firings reading 60, 118 and 176 rise by 58 from one to the next, so a ladder of three firings is reported.
Example 2
- Input
- readings = [77, 77, 77, 77]
- Output
- 4
Every firing reads 77, so the whole log is a ladder whose shared change is 0.
Example 3
- Input
- readings = [8, 500, 26, 44, 0, 62, 494]
- Output
- 4
The firings reading 8, 26, 44 and 62 each sit 18 above the one before, giving four firings in log order.
Constraints
- 2 <= readings.length <= 1000
- 0 <= readings[i] <= 500
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def longest_ladder(readings: list[int]) -> int:public int longestLadder(int[] readings)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 13 test cases: 13 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's
More on the same topics
- Number of Matching SubsequencesReal-world version: Labels That Read Through the TapeMedium
- Maximum Earnings From TaxiReal-world version: Booking the Tram Along the LineMedium
- Intersection of Two ArraysReal-world version: Shared Scan CodesEasy
- Missing NumberReal-world version: Unclaimed Badge NumberEasy