LeetCode Entry
2540. Minimum Common Value
Min common of two sorted list
2540. Minimum Common Value easy substack youtube
https://dmitrysamoylenko.com/leetcode/

Join me on Telegram
https://t.me/leetcode_daily_unstoppable/1364
Problem TLDR
Min common of two sorted list
Intuition
- two pointers: one goes forward, second tries to match
- or hashset
- or binary search
Approach
- binary search is the shortest
- rust itertools has nice way to merge sorted lists
Complexity
-
Time complexity: \(O(nlogn|n)\)
-
Space complexity: \(O(1)\)
Code
fun getCommon(a:IntArray, b: IntArray) =
a.find { b.binarySearch(it) >= 0 } ?: -1
pub fn get_common(a: Vec<i32>, b: Vec<i32>) -> i32 {
a.into_iter().merge_join_by(b, i32::cmp).find_map(|e|
match e {Both(a,_)=>Some(a), _=>None}).unwrap_or(-1)
}