321. Create Maximum Number

题目描述

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. You should try to optimize your time and space complexity.

Example 1:

nums1 = [3, 4, 6, 5]

nums2 = [9, 1, 2, 5, 8, 3]

k = 5

return [9, 8, 6, 5, 3]

Example 2:

nums1 = [6, 7]

nums2 = [6, 0, 4]

k = 5

return [6, 7, 6, 0, 4]

Example 3:

nums1 = [3, 9]

nums2 = [8, 9]

k = 3

return [9, 8, 9]

这个问题是要将两个数组合成一个数,我们先得解决一个简单一点的问题————从一个数组里挑出k个数字组成最大的数。

第一步

这个问题可以借助栈和贪心算法来实现。算法步骤:

  • 新建一个空栈stack
  • 遍历数组 nums
    • 弹出栈顶元素如果栈顶元素比nums[i]小,直到

      1、栈空

      2、剩余的数组不足以填满栈至 k
    • 如果栈的大小小于 k ,则压入nums[i]
  • 返回栈stack

    因为栈的长度知道是k,所以可以用数组来实现这个栈。时间复杂度为O(n),因为每个元素最多被push和pop一次。

    实现代码如下:
vector<int> maxArray(vector<int>& nums, int k){
int n = nums.size();
vector<int> result(k);
for (int i = 0, j = 0; i < n; i++){
while (n - i + j>k && j > 0 && result[j-1] < nums[i]) j--;
if (j < k) result[j++] = nums[i];
}
return result;
}

第二步

给定两个数组,长度分别为m 和n,要得到 k = m+n的最大数。

显然这个问题是原问题的特殊情况,显然这种情况下,我们首先想到的是贪心算法。

我们要做k次选择,每次我们就选两个数组中较大的那个数即可。这是显然的,但是如果两个数相等,我们应该选择哪个呢?

这就不是很显然的了,举个栗子,

nums1 = [6,7]
nums2 = [6,0,4]
k = 5
ans = [6,7,6,0,4]

所以相等的情况,我们就需要一直往后比,直到它们不相等分出大小为止。这就有点像归并排序中的merge函数了。因为每次都得一直往后比,所以时间复杂度是O(mn)。

这部分代码如下:

bool greater(vector<int>& nums1, int i, vector<int>& nums2, int j){
while (i < nums1.size() && j < nums2.size() && nums1[i] == nums2[j]){
i++;
j++;
}
return j == nums2.size() || (i<nums1.size() && nums1[i] > nums2[j]);
} vector<int> merge(vector<int>& nums1, vector<int>& nums2, int k) {
std::vector<int> ans(k);
int i = 0, j = 0;
for (int r = 0; r<k; r++){
if( greater(nums1, i, nums2, j) ) ans[r] = nums1[i++] ;
else ans[r] = nums2[j++];
}
return ans;
}

第三步

让我们回到原问题,我们首先把k个数分成两部分,i 和 k-i,我们可以用第一步中的函数求出两个数组中的长度为 i 和 k-i 的最大值。然后用第二步的方法将它们融合。最后我们从所有的结果中找出最大值。所以代码如下:

vector<int> maxNumber(vector<int>& nums1, vector<int>& nums2, int k) {
int m = nums1.size();
int n = nums2.size();
vector<int> result(k);
for (int i = std::max(0 , k - n); i <= k && i <= m; i++){
auto v1 = maxArray(nums1, i);
auto v2 = maxArray(nums2, k - i);
vector<int> candidate = merge(v1, v2, k);
if (greater(candidate, 0, result, 0)) result = candidate;
}
return result;
}

321. Create Maximum Number 解题方法详解的更多相关文章

  1. 402. Remove K Digits/738.Monotone Increasing Digits/321. Create Maximum Number

    Given a non-negative integer num represented as a string, remove k digits from the number so that th ...

  2. 321. Create Maximum Number

    /* * 321. Create Maximum Number * 2016-7-6 by Mingyang */ public int[] maxNumber(int[] nums1, int[] ...

  3. leetcode 402. Remove K Digits 、321. Create Maximum Number

    402. Remove K Digits https://www.cnblogs.com/grandyang/p/5883736.html https://blog.csdn.net/fuxuemin ...

  4. [LeetCode] 321. Create Maximum Number 创建最大数

    Given two arrays of length m and n with digits 0-9 representing two numbers. Create the maximum numb ...

  5. 321. Create Maximum Number (c++ ——> lexicographical_compare)

    Given two arrays of length m and n with digits 0-9 representing two numbers. Create the maximum numb ...

  6. LeetCode 321. Create Maximum Number

    原题链接在这里:https://leetcode.com/problems/create-maximum-number/description/ 题目: Given two arrays of len ...

  7. 321 Create Maximum Number 拼接最大数

    已知长度分别为 m 和 n 的两个数组,其元素由 0-9 构成,直观地表示两个自然数各位上的数字.现在从这两个数组中选出 k (k <= m + n) 个数字拼接成一个新的数,要求从同一个数组中 ...

  8. Python数据类型及其方法详解

    Python数据类型及其方法详解 我们在学习编程语言的时候,都会遇到数据类型,这种看着很基础也不显眼的东西,却是很重要,本文介绍了python的数据类型,并就每种数据类型的方法作出了详细的描述,可供知 ...

  9. oracle 重置序列从指定数字开始的方法详解

    原文 oracle 重置序列从指定数字开始的方法详解 重置oracle序列从指定数字开始 declare n ); v_startnum ):;--从多少开始 v_step ):;--步进 tsql ...

随机推荐

  1. 什么是SAD,SAE,SATD,SSD,SSE,MAD,MAE,MSD,MSE?

    SAD(Sum of Absolute Difference)=SAE(Sum of Absolute Error)即绝对误差和 SATD(Sum of Absolute Transformed Di ...

  2. Linux命令的常用

    使用chown命令更改文件拥有者 在 shell 中,可以使用chown命令来改变文件所有者.chown命令是change owner(改变拥有者)的缩写.需要要注意的是,用户必须是已经存在系统中的, ...

  3. SQL查询出每门课都大于80 分的学生姓名

    Course表如下: 查询出每门课都大于80 分的学生姓名有两种方法. 1.select  distinct name from Course where name not in (select di ...

  4. Linux下的GPT分区,使用parted命令

    Linux下的GPT分区,这是另外一种分区,针对MBR分区,它有很多优点: (1)几乎突破了分区个数的限制. 在GPT分区表中最多可以支持128个主分区. (2)单个分区容量几乎没有限制. 单个分区最 ...

  5. session在C#一般处理程序的调用方式

    在C#中有一个一般处理程序,可以快速地进行一些逻辑运算等功能,但在这个页面上,不能直接选择使用session进行页面间的值的传递,如何使得页面可以使用session呢 在页面开头写上 using Sy ...

  6. ethtool查看网卡以及修改网卡配置

    ethtool 命令详解 命令描述: ethtool 是用于查询及设置网卡参数的命令. 使用概要:ethtool ethx       //查询ethx网口基本设置,其中 x 是对应网卡的编号,如et ...

  7. [转载]本地配置的 *.dev,*.app域名 在谷歌浏览器中总是自动转跳到https上,导致不能访问?

    本地开发环境 .dev 不正常,找到文章mark一下 转自:https://segmentfault.com/q/1010000012339191

  8. JZOJ 4269. 【NOIP2015模拟10.27】挑竹签

    4269. [NOIP2015模拟10.27]挑竹签 (File IO): input:mikado.in output:mikado.out Time Limits: 1000 ms  Memory ...

  9. Java集合框架汇总

    HashMap是一个最常用的Map,它根据键的HashCode值存储数据,根据键可以直接获取它的值,具有很快的访问速度,遍历时,取得数据的顺序是完全随机的.HashMap最多只允许一条记录的键为NUL ...

  10. 使用shell脚本添加用户

    该文演示如何使用shell脚本完成添加用户,首先进行一个判断,如果用户存在,提示该用户已经存在,否则进行添加新的用户. 示例代码如下: #!/bin/bash grep_user() { R=`gre ...