LintCode 1691 买卖股票的最佳时机V
思路:DFS
public static void DFS(int[] a, int pos, int stock, int profit) {
int[] op = { -1, 0, 1 };
if (pos == a.length) {
max = Math.max(profit, max);
return;
}
if (stock == 0) {
for (int i = 0; i < 2; i++) {
DFS(a, pos + 1, stock + op[i] * -1, profit + op[i] * a[pos]);
}
} else if (stock > 0) {
for (int i = 0; i < 3; i++) {
DFS(a, pos + 1, stock + op[i] * -1, profit + op[i] * a[pos]);
}
}
}
}
static int max = Integer.MIN_VALUE;
public static int getAns(int[] a) {
// write your code here
DFS(a, 0, 0, 0);
return max;
}