LeetCode Entry
3637. Trionic Array I
Validate inc,dec,inc sequency
3637. Trionic Array I easy blog post substack youtube

Join me on Telegram
https://t.me/leetcode_daily_unstoppable/1257
Problem TLDR
Validate inc,dec,inc sequency #easy
Intuition
Brute-Force O(n^3) is accepted.
Approach
- we can count peaks
- we can convert to string and use regex
- chunk_by, eq in Rust
Complexity
-
Time complexity: \(O(n)\)
-
Space complexity: \(O(1)\)
Code
// 32ms
fun isTrionic(n: IntArray) =
n.map{it}.zipWithNext(Int::compareTo)
.joinToString("").matches(Regex("(-1)+1+(-1)+"))
/*
n.map{it}.zipWithNext(Int::compareTo).let { s ->
s[0]<0 && 0 !in s && s.windowed(2).count{(a,b)->a!=b}==2 }
n[0]<n[1] && 2 == (2..<n.size).count {
(n[it-2]<n[it-1])!=(n[it-1]<n[it]) } &&
(1..<n.size).all{n[it]!=n[it-1]}
*/
// 0ms
pub fn is_trionic(n: Vec<i32>) -> bool {
n.windows(2).map(|w|w[1].cmp(&w[0])as i8).collect::<Vec<_>>()
.chunk_by(i8::eq).map(|c|c[0]).eq([1,-1,1])
}