Manafall Daily Quest

Merge Intervals

Function Name: mergeIntervals

Description

Write a function 'mergeIntervals' that takes an array of intervals where each interval is a pair of integers representing an inclusive range. The function should merge all overlapping intervals and return an array of the merged intervals. An interval [a, b] is said to overlap with another interval [c, d] if they have at least one number in common. The resulting intervals should be outputted in ascending order, sorted by their start values.

The function should handle an empty array, and if no intervals overlap, it should return the intervals as they were.

Requirements

  • The function must accept an array of arrays as the only parameter.
  • Each subarray will have exactly two integers representing the start and end of an interval.
  • The function should return an array of merged intervals sorted by their start values.
  • An interval [a, b] should be considered valid if a <= b.
  • Assume all interval start and end values are non-negative integers.

Examples

Example 1: mergeIntervals([[1,3], [2,6], [8,10], [15,18]]) should return [[1,6], [8,10], [15,18]]Example 2: mergeIntervals([[1,4], [4,5]]) should return [[1,5]]Example 3: mergeIntervals([]) should return []Example 4: mergeIntervals([[5,5], [1,2], [2,4]]) should return [[1,4], [5,5]]

Links

https://en.wikipedia.org/wiki/Interval_(mathematics)

String Permutation Rank

Mon Oct 28 2024

Write a function that calculates the rank of a string permutation in lexicographically sorted order without actually generating all permutations. For example, in the sorted list of permutations of 'abc', the rank of 'abc' is 1, 'acb' is 2, and so on. The function should handle duplicates and handle upper and lowercase characters as different. You must not use external libraries or built-in permutation functions. The rank should start at 1.

Prev Quest

Sort Colors

Wed Oct 30 2024

Given an array consisting of 'n' elements that represent colors as red, white, or blue, sort the array in-place so that elements of the same color are adjacent, with the colors in the order of red, white and blue. Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively. You must solve this problem with a one-pass algorithm using only constant space.

Next Quest