Minimum external sort files
An external sort turns a stream of records into sorted runs before it merges them. Records arrive one at a time in a fixed order, and every open run file has to stay non-decreasing on the sort key, so a record can be appended to an open file only when its key is at least the last key that file already holds. When no open file can take a record, the sort has to open another one. Every record is written exactly once, and no record is held back or reordered. Given the keys in arrival order, return the fewest files the sort can finish with. Keys are integers, they may repeat, and they may be negative. An empty stream needs no files at all. Appending to any file that happens to fit is not the same as appending to the right one.
fewest_run_files(keys: list[int]) → int[[5,1,6,3]]out2[[8,1,9,3,0,3]]out3[[4,3,2,1]]out4State 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.
[[5,1,6,3]]2not run yetsample[[8,1,9,3,0,3]]3not run yetsample[[4,3,2,1]]4not run yetsample