From e061a8602f7be97d4a25d63afc973f4fe25f5327 Mon Sep 17 00:00:00 2001 From: Gleb Koval Date: Sun, 26 Jun 2022 16:42:44 +0100 Subject: [PATCH] non-decreasing-array --- non-decreasing-array/sol.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 non-decreasing-array/sol.rs diff --git a/non-decreasing-array/sol.rs b/non-decreasing-array/sol.rs new file mode 100644 index 0000000..ffa8591 --- /dev/null +++ b/non-decreasing-array/sol.rs @@ -0,0 +1,19 @@ +impl Solution { + pub fn check_possibility(nums: Vec) -> 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 + } +} \ No newline at end of file