• [Swift]LeetCode321. 拼接最大数 | Create Maximum Number


    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
    ➤微信公众号:山青咏芝(shanqingyongzhi)
    ➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
    ➤GitHub地址:https://github.com/strengthen/LeetCode
    ➤原文地址:https://www.cnblogs.com/strengthen/p/10260640.html 
    ➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
    ➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

    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 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]

    说明: 请尽可能地优化你算法的时间和空间复杂度。

    示例 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]

    380 ms

     1 class Solution {
     2     func maxNumber(_ nums1: [Int], _ nums2: [Int], _ k: Int) -> [Int] {
     3         let m = nums1.count
     4         let n = nums2.count
     5         
     6         var res = [Int]()
     7         
     8         let c = max(0, k-n)
     9         
    10         for i in c...min(k, m) {
    11             let r1 = maxNumArr(nums1, i)
    12             let r2 = maxNumArr(nums2, k-i)
    13             let tmp = maxNums(r1, r2, k)
    14             if isGreater(tmp, res, 0, 0) {
    15                 res = tmp
    16             }
    17         }
    18         
    19         return res
    20     }
    21     
    22     func maxNumArr(_ nums : [Int], _ k : Int) -> [Int] {
    23         var res = [Int]()
    24         for i in 0..<nums.count {
    25             let num = nums[i]
    26             while !res.isEmpty && num > res.last! && nums.count + res.count > k + i {
    27                 res.removeLast()
    28             }
    29             res.append(num)
    30             continue
    31         }
    32         return res
    33     }
    34     
    35     func maxNums(_ num1 : [Int], _ num2 : [Int],_ k : Int) -> [Int] {
    36         var res = [Int]()
    37         var i = 0 , j = 0
    38         for _ in 0..<k {
    39             if isGreater(num1, num2, i, j) {
    40                 res.append(num1[i])
    41                 i+=1
    42             }else {
    43                 res.append(num2[j])
    44                 j+=1
    45             }
    46         }
    47         
    48         return res
    49     }
    50     
    51     func isGreater(_ nums1: [Int], _ nums2 : [Int], _ i : Int, _ j : Int) -> Bool {
    52         var i = i , j = j
    53         while i < nums1.count , j < nums2.count && nums1[i] == nums2[j] {
    54             i+=1
    55             j+=1
    56         }
    57         
    58         return j == nums2.count || (i < nums1.count && nums1[i] > nums2[j])
    59     }
    60 }
  • 相关阅读:
    Working with macro signatures
    Reset and Clear Recent Items and Frequent Places in Windows 10
    git分支演示
    The current .NET SDK does not support targeting .NET Core 2.1. Either target .NET Core 2.0 or lower, or use a version of the .NET SDK that supports .NET Core 2.1.
    Build website project by roslyn through devenv.com
    Configure environment variables for different tools in jenkins
    NUnit Console Command Line
    Code Coverage and Unit Test in SonarQube
    头脑王者 物理化学生物
    头脑王者 常识,饮食
  • 原文地址:https://www.cnblogs.com/strengthen/p/10260640.html
Copyright © 2020-2023  润新知