Non-Maximum Suppression
Problem statement
Each detection box is [x1, y1, x2, y2, confidence], where the lower-left corner is inclusive and the upper-right coordinate defines the geometric boundary. Process boxes by descending confidence, breaking ties by smaller original index.
Keep a box unless its intersection-over-union with any already kept box is strictly greater than thresholdPercent / 100. Return kept original indices in selection order. Use exact integer cross-multiplication when comparing the ratio.
Function
nonMaximumSuppression(boxes: int[][], thresholdPercent: int) → int[]Examples
Example 1
boxes = [[0,0,10,10,90],[1,1,9,9,80],[20,20,30,30,70]]thresholdPercent = 50return = [0,2]Box 1 overlaps the higher-confidence box 0 above the threshold, while box 2 is separate.
Example 2
boxes = [[0,0,2,2,5],[1,0,3,2,5]]thresholdPercent = 40return = [0,1]Equal confidence selects index 0 first; the boxes overlap by one third, which is not greater than 40 percent.
Constraints
0 <= boxes.length <= 5000x1 < x2andy1 < y20 <= confidence <= 10000000 <= thresholdPercent <= 100