Sorting Problem

Kth Largest In An Array

Problem

Given an array of integers, find the k-th largest number in it.

Example


{
"numbers": [5, 1, 10, 3, 2],
"k": 2
}

and 

{
"numbers": [4, 1, 2, 2, 3],
"k": 4
}


Output:

5 and 2 

Solution


function swap(numbers, i, j) {
    let tmp = numbers[i];
    numbers[i] = numbers[j];
    numbers[j] = tmp;
}

function quickSelect(numbers, left, right, k) {
    let le = left;
    let ri = right;
    let mid = numbers[right];
    while (le < ri) {
        if (numbers[le++] > mid) swap(numbers, --le, --ri);
    }
    swap(numbers, le, right);
    let len = right - le;
    if (len === k - 1) return numbers[le];
    else if (len < k - 1) return quickSelect(numbers, left, le - 1, k - len - 1);
    else return quickSelect(numbers, le + 1, right, k);
};

function findKthLargest(numbers, k) {
    return quickSelect(numbers, 0, numbers.length - 1, k);
};

//Input [5, 1, 10, 3, 2] Output: 5
//Input [4, 1, 2, 2, 3] Output: 2
const result = findKthLargest([4, 1, 2, 2, 3], 4)
console.log(result);

Sorting Problem

Attend Meetings

Problem

Given a list of meeting intervals where each interval consists of a start and an end time, check if a person can attend all the given meetings such that only one meeting can be attended at a time.

Example

{
"intervals": [[1, 5], [5, 8], [10, 15]]
}

and 

{
"intervals": [[1, 5], [4, 8]]
}


Output:

1 and 0 

Solution


const canAttendMeetings = (intervals) => {

    //Solution 1
    const times = new Set();
    for (let i = 0; i < intervals.length; i += 1) {
        for (let j = intervals[i][0]; j < intervals[i][1]; j += 1) {
            if (times.has(j)) {
                return 0;
            } else {
                times.add(j);
            }
        }
    }
    
    return 1;

    //Solution 2
    for (let i = 0; i < intervals.length; i++) {
        for (let j = i + 1; j < intervals.length; j++) {
            // If overlap found, return 0
            if (!(
                intervals[i][1] <= intervals[j][0] ||
                intervals[j][1] <= intervals[i][0]
            )) {
                return 0;
            }
        }
    }
    return 1;
};

//Input: intervals = [[1, 5], [4, 8]]; //Output: 0
//Input: [[1, 5], [5, 8], [10, 15]]; Output: 1
const result = canAttendMeetings([[1, 5], [5, 8], [10, 15]]);
console.log(result);
Sorting Problem

Merge K Sorted Linked Lists

Problem

Given k linked lists where each one is sorted in the ascending order, merge all of them into a single sorted linked list.

Example


{
"lists": [
[1, 3, 5],
[3, 4],
[7]
]
}

Output:

[1, 3, 3, 4, 5, 7]

Solution

function merge(l1, l2) {
    const LinkedListNode = class {
        constructor(value) {
            this.value = value;
            this.next = null;
        }
    };
    let dummy = new LinkedListNode(0);
    let tail = dummy;
    while (l1 != null && l2 != null) {
        if (l1.value < l2.value) {
            tail.next = l1;
            l1 = l1.next;
        }
        else {
            tail.next = l2;
            l2 = l2.next;
        }
        tail = tail.next;
    }
    if (l1 != null) {
        tail.next = l1;
    }
    if (l2 != null) {
        tail.next = l2;
    }
    return dummy.next;
}

function merge_k_lists(lists) {
    if (lists.length === 0) {
        return null;
    }

    while (lists.length > 1) {
        let mergedLists = [];
        let a = lists.shift();
        let b = lists.shift();
        mergedLists = merge(a, b);
        lists.push(mergedLists);
    }

    return lists[0];
}


const lists = [
    [1, 3, 5],
    [3, 4],
    [7]
];

const result = merge_k_lists(lists);
console.log(result);
Sorting Problem

2 Sum In An Array (Indices)

Problem

Given an array and a target number, find the indices of the two values from the array that sum up to the given target number.

Example

{
"numbers": [5, 3, 10, 45, 1],
"target": 6
}

{
"numbers": [4, 1, 5, 0, -1],
"target": 10
}


Output:

[1, 3] or [-1, -1]

Solution

function two_sum(numbers, target) {
  for (let i = 0; i < numbers.length; i++) {
      for (let j = i + 1; j < numbers.length; j++) {
        if (numbers[i] + numbers[j] === target) {
          return [i, j];
        }
      }
    }
   return [-1, -1]
}