LeetCode #321 Create Maximum Number 拼接最大數

Description:
Given two arrays of length m and n with digits 0-9 representing two numbers. Create the maximum number of length k <= m + n from digits of the two. The relative order of the digits from the same array must be preserved. Return an array of the k digits.

Note:
You should try to optimize your time and space complexity.

Example:
Example 1:

Input:
nums1 = [3, 4, 6, 5]
nums2 = [9, 1, 2, 5, 8, 3]
k = 5
Output:
[9, 8, 6, 5, 3]

Example 2:

Input:
nums1 = [6, 7]
nums2 = [6, 0, 4]
k = 5
Output:
[6, 7, 6, 0, 4]

Example 3:

Input:
nums1 = [3, 9]
nums2 = [8, 9]
k = 3
Output:
[9, 8, 9]

題目描述:
給定長度分別爲 m 和 n 的兩個數組,其元素由 0-9 構成,表示兩個自然數各位上的數字。現在從這兩個數組中選出 k (k <= m + n) 個數字拼接成一個新的數,要求從同一個數組中取出的數字保持其在原數組中的相對順序。

求滿足該條件的最大數。結果返回一個表示該最大數的長度爲 k 的數組。

說明:
請儘可能地優化你算法的時間和空間複雜度。

示例 :
示例 1:

輸入:
nums1 = [3, 4, 6, 5]
nums2 = [9, 1, 2, 5, 8, 3]
k = 5
輸出:
[9, 8, 6, 5, 3]

示例 2:

輸入:
nums1 = [6, 7]
nums2 = [6, 0, 4]
k = 5
輸出:
[6, 7, 6, 0, 4]

示例 3:

輸入:
nums1 = [3, 9]
nums2 = [8, 9]
k = 3
輸出:
[9, 8, 9]

思路:
分治
考慮從一個數組中選出最大的 k個數
可以使用單調棧/雙指針實現, 先填充 j個數字到 result數組中, n - i表示 nums數組中還未被選中的部分, j表示已經選中的數, 如果 nums[i]大於已經選中的數, 就倒退指針, 這樣就能選出最大的 k個數
這樣, 如果從 2個數組中選出最大的 k個數
可以考慮 k分成 0 + k, 1 + (k - 1), ..., k + 0, 第一個數組中選出 i個數, 第二個數組中選出 k - i個數, 選出最大的結果即可
時間複雜度O(k ^ 2 * max(m, n)), 空間複雜度O(max(m, n, k)), m爲數組 nums1的長度, n爲數組 nums2的長度

代碼:
C++:

class Solution {
public:
    vector<int> maxNumber(vector<int>& nums1, vector<int>& nums2, int k) {
        vector<int> result(k);
        int m = nums1.size(), n = nums2.size();
        for (int i = max(0, k - n); i <= k && i <= m; i++)
        {
            vector<int> pick1 = pick_max(nums1, i), pick2 = pick_max(nums2, k - i);
            vector<int> temp = merge(pick1, pick2, k);
            if (greater(temp, 0, result, 0)) result = temp;
        }
        return result;
    }
private:
    vector<int> pick_max(vector<int> &nums, int k)
    {
        vector<int> result(k);
        int n = nums.size();
        for (int i = 0, j = 0; i < n; i++)
        {
            while (n - i + j > k && j > 0 && nums[i] > result[j - 1]) --j;
            if (j < k) result[j++] = nums[i];
        }
        return result;
    }
    
    vector<int> merge(vector<int> &nums1, vector<int> &nums2, int k)
    {
        vector<int> result(k);
        for (int i = 0, j = 0, r = 0; r < k; r++) result[r] = greater(nums1, i, nums2, j) ? nums1[i++] : nums2[j++];
        return result;
    }
    
    bool greater(vector<int> &nums1, int i, vector<int> &nums2, int j)
    {
        while (i < nums1.size() and j < nums2.size() and nums1[i] == nums2[j])
        {
            ++i;
            ++j;
        }
        return j == nums2.size() or (i < nums1.size() and nums1[i] > nums2[j]);
    }
};

Java:

class Solution {
    public int[] maxNumber(int[] nums1, int[] nums2, int k) {
        int result[] = new int[k], m = nums1.length, n = nums2.length;
        for (int i = Math.max(0, k - n); i <= k && i <= m; i++) {
            int temp[] = merge(pickMax(nums1, i), pickMax(nums2, k - i), k);
            if (greater(temp, 0, result, 0)) result = temp;
        }
        return result;
    }
    
    private int[] pickMax(int[] nums, int k) {
        int result[] = new int[k], n = nums.length;
        for (int i = 0, j = 0; i < n; i++) {
            while (n - i + j > k && j > 0 && nums[i] > result[j - 1]) --j;
            if (j < k) result[j++] = nums[i];
        }
        return result;
    }
    
    private int[] merge(int[] nums1, int[] nums2, int k) {
        int result[] = new int[k];
        for (int i = 0, j = 0, r = 0; r < k; r++) result[r] = greater(nums1, i, nums2, j) ? nums1[i++] : nums2[j++];
        return result;
    }
    
    private boolean greater(int[] nums1, int i, int[] nums2, int j) {
        while (i < nums1.length && j < nums2.length && nums1[i] == nums2[j]) {
            ++i;
            ++j;
        }
        return j == nums2.length || (i < nums1.length && nums1[i] > nums2[j]);
    }
}

Python:

class Solution:
    def maxNumber(self, nums1: List[int], nums2: List[int], k: int) -> List[int]:
        def pick_max(nums: List[int], k: int) -> List[int]:
            result, drop = [], len(nums) - k
            for num in nums:
                while drop and result and result[-1] < num:
                    result.pop()
                    drop -= 1
                result.append(num)
            return result[:k]
        def merge(nums1: List[int], nums2: List[int]) -> List[int]:
            result = []
            while nums1 or nums2:
                greater = nums1 if nums1 > nums2 else nums2
                result.append(greater.pop(0))
            return result
        return max(merge(pick_max(nums1, i), pick_max(nums2, k - i)) for i in range(k + 1) if i <= len(nums1) and k - i <= len(nums2))
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章