non-decreasing-array

This commit is contained in:
Gleb Koval 2022-06-26 16:42:44 +01:00
parent 8976c0171f
commit e061a8602f
Signed by: cyclane
GPG Key ID: 15E168A8B332382C
1 changed files with 19 additions and 0 deletions

View File

@ -0,0 +1,19 @@
impl Solution {
pub fn check_possibility(nums: Vec<i32>) -> bool {
nums.windows(2)
.scan(i32::MIN, |max, n| {
*max = n[0].max(*max);
Some(n[1] < *max)
})
.filter(|&result| result)
.count() <= 1 ||
nums.windows(2)
.rev()
.scan(i32::MAX, |min, n| {
*min = n[1].min(*min);
Some(n[0] > *min)
})
.filter(|&result| result)
.count() <= 1
}
}