题
思
深搜暴力解的
代码
class Solution {
Stack<int[]> stack = new Stack<int[]>();
int max = 0;
public int maximumRequests(int n, int[][] requests) {
dfs(requests, 0, n);
return max;
}
private void dfs(int[][] requests, int idx, int n) {
if (stack.size() > max && valid(n, stack)) {
max = stack.size();
}
for (int i = idx; i < requests.length; i++) {
stack.push(requests[i]);
dfs(requests, i + 1, n);
stack.pop();
}
}
private boolean valid(int n, Stack<int[]> stack) {
for (int i = 0; i < n; i++) {
int from = 0, to = 0;
for (int[] request : stack) {
if (request[0] == i) {
from++;
}
if (request[1] == i) {
to++;
}
}
if (from != to) {
return false;
}
}
return true;
}
}