Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created February 6, 2018 07:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jianminchen/dce86e5eae22fe3690c2a470a48379c4 to your computer and use it in GitHub Desktop.
Save jianminchen/dce86e5eae22fe3690c2a470a48379c4 to your computer and use it in GitHub Desktop.
Float number and operators -
For example, given the array [1, 12, -3], the maximum number 33 can be found using 1 - 12 * (-3), since all operators have flat preference, so 1 - 12 * (-3) will be handled from left to right, first operation is 1 - 12 with value -11, and then second operation is -11 * (-3) = 33.
1 12
13
-11
12
1/12
13*-3
13/-3
13 - -3
13 + -3
[1, 12, -3]
4 number
for each number,you have 4 options with -3
when you get maximum,you have to get 16 numbers
double evaluateMaximize(float[] terms) { // [1, 12, -3], 33, first two numbers 1 - 12 = -11
int N = terms.length;
if (N > 16)
throw new IllegalArgumentException("Too complex for this solution");
double[] intemediate = new float[1 << (2 * N)];
int iInterm = 0;
double maximized = 0;
intermediate[iInterm++] = terms[0];
for (int iT = 1; iT < N; iT++) {
int iMaxInterm = iInterm - 1;
double m = 0; //
for (int iPrev = iMaxInterm; iPrev >= 0; iPrev--) { //<--
double prev = intermediate[iPrev];
intermediate[iPrev * 4 + 0] = prev * terms[iT];
intermediate[iPrev * 4 + 1] = prev / terms[iT];
intermediate[iPrev * 4 + 2] = prev - terms[iT];
intermediate[iPrev * 4 + 3] = prev + terms[iT];
if (iT != N - 1) // skip if not the last term
continue;
m = Math.max(m, intermediate[iPrev * 4 + 3]);
m = Math.max(m, intermediate[iPrev * 4 + 2]);
m = Math.max(m, intermediate[iPrev * 4 + 1]);
m = Math.max(m, intermediate[iPrev * 4 + 0]);
}
maximized = m;
iInterm *= 4;
}
return maximized;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment