Showing posts with label coding challenge. Show all posts
Showing posts with label coding challenge. Show all posts

Saturday, 28 June 2008

Coding Challenge

Cedric posted this coding challenge, here is a solution, but not a clever one:

package beust.codingchallenge;

import java.util.HashSet;
import java.util.Set;

public class CC {
static final int MAX = 10 * 1000;
static Set set = new HashSet();
static int nbn = 0;
static int jump = 1, biggestJump = 0;

/**
* @param args
*/
public static void main(String[] args) {
long t0 = System.currentTimeMillis();
test();
long tf = System.currentTimeMillis() - t0;
System.out.println(tf + " ms");
}

public static void test() {
for(int i=1; i < MAX; i++) {
final String str = "" + i;
final int strLength = str.length();
set.clear();
for(int j=0; j < strLength; j++) {
set.add(str.charAt(j));
}
if (strLength == set.size()) {
System.out.println("> " + str);
nbn++;
if (jump > biggestJump) {
biggestJump = jump;
}
jump = 1;
} else {
jump++;
System.out.println("< " + str + ", jump = "+ jump);
}
} // for
System.out.println("nbn="+nbn);
System.out.println("jump="+jump);
}
}



It runs in 20 ms, the biggest jump is 124 and the count is 5274

Blog Archive