LeetCode Entry
3903. Smallest Stable Index I
Index of max suffix - min prefix not less than k
3903. Smallest Stable Index I easy substack youtube
https://dmitrysamoylenko.com/leetcode/

Join me on Telegram
https://t.me/leetcode_daily_unstoppable/1472
Problem TLDR
Index of max suffix - min prefix not less than k
Intuition
Small problem, brute force is accepted.
Approach
- don’t forget to inlcude the current position
Complexity
-
Time complexity: \(O(n^2)\)
-
Space complexity: \(O(n)\)
Code
fun firstStableIndex(n: IntArray, k: Int) =
n.indices.find{n.take(it+1).max()-n.drop(it).min()<=k}?:-1
pub fn first_stable_index(n: Vec<i32>, k: i32) -> i32 {
(0..n.len()).find(|&i| *n[..=i].iter().max().unwrap() - *n[i..].iter().min().unwrap() <= k).map_or(-1, |i| i as _)
}
Comments