原题链接在这里:https://leetcode.com/problems/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 + nfrom 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]

题解:

从nums1中挑出i个数, 从nums2中挑出k-i个数合并组成最大的数.

那么就可以分成两个小问题了.

第一个是如何从一个数组中挑出i个数, 使表示的数字最大.

第二个是完全合并两个数组使表达数字最大.

先看第一个问题, 挑出i个数使合成的数字最大. 利用stack, 若是剩下的数字个数还足够, 看stack顶部若是比当前数字小可以替换掉. 由于stack的size是固定的, 可以用array表示.

第二个问题是两个数组nums1, nums2, 完全合并成新的数组size为k = num1.length+nums2.length如何做到最大. 挑选nums1, nums2当前位置较大的数. 但若是相等的就要看后面的位.

最后挨个试i, 也就是从nums1中挑出数字的个数. 看组成的candidate是否更大, 维护res.

Note: i range, i >= Math.max(0, k - nums2.length). When nums2 is short, nums1 need to at least extract some digits.

i <= k, when nums1 is too long, extract number must be small or equal to k.

Time Complexity:  O((m+n)^3). m = nums1.length, n = nums2.length. 两个maxArray共用时O(k). merge用了i*(k-i). i从小到大试了个遍, 所以共用时O(k^2). k最大是m+n.

Space Complexity: O(m+n).

AC Java:

 class Solution {
public int[] maxNumber(int[] nums1, int[] nums2, int k) {
int [] res = new int[k];
for(int i = Math.max(0, k-nums2.length); i<=nums1.length&&i<=k; i++){
int [] candidate = merge(maxArray(nums1, i), maxArray(nums2, k-i), k);
if(greater(candidate, 0, res, 0)){
res = candidate;
}
}
return res;
} private int [] merge(int [] nums1, int [] nums2, int k){
int [] res = new int[k];
for(int i = 0, j = 0, r = 0; r<k; r++){
res[r] = greater(nums1, i, nums2, j) ? nums1[i++] : nums2[j++];
}
return res;
} 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]);
} private int [] maxArray(int [] nums, int k){
int len = nums.length;
int [] res = new int[k];
for(int i = 0, po = 0; i<len; i++){
while(len-i>k-po && po>0 && res[po-1]<nums[i]){
po--;
}
if(po < k){
res[po++] = nums[i];
}
}
return res;
}
}

类似Remove K Digits.

LeetCode 321. Create Maximum Number的更多相关文章

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

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

  2. 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 ...

  3. 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 ...

  4. 321. Create Maximum Number 解题方法详解

    321. Create Maximum Number 题目描述 Given two arrays of length m and n with digits 0-9 representing two ...

  5. 321. Create Maximum Number

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

  6. leetcode 321 Create Max Number

    leetcode 321 Create Max Number greedy的方法,由于有两个数组,我们很自然的想到从数组1中选i个数,数组2中选k-i个数,这样我们只需要遍历max(0, k-数组2长 ...

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

  8. 321 Create Maximum Number 拼接最大数

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

  9. [LintCode] Create Maximum Number 创建最大数

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

随机推荐

  1. int main(int argc, char *argv[])解释

    int main(int argc, char *argv[]) 详解: #include <stdio.h> int main(int argc, char *argv[]) { int ...

  2. 消息认证码 - MAC (Message Authentication Code)

    消息认证包括两个目标 1消息完整性认证: 确保张三发给我的消息是完整的,在传输过程中没有被第三方篡改 2消息的来源认证: 确保这个数据是张三发给我的,而不是李四发给我的 第一个目标通常使用散列函数来达 ...

  3. extend Thread 和 implements Runnable

    原文地址:extend Thread 和 implements Runnable 一个Thread的实例只能产生一个线程 or: 同一实例(Runnable实例)的多个线程 look: public ...

  4. .NET/C# 阻止屏幕关闭,阻止系统进入睡眠状态

    原文:.NET/C# 阻止屏幕关闭,阻止系统进入睡眠状态 在 Windows 系统中,一段时间不操作键盘和鼠标,屏幕便会关闭,系统会进入睡眠状态.但有些程序(比如游戏.视频和演示文稿)在运行过程中应该 ...

  5. Linux系统中五款好用的日志分析工具

    监控网络活动是一项繁琐的工作,但有充分的理由这样做.例如,它允许你查找和调查工作站和连接到网络的设备及服务器上的可疑登录,同时确定管理员滥用了什么.你还可以跟踪软件安装和数据传输,以实时识别潜在问题, ...

  6. ColdFusion 编写WebService 示例

    1.开发 Web Services,编写cfcdemo.cfc组件,代码如下: <cfcomponent style ="document" namespace = &quo ...

  7. JavaScript之循环语句

    (1)while语句 while(条件){ 条件为真,进入循环体.出现0 null undefined false其中任意一种情况,条件即为假 循环体 } 案例: var n=0; var count ...

  8. FreeRTOS优先级翻转

    举例 //高优先级任务的任务函数 void high_task(void *pvParameters) { while(1) { vTaskDelay(500); //延时500ms,也就是500个时 ...

  9. brython的问题

    brython 挺不错,但有bug. 再brython中使用mpmath做精确计算. 发现: int((103654973826275244659954807217085022028357821605 ...

  10. linux驱动开发手记【2】

    1./dev目录下,主设备号和次设备号.ls -l可以通过第一个字母是c或者b区分是字符设备或者是块设备.主设备号标识设备对应的驱动程序. 2.分配设备编号: 如果我们提前明确知道所需要的设备编号,则 ...