1235. Ratio Ladder Length
A sound engineer collects tones, each an integer frequency in the array nums. A ladder is a sequence x, x * ratio, x * ratio^2, ... in which every term is one of the given frequencies. A ladder always has at least one term, because any single frequency is a ladder by itself, and duplicates in nums do not make a ladder longer.
Return the number of terms in the longest ladder that can be built from the frequencies in nums with the given ratio. Multiply in 64-bit arithmetic, because a term can exceed the 32-bit range before you notice it is missing.
Store the values in a hash set and climb only from values whose smaller step is absent. The intended solution runs in O(n) time and O(n) space.
Example 1
- Input:
- nums = [1,2,4,8,3], ratio = 2
- Output:
- 4
- Explanation:
The ladder 1, 2, 4, 8 has four terms and the lone 3 cannot extend it.
Example 2
- Input:
- nums = [3,6,1,12,24,48,5], ratio = 2
- Output:
- 5
- Explanation:
Starting from 3 the terms 3, 6, 12, 24, 48 are all present, giving five.
Constraints
1 ≤ nums.length ≤ 1000
1 ≤ nums[i] ≤ 109
2 ≤ ratio ≤ 105
How this problem is judged
- Answers
- Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
Expected complexity
- Time
- O(n)
- Space
- O(n)