[LintCode] 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.
Have you met this question in a real interview?
Example
Given nums1 = [3, 4, 6, 5], nums2 = [9, 1, 2, 5, 8, 3], k = 5
return [9, 8, 6, 5, 3]
Given nums1 = [6, 7], nums2 = [6, 0, 4], k = 5
return [6, 7, 6, 0, 4]
Given nums1 = [3, 9], nums2 = [8, 9], k = 3
return [9, 8, 9]
LeetCode上的原题,请参见我之前的博客Create Maximum Number。
class Solution {
public:
/**
* @param nums1 an integer array of length m with digits 0-9
* @param nums2 an integer array of length n with digits 0-9
* @param k an integer and k <= m + n
* @return an integer array
*/
vector<int> maxNumber(vector<int>& nums1, vector<int>& nums2, int k) {
vector<int> res;
int m = nums1.size(), n = nums2.size();
for (int i = max(, k - n); i <= min(k, m); ++i) {
res = max(res, mergeVec(maxVec(nums1, i), maxVec(nums2, k - i)));
}
return res;
}
vector<int> maxVec(vector<int> nums, int k) {
if (k == ) return {};
vector<int> res;
int drop = nums.size() - k;
for (auto a : nums) {
while (drop && res.size() && res.back() < a) {
res.pop_back();
--drop;
}
res.push_back(a);
}
res.resize(k);
return res;
}
vector<int> mergeVec(vector<int> nums1, vector<int> nums2) {
vector<int> res;
while (nums1.size() + nums2.size()) {
vector<int> &t = nums1 > nums2 ? nums1 : nums2;
res.push_back(t[]);
t.erase(t.begin());
}
return res;
}
};
[LintCode] Create Maximum Number 创建最大数的更多相关文章
- [LeetCode] Create Maximum Number 创建最大数
Given two arrays of length m and n with digits 0-9 representing two numbers. Create the maximum numb ...
- [LeetCode] 321. Create Maximum Number 创建最大数
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) 个数字拼接成一个新的数,要求从同一个数组中 ...
- 321. Create Maximum Number 解题方法详解
321. Create Maximum Number 题目描述 Given two arrays of length m and n with digits 0-9 representing two ...
- 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 * 2016-7-6 by Mingyang */ public int[] maxNumber(int[] nums1, int[] ...
- 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 ...
- [Swift]LeetCode321. 拼接最大数 | Create Maximum Number
Given two arrays of length m and n with digits 0-9 representing two numbers. Create the maximum numb ...
- Leetcode: Create Maximum Number
Given two arrays of length m and n with digits 0-9 representing two numbers. Create the maximum numb ...
随机推荐
- 关于UltraISO打开iso文件后只有部分文件问题
背景:在安装CentOS 7的时候,用UltraISO打开之后,只有一个EFI文件,刻完U盘,却无法引导. 之前还以为偶没下载全,就又下了一遍,还好偶搞得的NetInstall,要不然就呵呵了. 解决 ...
- 湖南省第十二届大学生计算机程序设计竞赛 G Parenthesis
1809: Parenthesis Description Bobo has a balanced parenthesis sequence P=p1 p2…pn of length n and q ...
- 关于SQLSERVER联合查询一点看法
首先看一段代码 这个数据库表我就不发了,这段代码的意思是:查询Book表中大于该类图书价格平均值的图书信息, 先看()里的内容,我一个表起了两个别名,让这个表的相同的id相等,查出平均分,然后再看() ...
- Linux crontab 定时任务详解
1.每小时执行一次脚本 * */1 * * * /etc/init.d/smb restart #不是所有的系统都支持“*/1”这种写法可以试试: 0 * * * * /etc/init.d/smb ...
- 近实时运算的利器---presto在公司实践
1.起因 公司hadoop集群里的datanonde和tasktracker节点负载主要集中于晚上到凌晨,平日工作时间负载不是很高.但在工作时间内,公司业务人员有实时查询需求,现在主要 借助于hive ...
- Hdu2544 最短路径 四种方法
Problem Description 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt.但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以现在他们想要 ...
- css整理-02 颜色和字体
颜色 命名颜色 RGB指定颜色 数值: 0-255 百分比 三元组:红绿蓝 16进制RGB web安全颜色 在256色计算机系统上总能避免抖动的颜色 表示为rgb值20%和51的倍数 web安全色的简 ...
- 静态成员函数(面向对象的static关键字)
静态成员函数 与静态数据成员一样,我们也可以创建一个静态成员函数,它为类的全部服务而不是某一个类的具体对象服务.静态成员函数与静态数据成员一样,都是类的内部实现,属于类定义的一部分.普通的成员函数一般 ...
- daemontools管理fast-fail的zookeeper
daemontools项目:http://cr.yp.to/daemontools.html 1.安装daemontools mkdir /package /package cd /package w ...
- HDU3820 Golden Eggs(最小割)
题目大概说给一个n*m的格子,每个格子放金蛋或银蛋都会得到不同的价值,当然也可以不放,不过如果存在相邻的两个格子都是金蛋会损失价值g,都是银则损失s.问能得到的最大价值. 有点像二者选一的最小割模型, ...