Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

632. Smallest Range Covering Elements from K Lists #279

Open
Tcdian opened this issue Aug 1, 2020 · 1 comment
Open

632. Smallest Range Covering Elements from K Lists #279

Tcdian opened this issue Aug 1, 2020 · 1 comment

Comments

@Tcdian
Copy link
Owner

Tcdian commented Aug 1, 2020

632. Smallest Range Covering Elements from K Lists

你有 k 个升序排列的整数数组。找到一个最小区间,使得 k 个列表中的每个列表至少有一个数包含在其中。

我们定义如果 b-a < d-c 或者在 b-a == d-c 时 a < c,则区间 [a,b][c,d] 小。

Example

Input: [[4,10,15,24,26], [0,9,12,20], [5,18,22,30]]
Output: [20,24]
Explanation: 
List 1: [4, 10, 15, 24,26], 24 is in range [20,24].
List 2: [0, 9, 12, 20], 20 is in range [20,24].
List 3: [5, 18, 22, 30], 22 is in range [20,24].

Note

  • 给定的列表可能包含重复元素,所以在这里升序表示 >=
  • 1 <= k <= 3500
  • -105 <= 元素的值 <= 105
@Tcdian
Copy link
Owner Author

Tcdian commented Aug 1, 2020

Solution

  • JavaScript Solution
/**
 * @param {number[][]} nums
 * @return {number[]}
 */
var smallestRange = function(nums) {
    const groups = nums.map((group, groupIndex) => group.map((num) => [num, groupIndex]));
    const flatten = groups.reduce((prev, group) => [...prev, ...group]);
    const sorted = flatten.sort(([a], [b]) => a - b);
    const groupCounts = new Array(groups.length).fill(0);
    let includedGroupCount = 0;
    let result = [sorted[0][0], sorted[sorted.length - 1][0]];
    for (let left = 0, right = 0; right < sorted.length; right++) {
        if (groupCounts[sorted[right][1]] === 0) {
            includedGroupCount++;
        }
        groupCounts[sorted[right][1]] += 1;
        while(groupCounts[sorted[left][1]] > 1) {
            groupCounts[sorted[left][1]]--;
            left++;
        }
        if (includedGroupCount === groups.length && sorted[right][0] - sorted[left][0] < result[1] - result[0]) {
            result = [sorted[left][0], sorted[right][0]];
        }
    }
    return result;
};
  • TypeScript Solution
function smallestRange(nums: number[][]): number[] {
    const groups: [number, number][][] = nums.map((group, groupIndex) => group.map((num) => [num, groupIndex]));
    const flatten = groups.reduce((prev, group) => [...prev, ...group]);
    const sorted = flatten.sort(([a], [b]) => a - b);
    const groupCounts = new Array(groups.length).fill(0);
    let includedGroupCount = 0;
    let result: [number, number] = [sorted[0][0], sorted[sorted.length - 1][0]];
    for (let left = 0, right = 0; right < sorted.length; right++) {
        if (groupCounts[sorted[right][1]] === 0) {
            includedGroupCount++;
        }
        groupCounts[sorted[right][1]] += 1;
        while(groupCounts[sorted[left][1]] > 1) {
            groupCounts[sorted[left][1]]--;
            left++;
        }
        if (includedGroupCount === groups.length && sorted[right][0] - sorted[left][0] < result[1] - result[0]) {
            result = [sorted[left][0], sorted[right][0]];
        }
    }
    return result;
};

@Tcdian Tcdian removed the Classic label Jul 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant