All thanks to this brute O(n^3) force (but I am not ashamed, Petr's look more or less similar :P):
import java.util.*;
import java.math.*;
import static java.lang.Math.*;
import static java.util.Arrays.*;
import static java.util.Collections.*;
public class WhiteHats
{
public int whiteNumber(int[] c)
{
sort(c);
int n = c.length;
for(int w=0;w<=n;w++){
int cur[] = new int[n];
int cnt[] = new int[n];
for(int i=0;i<w;i++) cur[i] = 1;
for(int i=0;i<n;i++)
for(int j=0;j<n;j++) if(j!=i)
cnt[i] += cur[j];
sort(cnt);
boolean ok = true;
for(int i=0;ok && i<n;i++) ok = (cnt[i] == c[i]);
if(ok) return w;
}
return -1;
}
}
I hope I could stay yellow or even better increasing :)
Geen opmerkingen:
Een reactie posten