Fleet hit ratio optimization
A content delivery team is shrinking a fleet of edge nodes and must keep exactly keep of them. Over last week hits[i] counts the requests node i served from its cache and requests[i] counts the requests it received. The fleet wide hit ratio of a group is the total of their hits divided by the total of their requests, so a busy node pulls that figure further than a quiet one. Return the highest fleet wide hit ratio that any group of exactly keep nodes reaches, as a fraction between 0 and 1. Every requests entry is at least 1 and every hits entry is between 0 and its own requests entry. Return -1 when keep is below 1 or above the number of nodes.
best_hit_ratio(hits: list[int], requests: list[int], keep: int) → float[[90,50,0],[100,100,1],2]out0.8910891089108911[[3,7,2],[4,9,3],3]out0.75[[4,4],[5,5],3]out-1State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.
[[90,50,0],[100,100,1],2]0.8910891089108911not run yetsample[[3,7,2],[4,9,3],3]0.75not run yetsample[[4,4],[5,5],3]-1not run yetsample