You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int T = scanner.nextInt();
while (T -- > 0){
int n = scanner.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = scanner.nextInt();
}
System.out.println(checkV(arr) ? "YES" : "NO");
}
}
public static boolean checkV(int[] arr) {
int state = 0;
// 0 - init, 1 - up, 2 - down, 3 - up and down
for (int i = 1; i < arr.length; ++i) {
// trend not change
if (arr[i] == arr[i - 1]) {
continue;
}
if (arr[i] > arr[i - 1]) {
state = 1;
} else {
if (state == 0) {
state = 2;
} else if (state == 1) {
state = 3;
break;
}
}
}
return state != 3;
}
}
这道题使用的方法是状态机。
还不是很懂状态机类型的题目,还需要多做题。
The text was updated successfully, but these errors were encountered: