Saw this question on Careercup.com:
Given heights of n persons standing in a line and a list of numbers corresponding to each person (p) that gives the number of pers
Simple O(n^2) solution for this in Java:
Algorith:
We can optimise this solution even more by using segment tree. See this link.
class Person implements Comparable{
int height;
int pos;
Person(int height, int pos) {
this.height = height;
this.pos = pos;
}
@Override
public int compareTo(Person person) {
return this.height - person.height;
}
}
public class Solution {
public int[] order(int[] heights, int[] positions) {
int n = heights.length;
int[] ans = new int[n];
PriorityQueue pq = new PriorityQueue();
for( int i=0; i person.pos) break;
index++;
}
ans[index] = person.height;
}
return ans;
}
}