import java.util.HashMap;
class SmallestWindow {
public SmallestWindow(int[] inp, int[] query) {
int[] best
= { -1,
-1,
Integer.
MAX_VALUE }; for (int i = 0; i < query.length; i++)
M.put(query[i], new Pair(-1, i));
for (int i = 0; i < inp.length; i++) {
Pair currPair = M.get(inp[i]);
if (currPair == null)
continue;
if (currPair.P == 0) {
currPair.I = i;
continue;
}
currPair.I = M.get(query[currPair.P - 1]).I;
if (currPair.P == query.length - 1 && currPair.I != -1
&& i - currPair.I < best[2]) {
best[0] = currPair.I;
best[1] = i;
best[2] = i - currPair.I;
}
}
System.
out.
println("[" + best
[0] + ".." + best
[1] + "]"); }
public static void main
(String[] args
) { int[] inputArray = new int[] { 2, 0, 2, 1, 0, 1, 0, 7 };
int[][] queryArrays = new int[][] { { 2, 1, 7 }, { 2, 0 }, { 2, 1 } };
for (int[] q : queryArrays)
new SmallestWindow(inputArray, q);
}
public class Pair {
private int I, P;
public Pair(int I, int P) {
this.I = I;
this.P = P;
}
}
}
aW1wb3J0IGphdmEudXRpbC5IYXNoTWFwOwoKY2xhc3MgU21hbGxlc3RXaW5kb3cgewoJcHVibGljIFNtYWxsZXN0V2luZG93KGludFtdIGlucCwgaW50W10gcXVlcnkpIHsKCQlpbnRbXSBiZXN0ID0geyAtMSwgLTEsIEludGVnZXIuTUFYX1ZBTFVFIH07CgkJSGFzaE1hcDxJbnRlZ2VyLCBQYWlyPiBNID0gbmV3IEhhc2hNYXA8SW50ZWdlciwgUGFpcj4oKTsKCQlmb3IgKGludCBpID0gMDsgaSA8IHF1ZXJ5Lmxlbmd0aDsgaSsrKQoJCQlNLnB1dChxdWVyeVtpXSwgbmV3IFBhaXIoLTEsIGkpKTsKCQlmb3IgKGludCBpID0gMDsgaSA8IGlucC5sZW5ndGg7IGkrKykgewoJCQlQYWlyIGN1cnJQYWlyID0gTS5nZXQoaW5wW2ldKTsKCQkJaWYgKGN1cnJQYWlyID09IG51bGwpCgkJCQljb250aW51ZTsKCQkJaWYgKGN1cnJQYWlyL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