Untitled
class UserSolution { int N, MAX = 200001; int[] num = new int[MAX], max = new int[MAX], min = new int[MAX]; void init(int N, int mValue[]) { this.N = 0; add(N, mValue); } void add(int M, int mValue[]) { for (int i = 0; i < M; i++) { num[N] = max[N] = min[N] = mValue[i]; for (int j = N - 1; j >= 0; j--) { if (max[j] >= max[j + 1]) break; max[j] = max[j + 1]; } for (int j = N - 1; j >= 0; j--) { if (min[j] <= min[j + 1]) break; min[j] = min[j + 1]; } N++; } } void erase(int mFrom, int mTo) { int len = mTo - mFrom + 1; int idx = mFrom - 1; N -= len; for (int i = idx; i < N; i++) { num[i] = num[i + len]; max[i] = max[i + len]; min[i] = min[i + len]; } if (idx == N) { max[N - 1] = min[N - 1] = num[N - 1]; idx--; } for (int i = idx - 1; i >= 0; i--) { if (max[i] == max[i + 1]) break; max[i] = Math.max(num[i], max[i + 1]); } for (int i = idx - 1; i >= 0; i--) { if (min[i] == min[i + 1]) break; min[i] = Math.min(num[i], min[i + 1]); } } int find(int K) { return max[N - K] - min[N - K]; } }
Leave a Comment