Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
indy256 committed Jan 11, 2015
1 parent 9dc854e commit bb74872
Showing 1 changed file with 28 additions and 4 deletions.
32 changes: 28 additions & 4 deletions java/src/Lis2.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ public static int[] lis(int[] a) {
int len = 0;
for (int i = 0; i < n; i++) {
int pos = lower_bound(a, tail, len, a[i]);
if (pos == len) {
++len;
}
len = Math.max(len, pos + 1);
prev[i] = pos > 0 ? tail[pos - 1] : -1;
tail[pos] = i;
}
Expand Down Expand Up @@ -59,11 +57,37 @@ public static void main(String[] args) {
a[i] = rnd.nextInt(10);
int[] lis = lis(a);
checkLis(a, lis);
if (lis.length != lisSize(a))
if (lis.length != lisSize(a) || lis.length != lisSize2(a))
throw new RuntimeException();
}
}

static int lisSize2(int[] a) {
int[] last = new int[a.length];
Arrays.fill(last, Integer.MAX_VALUE);
int len = 0;
for (int v : a) {
int pos = lower_bound(last, v);
last[pos] = v;
len = Math.max(len, pos + 1);
}
return len;
}

static int lower_bound(int[] a, int key) {
int lo = -1;
int hi = a.length;
while (hi - lo > 1) {
int mid = (lo + hi) >>> 1;
if (a[mid] < key) {
lo = mid;
} else {
hi = mid;
}
}
return hi;
}

static void checkLis(int[] a, int[] lis) {
int n = a.length;
boolean found = false;
Expand Down

0 comments on commit bb74872

Please sign in to comment.