Great breakdown of the heap approach. The follow-up about switching to max-heap when k is close to n is clever, hadn't considere that optimization before. Ran into this exact problem in a mock interview last month and brute-forced it with sorting - wish I'd seen this explanation first.
Great breakdown of the heap approach. The follow-up about switching to max-heap when k is close to n is clever, hadn't considere that optimization before. Ran into this exact problem in a mock interview last month and brute-forced it with sorting - wish I'd seen this explanation first.
Glad it helped!