[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 ...
随机推荐
- Intellij Idea 使用
一.使用前需要修改的配置: 1.当类实现Serializable接口时,自动生成 serialVersionUID 1)Setting->Inspections->java->Ser ...
- eclipse项目中关于导入的项目里提示HttpServletRequest 不能引用的解决办法
eclipse项目中关于导入的项目里提示HttpServletRequest 不能引用的解决办法 当使用eclipse导入外部的web工程时,有时会提示HttpServletRequest, Serv ...
- 为什么接口类型可以直接new?
Runnable rn = new Runnable() { public void run() { } }; 实际相当于,jdk会自动生成一个匿名内部类,完成职责: class Anomymous ...
- ios 音乐播放
#import "ViewController.h" #import <AVFoundation/AVFoundation.h> @interface ViewCont ...
- 智能车学习(八)——菜单的实现
一.代码分享 1.头文件 #ifndef __MENU_H #define __MENU_H /***********宏定义************/ //页面声明 typedef enum Menu ...
- C++的模板特化 和 STL中iterator_traits模板的偏特化
C++中有类模板和函数模板,它们的定义如下所示: 类模板: template<class T1,class T2> class C { //... }; 函数模板: template< ...
- 从DataReader中手动串行化JSON
void WriteDataReader(StringBuilder sb, IDataReader reader) { ) { sb.Append("null"); return ...
- Jmeter 小攻略(转)
http://www.myexception.cn/open-source/1346307.html
- 安装Maven、Eclipse设置、添加地址JAR
1.下载Maven 地址:http://maven.apache.org/download.cgi 2.安装Maven 系统变量:MAVEN_HOME = D:\maven\apache-maven- ...
- 快销品 车销批发管理手持终端PDA系统 打印开单 入库 库存 盘点多功能一体
手持POS终端PDA移动开单 PDA通过扫描商品条码移动开单,实现便携式办公,伴随式销售,浩瀚技术研发团队开发的一款最新产品,PDA能通过WIFI无线局域网.GPRS互联网直接与主机连接,让公司业务人 ...