To ensure we’re both on the same page, I’ll need to clarify a few points:
Assuming the common interpretation:
Here’s a step-by-step strategy to solve the problem:
Overall, the time complexity is O(n + m), which is efficient given that we need to examine all elements at least once.
Here’s the Python code to implement the above strategy:
def min_common_value(list1, list2):
set1 = set(list1)
set2 = set(list2)
common_elements = set1.intersection(set2)
if not common_elements:
return -1
return min(common_elements)
# Example usage
list1 = [10, 20, 30, 40, 50]
list2 = [15, 25, 35, 40, 45]
print(min_common_value(list1, list2)) # Output: 40
set1 = set(list1)
and set2 = set(list2)
convert the input lists to sets.common_elements = set1.intersection(set2)
calculates the common elements.common_elements
is empty, return -1.min(common_elements)
.This solution is straightforward, efficient, and leverages Python’s built-in set operations to ensure optimal performance.
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?