Question: ans = Modify the following Python function alg into another Python function alg2 to improve its running time by using a heap. Assume that the

 ans = Modify the following Python function alg into another Python

ans = Modify the following Python function alg into another Python function alg2 to improve its running time by using a heap. Assume that the given function always takes a valid input (a list 'nums' and an integer 'target') and returns a list of the 'target' smallest numbers in 'nums'. The example input of nums = [3, 1, 4, 5] and target = 2 gives the output of (1,3]. def alg1(nums, target): [] maximum_num = max(nums) while target > 0: minimum_num = maximum_num for i in range(len(nums)): if i not in ans and nums[i]

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!