-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmonotonicarray.java
More file actions
39 lines (36 loc) · 1.06 KB
/
Copy pathmonotonicarray.java
File metadata and controls
39 lines (36 loc) · 1.06 KB
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
package arraylist;
import java.util.ArrayList;
public class monotonicarray {/*
public static boolean monotonic(ArrayList<Integer>nums){
int n=nums.size();
if(nums.get(0)>nums.get(1)){
for (int i = 0; i < n-1; i++) {
if(nums.get(i)<nums.get(i+1)){
return false;
}
}
}else{for (int i = 0; i < n-1; i++) {
if(nums.get(i)>nums.get(i+1)){
return false;
}
}
}
return true;
}*/
public static boolean monotonic(ArrayList<Integer> nums) {
boolean inc = true, dec = true;
for (int i = 0; i < nums.size() - 1; i++) {
if (nums.get(i) < nums.get(i + 1)) dec = false;
if (nums.get(i) > nums.get(i + 1)) inc = false;
}
return inc || dec;
}
public static void main(String[] args) {
ArrayList<Integer>nums=new ArrayList<>();
nums.add(6);
nums.add(5);
nums.add(4);
nums.add(4);
System.out.println(monotonic(nums));
}
}