import java.util.HashMap;
import java.util.Map;
/**
* User: uerturk
*/
/**
* Given an int array which might contain duplicates, find the largest
* subset of it which form a sequence.
* Eg. {1,6,10,4,7,9,5}
* then answer is 4,5,6,7
* Sorting is an obvious solution. Can this be done in O(n) time
*/
class FindTheLongestSequence {
private int follow
(int startNumber, Map
<Integer, Pair
<Boolean, Integer
>> pairMap
) { Pair
<Boolean, Integer
> currentPair
= pairMap.
get(startNumber
); if (currentPair == null) {
return 0;
} else {
if (currentPair.getFirst()) {
return currentPair.getSecond();
} else {
int result = follow(startNumber + 1, pairMap) + 1;
currentPair.setFirst(true);
currentPair.setSecond(result);
return result;
}
}
}
public void longestSequence() {
int[] ints = {1, 6, 10, 4, 7, 9, 5};
// in the map first is the number, in the pair first isVisited flag, second is the score
pairMap.
put(i,
new Pair
<Boolean, Integer
>(false,
1)); }
int[] results = new int[ints.length];
for (int i = 0; i < ints.length; i++) {
int result = follow(ints[i], pairMap);
results[i] = result;
System.
out.
println(ints
[i
] + ":" + result
); }
}
class Pair<First, Second> {
First first;
Second second;
public Pair(First first, Second second) {
this.first = first;
this.second = second;
}
public void setFirst(First first) {
this.first = first;
}
public void setSecond(Second second) {
this.second = second;
}
public First getFirst() {
return first;
}
public Second getSecond() {
return second;
}
public void set(First first, Second second) {
setFirst(first);
setSecond(second);
}
}
public static void main
(String[] args
) { new FindTheLongestSequence().longestSequence();
}
}
CmltcG9ydCBqYXZhLnV0aWwuSGFzaE1hcDsKaW1wb3J0IGphdmEudXRpbC5NYXA7CgovKioKICogVXNlcjogdWVydHVyawogKi8KCi8qKgogKiBHaXZlbiBhbiBpbnQgYXJyYXkgd2hpY2ggbWlnaHQgY29udGFpbiBkdXBsaWNhdGVzLCBmaW5kIHRoZSBsYXJnZXN0CiAqIHN1YnNldCBvZiBpdCB3aGljaCBmb3JtIGEgc2VxdWVuY2UuCiAqIEVnLiB7MSw2LDEwLDQsNyw5LDV9CiAqIHRoZW4gYW5zd2VyIGlzIDQsNSw2LDcKICogU29ydGluZyBpcyBhbiBvYnZpb3VzIHNvbHV0aW9uLiBDYW4gdGhpcyBiZSBkb25lIGluIE8obikgdGltZQogKi8KY2xhc3MgRmluZFRoZUxvbmdlc3RTZXF1ZW5jZSB7CiAgICBwcml2YXRlIGludCBmb2xsb3coaW50IHN0YXJ0TnVtYmVyLCBNYXA8SW50ZWdlciwgUGFpcjxCb29sZWFuLCBJbnRlZ2VyPj4gcGFpck1hcCkgewogICAgICAgIFBhaXI8Qm9vbGVhbiwgSW50ZWdlcj4gY3VycmVudFBhaXIgPSBwYWlyTWFwLmdldChzdGFydE51bWJlcik7CiAgICAgICAgaWYgKGN1cnJlbnRQYWlyID09IG51bGwpIHsKICAgICAgICAgICAgcmV0dXJuIDA7CiAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgaWYgKGN1cnJlbnRQYWlyLmdldEZpcnN0KCkpIHsKICAgICAgICAgICAgICAgIHJldHVybiBjdXJyZW50UGFpci5nZXRTZWNvbmQoKTsKICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgIGludCByZXN1bHQgPSBmb2xsb3coc3RhcnROdW1iZXIgKyAxLCBwYWlyTWFwKSArIDE7CiAgICAgICAgICAgICAgICBjdXJyZW50UGFpci5zZXRGaXJzdCh0cnVlKTsKICAgICAgICAgICAgICAgIGN1cnJlbnRQYWlyLnNldFNlY29uZChyZXN1bHQpOwogICAgICAgICAgICAgICAgcmV0dXJuIHJlc3VsdDsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgIH0KCiAgICBwdWJsaWMgdm9pZCBsb25nZXN0U2VxdWVuY2UoKSB7CiAgICAgICAgaW50W10gaW50cyA9IHsxLCA2LCAxMCwgNCwgNywgOSwgNX07CiAgICAgICAgLy8gaW4gdGhlIG1hcCBmaXJzdCBpcyB0aGUgbnVtYmVyLCBpbiB0aGUgcGFpciBmaXJzdCBpc1Zpc2l0ZWQgZmxhZywgc2Vjb25kIGlzIHRoZSBzY29yZQogICAgICAgIE1hcDxJbnRlZ2VyLCBQYWlyPEJvb2xlYW4sIEludGVnZXI+PiBwYWlyTWFwID0gbmV3IEhhc2hNYXA8SW50ZWdlciwgUGFpcjxCb29sZWFuLCBJbnRlZ2VyPj4oKTsKICAgICAgICBmb3IgKEludGVnZXIgaSA6IGludHMpIHsKICAgICAgICAgICAgcGFpck1hcC5wdXQoaSwgbmV3IFBhaXI8Qm9vbGVhbiwgSW50ZWdlcj4oZmFsc2UsIDEpKTsKICAgICAgICB9CgogICAgICAgIGludFtdIHJlc3VsdHMgPSBuZXcgaW50W2ludHMubGVuZ3RoXTsKICAgICAgICBmb3IgKGludCBpID0gMDsgaSA8IGludHMubGVuZ3RoOyBpKyspIHsKICAgICAgICAgICAgaW50IHJlc3VsdCA9IGZvbGxvdyhpbnRzW2ldLCBwYWlyTWFwKTsKICAgICAgICAgICAgcmVzdWx0c1tpXSA9IHJlc3VsdDsKICAgICAgICAgICAgU3lzdGVtLm91dC5wcmludGxuKGludHNbaV0gKyAiOiIgKyByZXN1bHQpOwogICAgICAgIH0KCiAgICB9CgogICAgY2xhc3MgUGFpcjxGaXJzdCwgU2Vjb25kPiB7CiAgICAgICAgRmlyc3QgZmlyc3Q7CiAgICAgICAgU2Vjb25kIHNlY29uZDsKCiAgICAgICAgcHVibGljIFBhaXIoRmlyc3QgZmlyc3QsIFNlY29uZCBzZWNvbmQpIHsKICAgICAgICAgICAgdGhpcy5maXJzdCA9IGZpcnN0OwogICAgICAgICAgICB0aGlzLnNlY29uZCA9IHNlY29uZDsKICAgICAgICB9CgogICAgICAgIHB1YmxpYyB2b2lkIHNldEZpcnN0KEZpcnN0IGZpcnN0KSB7CiAgICAgICAgICAgIHRoaXMuZmlyc3QgPSBmaXJzdDsKICAgICAgICB9CgogICAgICAgIHB1YmxpYyB2b2lkIHNldFNlY29uZChTZWNvbmQgc2Vjb25kKSB7CiAgICAgICAgICAgIHRoaXMuc2Vjb25kID0gc2Vjb25kOwogICAgICAgIH0KCiAgICAgICAgcHVibGljIEZpcnN0IGdldEZpcnN0KCkgewogICAgICAgICAgICByZXR1cm4gZmlyc3Q7CiAgICAgICAgfQoKICAgICAgICBwdWJsaWMgU2Vjb25kIGdldFNlY29uZCgpIHsKICAgICAgICAgICAgcmV0dXJuIHNlY29uZDsKICAgICAgICB9CgogICAgICAgIHB1YmxpYyB2b2lkIHNldChGaXJzdCBmaXJzdCwgU2Vjb25kIHNlY29uZCkgewogICAgICAgICAgICBzZXRGaXJzdChmaXJzdCk7CiAgICAgICAgICAgIHNldFNlY29uZChzZWNvbmQpOwogICAgICAgIH0KICAgIH0KCiAgICBwdWJsaWMgc3RhdGljIHZvaWQgbWFpbihTdHJpbmdbXSBhcmdzKSB7CiAgICAgICAgbmV3IEZpbmRUaGVMb25nZXN0U2VxdWVuY2UoKS5sb25nZXN0U2VxdWVuY2UoKTsKICAgIH0KfQo=