Search in sources :

Example 6 with Pair

use of iurii.job.interview.utils.pair.Pair in project algorithms by Iurii-Dziuban.

the class CustomerServiceCapacityTest method testNegative.

@Test
public void testNegative() {
    List<Pair> pairs = new ArrayList<>();
    pairs.add(new Pair(1481122000, 1481122025));
    pairs.add(new Pair(1481122030, 1481122045));
    pairs.add(new Pair(1481122050, 1481122070));
    assertThat(serviceCapacity.findNeededNumberOfEmployers(2, pairs)).isEqualTo(0);
}
Also used : ArrayList(java.util.ArrayList) Pair(iurii.job.interview.utils.pair.Pair) Test(org.junit.Test)

Example 7 with Pair

use of iurii.job.interview.utils.pair.Pair in project algorithms by Iurii-Dziuban.

the class ProcessorsForTasks method findTotalNumberOfProcessorsNeededWithSorting.

/**
 * Solution based on sorting O(n log n)
 *  By sorting ins and outs in the order they happen (based on Math.abs logic)
 */
public int findTotalNumberOfProcessorsNeededWithSorting(List<Pair> pairs) {
    ArrayList<Integer> positiveInsAndNegativeOuts = new ArrayList<>();
    for (Pair pair : pairs) {
        positiveInsAndNegativeOuts.add(pair.getFirst());
        positiveInsAndNegativeOuts.add(-pair.getSecond());
    }
    int maxNumber = 0;
    int currentNumberOfProcessors = 0;
    positiveInsAndNegativeOuts.sort(new ChecksComparator());
    for (int value : positiveInsAndNegativeOuts) {
        currentNumberOfProcessors += value > 0 ? 1 : -1;
        if (currentNumberOfProcessors > maxNumber) {
            maxNumber = currentNumberOfProcessors;
        }
    }
    return maxNumber;
}
Also used : ArrayList(java.util.ArrayList) Pair(iurii.job.interview.utils.pair.Pair)

Example 8 with Pair

use of iurii.job.interview.utils.pair.Pair in project algorithms by Iurii-Dziuban.

the class ProcessorsForTasks method findTotalNumberOfProcessorsNeededWithTimeline.

/**
 * Solution based on timeline  O(n + timeline)
 *  Adding or extracting 1 at the specific time point on timeline
 */
public int findTotalNumberOfProcessorsNeededWithTimeline(List<Pair> pairs) {
    int min = PairUtils.findMin(pairs);
    int max = PairUtils.findMax(pairs);
    int[] timeline = new int[max - min + 1];
    for (Pair pair : pairs) {
        timeline[pair.getFirst() - min]++;
        timeline[pair.getSecond() - min]--;
    }
    int curNumberOfProcessors = 0;
    int needed = 0;
    for (int value : timeline) {
        curNumberOfProcessors += value;
        if (curNumberOfProcessors > needed) {
            needed = curNumberOfProcessors;
        }
    }
    return needed;
}
Also used : Pair(iurii.job.interview.utils.pair.Pair)

Example 9 with Pair

use of iurii.job.interview.utils.pair.Pair in project algorithms by Iurii-Dziuban.

the class InverseFindTest method test.

@Test
public void test() {
    InverseFind inverseFind = new InverseFind();
    int[] array = inverseFind.mergeSort(new int[] { 3, 1, 2, 4, 7, 6 });
    Utilities.println(array);
    Utilities.println(inverseFind.list);
    assertThat(array).containsExactly(1, 2, 3, 4, 6, 7);
    assertThat(inverseFind.list).containsExactly(new Pair(3, 1), new Pair(3, 2), new Pair(7, 6));
}
Also used : Pair(iurii.job.interview.utils.pair.Pair) Test(org.junit.Test)

Example 10 with Pair

use of iurii.job.interview.utils.pair.Pair in project algorithms by Iurii-Dziuban.

the class ProcessorsForTasksTest method testOne.

@Test
public void testOne() {
    List<Pair> pairs = new ArrayList<>();
    pairs.add(new Pair(1481122000, 1481122020));
    pairs.add(new Pair(1481122020, 1481122040));
    pairs.add(new Pair(1481122040, 1481122050));
    assertThat(processorsForTasks.findTotalNumberOfProcessorsNeededWithTimeline(pairs)).isEqualTo(1);
}
Also used : ArrayList(java.util.ArrayList) Pair(iurii.job.interview.utils.pair.Pair) Test(org.junit.Test)

Aggregations

Pair (iurii.job.interview.utils.pair.Pair)14 ArrayList (java.util.ArrayList)10 Test (org.junit.Test)10 ArrayDeque (java.util.ArrayDeque)1