-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMonotonicArr.java
44 lines (37 loc) · 1020 Bytes
/
MonotonicArr.java
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
public class MonotonicArr {
// my solution(o(n)) two pass로 구하기
public boolean isMonotonic(int[] A) {
boolean flag = true;
boolean direct = false;
if (A[0] - A[A.length - 1] < 0)
direct = true;
for (int i = 0; i < A.length; i++) {
if (i == A.length - 1)
continue;
if (direct) {
if (A[i] > A[i + 1])
flag = false;
} else {
if (A[i] < A[i + 1])
flag = false;
}
}
return flag;
}
}
/* another solution(o(n)) one pass로 구하기
class Solution {
public boolean isMonotonic(int[] A) {
int store = 0;
for (int i = 0; i < A.length - 1; ++i) {
int c = Integer.compare(A[i], A[i+1]);
if (c != 0) {
if (c != store && store != 0)
return false;
store = c;
}
}
return true;
}
}
*/