Given a Matrix, extract the rows with a minimum difference in extreme values.
Input : test_list = [[4, 10, 18], [5, 0], [1, 4, 6], [19, 2]]
Output : [[1, 4, 6], [5, 0]]
Explanation : 6 - 1 = 5, 5 - 0 = 5, is minimum difference between extreme values.
Input : test_list = [[4, 10, 18], [5, 0], [2, 4, 6], [19, 2]]
Output : [[2, 4, 6]]
Explanation : 6 - 2 = 4, is min diff.
In this, we compute minimum difference between extreme values, and then use list comprehension to get particular row with that value difference between extreme values.
The original list is : [[4, 10, 18], [5, 3, 10], [1, 4, 6], [19, 2]] Rows with Minimum difference in extreme values : [[1, 4, 6]]
Time Complexity: O(n) where n is the number of elements in the list “test_list”. list comprehension + min() performs n number of operations.
Auxiliary Space: O(n), extra space of size n is required
In this, we perform task of filtering comparing with minimum value using filter() + lambda.
The original list is : [[4, 10, 18], [5, 3, 10], [1, 4, 6], [19, 2]] Rows with Minimum difference in extreme values : [[1, 4, 6]]
OutputThe original list is : [[4, 10, 18], [5, 3, 10], [1, 4, 6], [19, 2]]
Rows with Minimum difference in extreme values : [[1, 4, 6]]
Time complexity: O(nm), where n is the number of rows and m is the length of the longest row.
Auxiliary space: O(k), where k is the number of rows with the minimum difference in extreme values.