Skip to content

Instantly share code, notes, and snippets.

@phaniram
Created January 5, 2012 15:35
Show Gist options
  • Save phaniram/1565740 to your computer and use it in GitHub Desktop.
Save phaniram/1565740 to your computer and use it in GitHub Desktop.
Interview Street Challenges - Vertical Sticks
import java.util.*;
public class Vertical_sticks {
public boolean permute(int[] data) {
int k = data.length - 2;
while (data[k] >= data[k + 1]) {
k--;
if (k < 0) {
return false;
}
}
int l = data.length - 1;
while (data[k] >= data[l]) {
l--;
}
swap(data, k, l);
int length = data.length - (k + 1);
for (int i = 0; i < length / 2; i++) {
swap(data, k + 1 + i, data.length - i - 1);
}
return true;
}
private void swap(int[] data, int k, int l) {
data[k] = data[k] + data[l];
data[l] = data[k] - data[l];
data[k] = data[k] - data[l];
}
public double process(int[] set) {
Arrays.sort(set);
long v = 0, count = 0;
do {
v += perform(set);
count++;
} while (permute(set));
// System.out.println("v="+v+" and num.of permutations"+count);
double ret = (double) v / count;
return ret;
}
public int perform(int[] intArray) {
int len = intArray.length;
int total_len = 0;
for (int i = len - 1; i > 0; i--) {
int ray_len = 1;
// System.out.println("Comparing with "+intArray[i]);
for (int j = i - 1; j >= 0; j--) {
if (intArray[j] >= intArray[i]) {
// System.out.println(intArray[j]+">="+intArray[i]);
break;
}
ray_len++;
}
// System.out.println("Ray length is "+ray_len);
total_len += ray_len;
}
return total_len + 1;
}
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int num_of_testcases = sc.nextInt();
Vertical_sticks v_sticks = new Vertical_sticks();
for (int i = 0; i < num_of_testcases; i++) {
int num_of_points = sc.nextInt();
int sticks[] = new int[num_of_points];
for (int j = 0; j < num_of_points; j++) {
sticks[j] = sc.nextInt();
}
System.out.printf("%8.2f\n", v_sticks.process(sticks));
}
}
}
@zhenyiluo
Copy link

The time complexity is too high, can not pass all the test cases here: https://www.hackerrank.com/challenges/vertical-sticks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment