Python - Minimum in each record value list


The problem statement says that we have to find the minimum value present in each record value list. So in this article we will see different ways to find the minimum in each record.

Understanding the problem

In the given problem we are required to find the minimum value of each subarray. For example: let’s say we have an array as [[8,10,12], [12, 14, 16], [17, 18, 19]], so in this example we can see the minimum values for every subarray are 8, 12, 17 as these arrays are in a sorted form. So the required result should be like [8, 12, 17]. But we will have to find the minimum values from each unsorted subarray. For solving this problem we will use different methods and techniques of Python.

Logic for The Above Problem

To solve and understand the given problem first we will find the minimum value from the given array. And for finding the minimum value from the given array we can use the min function which is a built-in method present in Python to get the minimum value from the list or array.

In the second approach we will use a for loop to find and append the minimum values of the given subarrays.

And in the third approach we will be using the map method with the min function to get the required result.

Algorithm - Using min function in the array

  • Step 1 − At the first step we are required to create a function called min_in_record. This function is also accepting an argument called record which is keeping record of the items.

  • Step 2 − As we have defined the function now we will use the min function to find the minimum value present in the array and return it to show in the Output.

  • Step 3 − Now we will show the required result on the console using print method.

Example

def min_in_record(record):
   # Return the minimum value
   return min(record)
record = [[81,102,123], [124, 145, 166], [177, 188, 199]]

print("The minimum value found in the record - ", min_in_record(record))

Output

The minimum value found in the record -  [81, 124, 177]

Algorithm - Using for loop

  • Step 1 − As we have to find the minimum values in each subarray using a for loop in this algorithm. So first we will initialize the array having subarrays and name it as record.

  • Step 2 − After defining the array we are required to initialize an empty block of array called min_items. This array will store the minimum values of each subarray present in the above array.

  • Step 3 − Now we will use a for loop to traverse the given input array. Inside this loop we will first use a min function to get the minimum value of each subarray and store that value in a separate variable called min_item.

  • Step 4 − After finding the min_item in each subarray we will append those values in the above created array to store the minimum values.

  • Step 5 − In the last step we will print the value of min_items.

Example - Using a for loop

record = [[81,102,123], [124, 145, 166], [177, 188, 199]]

#initialize an empty array to store the minimum values
min_items = []

#Iterate the array items
for rec in record:
   min_item = min(rec)
   min_items.append(min_item)
#Print the required result
print(min_items)

Output

[81, 124, 177]

Algorithm - Using map function

  • Step 1 − In this algorithm we will be using the map function so first we will define an array containing subarrays and name it as record.

  • Step 2 − After defining the array we will use map function to map all the items present in the array and with the help of min function we will get the smallest value of each subarray and after getting the smallest elements we will convert it in the list using list keyword in Python. Store the result in the min_items variable.

  • Step 3 − In the last step we will print the result using the min_items.

Example - Using map function

#Initialize the array having subarrays
record = [[81,102,123], [124, 145, 166], [177, 188, 199]]
#Find the list of min items from the array
min_items = list(map(min, record))

print(min_items)

Output

[81, 124, 177]

Complexity

The time complexity for finding the minimum within each record value list is O(n * m), here n is the length of the given array and m is the length of the subarrays. As all the approaches are iterating the items of the given array n * m times so the resultant complexity is O(n * m). And the space complexity for the above algorithms is O(n), because the main array’s size is n and we are storing minimum values for each subarray.

Conclusion

So the conclusion is that we have successfully created the approach to get the minimum values in each record using Python in different manners. We have used various built-in methods of Python like min, map, append and also used for loop.

Updated on: 16-Oct-2023

37 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements