Skip to main content

Posts

Showing posts with the label algorithm

Encoding multi-dimensional value into 1-D with preserving closeness

After a very long time, this will be my first post. I hope that it will not be the last, and the rest will come. For a project about taxi trajectory prediction, I have applied k-NN algorithm on coordinate sequence of taxi trips. Did I have promising results? Well, it is arguable, but I have discovered some good techniques. One of them is Z-order curve (Figure 1). Figure 1: Four levels of Z-ordering (taken from Wikipedia  page) I was looking for a technique to decrease dimensionality of 2-D coordinate representation (lat-lon) into 1-D . But on this 1-D value, I would like to apply k-NN algorithm. For this purpose, 1-D values must preserve the closeness of two different coordinates. If I do linear ordering of latitude and longitude values, I couldn't achieve this goal, because closeness will depend on only one value which is placed on the right-most. Let's assume I do linear ordering as lat-lon:

Find max positive product of three integer in an array

In careercup.com, I have seen a problem. It basically asks to get an integer array(includes positives and negatives) and return maximum positive product of three distinct element in it. Sorting is not allowed. For example: arr = [9, 10, -12, 4, 7, -8, -13, 4, -8, 12] elements = (-13, -12, 12) There are two cases: either P1*P2*P3 or N1*N2*P1(Pi stands for ith biggest and Ni stands for ith smalltest number.). So, the algorithm finds 2 elements in the array by partitioning. One is the 2nd smallest  and the other is 3rd biggest number. Then, among biggest 2 element, it finds the maximum. The latest form of array is as follows: N1,N2,...........,P3,P2,P1 It compares N1*N2 ith. P3*P2. It returns elements of bigger one with P1.