Silence trim: hints, no solution
Easy · Two pointers
An audio editor stores a recording as a list of integer samples, where each sample is the signed amplitude at one instant. A sample is quiet when its absolute value is strictly less than a threshold.
Trim the recording by removing quiet samples from the start until a sample that is not quiet appears, and doing the same from the end. Quiet samples in the middle stay. Return how many samples are left after trimming. If every sample is quiet, or the recording is empty, return 0.
Examples
- Input
- samples = [0, 1, -2, 9, -8, 3, 0, 1], threshold = 3
- Output
- 3
- Note
- Three quiet samples go from the start and two from the end, leaving 9, -8 and 3.
- Input
- samples = [1, -1, 2], threshold = 5
- Output
- 0
- Note
- Every sample is below the threshold of 5, so nothing is left.
- Input
- samples = [5, 0, 0, 5], threshold = 1
- Output
- 4
- Note
- Quiet samples in the middle stay, so all four samples remain.
Constraints
- 0 <= samples.length <= 3000
- -1000 <= samples[i] <= 1000
- 0 <= threshold <= 1000
Hint 1: a nudge
Which samples decide how much gets removed, and which ones can you ignore completely?
Unlock the full hints and timed practice with a pass
The second and third hints and the timed, runner-checked practice are part of a pass.