- Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmonotonic-array.rs
60 lines (46 loc) · 1.12 KB
/
monotonic-array.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#![allow(dead_code, unused, unused_variables)]
fnmain(){}
structSolution;
implSolution{
pubfnis_monotonic1(a:Vec<i32>) -> bool{
if a.len() <= 1{
returntrue;
}
letmut f = None;
for i in1..a.len(){
if a[i] == a[i - 1]{
continue;
}
if f.is_none(){
f = Some(a[i] > a[i - 1]);
}
ifletSome(s) = f {
if s && a[i] < a[i - 1]{
returnfalse;
}
if !s && a[i] > a[i - 1]{
returnfalse;
}
}
}
true
}
pubfnis_monotonic(a:Vec<i32>) -> bool{
if a.len() <= 1{
returntrue;
}
letmut s = 0;
for i in1..a.len(){
if a[i] == a[i - 1]{
continue;
}
if s == 0 && a[i] - a[1] != 0{
s = a[i] - a[i - 1];
}
if s * a[i] - a[i - 1] < 0{
returnfalse;
}
}
true
}
}