321 Create Maximum Number 拼接最大数
已知长度分别为 m 和 n 的两个数组,其元素由 0-9 构成,直观地表示两个自然数各位上的数字。现在从这两个数组中选出 k (k <= m + n) 个数字拼接成一个新的数,要求从同一个数组中取出的数字保持其在原数组中的相对顺序。
求满足该条件的最大数。结果返回一个表示该最大数的长度为k的数组。
尽可能优化你的算法的时间和空间复杂度。
例 1:
nums1 = [3, 4, 6, 5]
nums2 = [9, 1, 2, 5, 8, 3]
k = 5
返回 [9, 8, 6, 5, 3]
例 2:
nums1 = [6, 7]
nums2 = [6, 0, 4]
k = 5
返回 [6, 7, 6, 0, 4]
例 3:
nums1 = [3, 9]
nums2 = [8, 9]
k = 3
返回 [9, 8, 9]
详见:https://leetcode.com/problems/create-maximum-number/description/
C++:
class Solution {
public:
vector<int> maxNumber(vector<int>& nums1, vector<int>& nums2, int k)
{
int m = nums1.size();
int n = nums2.size();
vector<int> result(k);
for (int i = std::max(0 , k - n); i <= k && i <= m; ++i)
{
auto v1 = maxArray(nums1, i);
auto v2 = maxArray(nums2, k - i);
vector<int> candidate = merge(v1, v2, k);
if (greater(candidate, 0, result, 0))
{
result = candidate;
}
}
return result;
}
bool greater(vector<int>& nums1, int i, vector<int>& nums2, int j)
{
while (i < nums1.size() && j < nums2.size() && nums1[i] == nums2[j])
{
i++;
j++;
}
return j == nums2.size() || (i<nums1.size() && nums1[i] > nums2[j]);
}
vector<int> merge(vector<int>& nums1, vector<int>& nums2, int k)
{
std::vector<int> ans(k);
int i = 0, j = 0;
for (int r = 0; r<k; r++){
if( greater(nums1, i, nums2, j) )
{
ans[r] = nums1[i++] ;
}
else
{
ans[r] = nums2[j++];
}
}
return ans;
}
vector<int> maxArray(vector<int>& nums, int k)
{
int n = nums.size();
vector<int> result(k);
for (int i = 0, j = 0; i < n; i++)
{
while (n - i + j>k && j > 0 && result[j-1] < nums[i])
{
j--;
}
if (j < k)
{
result[j++] = nums[i];
}
}
return result;
}
};
参考:https://www.cnblogs.com/CarryPotMan/p/5384172.html
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 ...
- 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 ...
- [LintCode] 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 ...
- 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 ...
- LeetCode 321. Create Maximum Number
原题链接在这里:https://leetcode.com/problems/create-maximum-number/description/ 题目: Given two arrays of len ...
随机推荐
- js eslint语法规范错误提示代码
最近在用eslint代码检测,因为之前不太注意代码规范,刚开始确实头疼,哈哈,不过用习惯了就会感觉还不错,其实也没有那样难调试 我看过之前有些人已经做过总结,自己记录下,方便自己以后查找 “Missi ...
- 【Codeforces 411A】Password Check
[链接] 我是链接,点我呀:) [题意] 题意 [题解] 傻逼模拟题 [代码] import java.io.*; import java.util.*; public class Main { st ...
- HTTP 请求的 GET 与 POST 方式的区别
HTTP 请求的 GET 与 POST 方式的区别 在客户机和服务器之间进行请求-响应时,两种最常被用到的方法是:GET 和 POST. GET - 从指定的资源请求数据. POST - 向指定的资源 ...
- Android ToggleButton:状态切换的Button
Android ToggleButton:状态切换的Button Android ToggleButton和Android Button类似,但是ToggleButton提供了一种选择机制,可以 ...
- Bellman-ford算法的学习http://blog.csdn.net/niushuai666/article/details/6791765
http://blog.csdn.net/niushuai666/article/details/6791765
- HDU - 3407 - String-Matching Automata
先上题目: String-Matching Automata Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...
- 推销员(codevs 5126)
题目描述 Description 阿明是一名推销员,他奉命到螺丝街推销他们公司的产品.螺丝街是一条死胡同,出口与入口是同一个,街道的一侧是围墙,另一侧是住户.螺丝街一共有N家住户,第i家住户到入口的距 ...
- dubbo 学习1
1.高性能优秀的服务框架,应用可通过高性能的RPC实现服务的输出和输入功能,可以和spring框架无缝集成. 2.主要核心部件 a.remoting 网络通信框架 实现了sync-over-asnc ...
- Container/Injection 为什么会出现容器的思路,以后会有什么的趋势,未来是怎样的
一.为什么会出现容器的思路? 容器概念始于 1979 年提出的 UNIX chroot,它是一个 UNIX 操作系统的系统调用,将一个进程及其子进程的根目录改变到文件系统中的一个新位置,让这些进程只能 ...
- Spring Boot配置文件规则以及使用方法官方文档查找以及Spring项目的官方文档查找方法
比如要使用Spring Boot实现一个功能,最直接的方式是Google,但是往往搜索出来的都比较乱,关键是乱在不同的版本上,比如1.x版本和2.x版本的配置是不一样的.最明显区别是在使用Thymel ...