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 ...
随机推荐
- Spring Cloud Zuul源码
一.Zuul源码分析(初始化流程.请求处理流程)
- Unity项目 - 坦克大战3D TankBattle
目录 游戏原型 项目演示 绘图资源 代码实现 技术探讨 参考来源 游戏原型 游戏玩法:在有界的战场上,玩家将驾驶坦克,代表绿色阵营,与你的队友一起击溃红蓝阵营的敌人,在这场三方大战中夺得胜利! 操作指 ...
- 使用winform程序控制window服务的操作
继上篇 c#之添加window服务(定时任务) 基础之上, 这篇文章主要讲述,使用winform程序来控制window服务的安装,启动,停止,卸载等操作 1.在同一个解决方案添加winform项目,如 ...
- vim操作常用命令总结
这里记录下linux在vim编辑器中的常用命令 vi 的三种模式: 一般模式:以vi打开一个文件时,就是一般模式:可以移动光标,删除字符或删除整行,可以复制.粘贴等操作 编辑模式:在一般模式按下 i ...
- Oracle.EntityFrameworkCore使用时报错:Specified cast is not valid
我用的是:Oracle.EntityframeworkCore 2.19.30 如果看到报错:System.InvalidCastException:“Specified cast is not va ...
- 【洛谷 P2633】 Count on a tree(主席树,树上差分)
题目链接 思维难度0 实现难度7 建出主席树后用两点的状态减去lca和lca父亲的状态,然后在新树上跑第\(k\)小 #include <cstdio> #include <cstr ...
- NameError: name “ ” is not defined
NameError: name “ ” is not defined 问题一:name ‘name’ is not defined "name"两端是双下划线"_&quo ...
- React Native 开发豆瓣评分(八)首页开发
首页完成效果展示: 一.开发占位图组件 在没有数据的时候使用占位图替代 items 的位置. 在 components 目录里创建 moviesItemPlaceholder.js import Re ...
- flutter报错--ProcessException: Process... gradlew.bat ...exited abnormally
在 VScode 中 debug flutter 是遇到如下问题: ProcessException: Process "G:\demo\flutter\hello_word\android ...
- 增强学习--TRPO算法
理论部分参考 推导 数学上的分析 代码