Code RoomGreedy switch flip descent
MediumPrep Room Coding #4893

Greedy switch flip descent

CodingAlgorithms & data structuresMid–Senior~24 min

An autotuner measured the p99 latency of a service under every combination of k on and off switches. latency has 2^k entries, and latency[m] is the reading for the combination whose bit s is set exactly when switch s is on. From wherever it stands the tuner considers every single switch flip, picks the flip landing on the lowest reading, and takes it only when that reading is strictly lower than the one it stands on. A tie between two flips goes to the lower switch number. It stops when no single flip improves on the current reading. Return one entry per starting combination, in mask order, holding the combination the tuner comes to rest on when it starts there. An empty table returns an empty list.

Implement
settled_switch_masks(latency: list[int]) → list[int]
Examples
in[[5,3,4,1]]out[3,3,3,3]
in[[9,2,2,5]]out[1,1,2,2]
in[[7]]out[0]
What a strong answer looks like

State 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.

0:00 of about 24 min
InputExpectedGot
[[5,3,4,1]][3,3,3,3]not run yetsample
[[9,2,2,5]][1,1,2,2]not run yetsample
[[7]][0]not run yetsample