LeetCode 321. Create Maximum Number
原题链接在这里: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;
}
}
LeetCode 321. Create Maximum Number的更多相关文章
- [LeetCode] 321. Create Maximum Number 创建最大数
Given two arrays of length m and n with digits 0-9 representing two numbers. Create the maximum numb ...
- 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 ...
- 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 ...
- 321. Create Maximum Number 解题方法详解
321. Create Maximum Number 题目描述 Given two arrays of length m and n with digits 0-9 representing two ...
- 321. Create Maximum Number
/* * 321. Create Maximum Number * 2016-7-6 by Mingyang */ public int[] maxNumber(int[] nums1, int[] ...
- leetcode 321 Create Max Number
leetcode 321 Create Max Number greedy的方法,由于有两个数组,我们很自然的想到从数组1中选i个数,数组2中选k-i个数,这样我们只需要遍历max(0, k-数组2长 ...
- 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 ...
- 321 Create Maximum Number 拼接最大数
已知长度分别为 m 和 n 的两个数组,其元素由 0-9 构成,直观地表示两个自然数各位上的数字.现在从这两个数组中选出 k (k <= m + n) 个数字拼接成一个新的数,要求从同一个数组中 ...
- [LintCode] Create Maximum Number 创建最大数
Given two arrays of length m and n with digits 0-9 representing two numbers. Create the maximum numb ...
随机推荐
- 基于卷积神经网络的人脸识别项目_使用Tensorflow-gpu+dilib+sklearn
https://www.cnblogs.com/31415926535x/p/11001669.html 基于卷积神经网络的人脸识别项目_使用Tensorflow-gpu+dilib+sklearn ...
- Linux 中的 ~/. 表示的意思
在Linux中, ~ 表示用户的目录, 如用户名是Gavin, 那么~/表示 /home/Gavin 所以~/. 表示 用户目录下的隐藏文件. 扩展: 若以用户身份登录 ~ 表示 /home 目录 ...
- Linux -- touch 命令
在Linux中,每个文件都关联一个时间戳,并且每个文件搜会存储最近一次访问的时间.最近一次修改的时间和最近一次变更的时间等信息.所以,无论何时我们创建一个新文件,访问或者修改一个已经存在的文件,文件的 ...
- CentOS7 修改Jenkins以root用户运行
修改Jenkins以root用户运行,这在正式环境中是不可取的,但在自己的测试环境中就无所谓了啦,怎么方便怎么来. 1. 修改Jenkins配置文件1.1 修改$JENKINS_USER打开jenki ...
- .NET/C# 检测电脑上安装的 .NET Framework 的版本
原文:.NET/C# 检测电脑上安装的 .NET Framework 的版本 如果你希望知道某台计算机上安装了哪些版本的 .NET Framework,那么正好本文可以帮助你解决问题. 本文内容 如何 ...
- docker容器日志管理(清理)
原文:docker容器日志管理(清理) 前言 在使用docker容器时候,其日志的管理是我们不得不考虑的事情.因为docker容器的日志文件会占据大量的磁盘空间.下面介绍的就是对docker容器日志的 ...
- This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=317567.
记事本打开csproj文件 搜索nuget 删除Target节点 类似如下: <Target Name="EnsureBclBuildImported" BeforeTarg ...
- Mycat分布式数据库架构解决方案--schema.xml详解
echo编辑整理,欢迎转载,转载请声明文章来源.欢迎添加echo微信(微信号:t2421499075)交流学习. 百战不败,依不自称常胜,百败不颓,依能奋力前行.--这才是真正的堪称强大!!! 该文件 ...
- “http”和“https”的区别是什么?优缺点是什么?
1. http 的URL 以http:// 开头,https以https:// 开头. 2. http 标准端口是80 ,https是443. 3.https 协议需要到ca申请证书,http不需要. ...
- Jmeter学习笔记(十四)——逻辑控制器
一.逻辑控制器简单介绍 Jmeter中逻辑控制器(Logic Controllers)的作用域只对其子节点的sampler有效,作用是控制采样器的执行顺序.放在逻辑控制器下面的所有的采样器都会当做一个 ...